-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
134 lines (134 loc) · 4.6 KB
/
gallery.js
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
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
self.Gallery = (function() {
function Gallery(items, renderer) {
this.items = items;
this.renderer = renderer;
this.loadItem = __bind(this.loadItem, this);
this.preloadNext = __bind(this.preloadNext, this);
this.setPreloaded = __bind(this.setPreloaded, this);
this.preloadedThumb = __bind(this.preloadedThumb, this);
this.thumbLoad = __bind(this.thumbLoad, this);
this.loading = false;
this.renderer.parent = this;
this.createIndex();
this.preloadThumbs();
this.renderer.load();
}
Gallery.prototype.createIndex = function() {
var item, tracker, _i, _len, _ref, _results;
this.index = {};
this.currentImage = 0;
tracker = 0;
_ref = this.items;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
item.preloaded = false;
this.index[item.full] = tracker;
_results.push(tracker++);
}
return _results;
};
Gallery.prototype.addItem = function(item) {
this.items[this.items.length] = item;
return this.createIndex();
};
Gallery.prototype.thumbLoad = function(thumb) {
return __bind(function() {
return this.loadItem(thumb.indexNumber);
}, this);
};
Gallery.prototype.preloadThumbs = function() {
var item, tempThumb, _i, _len, _ref, _results;
this.loadedThumbs = 0;
this.thumbs = [];
_ref = this.items;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
tempThumb = new Image();
tempThumb.addEventListener("load", this.preloadedThumb, false);
tempThumb.src = item.thumb;
_results.push(this.thumbs[this.index[item.full]] = item.thumb);
}
return _results;
};
Gallery.prototype.preloadedThumb = function() {
this.loadedThumbs++;
if (this.loadedThumbs === this.items.length) {
return this.renderer.addThumbs(this.thumbs);
}
};
Gallery.prototype.preload = function(item) {
var tempImage;
this.renderer.startLoading();
this.datestart = new Date();
this.loading = true;
tempImage = document.createElement("img");
tempImage.onload = __bind(function() {
return this.setPreloaded(item);
}, this);
return tempImage.src = item.full;
};
Gallery.prototype.setPreloaded = function(item) {
var diff;
this.dateend = new Date();
diff = this.dateend.getTime() - this.datestart.getTime();
console.log("Loaded image #" + (this.index[item.full] + 1) + " (" + diff + " ms).");
delete this.dateend;
delete this.datestart;
item.preloaded = true;
this.loading = false;
this.renderer.endLoading();
return this.load(item);
};
Gallery.prototype.preloadNext = function(item) {
var tempImage;
this.datestarttwo = new Date();
tempImage = document.createElement("img");
tempImage.onload = __bind(function() {
var diff;
this.dateendtwo = new Date();
diff = this.dateendtwo.getTime() - this.datestarttwo.getTime();
console.log("Loaded image #" + (this.index[item.full] + 1) + " (" + diff + " ms).");
delete this.dateendtwo;
delete this.datestarttwo;
return item.preloaded = true;
}, this);
return tempImage.src = item.full;
};
Gallery.prototype.load = function(item) {
if (!item.preloaded && !this.loading) {
return this.preload(item);
} else if (!this.loading) {
this.renderer.display(item);
this.renderer.setActiveThumb(this.index[item.full]);
if ((this.index[item.full] + 1) < this.items.length && !this.items[this.index[item.full] + 1].preloaded) {
this.preloadNext(this.items[this.index[item.full] + 1]);
}
return this.currentImage = this.index[item.full];
} else {
return this.load(item);
}
};
Gallery.prototype.loadItem = function(number) {
return this.load(this.items[number]);
};
Gallery.prototype.prev = function() {
this.currentImage--;
if (this.currentImage < 0) {
this.currentImage = this.items.length - 1;
}
return this.load(this.items[this.currentImage]);
};
Gallery.prototype.next = function() {
this.currentImage++;
if (this.currentImage > this.items.length - 1) {
this.currentImage = 0;
}
return this.load(this.items[this.currentImage]);
};
return Gallery;
})();
}).call(this);