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
) {
17 this.toolbar
= toolbar
;
19 this.toolbarFixed
= false;
20 addListener(window
, 'scroll', function(evt
){self
.windowScrollHandler(evt
);});
22 this.lastCBChecked
= undefined;
23 this.form
= undefined;
24 var parent
= this.grid
.parentNode
;
26 parent
= parent
.parentNode
;
27 if (parent
.tagName
=== 'FORM') {
31 else if (parent
.tagName
=== 'BODY') {
35 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
37 var fm
= new FormManager(this.form
);
38 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
39 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
43 Lightbox
.prototype.windowScrollHandler = function(evt
) {
44 if (this.toolbar
.offsetTop
< window
.scrollY
&& !this.toolbarFixed
) {
45 console
.log('this.toolbar.offsetTop', this.toolbar
.offsetTop
);
46 this.toolbarFixed
= true;
47 this.backThreshold
= this.toolbar
.offsetTop
;
48 this.switchToolBarPositioning(true);
50 else if (this.toolbarFixed
&& window
.scrollY
< this.backThreshold
) {
51 this.toolbarFixed
= false;
52 this.switchToolBarPositioning(false);
56 Lightbox
.prototype.mouseClickHandler = function(evt
) {
57 var target
= getTargetedObject(evt
);
58 if (target
.tagName
=== 'IMG') {
60 var link
= target
.parentNode
;
61 var button
= link
.parentNode
;
62 var slide
= button
.parentNode
;
64 if (link
.tagName
=== 'A') {
65 switch(link
.getAttribute('name')) {
66 case 'add_to_selection':
69 req
= new XMLHttpRequest();
71 req
.open("POST", url
, true);
72 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
75 slide
.className
= 'selected';
77 link
.setAttribute('name', 'remove_to_selection');
78 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
79 link
.title
= img
.alt
= 'Retirer de la sélection';
80 button
.className
= "button slide-deselect";
83 case 'remove_to_selection':
86 req
= new XMLHttpRequest();
88 req
.open("POST", url
, true);
89 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
91 slide
.className
= null;
92 link
.setAttribute('name', 'add_to_selection');
93 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
94 link
.title
= img
.alt
= 'Ajouter à la sélection';
95 button
.className
= "button slide-select";
100 slide
.widget
= new CartWidget(slide
, link
.href
);
103 case 'hide_for_anonymous':
106 req
= new XMLHttpRequest();
108 req
.open("POST", url
, true);
109 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
111 slide
.className
= 'hidden-slide';
112 link
.setAttribute('name', 'show_for_anonymous');
113 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
114 link
.title
= img
.alt
= 'Montrer au anonymes';
115 button
.className
= "button slide-show";
118 case 'show_for_anonymous':
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', 'hide_for_anonymous');
128 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
129 link
.title
= img
.alt
= 'Masquer pour les anonymes';
130 button
.className
= "button slide-hide";
134 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
137 cb
.setAttribute('checked', 'checked');
140 cb
.removeAttribute('checked');
142 this.selectCBRange(evt
);
146 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
147 switch(fm
.submitButton
.name
) {
149 this.hideSelection();
154 Lightbox
.prototype.onResponseLoad = function(req
) {
155 switch(req
.responseXML
.documentElement
.nodeName
) {
157 this.deleteSelection();
160 this.showSelection();
165 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
166 var tbs
= this.toolbar
.style
;
168 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
169 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
170 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
171 tbs
.position
= 'fixed';
173 this.toolbarPlaceholder
= document
.createElement('div');
174 var phs
= this.toolbarPlaceholder
.style
;
175 phs
.cssText
= tbs
.cssText
;
176 phs
.position
= 'relative';
177 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
180 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
181 tbs
.cssText
= this.toolbar
.defaultCssText
;
186 Lightbox
.prototype.hideSelection = function() {
188 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
189 e
= this.form
.elements
[i
];
190 if (e
.type
=== 'checkbox' && e
.checked
) {
191 slide
= e
.parentNode
.parentNode
;
192 slide
.classList
.add('zero_opacity');
197 Lightbox
.prototype.showSelection = function() {
199 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
200 e
= this.form
.elements
[i
];
201 if (e
.type
=== 'checkbox' && e
.checked
) {
202 slide
= e
.parentNode
.parentNode
;
203 slide
.classList
.remove('zero_opacity');
208 Lightbox
.prototype.deleteSelection = function() {
210 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
211 e
= this.form
.elements
[i
];
212 if (e
.type
=== 'checkbox' && e
.checked
) {
213 slide
= e
.parentNode
.parentNode
;
214 slide
.classList
.add('zero_width');
218 // if you change this, delay you should also change this css rule :
219 // .lightbox span { transition: width 1s
220 setTimeout(function(){self
._removeSelection();}, 1000);
223 Lightbox
.prototype._removeSelection = function() {
226 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
227 e
= this.form
.elements
[i
];
228 if (e
.type
=== 'checkbox' && e
.checked
) {
229 toRemove
.push(e
.parentNode
.parentNode
);
232 for (i
=0 ; i
<toRemove
.length
; i
++) {
234 slide
.parentNode
.removeChild(slide
);
236 this.cbIndex
= undefined;
239 Lightbox
.prototype.getCBIndex = function(cb
) {
241 // build checkbox index
244 var nodes
= this.grid
.childNodes
;
245 for (i
=0 ; i
<nodes
.length
; i
++) {
247 if (node
.nodeName
=== 'SPAN') {
248 c
= node
.getElementsByTagName('input')[0];
249 c
.index
= this.cbIndex
.length
;
250 this.cbIndex
[this.cbIndex
.length
] = c
;
257 Lightbox
.prototype.selectCBRange = function(evt
) {
258 var target
= getTargetedObject(evt
);
259 evt
= getEventObject(evt
);
260 var shift
= evt
.shiftKey
;
261 if (shift
&& this.lastCBChecked
) {
262 var from = this.getCBIndex(this.lastCBChecked
);
263 var to
= this.getCBIndex(target
);
264 var start
= Math
.min(from, to
);
265 var stop
= Math
.max(from, to
);
267 for (i
=start
; i
<stop
; i
++ ) {
268 this.cbIndex
[i
].setAttribute('checked', 'checked');
271 else if (target
.checked
) {
272 this.lastCBChecked
= target
;
275 this.lastCBChecked
= undefined;
280 var _outlineSelectedSlide
;
281 if (browser
.isGecko
) {
282 _outlineSelectedSlide = function(slide
) {
283 slide
.className
= 'selected';
287 _outlineSelectedSlide = function(slide
) {
288 if (slide
.className
&&
289 !reSelected
.test(slide
.className
)) {
290 slide
.className
= slide
.className
+ ' selected';