diff --git a/frontend/src/ketr-photos/ketr-photos.html b/frontend/src/ketr-photos/ketr-photos.html index ab3db11..ae3122f 100755 --- a/frontend/src/ketr-photos/ketr-photos.html +++ b/frontend/src/ketr-photos/ketr-photos.html @@ -686,7 +686,6 @@ this.visibleThumbs = []; this.thumbnails = []; this.notifyPath("thumbnails.length"); - this.cursor = null; Polymer.dom(this.$.thumbnails).innerHTML = ""; this.next = false; this.limit = undefined; @@ -825,6 +824,14 @@ return; } + if (this.processing) { + console.log("hide-or-show while processing"); + this.async(this.triggerVisibilityChecks.bind(this), 100); + return; + } + + this.processing = true; + var index, start, stop, length = this.thumbnails.length; start = 0; @@ -863,6 +870,7 @@ if (pos != 0) { console.log("DOM changed or viewport changed and search would never exit; re-scheduling check") //this.triggerVisibilityChecks(); + this.processing = false; return; } @@ -916,6 +924,8 @@ }.bind(this)); this.visibleThumbs = visible; + + this.processing = false; }); }, @@ -1039,10 +1049,18 @@ }, processItems: function() { + if (this.processing) { + console.log("processItems while processing"); + this.async(this.processItems.bind(this), 100); + return; + } + if (this.pendingPhotos.length == 0) { return; } + this.processing = true; + var lastPath = null; var albums = this.querySelectorAll(".album-line"); if (albums.length) { @@ -1053,13 +1071,19 @@ this.notifyPath("pendingPhotos.length"); for (var i = 0; i < photos.length; i++) { - var photo = photos[i], - thumbnail = document.createElement("photo-thumbnail"), - datetime; - thumbnail.item = photo; -// thumbnail.width = this.calcWidth; - thumbnail.addEventListener("load-image", this._imageTap.bind(this)); - thumbnail.addEventListener("load-album", this.loadAlbum.bind(this)); + var photo = photos[i], datetime; + + var j; + for (j = 0; j < this.thumbnails.length; j++) { + if (this.thumbnails[j].item.id == photo.id) { + break; + } + } + if (j != this.thumbnails.length) { + console.log("Photo already exists in thumbnails list. Did server return dups?"); + continue; + } + try { datetime = (photo.taken || photo.modified || photo.added).replace(/T.*/, ""); } catch (error) { @@ -1122,6 +1146,11 @@ } } + var thumbnail = document.createElement("photo-thumbnail"); + thumbnail.item = photo; + thumbnail.addEventListener("load-image", this._imageTap.bind(this)); + thumbnail.addEventListener("load-album", this.loadAlbum.bind(this)); + Polymer.dom(thumbnails).appendChild(thumbnail); this.thumbnails.push(thumbnail); this.notifyPath("thumbnails.length"); @@ -1129,6 +1158,8 @@ /* If the viewport is at the bottom when it finishes processing, trigger to load more. */ this.onScroll(); + + this.processing = false; }, pathTapped: function(event) { @@ -1225,8 +1256,7 @@ this.appendItems(results.items); if (results.more) { - var cursor = results.items[results.items.length - 1]; - this._loadPhotos(cursor.taken.toString().replace(/T.*/, "") + "_" + cursor.id, true, this.limit * 2); + this._loadPhotos(results.cursor, true, this.limit * 2); } }.bind(this, path)); diff --git a/server/routes/photos.js b/server/routes/photos.js index 9e1cbd5..d0656ea 100755 --- a/server/routes/photos.js +++ b/server/routes/photos.js @@ -24,13 +24,13 @@ const router = express.Router(); */ -router.get("/memories/*", function(req, res/*, next*/) { +router.get("/memories/:date?", function(req, res/*, next*/) { let limit = parseInt(req.query.limit) || 50, id, cursor, index; if (req.query.next) { let parts = req.query.next.split("_"); - cursor = parts[0]; + cursor = new Date(parts[0]); id = parseInt(parts[1]); } else { cursor = ""; @@ -38,19 +38,23 @@ router.get("/memories/*", function(req, res/*, next*/) { } if (id == -1) { - index = "strftime('%m%d',taken)=strftime('%m%d',:date)"; + index = "strftime('%m-%d',taken)=strftime('%m-%d',:date)"; } else { - index = "(strftime('%Y%m%d',taken)=strftime('%Y%m%d',:date) AND photos.id<"+id+ ")" + - " OR (strftime('%m%d',taken)=strftime('%m%d',:date) AND strftime('%Y',taken)!=strftime('%Y',:date))"; + index = "((strftime('%Y-%m-%d',taken)=strftime('%Y-%m-%d',:cursor) AND photos.id<"+id+ ") OR " + + "(strftime('%m-%d',taken)=strftime('%m-%d',:cursor) AND strftime('%Y',taken) limit; /* We queried one extra item to see if there are more than LIMIT available */ + let last; if (more) { photos.splice(limit); + last = photos[photos.length - 1]; } let results = { - items: photos + items: photos.sort(function(a, b) { + return new Date(b.taken) - new Date(a.taken); + }) }; + if (more) { + results.cursor = new Date(last.taken).toISOString().replace(/T.*/, "") + "_" + last.id; results.more = true; } return res.status(200).json(results); @@ -112,13 +112,13 @@ router.get("/*", function(req, res/*, next*/) { if (id == -1) { index = ""; } else { - index = "AND ((strftime('%m%d',taken)=strftime('%m%d',:cursor) AND photos.id<"+id+ ") OR DATE(taken)