cffa157be2daa14ce28676bf53dbf717bfaea94a
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 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 this.disableDefaultDragging();
51 addListener(this.grid
, 'dragstart', function(evt
){self
.onDragStart(evt
);});
52 addListener(this.grid
, 'dragover', function(evt
){self
.onDragOver(evt
);});
53 addListener(this.grid
, 'dragend', function(evt
){self
.onDragEnd(evt
);});
56 Lightbox
.prototype._buildSlidesIndex = function() {
59 for (i
=0 ; i
<this.grid
.childNodes
.length
; i
++) {
60 node
= this.grid
.childNodes
[i
];
61 if (node
.nodeType
=== 1) { // is element
62 this.slides
.push(node
);
65 this.lastSlide
= this.slides
[this.slides
.length
-1];
68 Lightbox
.prototype.windowScrollToolbarlHandler = function(evt
) {
69 if (this.toolbar
.offsetTop
< getWindowScrollY() && !this.toolbarFixed
) {
70 this.toolbarFixed
= true;
71 this.backThreshold
= this.toolbar
.offsetTop
;
72 this.switchToolBarPositioning(true);
74 else if (this.toolbarFixed
&& getWindowScrollY() < this.backThreshold
) {
75 this.toolbarFixed
= false;
76 this.switchToolBarPositioning(false);
79 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
81 !this.fetchingDisabled
&&
83 (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
84 - getWindowHeight()) {
85 this.fetchingDisabled
= true;
90 Lightbox
.prototype.mouseClickHandler = function(evt
) {
91 var target
= getTargetedObject(evt
);
92 if (target
.tagName
=== 'IMG') {
94 var link
= target
.parentNode
;
95 var button
= link
.parentNode
;
96 var slide
= button
.parentNode
;
98 if (link
.tagName
=== 'A') {
99 switch(link
.getAttribute('name')) {
100 case 'add_to_selection':
103 req
= new XMLHttpRequest();
105 req
.open("POST", url
, true);
106 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
109 slide
.className
= 'selected';
111 link
.setAttribute('name', 'remove_to_selection');
112 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
113 link
.title
= img
.alt
= 'Retirer de la sélection';
114 button
.className
= "button slide-deselect";
117 case 'remove_to_selection':
120 req
= new XMLHttpRequest();
122 req
.open("POST", url
, true);
123 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
125 slide
.className
= null;
126 link
.setAttribute('name', 'add_to_selection');
127 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
128 link
.title
= img
.alt
= 'Ajouter à la sélection';
129 button
.className
= "button slide-select";
134 slide
.widget
= new CartWidget(slide
, link
.href
);
137 case 'hide_for_anonymous':
140 req
= new XMLHttpRequest();
142 req
.open("POST", url
, true);
143 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
145 slide
.className
= 'hidden-slide';
146 link
.setAttribute('name', 'show_for_anonymous');
147 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
148 link
.title
= img
.alt
= 'Montrer au anonymes';
149 button
.className
= "button slide-show";
152 case 'show_for_anonymous':
155 req
= new XMLHttpRequest();
157 req
.open("POST", url
, true);
158 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
160 slide
.className
= null;
161 link
.setAttribute('name', 'hide_for_anonymous');
162 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
163 link
.title
= img
.alt
= 'Masquer pour les anonymes';
164 button
.className
= "button slide-hide";
168 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
171 cb
.setAttribute('checked', 'checked');
174 cb
.removeAttribute('checked');
176 this.selectCBRange(evt
);
180 Lightbox
.prototype.onChangeHandler = function(evt
) {
181 var target
= getTargetedObject(evt
);
182 if (target
.name
=== 'sort_on') {
183 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
188 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
189 switch(fm
.submitButton
.name
) {
191 this.hideSelection();
196 Lightbox
.prototype.onResponseLoad = function(req
) {
197 switch(req
.responseXML
.documentElement
.nodeName
) {
199 this.deleteSelection();
202 this.showSelection();
205 this.fm
.submitButton
= undefined;
209 this.fm
.loadResponse(req
);
214 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
215 var tbs
= this.toolbar
.style
;
217 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
218 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
219 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
220 tbs
.position
= 'fixed';
222 this.toolbarPlaceholder
= document
.createElement('div');
223 var phs
= this.toolbarPlaceholder
.style
;
224 phs
.cssText
= tbs
.cssText
;
225 phs
.position
= 'relative';
226 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
229 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
230 tbs
.cssText
= this.toolbar
.defaultCssText
;
235 Lightbox
.prototype.hideSelection = function() {
237 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
238 e
= this.form
.elements
[i
];
239 if (e
.type
=== 'checkbox' && e
.checked
) {
240 slide
= e
.parentNode
.parentNode
;
241 slide
.classList
.add('zero_opacity');
246 Lightbox
.prototype.showSelection = function() {
248 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
249 e
= this.form
.elements
[i
];
250 if (e
.type
=== 'checkbox' && e
.checked
) {
251 slide
= e
.parentNode
.parentNode
;
252 slide
.classList
.remove('zero_opacity');
257 Lightbox
.prototype.deleteSelection = function() {
259 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
260 e
= this.form
.elements
[i
];
261 if (e
.type
=== 'checkbox' && e
.checked
) {
262 slide
= e
.parentNode
.parentNode
;
263 slide
.classList
.add('zero_width');
267 // if you change this, delay you should also change this css rule :
268 // .lightbox span { transition: width 1s
269 setTimeout(function(){self
._removeSelection();}, 1000);
272 Lightbox
.prototype._removeSelection = function() {
275 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
276 e
= this.form
.elements
[i
];
277 if (e
.type
=== 'checkbox' && e
.checked
) {
278 toRemove
.push(e
.parentNode
.parentNode
);
281 for (i
=0 ; i
<toRemove
.length
; i
++) {
282 this.grid
.removeChild(toRemove
[i
]);
284 this._buildSlidesIndex();
285 this.cbIndex
= undefined;
286 this.windowScrollGridHandler();
289 Lightbox
.prototype.getCBIndex = function(cb
) {
291 // build checkbox index
294 for (i
=0 ; i
<this.slides
.length
; i
++) {
295 node
= this.slides
[i
];
296 c
= node
.getElementsByTagName('input')[0];
297 c
.index
= this.cbIndex
.length
;
298 this.cbIndex
.push(c
);
304 Lightbox
.prototype.selectCBRange = function(evt
) {
305 var target
= getTargetedObject(evt
);
306 evt
= getEventObject(evt
);
307 var shift
= evt
.shiftKey
;
308 if (shift
&& this.lastCBChecked
) {
309 var from = this.getCBIndex(this.lastCBChecked
);
310 var to
= this.getCBIndex(target
);
311 var start
= Math
.min(from, to
);
312 var stop
= Math
.max(from, to
);
314 for (i
=start
; i
<stop
; i
++ ) {
315 this.cbIndex
[i
].setAttribute('checked', 'checked');
318 else if (target
.checked
) {
319 this.lastCBChecked
= target
;
322 this.lastCBChecked
= undefined;
326 Lightbox
.prototype.refreshGrid = function() {
327 var req
= new XMLHttpRequest();
329 req
.onreadystatechange = function() {
330 switch (req
.readyState
) {
336 if (req
.status
=== 200) {
337 self
._refreshGrid(req
);
343 var url
= absolute_url() +
344 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
346 req
.open('GET', url
, true);
350 Lightbox
.prototype._refreshGrid = function(req
) {
351 var doc
= req
.responseXML
.documentElement
;
354 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
355 node
= doc
.childNodes
[i
];
356 if (node
.nodeType
=== 1) {
357 node
= getCopyOfNode(node
);
358 this.disableDefaultDragging(node
);
359 this.grid
.replaceChild(node
, this.slides
[j
]);
360 this.slides
[j
] = node
;
364 this.cbIndex
= undefined;
367 Lightbox
.prototype.fetchTail = function() {
368 var req
= new XMLHttpRequest();
370 req
.onreadystatechange = function() {
371 switch (req
.readyState
) {
377 if (req
.status
=== 200) {
378 self
._appendTail(req
);
384 var url
= absolute_url() +
385 '/portfolio_thumbnails_tail?start:int=' +
386 String(this.slides
.length
) +
390 req
.open('GET', url
, true);
394 Lightbox
.prototype._appendTail = function(req
) {
395 var doc
= req
.responseXML
.documentElement
;
397 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
398 node
= doc
.childNodes
[i
];
399 if (node
.nodeType
=== 1) {
400 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
401 this.disableDefaultDragging(this.lastSlide
);
402 this.slides
.push(this.lastSlide
);
404 c
= this.lastSlide
.getElementsByTagName('input')[0];
405 c
.index
= this.cbIndex
.length
;
406 this.cbIndex
.push(c
);
411 this.fetchingDisabled
= false;
412 if (doc
.getAttribute('nomore')) {
413 this.complete
= true;
415 this.windowScrollGridHandler();
419 var _outlineSelectedSlide
;
420 if (browser
.isGecko
) {
421 _outlineSelectedSlide = function(slide
) {
422 slide
.className
= 'selected';
426 _outlineSelectedSlide = function(slide
) {
427 if (slide
.className
&&
428 !reSelected
.test(slide
.className
)) {
429 slide
.className
= slide
.className
+ ' selected';
434 if (browser
.isGecko
) {
435 Lightbox
.prototype.disableDefaultDragging = function(element
) {
439 var i
, j
, name
, elements
;
440 var elementsNames
= ['a', 'img'];
441 for (i
=0 ; i
< elementsNames
.length
; i
++) {
442 name
= elementsNames
[i
];
443 elements
= element
.getElementsByTagName(name
);
444 for (j
=0 ; j
< elements
.length
; j
++) {
445 elements
[j
].draggable
=false;
451 Lightbox
.prototype.disableDefaultDragging = function() {};
454 Lightbox
.prototype.getSelectedSlides = function() {
457 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
458 e
= this.form
.elements
[i
];
459 if (e
.type
=== 'checkbox' && e
.checked
) {
460 slide
= e
.parentNode
.parentNode
;
467 Lightbox
.prototype.onDragStart = function(evt
) {
468 var target
= getTargetedObject(evt
);
469 this.dragged
= target
;
470 this.draggedSelection
= this.getSelectedSlides();
471 if (this.draggedSelection
.indexOf(target
) === -1) {
472 this.draggedSelection
.push(target
);
474 evt
.dataTransfer
.setData('text', '');
476 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
477 slide
= this.draggedSelection
[i
];
478 slide
.style
.opacity
= 0;
479 slide
.style
.width
= 0;
483 Lightbox
.prototype.onDragOver = function(evt
) {
484 var target
= getTargetedObject(evt
);
485 if (!target
) {return;}
486 while(target
.className
!== 'slide') {
487 target
= target
.parentNode
;
489 target
= target
.parentNode
;
490 if (target
!== this.dragged
) {
491 target
.classList
.add('dragover');
493 if (this.previousDragOver
&& this.previousDragOver
!== target
) {
494 this.previousDragOver
.classList
.remove('dragover');
496 this.previousDragOver
= target
;
499 Lightbox
.prototype.onDragEnd = function(evt
) {
500 if (this.previousDragOver
) {
501 this.previousDragOver
.classList
.remove('dragover');
503 for(i
=this.draggedSelection
.length
-1 ; i
>=0 ; i
--) {
505 slide
= this.draggedSelection
[i
].cloneNode(true);
506 this.grid
.insertBefore(slide
, this.previousDragOver
.nextSibling
);
507 slide
.style
.opacity
= 1;
508 slide
.style
.width
= '';
511 this.draggedSelection
= this.previousDragOver
= this.dragged
= undefined;