8dfbaf473ac44389411cecd4e90826e8879cb1bb
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;
206 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
207 var tbs
= this.toolbar
.style
;
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';
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
);
221 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
222 tbs
.cssText
= this.toolbar
.defaultCssText
;
227 Lightbox
.prototype.hideSelection = function() {
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');
238 Lightbox
.prototype.showSelection = function() {
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');
249 Lightbox
.prototype.deleteSelection = function() {
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');
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);
264 Lightbox
.prototype._removeSelection = function() {
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
);
273 for (i
=0 ; i
<toRemove
.length
; i
++) {
274 this.grid
.removeChild(toRemove
[i
]);
276 this._buildSlidesIndex();
277 this.cbIndex
= undefined;
278 this.windowScrollGridHandler();
281 Lightbox
.prototype.getCBIndex = function(cb
) {
283 // build checkbox index
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
);
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
);
306 for (i
=start
; i
<stop
; i
++ ) {
307 this.cbIndex
[i
].setAttribute('checked', 'checked');
310 else if (target
.checked
) {
311 this.lastCBChecked
= target
;
314 this.lastCBChecked
= undefined;
318 Lightbox
.prototype.refreshGrid = function() {
319 var req
= new XMLHttpRequest();
321 req
.onreadystatechange = function() {
322 switch (req
.readyState
) {
328 if (req
.status
=== 200) {
329 self
._refreshGrid(req
);
335 var url
= absolute_url() +
336 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
338 req
.open('GET', url
, true);
342 Lightbox
.prototype._refreshGrid = function(req
) {
343 var doc
= req
.responseXML
.documentElement
;
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
;
355 this.cbIndex
= undefined;
358 Lightbox
.prototype.fetchTail = function() {
359 var req
= new XMLHttpRequest();
361 req
.onreadystatechange = function() {
362 switch (req
.readyState
) {
368 if (req
.status
=== 200) {
369 self
._appendTail(req
);
375 var url
= absolute_url() +
376 '/portfolio_thumbnails_tail?start:int=' +
377 String(this.slides
.length
) +
381 req
.open('GET', url
, true);
385 Lightbox
.prototype._appendTail = function(req
) {
386 var doc
= req
.responseXML
.documentElement
;
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
);
394 c
= this.lastSlide
.getElementsByTagName('input')[0];
395 c
.index
= this.cbIndex
.length
;
396 this.cbIndex
.push(c
);
401 this.fetchingDisabled
= false;
402 if (doc
.getAttribute('nomore')) {
403 this.complete
= true;
405 this.windowScrollGridHandler();
409 var _outlineSelectedSlide
;
410 if (browser
.isGecko
) {
411 _outlineSelectedSlide = function(slide
) {
412 slide
.className
= 'selected';
416 _outlineSelectedSlide = function(slide
) {
417 if (slide
.className
&&
418 !reSelected
.test(slide
.className
)) {
419 slide
.className
= slide
.className
+ ' selected';