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
, container_type
) {
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
;
23 this.toolbarFixed
= false;
24 addListener(window
, 'scroll', function(evt
){self
.windowScrollToolbarlHandler(evt
);});
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
;
33 parent
= parent
.parentNode
;
34 if (parent
.tagName
=== 'FORM') {
38 else if (parent
.tagName
=== 'BODY') {
42 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
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
);};
51 Lightbox
.prototype._buildSlidesIndex = function() {
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
);
60 this.lastSlide
= this.slides
[this.slides
.length
-1];
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);
69 else if (this.toolbarFixed
&& getWindowScrollY() < this.backThreshold
) {
70 this.toolbarFixed
= false;
71 this.switchToolBarPositioning(false);
74 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
76 !this.fetchingDisabled
&&
78 (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
79 - getWindowHeight()) {
80 this.fetchingDisabled
= true;
85 Lightbox
.prototype.mouseClickHandler = function(evt
) {
86 var target
= getTargetedObject(evt
);
87 if (target
.tagName
=== 'IMG') {
89 var link
= target
.parentNode
;
90 var button
= link
.parentNode
;
91 var slide
= button
.parentNode
;
93 if (link
.tagName
=== 'A') {
94 switch(link
.getAttribute('name')) {
95 case 'add_to_selection':
98 req
= new XMLHttpRequest();
100 req
.open("POST", url
, true);
101 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
104 slide
.className
= 'selected';
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";
112 case 'remove_to_selection':
115 req
= new XMLHttpRequest();
117 req
.open("POST", url
, true);
118 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
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";
129 slide
.widget
= new CartWidget(slide
, link
.href
);
132 case 'hide_for_anonymous':
135 req
= new XMLHttpRequest();
137 req
.open("POST", url
, true);
138 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
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";
147 case 'show_for_anonymous':
150 req
= new XMLHttpRequest();
152 req
.open("POST", url
, true);
153 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
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";
163 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
166 cb
.setAttribute('checked', 'checked');
169 cb
.removeAttribute('checked');
171 this.selectCBRange(evt
);
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'};
183 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
184 switch(fm
.submitButton
.name
) {
186 this.hideSelection();
191 Lightbox
.prototype.onResponseLoad = function(req
) {
192 switch(req
.responseXML
.documentElement
.nodeName
) {
194 this.deleteSelection();
197 this.showSelection();
200 this.fm
.submitButton
= undefined;
204 this.fm
.loadResponse(req
);
209 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
210 var tbs
= this.toolbar
.style
;
212 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
213 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
214 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
215 tbs
.position
= 'fixed';
217 this.toolbarPlaceholder
= document
.createElement('div');
218 var phs
= this.toolbarPlaceholder
.style
;
219 phs
.cssText
= tbs
.cssText
;
220 phs
.position
= 'relative';
221 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
224 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
225 tbs
.cssText
= this.toolbar
.defaultCssText
;
230 Lightbox
.prototype.hideSelection = function() {
232 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
233 e
= this.form
.elements
[i
];
234 if (e
.type
=== 'checkbox' && e
.checked
) {
235 slide
= e
.parentNode
.parentNode
;
236 slide
.classList
.add('zero_opacity');
241 Lightbox
.prototype.showSelection = function() {
243 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
244 e
= this.form
.elements
[i
];
245 if (e
.type
=== 'checkbox' && e
.checked
) {
246 slide
= e
.parentNode
.parentNode
;
247 slide
.classList
.remove('zero_opacity');
252 Lightbox
.prototype.deleteSelection = function() {
254 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
255 e
= this.form
.elements
[i
];
256 if (e
.type
=== 'checkbox' && e
.checked
) {
257 slide
= e
.parentNode
.parentNode
;
258 slide
.classList
.add('zero_width');
262 // if you change this, delay you should also change this css rule :
263 // .lightbox span { transition: width 1s
264 setTimeout(function(){self
._removeSelection();}, 1000);
267 Lightbox
.prototype._removeSelection = function() {
270 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
271 e
= this.form
.elements
[i
];
272 if (e
.type
=== 'checkbox' && e
.checked
) {
273 toRemove
.push(e
.parentNode
.parentNode
);
276 for (i
=0 ; i
<toRemove
.length
; i
++) {
277 this.grid
.removeChild(toRemove
[i
]);
279 this._buildSlidesIndex();
280 this.cbIndex
= undefined;
281 this.windowScrollGridHandler();
284 Lightbox
.prototype.getCBIndex = function(cb
) {
286 // build checkbox index
289 for (i
=0 ; i
<this.slides
.length
; i
++) {
290 node
= this.slides
[i
];
291 c
= node
.getElementsByTagName('input')[0];
292 c
.index
= this.cbIndex
.length
;
293 this.cbIndex
.push(c
);
299 Lightbox
.prototype.selectCBRange = function(evt
) {
300 var target
= getTargetedObject(evt
);
301 evt
= getEventObject(evt
);
302 var shift
= evt
.shiftKey
;
303 if (shift
&& this.lastCBChecked
) {
304 var from = this.getCBIndex(this.lastCBChecked
);
305 var to
= this.getCBIndex(target
);
306 var start
= Math
.min(from, to
);
307 var stop
= Math
.max(from, to
);
309 for (i
=start
; i
<stop
; i
++ ) {
310 this.cbIndex
[i
].setAttribute('checked', 'checked');
313 else if (target
.checked
) {
314 this.lastCBChecked
= target
;
317 this.lastCBChecked
= undefined;
321 Lightbox
.prototype.refreshGrid = function() {
322 var req
= new XMLHttpRequest();
324 req
.onreadystatechange = function() {
325 switch (req
.readyState
) {
331 if (req
.status
=== 200) {
332 self
._refreshGrid(req
);
338 var url
= absolute_url() +
339 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
341 req
.open('GET', url
, true);
345 Lightbox
.prototype._refreshGrid = function(req
) {
346 var doc
= req
.responseXML
.documentElement
;
349 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
350 node
= doc
.childNodes
[i
];
351 if (node
.nodeType
=== 1) {
352 node
= getCopyOfNode(node
);
353 this.grid
.replaceChild(node
, this.slides
[j
]);
354 this.slides
[j
] = node
;
358 this.cbIndex
= undefined;
361 Lightbox
.prototype.fetchTail = function() {
362 var req
= new XMLHttpRequest();
364 req
.onreadystatechange = function() {
365 switch (req
.readyState
) {
371 if (req
.status
=== 200) {
372 self
._appendTail(req
);
378 var url
= absolute_url() +
379 '/portfolio_thumbnails_tail?start:int=' +
380 String(this.slides
.length
) +
384 req
.open('GET', url
, true);
388 Lightbox
.prototype._appendTail = function(req
) {
389 var doc
= req
.responseXML
.documentElement
;
391 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
392 node
= doc
.childNodes
[i
];
393 if (node
.nodeType
=== 1) {
394 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
395 this.slides
.push(this.lastSlide
);
397 c
= this.lastSlide
.getElementsByTagName('input')[0];
398 c
.index
= this.cbIndex
.length
;
399 this.cbIndex
.push(c
);
404 this.fetchingDisabled
= false;
405 if (doc
.getAttribute('nomore')) {
406 this.complete
= true;
408 this.windowScrollGridHandler();
412 var _outlineSelectedSlide
;
413 if (browser
.isGecko
) {
414 _outlineSelectedSlide = function(slide
) {
415 slide
.className
= 'selected';
419 _outlineSelectedSlide = function(slide
) {
420 if (slide
.className
&&
421 !reSelected
.test(slide
.className
)) {
422 slide
.className
= slide
.className
+ ' selected';