Skip to content
This repository was archived by the owner on Dec 25, 2023. It is now read-only.

Commit b582360

Browse files
authored
Support the back button in search results (#306)
* Also don't auto-play videos loaded after a delete
1 parent aa5bb70 commit b582360

File tree

8 files changed

+78
-41
lines changed

8 files changed

+78
-41
lines changed

scripts/main/album.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ album.isSmartID = function (id) {
2020
* @returns {boolean}
2121
*/
2222
album.isSearchID = function (id) {
23-
return id === SearchAlbumID;
23+
return id === SearchAlbumIDPrefix || id.startsWith(SearchAlbumIDPrefix + "/");
2424
};
2525

2626
/**
@@ -50,7 +50,7 @@ album.getID = function () {
5050

5151
// this is a Lambda
5252
let isID = (_id) => {
53-
return album.isSmartID(_id) || /*album.isSearchID(_id) || */ album.isModelID(_id);
53+
return album.isSmartID(_id) || album.isSearchID(_id) || album.isModelID(_id);
5454
};
5555

5656
if (photo.json) id = photo.json.album_id;
@@ -176,16 +176,24 @@ album.deleteSubByID = function (albumID) {
176176
/**
177177
* @param {string} albumID
178178
* @param {?AlbumLoadedCB} [albumLoadedCB=null]
179+
* @param {?string} parentID
179180
*
180181
* @returns {void}
181182
*/
182-
album.load = function (albumID, albumLoadedCB = null) {
183+
album.load = function (albumID, albumLoadedCB = null, parentID = null) {
183184
/**
184185
* @param {Album} data
185186
*/
186187
const processAlbum = function (data) {
187188
album.json = data;
188189

190+
if (parentID !== null) {
191+
// Used with search so that the back button sends back to the
192+
// search results.
193+
album.json.original_parent_id = album.json.parent_id;
194+
album.json.parent_id = parentID;
195+
}
196+
189197
if (albumLoadedCB === null) {
190198
lychee.animate(lychee.content, "contentZoomOut");
191199
}

scripts/main/contextMenu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ contextMenu.photo = function (photoID, e) {
380380
{ title: build.iconic("trash") + lychee.locale["DELETE"], fn: () => photo.delete([photoID]) },
381381
{ title: build.iconic("cloud-download") + lychee.locale["DOWNLOAD"], fn: () => photo.getArchive([photoID]) },
382382
];
383-
if (album.isSmartID(album.getID()) || album.isSearchID(album.getID) || album.isTagAlbum()) {
383+
if (album.isSmartID(album.getID()) || album.isSearchID(album.getID()) || album.isTagAlbum()) {
384384
// Cover setting not supported for smart or tag albums and search results.
385385
items.splice(2, 1);
386386
}

scripts/main/header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ header.bind = function () {
138138

139139
header.dom(".header__search").on("keyup click", function () {
140140
if ($(this).val().length > 0) {
141-
lychee.goto("search/" + encodeURIComponent($(this).val()));
141+
lychee.goto(SearchAlbumIDPrefix + "/" + encodeURIComponent($(this).val()));
142142
} else if (search.json !== null) {
143143
search.reset();
144144
}

scripts/main/lychee.js

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,31 @@ lychee.reloadIfLegacyIDs = function (albumID, photoID, autoplay) {
583583
};
584584

585585
/**
586+
* This is a "God method" that is used to load pretty much anything, based
587+
* on what's in the web browser's URL bar after the '#' character:
588+
*
589+
* (nothing) --> load root album, assign null to albumID and photoID
590+
* {albumID} --> load the album; albumID equals the given ID, photoID is null
591+
* {albumID}/{photoID} --> load album (if not already loaded) and then the
592+
* corresponding photo, assign the respective values to albumID and photoID
593+
* map --> load the map of all albums
594+
* map/{albumID} --> load the map of the respective album
595+
* search/{term} --> load or go back to "search" album for the given term,
596+
* assign 'search/{term}' as fictitious albumID and assign null to photoID
597+
* search/{term}/{photoID} --> load photo within fictitious search album,
598+
* assign 'search/{term}' as fictitious albumID and assign the given ID to
599+
* photoID
600+
*
586601
* @param {boolean} [autoplay=true]
587602
* @returns {void}
588603
*/
589604
lychee.load = function (autoplay = true) {
590605
let hash = document.location.hash.replace("#", "").split("/");
591606
let albumID = hash[0];
592-
let photoID = hash[1];
607+
if (albumID === SearchAlbumIDPrefix && hash.length > 1) {
608+
albumID += "/" + hash[1];
609+
}
610+
let photoID = hash[album.isSearchID(albumID) ? 2 : 1];
593611

594612
contextMenu.close();
595613
multiselect.close();
@@ -618,24 +636,6 @@ lychee.load = function (autoplay = true) {
618636
}
619637
mapview.open(albumID);
620638
lychee.footer_hide();
621-
} else if (albumID === "search") {
622-
// Search has been triggered
623-
const search_string = decodeURIComponent(photoID);
624-
625-
if (search_string.trim() === "") {
626-
// do nothing on "only space" search strings
627-
return;
628-
}
629-
// If public search is disabled -> do nothing
630-
if (lychee.publicMode === true && !lychee.public_search) {
631-
loadingBar.show("error", lychee.locale["ERROR_SEARCH_DEACTIVATED"]);
632-
return;
633-
}
634-
635-
header.dom(".header__search").val(search_string);
636-
search.find(search_string);
637-
638-
lychee.footer_show();
639639
} else {
640640
if (lychee.reloadIfLegacyIDs(albumID, photoID, autoplay)) {
641641
return;
@@ -673,12 +673,7 @@ lychee.load = function (autoplay = true) {
673673
// If we don't have an album or the wrong album load the album
674674
// first and let the album loader load the photo afterwards or
675675
// load the photo directly.
676-
if (
677-
lychee.content.html() === "" ||
678-
album.json === null ||
679-
album.json.id !== albumID ||
680-
(header.dom(".header__search").length && header.dom(".header__search").val().length !== 0)
681-
) {
676+
if (lychee.content.html() === "" || album.json === null || album.json.id !== albumID) {
682677
lychee.content.hide();
683678
album.load(albumID, loadPhoto);
684679
} else {
@@ -703,8 +698,6 @@ lychee.load = function (autoplay = true) {
703698
if (visible.sidebar()) sidebar.toggle(false);
704699
mapview.open();
705700
lychee.footer_hide();
706-
} else if (albumID === "search") {
707-
// search string is empty -> do nothing
708701
} else {
709702
if (lychee.reloadIfLegacyIDs(albumID, photoID, autoplay)) {
710703
return;
@@ -723,12 +716,46 @@ lychee.load = function (autoplay = true) {
723716
if (visible.sidebar() && (album.isSmartID(albumID) || album.isSearchID(albumID))) sidebar.toggle(false);
724717
$("#sensitive_warning").hide();
725718
if (album.json && albumID === album.json.id) {
726-
view.album.title();
719+
if (album.isSearchID(albumID)) {
720+
// We are probably coming back to the search results from
721+
// viewing an image. Because search results is not a
722+
// regular album, it needs to be treated a little
723+
// differently.
724+
header.setMode("albums");
725+
lychee.setTitle(lychee.locale["SEARCH_RESULTS"], false);
726+
} else {
727+
view.album.title();
728+
}
727729
lychee.content.show();
728730
tabindex.makeFocusable(lychee.content, true);
729731
// If the album was loaded in the background (when content is
730732
// hidden), scrolling may not have worked.
731733
view.album.content.restoreScroll();
734+
} else if (album.isSearchID(albumID)) {
735+
// Search has been triggered
736+
let search_string = decodeURIComponent(hash[1]).trim();
737+
738+
if (search_string === "") {
739+
// do nothing on "only space" search strings
740+
return;
741+
}
742+
// If public search is disabled -> do nothing
743+
if (lychee.publicMode === true && !lychee.public_search) {
744+
loadingBar.show("error", lychee.locale["ERROR_SEARCH_DEACTIVATED"]);
745+
return;
746+
}
747+
748+
header.dom(".header__search").val(search_string);
749+
search.find(search_string);
750+
} else if (visible.search()) {
751+
// Somebody clicked on an album in search results. We
752+
// will alter the parent_id of that album once it's loaded
753+
// so that the back button sends us back to the search
754+
// results.
755+
// Trash data so that it's reloaded if needed (just as we
756+
// would for a regular parent album).
757+
search.json = null;
758+
album.load(albumID, null, album.getID());
732759
} else {
733760
album.load(albumID);
734761
}

scripts/main/photo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ photo.delete = function (photoIDs) {
328328
// Show album otherwise.
329329
if (visible.photo()) {
330330
if (nextPhotoID !== null && nextPhotoID !== photo.getID()) {
331-
lychee.goto(album.getID() + "/" + nextPhotoID);
331+
lychee.goto(album.getID() + "/" + nextPhotoID, false);
332332
} else if (previousPhotoID !== null && previousPhotoID !== photo.getID()) {
333-
lychee.goto(album.getID() + "/" + previousPhotoID);
333+
lychee.goto(album.getID() + "/" + previousPhotoID, false);
334334
} else {
335335
lychee.goto(album.getID());
336336
}

scripts/main/search.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @type {string}
1111
*/
12-
const SearchAlbumID = "search";
12+
const SearchAlbumIDPrefix = "search";
1313

1414
/**
1515
* @typedef SearchAlbum
@@ -18,7 +18,7 @@ const SearchAlbumID = "search";
1818
* mostly compatible with the other album types, i.e.
1919
* {@link Album}, {@link TagAlbum} and {@link SmartAlbum}.
2020
*
21-
* @property {string} id - always equals `SearchAlbumID`
21+
* @property {string} id - always equals `SearchAlbumIDPrefix/search-term`
2222
* @property {string} title - always equals `lychee.locale["SEARCH_RESULTS"]`
2323
* @property {Photo[]} photos - the found photos
2424
* @property {Album[]} albums - the found albums
@@ -46,16 +46,18 @@ search.find = function (term) {
4646

4747
/** @param {SearchResult} data */
4848
const successHandler = function (data) {
49-
// Do nothing, if search result is identical to previous result
5049
if (search.json && search.json.checksum === data.checksum) {
50+
// If search result is identical to previous result, just
51+
// update the album id with the new search term and bail out.
52+
album.json.id = SearchAlbumIDPrefix + "/" + term;
5153
return;
5254
}
5355

5456
search.json = data;
5557

5658
// Create and assign a `SearchAlbum`
5759
album.json = {
58-
id: SearchAlbumID,
60+
id: SearchAlbumIDPrefix + "/" + term,
5961
title: lychee.locale["SEARCH_RESULTS"],
6062
photos: search.json.photos,
6163
albums: search.json.albums,

scripts/main/sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ sidebar.triggerSearch = function (search_string) {
115115

116116
search.json = null;
117117
// We're either logged in or public search is allowed
118-
lychee.goto("search/" + encodeURIComponent(search_string));
118+
lychee.goto(SearchAlbumIDPrefix + "/" + encodeURIComponent(search_string));
119119
};
120120

121121
/**

scripts/main/visible.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ visible.config = function () {
3434

3535
/** @returns {boolean} */
3636
visible.search = function () {
37-
return search.json !== null;
37+
return visible.albums() && album.json !== null && album.isSearchID(album.json.id);
3838
};
3939

4040
/** @returns {boolean} */

0 commit comments

Comments
 (0)