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
, orderable
) {
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 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 this._DDOrderingListeners
= {'dragstart' : function(evt
){self
.onDragStart(evt
);},
52 'dragover' : function(evt
){self
.onDragOver(evt
);},
53 'dragend' : function(evt
){self
.onDragEnd(evt
);}
55 if(orderable
) {this.enableDDOrdering();}
58 Lightbox
.prototype._buildSlidesIndex = function() {
61 for (i
=0 ; i
<this.grid
.childNodes
.length
; i
++) {
62 node
= this.grid
.childNodes
[i
];
63 if (node
.nodeType
=== 1) { // is element
64 this.slides
.push(node
);
67 this.lastSlide
= this.slides
[this.slides
.length
-1];
70 Lightbox
.prototype.windowScrollToolbarlHandler = function(evt
) {
71 if (this.toolbar
.offsetTop
< getWindowScrollY() && !this.toolbarFixed
) {
72 this.toolbarFixed
= true;
73 this.backThreshold
= this.toolbar
.offsetTop
;
74 this.switchToolBarPositioning(true);
76 else if (this.toolbarFixed
&& getWindowScrollY() < this.backThreshold
) {
77 this.toolbarFixed
= false;
78 this.switchToolBarPositioning(false);
82 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
84 !this.fetchingDisabled
&&
85 getWindowScrollY() > (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
- getWindowHeight()) {
86 this.fetchingDisabled
= true;
91 Lightbox
.prototype.mouseClickHandler = function(evt
) {
92 var target
= getTargetedObject(evt
);
93 if (target
.tagName
=== 'IMG') {
95 var link
= target
.parentNode
;
96 var button
= link
.parentNode
;
97 var slide
= button
.parentNode
;
99 if (link
.tagName
=== 'A') {
100 switch(link
.getAttribute('name')) {
101 case 'add_to_selection':
104 req
= new XMLHttpRequest();
106 req
.open("POST", url
, true);
107 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
110 slide
.className
= 'selected';
112 link
.setAttribute('name', 'remove_to_selection');
113 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
114 link
.title
= img
.alt
= 'Retirer de la sélection';
115 button
.className
= "button slide-deselect";
118 case 'remove_to_selection':
121 req
= new XMLHttpRequest();
123 req
.open("POST", url
, true);
124 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
126 slide
.className
= null;
127 link
.setAttribute('name', 'add_to_selection');
128 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
129 link
.title
= img
.alt
= 'Ajouter à la sélection';
130 button
.className
= "button slide-select";
135 slide
.widget
= new CartWidget(slide
, link
.href
);
138 case 'hide_for_anonymous':
141 req
= new XMLHttpRequest();
143 req
.open("POST", url
, true);
144 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
146 slide
.className
= 'hidden-slide';
147 link
.setAttribute('name', 'show_for_anonymous');
148 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
149 link
.title
= img
.alt
= 'Montrer au anonymes';
150 button
.className
= "button slide-show";
153 case 'show_for_anonymous':
156 req
= new XMLHttpRequest();
158 req
.open("POST", url
, true);
159 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
161 slide
.className
= null;
162 link
.setAttribute('name', 'hide_for_anonymous');
163 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
164 link
.title
= img
.alt
= 'Masquer pour les anonymes';
165 button
.className
= "button slide-hide";
169 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
172 cb
.setAttribute('checked', 'checked');
175 cb
.removeAttribute('checked');
177 this.selectCBRange(evt
);
181 Lightbox
.prototype.onChangeHandler = function(evt
) {
182 var target
= getTargetedObject(evt
);
183 if (target
.name
=== 'sort_on') {
184 if (target
.value
=== 'position') {this.enableDDOrdering();}
185 else {this.disableDDOrdering();}
186 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
191 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
192 switch(fm
.submitButton
.name
) {
194 this.hideSelection();
199 Lightbox
.prototype.onResponseLoad = function(req
) {
200 switch(req
.responseXML
.documentElement
.nodeName
) {
202 this.deleteSelection();
205 this.showSelection();
208 this.fm
.submitButton
= undefined;
212 this.fm
.loadResponse(req
);
217 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
218 var tbs
= this.toolbar
.style
;
220 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
221 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
222 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
223 tbs
.position
= 'fixed';
225 this.toolbarPlaceholder
= document
.createElement('div');
226 var phs
= this.toolbarPlaceholder
.style
;
227 phs
.cssText
= tbs
.cssText
;
228 phs
.position
= 'relative';
229 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
232 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
233 tbs
.cssText
= this.toolbar
.defaultCssText
;
238 Lightbox
.prototype.hideSelection = 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
.add('zero_opacity');
249 Lightbox
.prototype.showSelection = 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
.remove('zero_opacity');
260 Lightbox
.prototype.deleteSelection = function() {
262 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
263 e
= this.form
.elements
[i
];
264 if (e
.type
=== 'checkbox' && e
.checked
) {
265 slide
= e
.parentNode
.parentNode
;
266 slide
.classList
.add('zero_width');
270 // if you change this, delay you should also change this css rule :
271 // .lightbox span { transition: width 1s
272 setTimeout(function(){self
._removeSelection();}, 1000);
275 Lightbox
.prototype._removeSelection = function() {
278 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
279 e
= this.form
.elements
[i
];
280 if (e
.type
=== 'checkbox' && e
.checked
) {
281 toRemove
.push(e
.parentNode
.parentNode
);
284 for (i
=0 ; i
<toRemove
.length
; i
++) {
285 this.grid
.removeChild(toRemove
[i
]);
287 this._buildSlidesIndex();
288 this.cbIndex
= undefined;
289 this.windowScrollGridHandler();
292 Lightbox
.prototype.getCBIndex = function(cb
) {
294 // build checkbox index
297 for (i
=0 ; i
<this.slides
.length
; i
++) {
298 node
= this.slides
[i
];
299 c
= node
.getElementsByTagName('input')[0];
300 c
.index
= this.cbIndex
.length
;
301 this.cbIndex
.push(c
);
307 Lightbox
.prototype.selectCBRange = function(evt
) {
308 var target
= getTargetedObject(evt
);
309 evt
= getEventObject(evt
);
310 var shift
= evt
.shiftKey
;
311 if (shift
&& this.lastCBChecked
) {
312 var from = this.getCBIndex(this.lastCBChecked
);
313 var to
= this.getCBIndex(target
);
314 var start
= Math
.min(from, to
);
315 var stop
= Math
.max(from, to
);
317 for (i
=start
; i
<stop
; i
++ ) {
318 this.cbIndex
[i
].setAttribute('checked', 'checked');
321 else if (target
.checked
) {
322 this.lastCBChecked
= target
;
325 this.lastCBChecked
= undefined;
329 Lightbox
.prototype.refreshGrid = function() {
330 var req
= new XMLHttpRequest();
332 req
.onreadystatechange = function() {
333 switch (req
.readyState
) {
339 if (req
.status
=== 200) {
340 self
._refreshGrid(req
);
346 var url
= absolute_url() +
347 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
349 req
.open('GET', url
, true);
353 Lightbox
.prototype._refreshGrid = function(req
) {
354 var doc
= req
.responseXML
.documentElement
;
357 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
358 node
= doc
.childNodes
[i
];
359 if (node
.nodeType
=== 1) {
360 node
= getCopyOfNode(node
);
361 this.disableDefaultDragging(node
);
362 this.grid
.replaceChild(node
, this.slides
[j
]);
363 this.slides
[j
] = node
;
367 this.cbIndex
= undefined;
370 Lightbox
.prototype.fetchTail = function() {
371 var req
= new XMLHttpRequest();
373 req
.onreadystatechange = function() {
374 switch (req
.readyState
) {
380 if (req
.status
=== 200) {
381 self
._appendTail(req
);
387 var url
= absolute_url() +
388 '/portfolio_thumbnails_tail?start:int=' +
389 String(this.slides
.length
) +
393 req
.open('GET', url
, true);
397 Lightbox
.prototype._appendTail = function(req
) {
398 var doc
= req
.responseXML
.documentElement
;
400 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
401 node
= doc
.childNodes
[i
];
402 if (node
.nodeType
=== 1) {
403 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
404 this.disableDefaultDragging(this.lastSlide
);
405 this.slides
.push(this.lastSlide
);
407 c
= this.lastSlide
.getElementsByTagName('input')[0];
408 c
.index
= this.cbIndex
.length
;
409 this.cbIndex
.push(c
);
414 this.fetchingDisabled
= false;
415 if (doc
.getAttribute('nomore')) {
416 this.complete
= true;
418 this.windowScrollGridHandler();
422 var _outlineSelectedSlide
;
423 if (browser
.isGecko
) {
424 _outlineSelectedSlide = function(slide
) {
425 slide
.className
= 'selected';
429 _outlineSelectedSlide = function(slide
) {
430 if (slide
.className
&&
431 !reSelected
.test(slide
.className
)) {
432 slide
.className
= slide
.className
+ ' selected';
437 if (browser
.isGecko
) {
438 Lightbox
.prototype.disableDefaultDragging = function(element
) {
442 var i
, j
, name
, elements
;
443 var elementsNames
= ['a', 'img'];
444 for (i
=0 ; i
< elementsNames
.length
; i
++) {
445 name
= elementsNames
[i
];
446 elements
= element
.getElementsByTagName(name
);
447 for (j
=0 ; j
< elements
.length
; j
++) {
448 elements
[j
].draggable
=false;
454 Lightbox
.prototype.disableDefaultDragging = function() {};
457 Lightbox
.prototype.getSelectedSlides = function() {
460 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
461 e
= this.form
.elements
[i
];
462 if (e
.type
=== 'checkbox' && e
.checked
) {
463 slide
= e
.parentNode
.parentNode
;
471 Lightbox
.prototype.enableDDOrdering = function() {
472 addListener(this.grid
, 'dragstart', this._DDOrderingListeners
.dragstart
);
473 addListener(this.grid
, 'dragover', this._DDOrderingListeners
.dragover
);
474 addListener(this.grid
, 'dragend', this._DDOrderingListeners
.dragend
);
477 Lightbox
.prototype.disableDDOrdering = function() {
478 removeListener(this.grid
, 'dragstart', this._DDOrderingListeners
.dragstart
);
479 removeListener(this.grid
, 'dragover', this._DDOrderingListeners
.dragover
);
480 removeListener(this.grid
, 'dragend', this._DDOrderingListeners
.dragend
);
483 Lightbox
.prototype.onDragStart = function(evt
) {
484 var target
= getTargetedObject(evt
);
485 this.dragged
= target
;
486 this.draggedSelection
= this.getSelectedSlides();
487 if (this.draggedSelection
.indexOf(target
) === -1) {
488 this.draggedSelection
.push(target
);
490 evt
.dataTransfer
.setData('text', '');
492 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
493 slide
= this.draggedSelection
[i
];
494 slide
.style
.opacity
= 0;
495 slide
.style
.width
= 0;
499 Lightbox
.prototype.onDragOver = function(evt
) {
503 var target
= getTargetedObject(evt
);
504 while(target
&& target
.className
!== 'slide') {
505 target
= target
.parentNode
;
507 if (!target
) {return;}
508 target
= target
.parentNode
;
509 if (target
!== this.dragged
) {
510 target
.classList
.add('dragover');
512 if (this.lastDropTarget
&& this.lastDropTarget
!== target
) {
513 this.lastDropTarget
.classList
.remove('dragover');
515 this.lastDropTarget
= target
;
518 Lightbox
.prototype.onDragEnd = function(evt
) {
519 if (this.lastDropTarget
) {
520 this.lastDropTarget
.classList
.remove('dragover');
522 this.pendingMovedSlides
= [];
523 for(i
=this.draggedSelection
.length
-1 ; i
>=0 ; i
--) {
524 slide
= this.draggedSelection
[i
].cloneNode(true);
525 this.pendingMovedSlides
.push(slide
);
526 this.grid
.insertBefore(slide
, this.lastDropTarget
.nextSibling
);
527 slide
.style
.opacity
= 1;
528 slide
.style
.width
= '';
530 this.moveSelectedPhotos();
532 // this.draggedSelection = this.lastDropTarget
533 this.dragged
= undefined;
536 Lightbox
.prototype.moveSelectedPhotos = function() {
537 var req
= new XMLHttpRequest();
539 req
.onreadystatechange = function() {
540 switch (req
.readyState
) {
546 self
._moveSelectedPhotos(req
);
551 var url
= absolute_url() + '/portfolio_move_photos';
552 req
.open("POST", url
, true);
553 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
554 var query
= 'container_type=' + this.container_type
;
556 for (i
=0 ; i
<this.draggedSelection
.length
; i
++) {
557 query
+= '&uids:list=' + this.draggedSelection
[i
].getAttribute('name');
559 query
+= '&afterUid=' + this.lastDropTarget
.getAttribute('name');
563 Lightbox
.prototype._moveSelectedPhotos = function(req
) {
565 if (req
.status
=== 200) {
566 var doc
= req
.responseXML
.documentElement
;
567 if (doc
.nodeName
=== 'ok') {
568 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
569 slide
= this.draggedSelection
[i
];
570 this.grid
.removeChild(slide
);
571 cb
= this.pendingMovedSlides
[i
].getElementsByTagName('input')[0];
573 cb
.removeAttribute('checked');
575 this.pendingMovedSlides
= undefined;
576 this.cbIndex
= undefined;
581 for(i
=0 ; i
<this.pendingMovedSlides
.length
; i
++) {
582 slide
= this.pendingMovedSlides
[i
];
583 this.grid
.removeChild(slide
);
586 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
587 slide
= this.draggedSelection
[i
];
588 slide
.style
.opacity
= 1;
589 slide
.style
.width
= '';