This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
lychee.js
1614 lines (1469 loc) · 49.4 KB
/
lychee.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @description This module provides the basic functions of Lychee.
*/
const lychee = {
/**
* The version of the backend in human-readable
* @type {Version}
*/
version: null,
updateGit: "https://github.com/LycheeOrg/Lychee",
updateURL: "https://github.com/LycheeOrg/Lychee/releases",
website: "https://LycheeOrg.github.io",
publicMode: false,
viewMode: false,
grants_full_photo_access: true,
grants_download: false,
public_photos_hidden: true,
share_button_visible: false,
/**
* The authenticated user or `null` if unauthenticated
* @type {?User}
*/
user: null,
/**
* The rights granted by the backend
* @type {?GlobalRightsDTO}
*/
rights: null,
/**
* Values:
*
* - `0`: Use default, "square" layout.
* - `1`: Use Flickr-like "justified" layout.
* - `2`: Use Google-like "unjustified" layout
*
* @type {number}
*/
layout: 1,
/**
* Display search in public mode.
* @type {boolean}
*/
public_search: false,
/**
* Overlay display type
* @type {string}
*/
image_overlay_type: "exif",
/**
* Image overlay type default type
* @type {string}
*/
image_overlay_type_default: "exif",
/**
* Display photo coordinates on map
* @type {boolean}
*/
map_display: false,
/**
* Display photos of public album on map (user not logged in)
* @type {boolean}
*/
map_display_public: false,
/**
* Use the GPS direction data on displayed maps
* @type {boolean}
*/
map_display_direction: true,
/**
* Provider of OSM Tiles
* @type {string}
*/
map_provider: "Wikimedia",
/**
* Include photos of subalbums on map
* @type {boolean}
*/
map_include_subalbums: false,
/**
* Retrieve location name from GPS data
* @type {boolean}
*/
location_decoding: false,
/**
* Caching mode for GPS data decoding
* @type {string}
*/
location_decoding_caching_type: "Harddisk",
/**
* Show location name
* @type {boolean}
*/
location_show: false,
/**
* Show location name for public albums
* @type {boolean}
*/
location_show_public: false,
/**
* Tolerance for navigating when swiping images to the left and right on mobile
* @type {number}
*/
swipe_tolerance_x: 150,
/**
* Tolerance for navigating when swiping images up and down
* @type {number}
*/
swipe_tolerance_y: 250,
/**
* Is landing page enabled?
* @type {boolean}
*/
landing_page_enabled: false,
delete_imported: false,
import_via_symlink: false,
skip_duplicates: false,
nsfw_visible: true,
nsfw_visible_saved: true,
nsfw_blur: false,
nsfw_warning: false,
/** @type {string} */
nsfw_banner_override: "",
album_subtitle_type: "oldstyle",
album_decoration: "layers",
album_decoration_orientation: "row",
upload_processing_limit: 4,
/**
* Allow users to change their username
* @type {boolean}
*/
allow_username_change: true,
/**
* The URL to the Facebook page related to this site
* @type {string}
*/
sm_facebook_url: "",
/**
* The URL to the Flickr page related to this site
* @type {string}
*/
sm_flickr_url: "",
/**
* The URL to the Instagram page related to this site
* @type {string}
*/
sm_instagram_url: "",
/**
* The URL to the Twitter page related to this site
* @type {string}
*/
sm_twitter_url: "",
/**
* The URL to the YouTube channel related to this site
* @type {string}
*/
sm_youtube_url: "",
/**
* Indicates whether RSS feeds are enabled or not
* @type {boolean}
*/
rss_enable: false,
/**
* An array of RSS feeds provided by the site
* @type {Feed[]}
*/
rss_feeds: [],
/**
* The site title.
* @type {string}
*/
site_title: "",
/**
* The name of the site owner.
* @type {string}
*/
site_owner: "",
/**
* Begin of copyright.
* @type {string}
*/
site_copyright_begin: "",
/**
* End of copyright.
* @type {string}
*/
site_copyright_end: "",
/**
* Determines if social media links are shown in footer.
* @type {boolean}
*/
footer_show_social_media: false,
/**
* Determines if copyright notice is shown in footer.
* @type {boolean}
*/
footer_show_copyright: false,
/**
* An optional line of text to be shown in the footer.
* @type {string}
*/
footer_additional_text: "",
/**
* Determines whether frame mode is enabled or not
* @type {boolean}
*/
mod_frame_enabled: false,
/**
* Refresh rate in seconds for the frame mode.
* @type {number}
*/
mod_frame_refresh: 30,
// this is device specific config, in this case default is Desktop.
header_auto_hide: true,
active_focus_on_page_load: false,
enable_button_visibility: true,
enable_button_share: true,
enable_button_archive: true,
enable_button_move: true,
enable_button_trash: true,
enable_button_fullscreen: true,
enable_button_download: true,
enable_button_add: true,
enable_button_more: true,
enable_button_rotate: true,
enable_close_tab_on_esc: false,
enable_tabindex: false,
enable_contextmenu_header: true,
hide_content_during_imgview: false,
checkForUpdates: true,
update_json: false,
update_available: false,
new_photos_notification: false,
/** @type {?SortingCriterion} */
sorting_photos: null,
/** @type {?SortingCriterion} */
sorting_albums: null,
/**
* The absolute path of the server-side installation directory of Lychee, e.g. `/var/www/lychee`
* @type {string}
*/
location: "",
lang: "",
/** @type {string[]} */
lang_available: [],
dropbox: false,
dropboxKey: "",
content: $("#lychee_view_content"),
imageview: $("#imageview"),
footer: $("#lychee_footer"),
/** @type {Locale} */
locale: {},
nsfw_unlocked_albums: [],
};
/**
* @returns {string}
*/
lychee.diagnostics = function () {
return "/Diagnostics";
};
/**
* @returns {string}
*/
lychee.logs = function () {
return "/Logs";
};
/**
* @returns {void}
*/
lychee.aboutDialog = function () {
const aboutDialogBody = `
<h1>Lychee <span class="version-number"></span></h1>
<p class="update-status up-to-date-release"><a target='_blank' href='${lychee.updateURL}'></a></p>
<p class="update-status up-to-date-git"><a target='_blank' href='${lychee.updateGit}'></a></p>
<h2></h2>
<p class="about-desc"></p>`;
/**
* @param {ModalDialogFormElements} formElements
* @param {HTMLDivElement} dialog
* @returns {void}
*/
const initAboutDialog = function (formElements, dialog) {
dialog.querySelector("span.version-number").textContent = lychee.version.major + "." + lychee.version.minor + "." + lychee.version.patch;
// If Release is available : show release
// If Git is available : show git
if (lychee.update_available) {
dialog.querySelector("p.up-to-date-release a").textContent = lychee.locale["UPDATE_AVAILABLE"];
dialog.querySelector("p.up-to-date-release").classList.remove("up-to-date-release");
} else if (lychee.update_json) {
dialog.querySelector("p.up-to-date-git a").textContent = lychee.locale["UPDATE_AVAILABLE"];
dialog.querySelector("p.up-to-date-git").classList.remove("up-to-date-git");
}
dialog.querySelector("h2").textContent = lychee.locale["ABOUT_SUBTITLE"];
// We should not use `innerHTML`, but either hard-code HTML or build it
// programmatically.
// Also, localized strings should not contain HTML tags.
// TODO: Find a better solution for this.
dialog.querySelector("p.about-desc").innerHTML = sprintf(lychee.locale["ABOUT_DESCRIPTION"], lychee.website);
};
basicModal.show({
body: aboutDialogBody,
readyCB: initAboutDialog,
classList: ["about-dialog"],
buttons: {
cancel: {
title: lychee.locale["CLOSE"],
fn: basicModal.close,
},
},
});
};
/**
* @param {boolean} isFirstInitialization must be set to `false` if called
* for re-initialization to prevent
* multiple registrations of global
* event handlers
* @returns {void}
*/
lychee.init = function (isFirstInitialization = true) {
api.post(
"Session::init",
{},
/** @param {InitializationData} data */
function (data) {
lychee.parseInitializationData(data);
if (data.user !== null || data.rights.settings.can_edit) {
// Authenticated or no admin is registered
leftMenu.build();
leftMenu.bind();
lychee.setMode("logged_in");
} else {
lychee.setMode("public");
}
if (isFirstInitialization) {
$(window).on("popstate", function () {
const autoplay = history.state && history.state.hasOwnProperty("autoplay") ? history.state.autoplay : true;
lychee.load(autoplay);
});
lychee.load();
}
}
);
};
/**
* @param {InitializationData} data
* @returns {void}
*/
lychee.parseInitializationData = function (data) {
lychee.user = data.user;
lychee.rights = data.rights;
lychee.update_json = data.update_json;
lychee.update_available = data.update_available;
lychee.version = data.config.version;
// we copy the locale that exists only.
// This ensures forward and backward compatibility.
// e.g. if the front localization is unfinished in a language
// or if we need to change some locale string
for (let key in data.locale) {
lychee.locale[key] = data.locale[key];
}
lychee.parsePublicInitializationData(data);
if (lychee.user !== null || lychee.rights.settings.can_edit) {
lychee.parseProtectedInitializationData(data);
}
lychee.initHtmlHeader();
lychee.localizeStaticGuiElements();
};
/**
* Initializes the HTML header of the page according to the loaded
* configuration.
*
* This method is comparable to {@link lychee.setMetaData} except that this
* method sets data in the HTML header which does not change for each page
* but is static for the entire site.
*/
lychee.initHtmlHeader = function () {
// General Meta Data
document.querySelector('meta[name="author"]').content = lychee.site_owner;
document.querySelector('meta[name="publisher"]').content = lychee.site_owner;
// RSS feeds
if (lychee.rss_enable) {
const head = document.querySelector("head");
lychee.rss_feeds.forEach(function (feed) {
const link = document.createElement("link");
link.rel = "alternate";
link.type = feed.mimetype;
link.href = feed.url;
link.title = feed.title;
head.appendChild(link);
});
}
};
/**
* Applies the current `lychee.locale` to those GUI elements which are
* static part of the HTML.
*
* Note, `lychee.setMode` removes some elements (e.g. the input element
* for search) depending on the mode.
* Hence, we must take some precautions as some elements might be `null`.
* TODO: Fix that.
*
* @return {void}
*/
lychee.localizeStaticGuiElements = function () {
// Toolbars in the header
const tbPublic = document.querySelector("div#lychee_toolbar_public");
tbPublic.querySelector("a#button_signin").title = lychee.locale["SIGN_IN"];
const tbPublicSearch = tbPublic.querySelector("input.header__search");
if (tbPublicSearch instanceof HTMLInputElement) {
// See remark about `lychee.setMode` in the jsDoc comment of this method.
tbPublicSearch.placeholder = lychee.locale["SEARCH"];
}
tbPublic.querySelector("a.button--map-albums").title = lychee.locale["DISPLAY_FULL_MAP"];
const tbAlbums = document.querySelector("div#lychee_toolbar_albums");
tbAlbums.querySelector("a#button_settings").title = lychee.locale["SETTINGS"];
const tbAlbumsSearch = tbAlbums.querySelector("input.header__search");
if (tbAlbumsSearch instanceof HTMLInputElement) {
// See remark about `lychee.setMode` in the jsDoc comment of this method.
tbAlbumsSearch.placeholder = lychee.locale["SEARCH"];
}
tbAlbums.querySelector("a.button--map-albums").title = lychee.locale["DISPLAY_FULL_MAP"];
tbAlbums.querySelector("a.button_add").title = lychee.locale["ADD"];
const tbAlbum = document.querySelector("div#lychee_toolbar_album");
tbAlbum.querySelector("a#button_back_home").title = lychee.locale["CLOSE_ALBUM"];
tbAlbum.querySelector("a#button_visibility_album").title = lychee.locale["VISIBILITY_ALBUM"];
tbAlbum.querySelector("a#button_sharing_album_users").title = lychee.locale["SHARING_ALBUM_USERS"];
tbAlbum.querySelector("a#button_nsfw_album").title = lychee.locale["ALBUM_MARK_NSFW"];
tbAlbum.querySelector("a#button_share_album").title = lychee.locale["SHARE_ALBUM"];
tbAlbum.querySelector("a#button_archive").title = lychee.locale["DOWNLOAD_ALBUM"];
tbAlbum.querySelector("a#button_info_album").title = lychee.locale["ABOUT_ALBUM"];
tbAlbum.querySelector("a#button_map_album").title = lychee.locale["DISPLAY_FULL_MAP"];
tbAlbum.querySelector("a#button_move_album").title = lychee.locale["MOVE_ALBUM"];
tbAlbum.querySelector("a#button_trash_album").title = lychee.locale["DELETE_ALBUM"];
tbAlbum.querySelector("a#button_fs_album_enter").title = lychee.locale["FULLSCREEN_ENTER"];
tbAlbum.querySelector("a#button_fs_album_exit").title = lychee.locale["FULLSCREEN_EXIT"];
tbAlbum.querySelector("a.button_add").title = lychee.locale["ADD"];
const tbPhoto = document.querySelector("div#lychee_toolbar_photo");
tbPhoto.querySelector("a#button_back").title = lychee.locale["CLOSE_PHOTO"];
tbPhoto.querySelector("a#button_star").title = lychee.locale["STAR_PHOTO"];
tbPhoto.querySelector("a#button_visibility").title = lychee.locale["VISIBILITY_PHOTO"];
tbPhoto.querySelector("a#button_rotate_ccwise").title = lychee.locale["PHOTO_EDIT_ROTATECCWISE"];
tbPhoto.querySelector("a#button_rotate_cwise").title = lychee.locale["PHOTO_EDIT_ROTATECWISE"];
tbPhoto.querySelector("a#button_share").title = lychee.locale["SHARE_PHOTO"];
tbPhoto.querySelector("a#button_info").title = lychee.locale["ABOUT_PHOTO"];
tbPhoto.querySelector("a#button_map").title = lychee.locale["DISPLAY_FULL_MAP"];
tbPhoto.querySelector("a#button_move").title = lychee.locale["MOVE"];
tbPhoto.querySelector("a#button_trash").title = lychee.locale["DELETE"];
tbPhoto.querySelector("a#button_fs_enter").title = lychee.locale["FULLSCREEN_ENTER"];
tbPhoto.querySelector("a#button_fs_exit").title = lychee.locale["FULLSCREEN_EXIT"];
tbPhoto.querySelector("a#button_more").title = lychee.locale["MORE"];
const tbMap = document.querySelector("div#lychee_toolbar_map");
tbMap.querySelector("a#button_back_map").title = lychee.locale["CLOSE_MAP"];
const tbConfig = document.querySelector("div#lychee_toolbar_config");
tbConfig.querySelector("a#button_close_config").title = lychee.locale["CLOSE"];
// Sidebar
document.querySelector("#lychee_sidebar_header h1").textContent = lychee.locale["PHOTO_ABOUT"];
// NSFW Warning Banner
/** @type {HTMLDivElement} */
const nsfwBanner = document.querySelector("#sensitive_warning");
nsfwBanner.innerHTML = lychee.nsfw_banner_override ? lychee.nsfw_banner_override : lychee.locale["NSFW_BANNER"];
// Footer
const footer = document.querySelector("#lychee_footer");
footer.querySelector("p.home_copyright").textContent = lychee.footer_show_copyright
? sprintf(
lychee.locale["FOOTER_COPYRIGHT"],
lychee.site_owner,
lychee.site_copyright_begin === lychee.site_copyright_end
? lychee.site_copyright_begin
: lychee.site_copyright_begin + "–" + lychee.site_copyright_end
)
: "";
footer.querySelector("p.personal_text").textContent = lychee.footer_additional_text;
footer.querySelector("p.hosted_by a").textContent = lychee.locale["HOSTED_WITH_LYCHEE"];
/** @type {HTMLDivElement} */
const footerSocialMedia = footer.querySelector("div#home_socials");
if (lychee.footer_show_social_media) {
footerSocialMedia.style.display = null;
footerSocialMedia.querySelector("a#facebook").href = lychee.sm_facebook_url;
footerSocialMedia.querySelector("a#flickr").href = lychee.sm_flickr_url;
footerSocialMedia.querySelector("a#instagram").href = lychee.sm_instagram_url;
footerSocialMedia.querySelector("a#twitter").href = lychee.sm_twitter_url;
footerSocialMedia.querySelector("a#youtube").href = lychee.sm_youtube_url;
} else {
footerSocialMedia.style.display = "none";
}
};
/**
* Parses the configuration settings which are always available.
*
* TODO: If configuration management is re-factored on the backend, remember to use proper types in the first place
*
* @param {InitializationData} data
* @returns {void}
*/
lychee.parsePublicInitializationData = function (data) {
lychee.allow_username_change = data.config.allow_username_change === "1";
lychee.sorting_photos = data.config.sorting_photos;
lychee.sorting_albums = data.config.sorting_albums;
lychee.share_button_visible = data.config.share_button_visible === "1";
lychee.album_subtitle_type = data.config.album_subtitle_type || "oldstyle";
lychee.album_decoration = data.config.album_decoration || "layers";
lychee.album_decoration_orientation = data.config.album_decoration_orientation || "row";
lychee.checkForUpdates = data.config.check_for_updates;
lychee.layout = Number.parseInt(data.config.layout, 10);
if (Number.isNaN(lychee.layout)) lychee.layout = 1;
lychee.landing_page_enable = data.config.landing_page_enable === "1";
lychee.public_search = data.config.public_search === "1";
lychee.image_overlay_type = data.config.image_overlay_type || "exif";
lychee.image_overlay_type_default = lychee.image_overlay_type;
lychee.map_display = data.config.map_display === "1";
lychee.map_display_public = data.config.map_display_public === "1";
lychee.map_display_direction = data.config.map_display_direction === "1";
lychee.map_provider = data.config.map_provider || "Wikimedia";
lychee.map_include_subalbums = data.config.map_include_subalbums === "1";
lychee.location_show = data.config.location_show === "1";
lychee.location_show_public = data.config.location_show_public === "1";
lychee.swipe_tolerance_x = Number.parseInt(data.config.swipe_tolerance_x, 10) || 150;
lychee.swipe_tolerance_y = Number.parseInt(data.config.swipe_tolerance_y, 10) || 250;
lychee.nsfw_visible = data.config.nsfw_visible === "1";
lychee.nsfw_visible_saved = lychee.nsfw_visible;
lychee.nsfw_blur = data.config.nsfw_blur === "1";
lychee.nsfw_warning = data.config.nsfw_warning === "1";
lychee.nsfw_banner_override = data.config.nsfw_banner_override || "";
lychee.sm_facebook_url = data.config.sm_facebook_url;
lychee.sm_flickr_url = data.config.sm_flickr_url;
lychee.sm_instagram_url = data.config.sm_instagram_url;
lychee.sm_twitter_url = data.config.sm_twitter_url;
lychee.sm_youtube_url = data.config.sm_youtube_url;
lychee.rss_enable = data.config.rss_enable === "1";
lychee.rss_feeds = data.config.rss_feeds;
lychee.site_title = data.config.site_title;
lychee.site_owner = data.config.site_owner;
lychee.site_copyright_begin = data.config.site_copyright_begin;
lychee.site_copyright_end = data.config.site_copyright_end;
lychee.footer_show_social_media = data.config.footer_show_social_media === "1";
lychee.footer_show_copyright = data.config.footer_show_copyright === "1";
lychee.footer_additional_text = data.config.footer_additional_text;
lychee.mod_frame_enabled = data.config.mod_frame_enabled === "1";
lychee.mod_frame_refresh = Number.parseInt(data.config.mod_frame_refresh, 10) || 30;
const isTv = window.matchMedia("tv").matches;
lychee.header_auto_hide = !isTv;
lychee.active_focus_on_page_load = isTv;
lychee.enable_button_visibility = !isTv;
lychee.enable_button_share = !isTv;
lychee.enable_button_archive = !isTv;
lychee.enable_button_move = !isTv;
lychee.enable_button_trash = !isTv;
lychee.enable_button_fullscreen = !isTv;
lychee.enable_button_download = !isTv;
lychee.enable_button_add = !isTv;
lychee.enable_button_more = !isTv;
lychee.enable_button_rotate = !isTv;
lychee.enable_close_tab_on_esc = isTv;
lychee.enable_tabindex = isTv;
lychee.enable_contextmenu_header = !isTv;
lychee.hide_content_during_imgview = isTv;
};
/**
* Parses the configuration settings which are only available, if a user is authenticated.
*
* TODO: If configuration management is re-factored on the backend, remember to use proper types in the first place
*
* @param {InitializationData} data
* @returns {void}
*/
lychee.parseProtectedInitializationData = function (data) {
lychee.dropboxKey = data.config.dropbox_key || "";
lychee.location = data.config.location || "";
lychee.checkForUpdates = data.config.check_for_updates === "1";
lychee.lang = data.config.lang || "";
lychee.lang_available = data.config.lang_available || [];
lychee.location_decoding = data.config.location_decoding === "1";
lychee.default_license = data.config.default_license || "none";
lychee.css = data.config.css || "";
lychee.grants_full_photo_access = data.config.grants_full_photo_access === "1";
lychee.grants_download = data.config.grants_download === "1";
lychee.public_photos_hidden = data.config.public_photos_hidden === "1";
lychee.delete_imported = data.config.delete_imported === "1";
lychee.import_via_symlink = data.config.import_via_symlink === "1";
lychee.skip_duplicates = data.config.skip_duplicates === "1";
lychee.editor_enabled = data.config.editor_enabled === "1";
lychee.new_photos_notification = data.config.new_photos_notification === "1";
lychee.upload_processing_limit = Number.parseInt(data.config.upload_processing_limit, 10) || 4;
};
/**
* @param {{username: string, password: string}} data
* @returns {void}
*/
lychee.login = function (data) {
if (!data.username.trim()) {
basicModal.focusError("username");
return;
}
if (!data.password.trim()) {
basicModal.focusError("password");
return;
}
api.post(
"Session::login",
data,
() => window.location.reload(),
null,
function (jqXHR) {
if (jqXHR.status === 401) {
basicModal.focusError("password");
return true;
} else {
return false;
}
}
);
};
/**
* @returns {void}
*/
lychee.loginDialog = function () {
const loginDialogBody = `
<a id='signInKeyLess' class="button"><svg class='iconic'><use xlink:href='#key'/></svg></a>
<form class="force-first-child">
<div class="input-group stacked">
<input class='text' name='username' autocomplete='username' type='text' autocapitalize='off' data-tabindex='${tabindex.get_next_tab_index()}'>
</div>
<div class="input-group stacked">
<input class='text' name='password' autocomplete='current-password' type='password' data-tabindex='${tabindex.get_next_tab_index()}'>
</div>
</form>
<p class='version'>Lychee <span class='version-number'></span>
<span class="update-status up-to-date-release"> – <a target='_blank' href='${lychee.updateURL}' data-tabindex='-1'></a></span>
<span class="update-status up-to-date-git"> – <a target='_blank' href='${lychee.updateGit}' data-tabindex='-1'></a></span>
</p>
`;
/**
* @param {ModalDialogFormElements} formElements
* @param {HTMLDivElement} dialog
* @returns {void}
*/
const initLoginDialog = function (formElements, dialog) {
tabindex.makeUnfocusable(header.dom());
tabindex.makeUnfocusable(lychee.content);
tabindex.makeUnfocusable(lychee.imageview);
tabindex.makeFocusable($(dialog));
formElements.username.placeholder = lychee.locale["USERNAME"];
formElements.password.placeholder = lychee.locale["PASSWORD"];
if (!!lychee.version) {
dialog.querySelector("span.version-number").textContent = lychee.version.major + "." + lychee.version.minor + "." + lychee.version.patch;
} else {
dialog.querySelector("span.version-number").textContent = "";
}
// If Release is available : show release
// If Git is available : show git
if (lychee.update_available) {
dialog.querySelector("span.up-to-date-release a").textContent = lychee.locale["UPDATE_AVAILABLE"];
dialog.querySelector("span.up-to-date-release").classList.remove("up-to-date-release");
} else if (lychee.update_json) {
dialog.querySelector("span.up-to-date-git a").textContent = lychee.locale["UPDATE_AVAILABLE"];
dialog.querySelector("span.up-to-date-git").classList.remove("up-to-date-git");
}
// This feels awkward, because this hooks into the modal dialog in some
// unpredictable way.
// It would be better to have a checkbox for password-less login in the
// dialog and then let the action handler of the modal dialog, i.e.
// `lychee.login` handle both cases.
// TODO: Refactor this.
dialog.querySelector("#signInKeyLess").addEventListener("click", u2f.login);
};
basicModal.show({
body: loginDialogBody,
readyCB: initLoginDialog,
classList: ["login"],
buttons: {
action: {
title: lychee.locale["SIGN_IN"],
fn: lychee.login,
attributes: { "data-tabindex": tabindex.get_next_tab_index() },
},
cancel: {
title: lychee.locale["CANCEL"],
fn: basicModal.close,
attributes: { "data-tabindex": tabindex.get_next_tab_index() },
},
},
});
};
/**
* @returns {void}
*/
lychee.logout = function () {
api.post("Session::logout", {}, () => window.location.reload());
};
/**
* @param {?string} [url=null]
* @param {boolean} [autoplay=true]
*
* @returns {void}
*/
lychee.goto = function (url = null, autoplay = true) {
url = "#" + (url !== null ? url : "");
history.pushState({ autoplay: autoplay }, null, url);
lychee.load(autoplay);
};
/**
* @param {?string} [albumID=null]
* @param {boolean} [autoplay=true]
*
* @returns {void}
*/
lychee.gotoMap = function (albumID = null, autoplay = true) {
// If map functionality is disabled -> go to album
if (!lychee.map_display) {
loadingBar.show("error", lychee.locale["ERROR_MAP_DEACTIVATED"]);
return;
}
lychee.goto("map/" + (albumID !== null ? albumID : ""), autoplay);
};
/**
* Triggers a reload, if the given IDs are in legacy format.
*
* If any of the IDs is in legacy format, the method first translates the IDs
* into the modern format via an AJAX call to the backend and then triggers
* an asynchronous reloading of the page with the resolved, modern IDs.
* The function returns `true` in this case.
*
* If the IDs are already in modern format (and thus neither a translation
* nor a reloading is required), the function returns `false`.
* In this case this function is basically a no-op.
*
* @param {?string} albumID the album ID
* @param {?string} photoID the photo ID
* @param {boolean} autoplay indicates whether playback should start
* automatically, if the indicated photo is a video
*
* @returns {boolean} `true`, if any of the IDs has been in legacy format
* and an asynchronous reloading has been scheduled
*/
lychee.reloadIfLegacyIDs = function (albumID, photoID, autoplay) {
/** @param {?string} id the inspected ID */
const isLegacyID = function (id) {
// The legacy IDs were pure numeric values. We exclude values which
// have 24 digits, because these could also be modern IDs.
// A modern IDs is a 24 character long, base64 encoded value and thus
// could also match 24 digits by accident.
return id && id.length !== 24 && parseInt(id, 10).toString() === id;
};
if (!isLegacyID(albumID) && !isLegacyID(photoID)) {
// this function is a no-op if neither ID is in legacy format
return false;
}
/**
* Callback to be called asynchronously which executes the actual reloading.
*
* @param {?string} newAlbumID
* @param {?string} newPhotoID
*
* @returns {void}
*/
const reloadWithNewIDs = function (newAlbumID, newPhotoID) {
let newUrl = "";
if (newAlbumID) {
newUrl += newAlbumID;
newUrl += newPhotoID ? "/" + newPhotoID : "";
}
lychee.goto(newUrl, autoplay);
};
// We have to deal with three cases:
// 1. the album and photo ID need to be translated
// 2. only the album ID needs to be translated
// 3. only the photo ID needs to be translated
let params = {};
if (isLegacyID(albumID)) params.albumID = parseInt(albumID, 10);
if (isLegacyID(photoID)) params.photoID = parseInt(photoID, 10);
api.post("Legacy::translateLegacyModelIDs", params, function (data) {
reloadWithNewIDs(data.hasOwnProperty("albumID") ? data.albumID : albumID, data.hasOwnProperty("photoID") ? data.photoID : photoID);
});
return true;
};
/**
* This is a "God method" that is used to load pretty much anything, based
* on what's in the web browser's URL.
*
* Traditionally, Lychee has been using client-side navigation based on
* URL fragments (i.e. based on the part after the '#' character)
* Fragments can match one of the following cases:
*
* - (nothing): load root album, assign `null` to `albumID` and `photoID`
* - `{albumID}`: load the album; `albumID` equals the given ID, `photoID` is
* null
* - `{albumID}/{photoID}`: load album (if not already loaded) and then the
* corresponding photo, assign the respective values to `albumID` and
* `photoID`
* - `map`: load the map of all albums
* - `map/{albumID}`: load the map of the respective album
* - `search/{term}`: load or go back to "search" album for the given term,
* assign `search/{term}` as fictitious `albumID` and assign `null` to
* `photoID`
* - `search/{term}/{photoID}`: load photo within fictitious search album,
* assign `search/{term}` as fictitious `albumID` and assign the given ID
* to `photoID`
* - `view/{photoID}`: load the photo in "view" mode, i.e. a special photo
* view which displays the photo as standalone (not in an album carousel)
* which assumes that the user is always unauthenticated.
* - `frame`: shows random, starred photos in a kiosk mode
*
* Additionally, Lychee supports the following proper paths:
*
* - `/view/{photoID}` and `/view?p={photoID}`: See `view/{photoID}` above
* for the fragment-based approach
* - `/frame`: See `frame` above for the fragment-based approach.
*
* @param {boolean} [autoplay=true]
* @returns {void}
*/
lychee.load = function (autoplay = true) {
let albumID = "";
let photoID = "";
const viewMatch = document.location.href.match(/\/view(?:\/|(\?p=))(?<photoID>[-_0-9A-Za-z]+)$/);
const hashMatch = document.location.hash.replace("#", "").split("/");
if (/\/frame\/?$/.test(document.location.href)) {
albumID = "frame";
photoID = "";
} else if (viewMatch !== null && viewMatch.groups.photoID) {
albumID = "view";
photoID = viewMatch.groups.photoID;
} else {
albumID = hashMatch[0];
if (albumID === SearchAlbumIDPrefix && hashMatch.length > 1) {
albumID += "/" + hashMatch[1];
}
photoID = hashMatch[album.isSearchID(albumID) ? 2 : 1];
}
contextMenu.close();
multiselect.close();
tabindex.reset();
// If Lychee is currently in frame or view mode, we need to re-initialize.
// Note, this is a temporary nasty hack.
// In an optimal world, we would simply call `lychee.setMode` to leave
// view or frame mode and to enter gallery or public mode.
// However, `lychee.setMode` does not support that direction (see comment
// here).
// Hence, in order to get back to a "full" mode, we need to re-initialize
// completely.
const bodyClasses = document.querySelector("body").classList;
if (bodyClasses.contains("mode-frame") || bodyClasses.contains("mode-view")) {
lychee.init(false);
return;
}
if (albumID && photoID) {
if (albumID === "map") {
// If map functionality is disabled -> do nothing
if (!lychee.map_display) {
loadingBar.show("error", lychee.locale["ERROR_MAP_DEACTIVATED"]);
return;
}
$(".no_content").remove();
// show map
// albumID has been stored in photoID due to URL format #map/albumID
albumID = photoID;
photoID = null;
// Trash data
photo.json = null;
// Show Album -> it's below the map
if (visible.photo()) view.photo.hide();
if (visible.sidebar()) sidebar.toggle(false);
if (album.json && albumID === album.json.id) {
view.album.title();
}
mapview.open(albumID);
lychee.footer_hide();
} else {
if (lychee.reloadIfLegacyIDs(albumID, photoID, autoplay)) {
return;
}
$(".no_content").remove();
// Show photo
// Trash data
photo.json = null;
/**
* @param {boolean} isParentAlbumAccessible
* @returns {void}
*/
const loadPhoto = function (isParentAlbumAccessible) {
if (!isParentAlbumAccessible) {
lychee.setMode("view");
}
photo.load(photoID, albumID, autoplay);
// Make imageview focusable
tabindex.makeFocusable(lychee.imageview);
// Make thumbnails unfocusable and store which element had focus
tabindex.makeUnfocusable(lychee.content, true);
// hide contentview if requested
if (lychee.hide_content_during_imgview) lychee.content.hide();
lychee.footer_hide();
};
// Load Photo
if (albumID === "view") {
// If the photo shall be displayed in "view" mode, delete
// any album which we possibly have and load the photo as
// if the parent album was inaccessible (even if a user is
// authenticated).
albumID = null;
album.refresh();
lychee.content.empty();
loadPhoto(false);
} else if (lychee.content.html() === "" || album.json === null || album.json.id !== albumID) {
// If we don't have an album or the wrong album load the album
// first and let the album loader load the photo afterwards or
// load the photo directly.
lychee.content.hide();
album.load(albumID, loadPhoto);
} else {
loadPhoto(true);
}
}
} else if (albumID) {
if (albumID === "map") {
$(".no_content").remove();
// Show map of all albums
// If map functionality is disabled -> do nothing
if (!lychee.map_display) {
loadingBar.show("error", lychee.locale["ERROR_MAP_DEACTIVATED"]);