Extract model from website generation

pull/47/head
Romain 8 years ago
parent b023b72cc0
commit fd9bf181fd

@ -4,8 +4,8 @@ var path = require('path');
var async = require('async');
var make = require('./utils/make');
var metadata = require('./input/metadata');
var hierarchy = require('./model/hierarchy.js')
var thumbs = require('./output-media/thumbs');
var hierarchy = require('./output-website/album-hierarchy.js')
var website = require('./output-website/website');
var collection = require('./collection');
@ -115,7 +115,7 @@ exports.build = function(opts) {
}),
callbackStep('Album hierarchy', function(next) {
albums = hierarchy.from(allFiles, opts);
albums = hierarchy.createAlbums(allFiles, opts);
next();
}),

@ -2,7 +2,7 @@ var _ = require('lodash');
var index = 0;
// number of images to show in the album preview grid
var PREVIEW_COUNT = 20;
var PREVIEW_COUNT = 4;
var SORT_ALBUMS_BY = {
title: function(album) { return album.title; },
@ -22,7 +22,7 @@ var PREVIEW_MISSING = {
function Album(opts) {
if (typeof opts === 'string') opts = { title: opts };
this.id = ++index;
this.id = opts.id || ++index;
this.title = opts.title || ('Album ' + this.id);
this.filename = sanitise(this.title);
this.files = opts.files || [];
@ -31,6 +31,7 @@ function Album(opts) {
this.home = false;
this.stats = null;
this.previews = null;
this.allFiles = [];
}
Album.prototype.finalize = function(options) {
@ -53,6 +54,7 @@ Album.prototype.finalize = function(options) {
this.calculateSummary();
this.sort(options);
this.pickPreviews();
this.aggregateAllFiles();
};
Album.prototype.calculateStats = function() {
@ -109,6 +111,11 @@ Album.prototype.pickPreviews = function() {
}
};
Album.prototype.aggregateAllFiles = function() {
var nestedFiles = _.flatten(_.map(this.albums, 'allFiles'))
this.allFiles = _.concat(nestedFiles, this.files);
};
function sanitise(filename) {
return filename.replace(/[^a-z0-9-_]/ig, '');
}
@ -119,4 +126,9 @@ function itemCount(count, type) {
return '' + count + ' ' + type + plural;
}
// for testing purposes
Album.resetIds = function() {
index = 0;
};
module.exports = Album;

@ -6,7 +6,7 @@ var Album = require('./album');
var byFolder = require('./by-folder');
var byDate = require('./by-date');
exports.from = function(collection, opts) {
exports.createAlbums = function(collection, opts) {
// top-level album for the home page
var home = new Album('Home');

@ -1,4 +1,4 @@
var File = require('../src/file');
var File = require('../src/model/file');
exports.metadata = function() {
return {

@ -1,5 +1,5 @@
var should = require('should/as-function');
var Album = require('../../src/output-website/album');
var Album = require('../../src/model/album');
var fixtures = require('../fixtures');
describe('Album', function() {

@ -1,10 +1,14 @@
var should = require('should/as-function');
var Album = require('../../src/output-website/album.js');
var bydate = require('../../src/output-website/by-date.js');
var Album = require('../../src/model/album.js');
var bydate = require('../../src/model/by-date.js');
var fixtures = require('../fixtures');
describe('ByDate', function() {
beforeEach(function() {
Album.resetIds();
});
it('creates top-level albums grouped by month', function () {
// create files from different dates
var a_2016_06 = fixtures.photo({date: fixtures.date('2016-06-01')});
@ -19,10 +23,12 @@ describe('ByDate', function() {
// assert on the result
should(albums).eql([
new Album({
id: 1,
'title': '2016-06',
files: [a_2016_06, b_2016_06]
}),
new Album({
id: 2,
title: '2016-07',
files: [c_2016_07, d_2016_07]
})
@ -43,24 +49,29 @@ describe('ByDate', function() {
// assert on the result
should(albums).eql([
new Album({
id: 1,
'title': '2015',
files: [],
albums: [
new Album({
id: 2,
title: '06',
files: [a_2015_06, b_2015_06]
})
]
}),
new Album({
id: 3,
title: '2016',
files: [],
albums: [
new Album({
id: 4,
title: '07',
files: [c_2016_07]
}),
new Album({
id: 5,
title: '08',
files: [d_2016_08]
})

@ -1,10 +1,14 @@
var should = require('should/as-function');
var Album = require('../../src/output-website/album.js');
var byfolder = require('../../src/output-website/by-folder.js');
var Album = require('../../src/model/album.js');
var byfolder = require('../../src/model/by-folder.js');
var fixtures = require('../fixtures');
describe('ByFolder', function() {
beforeEach(function() {
Album.resetIds();
});
it('creates albums by folders', function () {
// create files in different folders
var london1 = fixtures.photo({path: 'london/IMG_000001.jpg'});
@ -17,10 +21,12 @@ describe('ByFolder', function() {
// assert on the result
should(albums).eql([
new Album({
id: 1,
title: 'london',
files: [london1, london2]
}),
new Album({
id: 2,
title: 'newyork',
files: [newyork1, newyork2]
})
@ -37,20 +43,24 @@ describe('ByFolder', function() {
// assert on the result
should(albums).eql([
new Album({
id: 1,
title: 'a',
files: [],
albums: [
new Album({
id: 2,
title: 'b',
files: [],
albums: [
new Album({
id: 3,
title: 'c',
files: [photo1]
})
]
}),
new Album({
id: 4,
title: 'd',
files: [photo2]
})

@ -1,6 +1,6 @@
var should = require('should/as-function');
var File = require('../src/file');
var fixtures = require('./fixtures');
var File = require('../../src/model/file');
var fixtures = require('../fixtures');
describe('File', function() {
Loading…
Cancel
Save