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 registerStartupFunction(function(){ self
.windowScrollGridHandler();});
28 this.lastCBChecked
= undefined;
29 this.form
= undefined;
30 var parent
= this.grid
.parentNode
;
32 parent
= parent
.parentNode
;
33 if (parent
.tagName
=== 'FORM') {
37 else if (parent
.tagName
=== 'BODY') {
41 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
43 var fm
= this.fm
= new FormManager(this.form
);
44 addListener(this.form
, 'change', function(evt
){self
.onChangeHandler(evt
);});
45 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
46 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
50 Lightbox
.prototype._buildSlidesIndex = function() {
53 for (i
=0 ; i
<this.grid
.childNodes
.length
; i
++) {
54 node
= this.grid
.childNodes
[i
];
55 if (node
.nodeType
=== 1) { // is element
56 this.slides
.push(node
);
59 this.lastSlide
= this.slides
[this.slides
.length
-1];
62 Lightbox
.prototype.windowScrollToolbarlHandler = function(evt
) {
63 if (this.toolbar
.offsetTop
< getWindowScrollY() && !this.toolbarFixed
) {
64 this.toolbarFixed
= true;
65 this.backThreshold
= this.toolbar
.offsetTop
;
66 this.switchToolBarPositioning(true);
68 else if (this.toolbarFixed
&& getWindowScrollY() < this.backThreshold
) {
69 this.toolbarFixed
= false;
70 this.switchToolBarPositioning(false);
73 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
75 !this.fetchingDisabled
&&
77 (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
78 - getWindowHeight()) {
79 this.fetchingDisabled
= true;
84 Lightbox
.prototype.mouseClickHandler = function(evt
) {
85 var target
= getTargetedObject(evt
);
86 if (target
.tagName
=== 'IMG') {
88 var link
= target
.parentNode
;
89 var button
= link
.parentNode
;
90 var slide
= button
.parentNode
;
92 if (link
.tagName
=== 'A') {
93 switch(link
.getAttribute('name')) {
94 case 'add_to_selection':
97 req
= new XMLHttpRequest();
99 req
.open("POST", url
, true);
100 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
103 slide
.className
= 'selected';
105 link
.setAttribute('name', 'remove_to_selection');
106 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
107 link
.title
= img
.alt
= 'Retirer de la sélection';
108 button
.className
= "button slide-deselect";
111 case 'remove_to_selection':
114 req
= new XMLHttpRequest();
116 req
.open("POST", url
, true);
117 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
119 slide
.className
= null;
120 link
.setAttribute('name', 'add_to_selection');
121 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
122 link
.title
= img
.alt
= 'Ajouter à la sélection';
123 button
.className
= "button slide-select";
128 slide
.widget
= new CartWidget(slide
, link
.href
);
131 case 'hide_for_anonymous':
134 req
= new XMLHttpRequest();
136 req
.open("POST", url
, true);
137 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
139 slide
.className
= 'hidden-slide';
140 link
.setAttribute('name', 'show_for_anonymous');
141 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
142 link
.title
= img
.alt
= 'Montrer au anonymes';
143 button
.className
= "button slide-show";
146 case 'show_for_anonymous':
149 req
= new XMLHttpRequest();
151 req
.open("POST", url
, true);
152 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
154 slide
.className
= null;
155 link
.setAttribute('name', 'hide_for_anonymous');
156 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
157 link
.title
= img
.alt
= 'Masquer pour les anonymes';
158 button
.className
= "button slide-hide";
162 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
165 cb
.setAttribute('checked', 'checked');
168 cb
.removeAttribute('checked');
170 this.selectCBRange(evt
);
174 Lightbox
.prototype.onChangeHandler = function(evt
) {
175 var target
= getTargetedObject(evt
);
176 if (target
.name
=== 'sort_on') {
177 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
182 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
183 switch(fm
.submitButton
.name
) {
185 this.hideSelection();
190 Lightbox
.prototype.onResponseLoad = function(req
) {
191 switch(req
.responseXML
.documentElement
.nodeName
) {
193 this.deleteSelection();
196 this.showSelection();
199 this.fm
.submitButton
= undefined;
205 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
206 var tbs
= this.toolbar
.style
;
208 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
209 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
210 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
211 tbs
.position
= 'fixed';
213 this.toolbarPlaceholder
= document
.createElement('div');
214 var phs
= this.toolbarPlaceholder
.style
;
215 phs
.cssText
= tbs
.cssText
;
216 phs
.position
= 'relative';
217 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
220 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
221 tbs
.cssText
= this.toolbar
.defaultCssText
;
226 Lightbox
.prototype.hideSelection = function() {
228 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
229 e
= this.form
.elements
[i
];
230 if (e
.type
=== 'checkbox' && e
.checked
) {
231 slide
= e
.parentNode
.parentNode
;
232 slide
.classList
.add('zero_opacity');
237 Lightbox
.prototype.showSelection = function() {
239 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
240 e
= this.form
.elements
[i
];
241 if (e
.type
=== 'checkbox' && e
.checked
) {
242 slide
= e
.parentNode
.parentNode
;
243 slide
.classList
.remove('zero_opacity');
248 Lightbox
.prototype.deleteSelection = function() {
250 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
251 e
= this.form
.elements
[i
];
252 if (e
.type
=== 'checkbox' && e
.checked
) {
253 slide
= e
.parentNode
.parentNode
;
254 slide
.classList
.add('zero_width');
258 // if you change this, delay you should also change this css rule :
259 // .lightbox span { transition: width 1s
260 setTimeout(function(){self
._removeSelection();}, 1000);
263 Lightbox
.prototype._removeSelection = function() {
266 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
267 e
= this.form
.elements
[i
];
268 if (e
.type
=== 'checkbox' && e
.checked
) {
269 toRemove
.push(e
.parentNode
.parentNode
);
272 for (i
=0 ; i
<toRemove
.length
; i
++) {
273 this.grid
.removeChild(toRemove
[i
]);
275 this._buildSlidesIndex();
276 this.cbIndex
= undefined;
277 this.windowScrollGridHandler();
280 Lightbox
.prototype.getCBIndex = function(cb
) {
282 // build checkbox index
285 for (i
=0 ; i
<this.slides
.length
; i
++) {
286 node
= this.slides
[i
];
287 c
= node
.getElementsByTagName('input')[0];
288 c
.index
= this.cbIndex
.length
;
289 this.cbIndex
.push(c
);
295 Lightbox
.prototype.selectCBRange = function(evt
) {
296 var target
= getTargetedObject(evt
);
297 evt
= getEventObject(evt
);
298 var shift
= evt
.shiftKey
;
299 if (shift
&& this.lastCBChecked
) {
300 var from = this.getCBIndex(this.lastCBChecked
);
301 var to
= this.getCBIndex(target
);
302 var start
= Math
.min(from, to
);
303 var stop
= Math
.max(from, to
);
305 for (i
=start
; i
<stop
; i
++ ) {
306 this.cbIndex
[i
].setAttribute('checked', 'checked');
309 else if (target
.checked
) {
310 this.lastCBChecked
= target
;
313 this.lastCBChecked
= undefined;
317 Lightbox
.prototype.refreshGrid = function() {
318 var req
= new XMLHttpRequest();
320 req
.onreadystatechange = function() {
321 switch (req
.readyState
) {
327 if (req
.status
=== 200) {
328 self
._refreshGrid(req
);
334 var url
= absolute_url() +
335 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
337 req
.open('GET', url
, true);
341 Lightbox
.prototype._refreshGrid = function(req
) {
342 var doc
= req
.responseXML
.documentElement
;
345 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
346 node
= doc
.childNodes
[i
];
347 if (node
.nodeType
=== 1) {
348 node
= getCopyOfNode(node
);
349 this.grid
.replaceChild(node
, this.slides
[j
]);
350 this.slides
[j
] = node
;
354 this.cbIndex
= undefined;
357 Lightbox
.prototype.fetchTail = function() {
358 var req
= new XMLHttpRequest();
360 req
.onreadystatechange = function() {
361 switch (req
.readyState
) {
367 if (req
.status
=== 200) {
368 self
._appendTail(req
);
374 var url
= absolute_url() +
375 '/portfolio_thumbnails_tail?start:int=' +
376 String(this.slides
.length
) +
378 req
.open('GET', url
, true);
382 Lightbox
.prototype._appendTail = function(req
) {
383 var doc
= req
.responseXML
.documentElement
;
385 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
386 node
= doc
.childNodes
[i
];
387 if (node
.nodeType
=== 1) {
388 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
389 this.slides
.push(this.lastSlide
);
391 c
= this.lastSlide
.getElementsByTagName('input')[0];
392 c
.index
= this.cbIndex
.length
;
393 this.cbIndex
.push(c
);
398 this.fetchingDisabled
= false;
399 if (doc
.getAttribute('nomore')) {
400 this.complete
= true;
402 this.windowScrollGridHandler();
406 var _outlineSelectedSlide
;
407 if (browser
.isGecko
) {
408 _outlineSelectedSlide = function(slide
) {
409 slide
.className
= 'selected';
413 _outlineSelectedSlide = function(slide
) {
414 if (slide
.className
&&
415 !reSelected
.test(slide
.className
)) {
416 slide
.className
= slide
.className
+ ' selected';