From 315e08e0e5512a341057aabeae18eb08d506bbcc Mon Sep 17 00:00:00 2001 From: Romain Date: Thu, 3 Nov 2016 16:00:50 +1100 Subject: [PATCH] Fix #30 Fix #31 allow asc/desc order when sorting albums and media --- bin/thumbsup.js | 33 +++++++++++++++++++++++++-------- src/model/album.js | 17 +++++++---------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/bin/thumbsup.js b/bin/thumbsup.js index 8b17716..800c232 100755 --- a/bin/thumbsup.js +++ b/bin/thumbsup.js @@ -72,19 +72,29 @@ var opts = yargs }, // Deprecated for 'sort-folders': { - description: 'How to sort gallery folders', + description: 'How to sort albums [deprecated]', choices: ['name', 'date'] }, - 'sort-albums': { + 'sort-albums-by': { description: 'How to sort albums', - choices: ['name', 'date'], + choices: ['title', 'start-date', 'end-date'], default: 'date' }, - 'sort-media': { + 'sort-albums-direction': { + description: 'Album sorting direction', + choices: ['asc', 'desc'], + default: 'asc' + }, + 'sort-media-by': { description: 'How to sort photos and videos', - choices: ['name', 'date'], + choices: ['filename', 'date'], default: 'date' }, + 'sort-media-direction': { + description: 'Media sorting direction', + choices: ['asc', 'desc'], + default: 'asc' + }, 'theme': { description: 'Name of the gallery theme to apply', choices: ['classic', 'cards', 'mosaic'], @@ -106,9 +116,14 @@ var opts = yargs .config('config') .epilogue('The optional JSON config should contain a single object with one key ' + 'per argument, not including the trailing "--". For example:\n\n' + - '{ "sort-albums": "date" }') + '{ "sort-albums-by": "start-date" }') .argv; + +// Compatibility +if (opts['sort-folders'] == 'name') opts['sort-albums-by'] = 'title'; +if (opts['sort-folders'] == 'name') opts['sort-albums-by'] = 'start-date'; + index.build({ input: path.resolve(opts['input']), output: path.resolve(opts['output']), @@ -117,10 +132,12 @@ index.build({ largeSize: opts['large-size'], originalPhotos: opts['original-photos'], originalVideos: opts['original-videos'], - sortAlbums: opts['sort-folders'] || opts['sort-albums'], - sortMedia: opts['sort-media'], albumsFrom: opts['albums-from'], albumsDateFormat: opts['albums-date-format'], + sortAlbumsBy: opts['sort-albums-by'], + sortAlbumsDirection: opts['sort-albums-direction'], + sortMediaBy: opts['sort-media-by'], + sortMediaDirection: opts['sort-media-direction'], theme: opts['theme'], css: opts['css'], googleAnalytics: opts['google-analytics'], diff --git a/src/model/album.js b/src/model/album.js index 437c252..33fafd8 100644 --- a/src/model/album.js +++ b/src/model/album.js @@ -5,13 +5,14 @@ var index = 0; var PREVIEW_COUNT = 4; var SORT_ALBUMS_BY = { - title: function(album) { return album.title; }, - date: function(album) { return album.stats.fromDate; } + 'title': function(album) { return album.title; }, + 'start-date': function(album) { return album.stats.fromDate; }, + 'end-date': function(album) { return album.stats.toDate; } }; var SORT_MEDIA_BY = { - filename: function(file) { return file.filename; }, - date: function(file) { return file.date; } + 'filename': function(file) { return file.filename; }, + 'date': function(file) { return file.date; } }; var PREVIEW_MISSING = { @@ -35,10 +36,6 @@ function Album(opts) { } Album.prototype.finalize = function(options) { - options = _.defaults(options, { - sortAlbums: 'date', - sortMedia: 'date' - }); // is this the top-level album? this.home = this.depth === 0; // finalize all nested albums first (recursive) @@ -89,8 +86,8 @@ Album.prototype.calculateSummary = function() { }; Album.prototype.sort = function(options) { - this.files = _.sortBy(this.files, SORT_MEDIA_BY[options.sortMedia]); - this.albums = _.sortBy(this.albums, SORT_ALBUMS_BY[options.sortAlbums]); + this.files = _.orderBy(this.files, SORT_MEDIA_BY[options.sortMediaBy], options.sortMediaDirection); + this.albums = _.orderBy(this.albums, SORT_ALBUMS_BY[options.sortAlbumsBy], options.sortAlbumsDirection); this.albums.forEach(function(nested) { nested.sort(options); });