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 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 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
&&
86 (this.lastSlide
.firstElementChild
|| this.lastSlide
.children
[0]).offsetTop
87 - getWindowHeight()) {
88 this.fetchingDisabled
= true;
93 Lightbox
.prototype.mouseClickHandler = function(evt
) {
94 var target
= getTargetedObject(evt
);
95 if (target
.tagName
=== 'IMG') {
97 var link
= target
.parentNode
;
98 var button
= link
.parentNode
;
99 var slide
= button
.parentNode
;
101 if (link
.tagName
=== 'A') {
102 switch(link
.getAttribute('name')) {
103 case 'add_to_selection':
106 req
= new XMLHttpRequest();
108 req
.open("POST", url
, true);
109 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
112 slide
.className
= 'selected';
114 link
.setAttribute('name', 'remove_to_selection');
115 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
116 link
.title
= img
.alt
= 'Retirer de la sélection';
117 button
.className
= "button slide-deselect";
120 case 'remove_to_selection':
123 req
= new XMLHttpRequest();
125 req
.open("POST", url
, true);
126 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
128 slide
.className
= null;
129 link
.setAttribute('name', 'add_to_selection');
130 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
131 link
.title
= img
.alt
= 'Ajouter à la sélection';
132 button
.className
= "button slide-select";
137 slide
.widget
= new CartWidget(slide
, link
.href
);
140 case 'hide_for_anonymous':
143 req
= new XMLHttpRequest();
145 req
.open("POST", url
, true);
146 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
148 slide
.className
= 'hidden-slide';
149 link
.setAttribute('name', 'show_for_anonymous');
150 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
151 link
.title
= img
.alt
= 'Montrer au anonymes';
152 button
.className
= "button slide-show";
155 case 'show_for_anonymous':
158 req
= new XMLHttpRequest();
160 req
.open("POST", url
, true);
161 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
163 slide
.className
= null;
164 link
.setAttribute('name', 'hide_for_anonymous');
165 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
166 link
.title
= img
.alt
= 'Masquer pour les anonymes';
167 button
.className
= "button slide-hide";
171 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
174 cb
.setAttribute('checked', 'checked');
177 cb
.removeAttribute('checked');
179 this.selectCBRange(evt
);
183 Lightbox
.prototype.onChangeHandler = function(evt
) {
184 var target
= getTargetedObject(evt
);
185 if (target
.name
=== 'sort_on') {
186 if (target
.value
=== 'position') {this.enableDDOrdering();}
187 else {this.disableDDOrdering();}
188 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
193 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
194 switch(fm
.submitButton
.name
) {
196 this.hideSelection();
201 Lightbox
.prototype.onResponseLoad = function(req
) {
202 switch(req
.responseXML
.documentElement
.nodeName
) {
204 this.deleteSelection();
207 this.showSelection();
210 this.fm
.submitButton
= undefined;
214 this.fm
.loadResponse(req
);
219 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
220 var tbs
= this.toolbar
.style
;
222 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
223 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
224 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
225 tbs
.position
= 'fixed';
227 this.toolbarPlaceholder
= document
.createElement('div');
228 var phs
= this.toolbarPlaceholder
.style
;
229 phs
.cssText
= tbs
.cssText
;
230 phs
.position
= 'relative';
231 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
234 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
235 tbs
.cssText
= this.toolbar
.defaultCssText
;
240 Lightbox
.prototype.hideSelection = function() {
242 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
243 e
= this.form
.elements
[i
];
244 if (e
.type
=== 'checkbox' && e
.checked
) {
245 slide
= e
.parentNode
.parentNode
;
246 slide
.classList
.add('zero_opacity');
251 Lightbox
.prototype.showSelection = function() {
253 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
254 e
= this.form
.elements
[i
];
255 if (e
.type
=== 'checkbox' && e
.checked
) {
256 slide
= e
.parentNode
.parentNode
;
257 slide
.classList
.remove('zero_opacity');
262 Lightbox
.prototype.deleteSelection = function() {
264 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
265 e
= this.form
.elements
[i
];
266 if (e
.type
=== 'checkbox' && e
.checked
) {
267 slide
= e
.parentNode
.parentNode
;
268 slide
.classList
.add('zero_width');
272 // if you change this, delay you should also change this css rule :
273 // .lightbox span { transition: width 1s
274 setTimeout(function(){self
._removeSelection();}, 1000);
277 Lightbox
.prototype._removeSelection = function() {
280 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
281 e
= this.form
.elements
[i
];
282 if (e
.type
=== 'checkbox' && e
.checked
) {
283 toRemove
.push(e
.parentNode
.parentNode
);
286 for (i
=0 ; i
<toRemove
.length
; i
++) {
287 this.grid
.removeChild(toRemove
[i
]);
289 this._buildSlidesIndex();
290 this.cbIndex
= undefined;
291 this.windowScrollGridHandler();
294 Lightbox
.prototype.getCBIndex = function(cb
) {
296 // build checkbox index
299 for (i
=0 ; i
<this.slides
.length
; i
++) {
300 node
= this.slides
[i
];
301 c
= node
.getElementsByTagName('input')[0];
302 c
.index
= this.cbIndex
.length
;
303 this.cbIndex
.push(c
);
309 Lightbox
.prototype.selectCBRange = function(evt
) {
310 var target
= getTargetedObject(evt
);
311 evt
= getEventObject(evt
);
312 var shift
= evt
.shiftKey
;
313 if (shift
&& this.lastCBChecked
) {
314 var from = this.getCBIndex(this.lastCBChecked
);
315 var to
= this.getCBIndex(target
);
316 var start
= Math
.min(from, to
);
317 var stop
= Math
.max(from, to
);
319 for (i
=start
; i
<stop
; i
++ ) {
320 this.cbIndex
[i
].setAttribute('checked', 'checked');
323 else if (target
.checked
) {
324 this.lastCBChecked
= target
;
327 this.lastCBChecked
= undefined;
331 Lightbox
.prototype.refreshGrid = function() {
332 var req
= new XMLHttpRequest();
334 req
.onreadystatechange = function() {
335 switch (req
.readyState
) {
341 if (req
.status
=== 200) {
342 self
._refreshGrid(req
);
348 var url
= absolute_url() +
349 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
351 req
.open('GET', url
, true);
355 Lightbox
.prototype._refreshGrid = function(req
) {
356 var doc
= req
.responseXML
.documentElement
;
359 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
360 node
= doc
.childNodes
[i
];
361 if (node
.nodeType
=== 1) {
362 node
= getCopyOfNode(node
);
363 this.disableDefaultDragging(node
);
364 this.grid
.replaceChild(node
, this.slides
[j
]);
365 this.slides
[j
] = node
;
369 this.cbIndex
= undefined;
372 Lightbox
.prototype.fetchTail = function() {
373 var req
= new XMLHttpRequest();
375 req
.onreadystatechange = function() {
376 switch (req
.readyState
) {
382 if (req
.status
=== 200) {
383 self
._appendTail(req
);
389 var url
= absolute_url() +
390 '/portfolio_thumbnails_tail?start:int=' +
391 String(this.slides
.length
) +
395 req
.open('GET', url
, true);
399 Lightbox
.prototype._appendTail = function(req
) {
400 var doc
= req
.responseXML
.documentElement
;
402 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
403 node
= doc
.childNodes
[i
];
404 if (node
.nodeType
=== 1) {
405 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
406 this.disableDefaultDragging(this.lastSlide
);
407 this.slides
.push(this.lastSlide
);
409 c
= this.lastSlide
.getElementsByTagName('input')[0];
410 c
.index
= this.cbIndex
.length
;
411 this.cbIndex
.push(c
);
416 this.fetchingDisabled
= false;
417 if (doc
.getAttribute('nomore')) {
418 this.complete
= true;
420 this.windowScrollGridHandler();
424 var _outlineSelectedSlide
;
425 if (browser
.isGecko
) {
426 _outlineSelectedSlide = function(slide
) {
427 slide
.className
= 'selected';
431 _outlineSelectedSlide = function(slide
) {
432 if (slide
.className
&&
433 !reSelected
.test(slide
.className
)) {
434 slide
.className
= slide
.className
+ ' selected';
439 if (browser
.isGecko
) {
440 Lightbox
.prototype.disableDefaultDragging = function(element
) {
444 var i
, j
, name
, elements
;
445 var elementsNames
= ['a', 'img'];
446 for (i
=0 ; i
< elementsNames
.length
; i
++) {
447 name
= elementsNames
[i
];
448 elements
= element
.getElementsByTagName(name
);
449 for (j
=0 ; j
< elements
.length
; j
++) {
450 elements
[j
].draggable
=false;
456 Lightbox
.prototype.disableDefaultDragging = function() {};
459 Lightbox
.prototype.getSelectedSlides = function() {
462 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
463 e
= this.form
.elements
[i
];
464 if (e
.type
=== 'checkbox' && e
.checked
) {
465 slide
= e
.parentNode
.parentNode
;
473 Lightbox
.prototype.enableDDOrdering = function() {
474 addListener(this.grid
, 'dragstart', this._DDOrderingListeners
.dragstart
);
475 addListener(this.grid
, 'dragover', this._DDOrderingListeners
.dragover
);
476 addListener(this.grid
, 'dragend', this._DDOrderingListeners
.dragend
);
479 Lightbox
.prototype.disableDDOrdering = function() {
480 removeListener(this.grid
, 'dragstart', this._DDOrderingListeners
.dragstart
);
481 removeListener(this.grid
, 'dragover', this._DDOrderingListeners
.dragover
);
482 removeListener(this.grid
, 'dragend', this._DDOrderingListeners
.dragend
);
485 Lightbox
.prototype.onDragStart = function(evt
) {
486 var target
= getTargetedObject(evt
);
487 this.dragged
= target
;
488 this.draggedSelection
= this.getSelectedSlides();
489 if (this.draggedSelection
.indexOf(target
) === -1) {
490 this.draggedSelection
.push(target
);
492 evt
.dataTransfer
.setData('text', '');
494 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
495 slide
= this.draggedSelection
[i
];
496 slide
.style
.opacity
= 0;
497 slide
.style
.width
= 0;
501 Lightbox
.prototype.onDragOver = function(evt
) {
505 var target
= getTargetedObject(evt
);
506 while(target
&& target
.className
!== 'slide') {
507 target
= target
.parentNode
;
509 if (!target
) {return;}
510 target
= target
.parentNode
;
511 if (target
!== this.dragged
) {
512 target
.classList
.add('dragover');
514 if (this.lastDropTarget
&& this.lastDropTarget
!== target
) {
515 this.lastDropTarget
.classList
.remove('dragover');
517 this.lastDropTarget
= target
;
520 Lightbox
.prototype.onDragEnd = function(evt
) {
521 if (this.lastDropTarget
) {
522 this.lastDropTarget
.classList
.remove('dragover');
524 this.pendingMovedSlides
= [];
525 for(i
=this.draggedSelection
.length
-1 ; i
>=0 ; i
--) {
526 slide
= this.draggedSelection
[i
].cloneNode(true);
527 this.pendingMovedSlides
.push(slide
);
528 this.grid
.insertBefore(slide
, this.lastDropTarget
.nextSibling
);
529 slide
.style
.opacity
= 1;
530 slide
.style
.width
= '';
532 this.moveSelectedPhotos();
534 // this.draggedSelection = this.lastDropTarget
535 this.dragged
= undefined;
538 Lightbox
.prototype.moveSelectedPhotos = function() {
539 var req
= new XMLHttpRequest();
541 req
.onreadystatechange = function() {
542 switch (req
.readyState
) {
548 self
._moveSelectedPhotos(req
);
553 var url
= absolute_url() + '/portfolio_move_photos';
554 req
.open("POST", url
, true);
555 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
556 var query
= 'container_type=' + this.container_type
;
558 for (i
=0 ; i
<this.draggedSelection
.length
; i
++) {
559 query
+= '&uids:list=' + this.draggedSelection
[i
].getAttribute('name');
561 query
+= '&afterUid=' + this.lastDropTarget
.getAttribute('name');
565 Lightbox
.prototype._moveSelectedPhotos = function(req
) {
567 if (req
.status
=== 200) {
568 var doc
= req
.responseXML
.documentElement
;
569 if (doc
.nodeName
=== 'ok') {
570 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
571 slide
= this.draggedSelection
[i
];
572 this.grid
.removeChild(slide
);
573 cb
= this.pendingMovedSlides
[i
].getElementsByTagName('input')[0];
575 cb
.removeAttribute('checked');
577 this.pendingMovedSlides
= undefined;
578 this.cbIndex
= undefined;
583 for(i
=0 ; i
<this.pendingMovedSlides
.length
; i
++) {
584 slide
= this.pendingMovedSlides
[i
];
585 this.grid
.removeChild(slide
);
588 for(i
=0 ; i
<this.draggedSelection
.length
; i
++) {
589 slide
= this.draggedSelection
[i
];
590 slide
.style
.opacity
= 1;
591 slide
.style
.width
= '';