2 * 2008-2014 Benoit Pin - MINES ParisTech
4 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
12 var reSelected
= /.*selected.*/;
14 Lightbox = function(grid
, toolbar
, complete
) {
17 this._buildSlidesIndex(); // set this.slides and this.lastSlide;
18 this.fetchingDisabled
= false;
19 this.complete
= complete
;
20 this.toolbar
= toolbar
;
22 this.toolbarFixed
= false;
23 addListener(window
, 'scroll', function(evt
){self
.windowScrollToolbarlHandler(evt
);});
25 addListener(window
, 'scroll', function(evt
){self
.windowScrollGridHandler(evt
);});
26 addListener(window
, 'load', function(evt
){ self
.windowScrollGridHandler();});
27 this.lastCBChecked
= undefined;
28 this.form
= undefined;
29 var parent
= this.grid
.parentNode
;
31 parent
= parent
.parentNode
;
32 if (parent
.tagName
=== 'FORM') {
36 else if (parent
.tagName
=== 'BODY') {
40 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
42 var fm
= this.fm
= new FormManager(this.form
);
43 addListener(this.form
, 'change', function(evt
){self
.onChangeHandler(evt
);});
44 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
45 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
49 Lightbox
.prototype._buildSlidesIndex = function() {
52 for (i
=0 ; i
<this.grid
.childNodes
.length
; i
++) {
53 node
= this.grid
.childNodes
[i
];
54 if (node
.nodeType
=== 1) { // is element
55 this.slides
.push(node
);
58 this.lastSlide
= this.slides
[this.slides
.length
-1];
61 Lightbox
.prototype.windowScrollToolbarlHandler = function(evt
) {
62 if (this.toolbar
.offsetTop
< getWindowScrollY() && !this.toolbarFixed
) {
63 this.toolbarFixed
= true;
64 this.backThreshold
= this.toolbar
.offsetTop
;
65 this.switchToolBarPositioning(true);
67 else if (this.toolbarFixed
&& getWindowScrollY() < this.backThreshold
) {
68 this.toolbarFixed
= false;
69 this.switchToolBarPositioning(false);
72 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
74 !this.fetchingDisabled
&&
76 (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
77 - getWindowHeight()) {
78 this.fetchingDisabled
= true;
83 Lightbox
.prototype.mouseClickHandler = function(evt
) {
84 var target
= getTargetedObject(evt
);
85 if (target
.tagName
=== 'IMG') {
87 var link
= target
.parentNode
;
88 var button
= link
.parentNode
;
89 var slide
= button
.parentNode
;
91 if (link
.tagName
=== 'A') {
92 switch(link
.getAttribute('name')) {
93 case 'add_to_selection':
96 req
= new XMLHttpRequest();
98 req
.open("POST", url
, true);
99 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
102 slide
.className
= 'selected';
104 link
.setAttribute('name', 'remove_to_selection');
105 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
106 link
.title
= img
.alt
= 'Retirer de la sélection';
107 button
.className
= "button slide-deselect";
110 case 'remove_to_selection':
113 req
= new XMLHttpRequest();
115 req
.open("POST", url
, true);
116 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
118 slide
.className
= null;
119 link
.setAttribute('name', 'add_to_selection');
120 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
121 link
.title
= img
.alt
= 'Ajouter à la sélection';
122 button
.className
= "button slide-select";
127 slide
.widget
= new CartWidget(slide
, link
.href
);
130 case 'hide_for_anonymous':
133 req
= new XMLHttpRequest();
135 req
.open("POST", url
, true);
136 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
138 slide
.className
= 'hidden-slide';
139 link
.setAttribute('name', 'show_for_anonymous');
140 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
141 link
.title
= img
.alt
= 'Montrer au anonymes';
142 button
.className
= "button slide-show";
145 case 'show_for_anonymous':
148 req
= new XMLHttpRequest();
150 req
.open("POST", url
, true);
151 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
153 slide
.className
= null;
154 link
.setAttribute('name', 'hide_for_anonymous');
155 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
156 link
.title
= img
.alt
= 'Masquer pour les anonymes';
157 button
.className
= "button slide-hide";
161 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
164 cb
.setAttribute('checked', 'checked');
167 cb
.removeAttribute('checked');
169 this.selectCBRange(evt
);
173 Lightbox
.prototype.onChangeHandler = function(evt
) {
174 var target
= getTargetedObject(evt
);
175 if (target
.name
=== 'sort_on') {
176 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
181 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
182 switch(fm
.submitButton
.name
) {
184 this.hideSelection();
189 Lightbox
.prototype.onResponseLoad = function(req
) {
190 switch(req
.responseXML
.documentElement
.nodeName
) {
192 this.deleteSelection();
195 this.showSelection();
198 this.fm
.submitButton
= undefined;
204 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
205 var tbs
= this.toolbar
.style
;
207 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
208 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
209 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
210 tbs
.position
= 'fixed';
212 this.toolbarPlaceholder
= document
.createElement('div');
213 var phs
= this.toolbarPlaceholder
.style
;
214 phs
.cssText
= tbs
.cssText
;
215 phs
.position
= 'relative';
216 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
219 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
220 tbs
.cssText
= this.toolbar
.defaultCssText
;
225 Lightbox
.prototype.hideSelection = function() {
227 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
228 e
= this.form
.elements
[i
];
229 if (e
.type
=== 'checkbox' && e
.checked
) {
230 slide
= e
.parentNode
.parentNode
;
231 slide
.classList
.add('zero_opacity');
236 Lightbox
.prototype.showSelection = function() {
238 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
239 e
= this.form
.elements
[i
];
240 if (e
.type
=== 'checkbox' && e
.checked
) {
241 slide
= e
.parentNode
.parentNode
;
242 slide
.classList
.remove('zero_opacity');
247 Lightbox
.prototype.deleteSelection = function() {
249 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
250 e
= this.form
.elements
[i
];
251 if (e
.type
=== 'checkbox' && e
.checked
) {
252 slide
= e
.parentNode
.parentNode
;
253 slide
.classList
.add('zero_width');
257 // if you change this, delay you should also change this css rule :
258 // .lightbox span { transition: width 1s
259 setTimeout(function(){self
._removeSelection();}, 1000);
262 Lightbox
.prototype._removeSelection = function() {
265 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
266 e
= this.form
.elements
[i
];
267 if (e
.type
=== 'checkbox' && e
.checked
) {
268 toRemove
.push(e
.parentNode
.parentNode
);
271 for (i
=0 ; i
<toRemove
.length
; i
++) {
272 this.grid
.removeChild(toRemove
[i
]);
274 this._buildSlidesIndex();
275 this.cbIndex
= undefined;
276 this.windowScrollGridHandler();
279 Lightbox
.prototype.getCBIndex = function(cb
) {
281 // build checkbox index
284 for (i
=0 ; i
<this.slides
.length
; i
++) {
285 node
= this.slides
[i
];
286 c
= node
.getElementsByTagName('input')[0];
287 c
.index
= this.cbIndex
.length
;
288 this.cbIndex
.push(c
);
294 Lightbox
.prototype.selectCBRange = function(evt
) {
295 var target
= getTargetedObject(evt
);
296 evt
= getEventObject(evt
);
297 var shift
= evt
.shiftKey
;
298 if (shift
&& this.lastCBChecked
) {
299 var from = this.getCBIndex(this.lastCBChecked
);
300 var to
= this.getCBIndex(target
);
301 var start
= Math
.min(from, to
);
302 var stop
= Math
.max(from, to
);
304 for (i
=start
; i
<stop
; i
++ ) {
305 this.cbIndex
[i
].setAttribute('checked', 'checked');
308 else if (target
.checked
) {
309 this.lastCBChecked
= target
;
312 this.lastCBChecked
= undefined;
316 Lightbox
.prototype.refreshGrid = function() {
317 var req
= new XMLHttpRequest();
319 req
.onreadystatechange = function() {
320 switch (req
.readyState
) {
326 if (req
.status
=== 200) {
327 self
._refreshGrid(req
);
333 var url
= absolute_url() +
334 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
336 req
.open('GET', url
, true);
340 Lightbox
.prototype._refreshGrid = function(req
) {
341 var doc
= req
.responseXML
.documentElement
;
344 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
345 node
= doc
.childNodes
[i
];
346 if (node
.nodeType
=== 1) {
347 node
= getCopyOfNode(node
);
348 this.grid
.replaceChild(node
, this.slides
[j
]);
349 this.slides
[j
] = node
;
353 this.cbIndex
= undefined;
356 Lightbox
.prototype.fetchTail = function() {
357 var req
= new XMLHttpRequest();
359 req
.onreadystatechange = function() {
360 switch (req
.readyState
) {
366 if (req
.status
=== 200) {
367 self
._appendTail(req
);
373 var url
= absolute_url() +
374 '/portfolio_thumbnails_tail?start:int=' +
375 String(this.slides
.length
+ 1 ) +
377 req
.open('GET', url
, true);
381 Lightbox
.prototype._appendTail = function(req
) {
382 var doc
= req
.responseXML
.documentElement
;
384 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
385 node
= doc
.childNodes
[i
];
386 if (node
.nodeType
=== 1) {
387 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
388 this.slides
.push(this.lastSlide
);
390 c
= this.lastSlide
.getElementsByTagName('input')[0];
391 c
.index
= this.cbIndex
.length
;
392 this.cbIndex
.push(c
);
397 this.fetchingDisabled
= false;
398 if (doc
.getAttribute('nomore')) {
399 this.complete
= true;
401 this.windowScrollGridHandler();
405 var _outlineSelectedSlide
;
406 if (browser
.isGecko
) {
407 _outlineSelectedSlide = function(slide
) {
408 slide
.className
= 'selected';
412 _outlineSelectedSlide = function(slide
) {
413 if (slide
.className
&&
414 !reSelected
.test(slide
.className
)) {
415 slide
.className
= slide
.className
+ ' selected';