Chargement des vignettes après coup pour les tables lumineuses.
[Portfolio.git] / skins / photo_lightbox_viewer.js
1 /*
2 * 2008-2014 Benoit Pin - MINES ParisTech
3 * http://plinn.org
4 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
5 */
6
7
8 var Lightbox;
9
10 (function(){
11
12 var reSelected = /.*selected.*/;
13
14 Lightbox = function(grid, toolbar, complete, container_type) {
15 var self = this;
16 this.grid = grid;
17 this._buildSlidesIndex(); // set this.slides and this.lastSlide;
18 this.fetchingDisabled = false;
19 this.complete = complete;
20 this.container_type = container_type;
21 this.toolbar = toolbar;
22 if (toolbar) {
23 this.toolbarFixed = false;
24 addListener(window, 'scroll', function(evt){self.windowScrollToolbarlHandler(evt);});
25 }
26 addListener(window, 'scroll', function(evt){self.windowScrollGridHandler(evt);});
27 // addListener(window, 'load', function(evt){ self.windowScrollGridHandler();});
28 registerStartupFunction(function(){ self.windowScrollGridHandler();});
29 this.lastCBChecked = undefined;
30 this.form = undefined;
31 var parent = this.grid.parentNode;
32 while(parent) {
33 parent = parent.parentNode;
34 if (parent.tagName === 'FORM') {
35 this.form = parent;
36 break;
37 }
38 else if (parent.tagName === 'BODY') {
39 break;
40 }
41 }
42 addListener(this.grid, 'click', function(evt){self.mouseClickHandler(evt);});
43 if (this.form) {
44 var fm = this.fm = new FormManager(this.form);
45 addListener(this.form, 'change', function(evt){self.onChangeHandler(evt);});
46 fm.onBeforeSubmit = function(fm_, evt) {return self.onBeforeSubmit(fm_, evt);};
47 fm.onResponseLoad = function(req) {return self.onResponseLoad(req);};
48 }
49 };
50
51 Lightbox.prototype._buildSlidesIndex = function() {
52 this.slides = [];
53 var node, i;
54 for (i=0 ; i<this.grid.childNodes.length ; i++) {
55 node = this.grid.childNodes[i];
56 if (node.nodeType === 1) { // is element
57 this.slides.push(node);
58 }
59 }
60 this.lastSlide = this.slides[this.slides.length-1];
61 };
62
63 Lightbox.prototype.windowScrollToolbarlHandler = function(evt) {
64 if (this.toolbar.offsetTop < getWindowScrollY() && !this.toolbarFixed) {
65 this.toolbarFixed = true;
66 this.backThreshold = this.toolbar.offsetTop;
67 this.switchToolBarPositioning(true);
68 }
69 else if (this.toolbarFixed && getWindowScrollY() < this.backThreshold) {
70 this.toolbarFixed = false;
71 this.switchToolBarPositioning(false);
72 }
73 };
74 Lightbox.prototype.windowScrollGridHandler = function(evt) {
75 if (!this.complete &&
76 !this.fetchingDisabled &&
77 getWindowScrollY() >
78 (this.lastSlide.firstElementChild || this.lastSlide.children[0]).offsetTop
79 - getWindowHeight()) {
80 this.fetchingDisabled = true;
81 this.fetchTail();
82 }
83 };
84
85 Lightbox.prototype.mouseClickHandler = function(evt) {
86 var target = getTargetedObject(evt);
87 if (target.tagName === 'IMG') {
88 var img = target;
89 var link = target.parentNode;
90 var button = link.parentNode;
91 var slide = button.parentNode;
92 var req, url;
93 if (link.tagName === 'A') {
94 switch(link.getAttribute('name')) {
95 case 'add_to_selection':
96 disableDefault(evt);
97 link.blur();
98 req = new XMLHttpRequest();
99 url = link.href;
100 req.open("POST", url, true);
101 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
102 req.send("ajax=1");
103
104 slide.className = 'selected';
105
106 link.setAttribute('name', 'remove_to_selection');
107 link.href = url.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
108 link.title = img.alt = 'Retirer de la sélection';
109 button.className = "button slide-deselect";
110 break;
111
112 case 'remove_to_selection':
113 disableDefault(evt);
114 link.blur();
115 req = new XMLHttpRequest();
116 url = link.href;
117 req.open("POST", url, true);
118 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
119 req.send("ajax=1");
120 slide.className = null;
121 link.setAttribute('name', 'add_to_selection');
122 link.href = url.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
123 link.title = img.alt = 'Ajouter à la sélection';
124 button.className = "button slide-select";
125 break;
126
127 case 'add_to_cart' :
128 disableDefault(evt);
129 slide.widget = new CartWidget(slide, link.href);
130 break;
131
132 case 'hide_for_anonymous':
133 disableDefault(evt);
134 link.blur();
135 req = new XMLHttpRequest();
136 url = link.href;
137 req.open("POST", url, true);
138 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
139 req.send(null);
140 slide.className = 'hidden-slide';
141 link.setAttribute('name', 'show_for_anonymous');
142 link.href = url.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
143 link.title = img.alt = 'Montrer au anonymes';
144 button.className = "button slide-show";
145 break;
146
147 case 'show_for_anonymous':
148 disableDefault(evt);
149 link.blur();
150 req = new XMLHttpRequest();
151 url = link.href;
152 req.open("POST", url, true);
153 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
154 req.send(null);
155 slide.className = null;
156 link.setAttribute('name', 'hide_for_anonymous');
157 link.href = url.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
158 link.title = img.alt = 'Masquer pour les anonymes';
159 button.className = "button slide-hide";
160 break;
161 }
162 }
163 } else if(target.tagName === 'INPUT' && target.type === 'checkbox') {
164 var cb = target;
165 if (cb.checked) {
166 cb.setAttribute('checked', 'checked');
167 }
168 else {
169 cb.removeAttribute('checked');
170 }
171 this.selectCBRange(evt);
172 }
173 };
174
175 Lightbox.prototype.onChangeHandler = function(evt) {
176 var target = getTargetedObject(evt);
177 if (target.name === 'sort_on') {
178 this.fm.submitButton = {'name' : 'set_sorting', 'value' : 'ok'};
179 this.fm.submit(evt);
180 }
181 };
182
183 Lightbox.prototype.onBeforeSubmit = function(fm, evt) {
184 switch(fm.submitButton.name) {
185 case 'delete' :
186 this.hideSelection();
187 break;
188 }
189 };
190
191 Lightbox.prototype.onResponseLoad = function(req) {
192 switch(req.responseXML.documentElement.nodeName) {
193 case 'deleted' :
194 this.deleteSelection();
195 break;
196 case 'error' :
197 this.showSelection();
198 break;
199 case 'sorted' :
200 this.fm.submitButton = undefined;
201 this.refreshGrid();
202 break;
203 }
204 };
205
206 Lightbox.prototype.switchToolBarPositioning = function(fixed) {
207 var tbs = this.toolbar.style;
208 if (fixed) {
209 this.toolbar.defaultCssText = this.toolbar.style.cssText;
210 tbs.width = String(this.toolbar.offsetWidth) + 'px';
211 tbs.height = String(this.toolbar.offsetHeight) + 'px';
212 tbs.position = 'fixed';
213 tbs.top = '0';
214 this.toolbarPlaceholder = document.createElement('div');
215 var phs = this.toolbarPlaceholder.style;
216 phs.cssText = tbs.cssText;
217 phs.position = 'relative';
218 this.toolbar.parentNode.insertBefore(this.toolbarPlaceholder, this.toolbar);
219 }
220 else {
221 this.toolbarPlaceholder.parentNode.removeChild(this.toolbarPlaceholder);
222 tbs.cssText = this.toolbar.defaultCssText;
223 }
224 };
225
226
227 Lightbox.prototype.hideSelection = function() {
228 var i, e, slide;
229 for (i=0 ; i<this.form.elements.length ; i++) {
230 e = this.form.elements[i];
231 if (e.type === 'checkbox' && e.checked) {
232 slide = e.parentNode.parentNode;
233 slide.classList.add('zero_opacity');
234 }
235 }
236 };
237
238 Lightbox.prototype.showSelection = function() {
239 var i, e, slide;
240 for (i=0 ; i<this.form.elements.length ; i++) {
241 e = this.form.elements[i];
242 if (e.type === 'checkbox' && e.checked) {
243 slide = e.parentNode.parentNode;
244 slide.classList.remove('zero_opacity');
245 }
246 }
247 };
248
249 Lightbox.prototype.deleteSelection = function() {
250 var i, e, slide;
251 for (i=0 ; i<this.form.elements.length ; i++) {
252 e = this.form.elements[i];
253 if (e.type === 'checkbox' && e.checked) {
254 slide = e.parentNode.parentNode;
255 slide.classList.add('zero_width');
256 }
257 }
258 var self = this;
259 // if you change this, delay you should also change this css rule :
260 // .lightbox span { transition: width 1s
261 setTimeout(function(){self._removeSelection();}, 1000);
262 };
263
264 Lightbox.prototype._removeSelection = function() {
265 var i, e;
266 var toRemove = [];
267 for (i=0 ; i<this.form.elements.length ; i++) {
268 e = this.form.elements[i];
269 if (e.type === 'checkbox' && e.checked) {
270 toRemove.push(e.parentNode.parentNode);
271 }
272 }
273 for (i=0 ; i<toRemove.length ; i++) {
274 this.grid.removeChild(toRemove[i]);
275 }
276 this._buildSlidesIndex();
277 this.cbIndex = undefined;
278 this.windowScrollGridHandler();
279 };
280
281 Lightbox.prototype.getCBIndex = function(cb) {
282 if (!this.cbIndex) {
283 // build checkbox index
284 this.cbIndex = [];
285 var i, node, c;
286 for (i=0 ; i<this.slides.length ; i++) {
287 node = this.slides[i];
288 c = node.getElementsByTagName('input')[0];
289 c.index = this.cbIndex.length;
290 this.cbIndex.push(c);
291 }
292 }
293 return cb.index;
294 };
295
296 Lightbox.prototype.selectCBRange = function(evt) {
297 var target = getTargetedObject(evt);
298 evt = getEventObject(evt);
299 var shift = evt.shiftKey;
300 if (shift && this.lastCBChecked) {
301 var from = this.getCBIndex(this.lastCBChecked);
302 var to = this.getCBIndex(target);
303 var start = Math.min(from, to);
304 var stop = Math.max(from, to);
305 var i;
306 for (i=start ; i<stop ; i++ ) {
307 this.cbIndex[i].setAttribute('checked', 'checked');
308 }
309 }
310 else if (target.checked) {
311 this.lastCBChecked = target;
312 }
313 else {
314 this.lastCBChecked = undefined;
315 }
316 };
317
318 Lightbox.prototype.refreshGrid = function() {
319 var req = new XMLHttpRequest();
320 self = this;
321 req.onreadystatechange = function() {
322 switch (req.readyState) {
323 case 1 :
324 showProgressImage();
325 break;
326 case 4 :
327 hideProgressImage();
328 if (req.status === 200) {
329 self._refreshGrid(req);
330 }
331 break;
332 }
333 };
334
335 var url = absolute_url() +
336 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
337 this.slides.length;
338 req.open('GET', url, true);
339 req.send();
340 };
341
342 Lightbox.prototype._refreshGrid = function(req) {
343 var doc = req.responseXML.documentElement;
344 var i, node;
345 var j = 0;
346 for (i=0 ; i<doc.childNodes.length ; i++) {
347 node = doc.childNodes[i];
348 if (node.nodeType === 1) {
349 node = getCopyOfNode(node);
350 this.grid.replaceChild(node, this.slides[j]);
351 this.slides[j] = node;
352 j++;
353 }
354 }
355 this.cbIndex = undefined;
356 };
357
358 Lightbox.prototype.fetchTail = function() {
359 var req = new XMLHttpRequest();
360 var self = this;
361 req.onreadystatechange = function() {
362 switch (req.readyState) {
363 case 1 :
364 showProgressImage();
365 break;
366 case 4 :
367 hideProgressImage();
368 if (req.status === 200) {
369 self._appendTail(req);
370 }
371 break;
372 }
373 };
374
375 var url = absolute_url() +
376 '/portfolio_thumbnails_tail?start:int=' +
377 String(this.slides.length) +
378 '&size:int=10' +
379 '&container_type=' +
380 this.container_type;
381 req.open('GET', url, true);
382 req.send();
383 };
384
385 Lightbox.prototype._appendTail = function(req) {
386 var doc = req.responseXML.documentElement;
387 var i, node, c;
388 for (i=0 ; i<doc.childNodes.length ; i++) {
389 node = doc.childNodes[i];
390 if (node.nodeType === 1) {
391 this.lastSlide = this.grid.appendChild(getCopyOfNode(node));
392 this.slides.push(this.lastSlide);
393 if (this.cbIndex) {
394 c = this.lastSlide.getElementsByTagName('input')[0];
395 c.index = this.cbIndex.length;
396 this.cbIndex.push(c);
397
398 }
399 }
400 }
401 this.fetchingDisabled = false;
402 if (doc.getAttribute('nomore')) {
403 this.complete = true;
404 }
405 this.windowScrollGridHandler();
406 };
407
408
409 var _outlineSelectedSlide;
410 if (browser.isGecko) {
411 _outlineSelectedSlide = function(slide) {
412 slide.className = 'selected';
413 };
414 }
415 else {
416 _outlineSelectedSlide = function(slide) {
417 if (slide.className &&
418 !reSelected.test(slide.className)) {
419 slide.className = slide.className + ' selected';
420 }
421 };
422 }
423
424 }());