Skip to content

Commit fbac64f

Browse files
committed
Add dashboard widget: On this day
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 49b937a commit fbac64f

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace OCA\Photos\AppInfo;
2727

28+
use OCA\Photos\Dashboard\OnThisDay;
2829
use OCA\DAV\Connector\Sabre\Principal;
2930
use OCP\AppFramework\App;
3031
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -61,6 +62,7 @@ public function __construct() {
6162
}
6263

6364
public function register(IRegistrationContext $context): void {
65+
$context->registerDashboardWidget(OnThisDay::class);
6466
/** Register $principalBackend for the DAV collection */
6567
$context->registerServiceAlias('principalBackend', Principal::class);
6668
$context->registerEventListener(NodeDeletedEvent::class, MoveToTrashListener::class);

lib/Dashboard/OnThisDay.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
namespace OCA\Photos\Dashboard;
3+
4+
use OCA\Photos\AppInfo\Application;
5+
use OCP\AppFramework\Services\IInitialState;
6+
use OCP\IInitialStateService;
7+
use OCP\Util;
8+
9+
class OnThisDay implements \OCP\Dashboard\IWidget
10+
{
11+
/**
12+
* @var \OCP\IL10N
13+
*/
14+
private $l;
15+
/**
16+
* @var \OCP\IURLGenerator
17+
*/
18+
private $url;
19+
/**
20+
* @var \OCP\AppFramework\Services\IInitialState
21+
*/
22+
private $initialState;
23+
24+
public function __construct(\OCP\IL10N $l, \OCP\IURLGenerator $url, IInitialStateService $initialState)
25+
{
26+
$this->l = $l;
27+
$this->url = $url;
28+
$this->initialState = $initialState;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function getId(): string
35+
{
36+
return 'photos.onthisday';
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function getTitle(): string
43+
{
44+
return $this->l->t('On This Day');
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function getOrder(): int
51+
{
52+
return 20;
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function getIconClass(): string
59+
{
60+
return 'icon-calendar-dark';
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function getUrl(): ?string
67+
{
68+
return $this->url->linkToRoute('photos.page.indexthisday');
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function load(): void
75+
{
76+
Util::addScript('photos', 'photos-dashboard');
77+
$this->initialState->provideInitialState('photos', 'image-mimes', Application::IMAGE_MIMES);
78+
$this->initialState->provideInitialState('photos', 'video-mimes', Application::VIDEO_MIMES);
79+
}
80+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--
2+
- Copyright (c) 2020. The Nextcloud Bookmarks contributors.
3+
-
4+
- This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
5+
-->
6+
7+
<template>
8+
<div>
9+
<template v-if="items.length">
10+
<File :item="{injected: items[0]}" />
11+
<p class="subline">
12+
<a :href="moreUrl" class="button">{{ t('photos', 'More photos from this day') }}</a>
13+
</p>
14+
</template>
15+
<p v-else class="icon-loading" />
16+
</div>
17+
</template>
18+
19+
<script>
20+
import { generateUrl } from '@nextcloud/router'
21+
import getPhotos from '../../services/PhotoSearch.js'
22+
import { allMimes } from '../../services/AllowedMimes.js'
23+
import File from '../File.vue'
24+
25+
const NUMBER_OF_IMAGES = 10
26+
27+
export default {
28+
name: 'DashboardOnThisDay',
29+
components: { File },
30+
data() {
31+
return {
32+
loading: true,
33+
items: [],
34+
}
35+
},
36+
computed: {
37+
moreUrl() {
38+
return generateUrl('/apps/photos/thisday')
39+
},
40+
},
41+
async created() {
42+
let files
43+
try {
44+
// Load next batch of images
45+
files = await getPhotos(false, {
46+
page: 0,
47+
perPage: NUMBER_OF_IMAGES,
48+
mimesType: allMimes,
49+
onThisDay: true,
50+
})
51+
} catch (e) {
52+
console.error(e)
53+
return
54+
}
55+
// eslint-disable-next-line no-console
56+
console.log(files)
57+
58+
this.items = files.map(file => ({ ...file, mime: 'image' }))
59+
this.loading = false
60+
},
61+
}
62+
</script>
63+
<style scoped>
64+
.subline {
65+
text-align: center;
66+
margin-top: 20px;
67+
}
68+
</style>

src/dashboard.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
3+
*
4+
* @author John Molakvoæ <skjnldsv@protonmail.com>
5+
*
6+
* @license AGPL-3.0-or-later
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
import { generateFilePath } from '@nextcloud/router'
24+
import { getRequestToken } from '@nextcloud/auth'
25+
import { translate, translatePlural } from '@nextcloud/l10n'
26+
import Vue from 'vue'
27+
28+
import store from './store'
29+
import DashboardOnThisDay from './components/Dashboard/DashboardOnThisDay.vue'
30+
31+
// CSP config for webpack dynamic chunk loading
32+
// eslint-disable-next-line
33+
__webpack_nonce__ = btoa(getRequestToken())
34+
35+
// Correct the root of the app for chunk loading
36+
// OC.linkTo matches the apps folders
37+
// OC.generateUrl ensure the index.php (or not)
38+
// We do not want the index.php since we're loading files
39+
// eslint-disable-next-line
40+
__webpack_public_path__ = generateFilePath('photos', '', 'js/')
41+
42+
Vue.prototype.t = translate
43+
Vue.prototype.n = translatePlural
44+
45+
window.addEventListener('DOMContentLoaded', () => {
46+
OCA.Dashboard.register('photos.onthisday', (el) => {
47+
global.PhotosOnThisDay = new Vue({
48+
el,
49+
store,
50+
render: h => h(DashboardOnThisDay),
51+
})
52+
})
53+
})

webpack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ webpackConfig.plugins.push(
8080
})
8181
)
8282

83+
webpackConfig.entry.dashboard = path.resolve(path.join('src', 'dashboard.js'))
84+
8385
module.exports = webpackConfig

0 commit comments

Comments
 (0)