Blame view
sources/apps/gallery/js/album.js
3.85 KB
|
6d9380f96
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
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 = $('<a/>').addClass('album').attr('href', '#' + album.path);
a.append($('<label/>').text(album.name));
a.append(img);
img.height = targetHeight;
img.width = targetHeight * img.ratio;
return a;
});
};
/**
*
* @param {number} width
* @returns {$.Deferred<Row>}
*/
Album.prototype.getNextRow = function (width) {
/**
* Add images to the row until it's full
*
* @param {Album} album
* @param {Row} row
* @param {GalleryImage[]} images
* @returns {$.Deferred<Row>}
*/
var addImages = function (album, row, images) {
// pre-load thumbnails in parallel
for (var i = 0; i < 3 ;i++){
if (images[album.viewedItems + i]) {
images[album.viewedItems + i].getThumbnail();
}
}
var image = images[album.viewedItems];
return row.addImage(image).then(function (more) {
album.viewedItems++;
if (more && album.viewedItems < images.length) {
return addImages(album, row, images);
} else {
return row;
}
});
};
var items = this.subAlbums.concat(this.images);
var row = new Row(width);
return addImages(this, row, items);
};
function Row(targetWidth) {
this.targetWidth = targetWidth;
this.items = [];
this.width = 8; // 4px margin to start with
}
/**
* @param {GalleryImage} image
* @return {$.Deferred<bool>} true if more images can be added to the row
*/
Row.prototype.addImage = function (image) {
var row = this;
var def = new $.Deferred();
image.getThumbnailWidth().then(function (width) {
row.items.push(image);
row.width += width + 4; // add 4px for the margin
def.resolve(!row.isFull());
}, function () {
console.log('Error getting thumbnail for ' + image);
def.resolve(true);
});
return def;
};
Row.prototype.getDom = function () {
var scaleRation = (this.width > this.targetWidth) ? this.targetWidth / this.width : 1;
var targetHeight = 200 * scaleRation;
var row = $('<div/>').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 = $('<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;
};
|