function Album(path, subAlbums, images, name) { this.path = path; this.subAlbums = subAlbums; this.images = images; this.viewedItems = 0; this.name = name; this.domDef = null; } Album.prototype.getThumbnail = function () { if (this.images.length) { return this.images[0].getThumbnail(true); } else { return this.subAlbums[0].getThumbnail(); } }; Album.prototype.getThumbnailWidth = function () { return this.getThumbnail().then(function (img) { return img.width; }); }; Album.prototype.getDom = function (targetHeight) { var album = this; return this.getThumbnail().then(function (img) { var a = $('').addClass('album').attr('href', '#' + album.path); a.append($('
').addClass('row'); /** * @param row * @param {GalleryImage[]} items * @param i * @returns {*} */ var addImageToDom = function (row, items, i) { return items[i].getDom(targetHeight).then(function (itemDom) { i++; row.append(itemDom); if (i < items.length) { return addImageToDom(row, items, i); } else { return row; } }); }; return addImageToDom(row, this.items, 0); }; /** * @returns {boolean} */ Row.prototype.isFull = function () { return this.width > this.targetWidth; }; function GalleryImage(src, path) { this.src = src; this.path = path; this.thumbnail = null; this.domDef = null; this.domHeigth = null; } GalleryImage.prototype.getThumbnail = function (square) { return Thumbnail.get(this.src, square).queue(); }; GalleryImage.prototype.getThumbnailWidth = function () { return this.getThumbnail().then(function (img) { return img.width; }); }; GalleryImage.prototype.getDom = function (targetHeight) { var image = this; if (this.domDef === null || this.domHeigth !== targetHeight) { this.domHeigth = targetHeight; this.domDef = this.getThumbnail().then(function (img) { var a = $('').addClass('image').attr('href', '#' + image.src).attr('data-path', image.path); img.height = targetHeight; img.width = targetHeight * img.ratio; console.log(targetHeight * img.ratio); img.setAttribute('width', 'auto'); a.append(img); return a; }); } return this.domDef; };