cf39a0aa4af2b29f938914e0de2bb7069acd4443
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
) {
19 for (i
=0 ; i
<this.grid
.childNodes
.length
; i
++) {
20 node
= this.grid
.childNodes
[i
];
21 if (node
.nodeType
=== 1) { // is element
22 this.slides
.push(node
);
25 this.lastSlide
= this.slides
[this.slides
.length
-1];
26 this.fetchingDisabled
= false;
27 this.complete
= complete
;
28 console
.log('complete:', complete
)
29 this.toolbar
= toolbar
;
31 this.toolbarFixed
= false;
32 addListener(window
, 'scroll', function(evt
){self
.windowScrollToolbarlHandler(evt
);});
34 addListener(window
, 'scroll', function(evt
){self
.windowScrollGridHandler(evt
);});
35 addListener(window
, 'load', function(evt
){ self
.windowScrollGridHandler();});
36 this.lastCBChecked
= undefined;
37 this.form
= undefined;
38 var parent
= this.grid
.parentNode
;
40 parent
= parent
.parentNode
;
41 if (parent
.tagName
=== 'FORM') {
45 else if (parent
.tagName
=== 'BODY') {
49 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
51 var fm
= this.fm
= new FormManager(this.form
);
52 addListener(this.form
, 'change', function(evt
){self
.onChangeHandler(evt
);});
53 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
54 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
58 Lightbox
.prototype.windowScrollToolbarlHandler = function(evt
) {
59 if (this.toolbar
.offsetTop
< window
.scrollY
&& !this.toolbarFixed
) {
60 this.toolbarFixed
= true;
61 this.backThreshold
= this.toolbar
.offsetTop
;
62 this.switchToolBarPositioning(true);
64 else if (this.toolbarFixed
&& window
.scrollY
< this.backThreshold
) {
65 this.toolbarFixed
= false;
66 this.switchToolBarPositioning(false);
69 Lightbox
.prototype.windowScrollGridHandler = function(evt
) {
71 !this.fetchingDisabled
&&
72 window
.scrollY
> this.lastSlide
.firstElementChild
.offsetTop
- getWindowHeight()) {
73 this.fetchingDisabled
= true;
78 Lightbox
.prototype.mouseClickHandler = function(evt
) {
79 var target
= getTargetedObject(evt
);
80 if (target
.tagName
=== 'IMG') {
82 var link
= target
.parentNode
;
83 var button
= link
.parentNode
;
84 var slide
= button
.parentNode
;
86 if (link
.tagName
=== 'A') {
87 switch(link
.getAttribute('name')) {
88 case 'add_to_selection':
91 req
= new XMLHttpRequest();
93 req
.open("POST", url
, true);
94 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
97 slide
.className
= 'selected';
99 link
.setAttribute('name', 'remove_to_selection');
100 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
101 link
.title
= img
.alt
= 'Retirer de la sélection';
102 button
.className
= "button slide-deselect";
105 case 'remove_to_selection':
108 req
= new XMLHttpRequest();
110 req
.open("POST", url
, true);
111 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
113 slide
.className
= null;
114 link
.setAttribute('name', 'add_to_selection');
115 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
116 link
.title
= img
.alt
= 'Ajouter à la sélection';
117 button
.className
= "button slide-select";
122 slide
.widget
= new CartWidget(slide
, link
.href
);
125 case 'hide_for_anonymous':
128 req
= new XMLHttpRequest();
130 req
.open("POST", url
, true);
131 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
133 slide
.className
= 'hidden-slide';
134 link
.setAttribute('name', 'show_for_anonymous');
135 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
136 link
.title
= img
.alt
= 'Montrer au anonymes';
137 button
.className
= "button slide-show";
140 case 'show_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
= null;
149 link
.setAttribute('name', 'hide_for_anonymous');
150 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
151 link
.title
= img
.alt
= 'Masquer pour les anonymes';
152 button
.className
= "button slide-hide";
156 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
159 cb
.setAttribute('checked', 'checked');
162 cb
.removeAttribute('checked');
164 this.selectCBRange(evt
);
168 Lightbox
.prototype.onChangeHandler = function(evt
) {
169 var target
= getTargetedObject(evt
);
170 if (target
.name
=== 'sort_on') {
171 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
176 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
177 switch(fm
.submitButton
.name
) {
179 this.hideSelection();
184 Lightbox
.prototype.onResponseLoad = function(req
) {
185 switch(req
.responseXML
.documentElement
.nodeName
) {
187 this.deleteSelection();
190 this.showSelection();
193 this.fm
.submitButton
= undefined;
199 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
200 var tbs
= this.toolbar
.style
;
202 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
203 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
204 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
205 tbs
.position
= 'fixed';
207 this.toolbarPlaceholder
= document
.createElement('div');
208 var phs
= this.toolbarPlaceholder
.style
;
209 phs
.cssText
= tbs
.cssText
;
210 phs
.position
= 'relative';
211 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
214 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
215 tbs
.cssText
= this.toolbar
.defaultCssText
;
220 Lightbox
.prototype.hideSelection = function() {
222 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
223 e
= this.form
.elements
[i
];
224 if (e
.type
=== 'checkbox' && e
.checked
) {
225 slide
= e
.parentNode
.parentNode
;
226 slide
.classList
.add('zero_opacity');
231 Lightbox
.prototype.showSelection = function() {
233 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
234 e
= this.form
.elements
[i
];
235 if (e
.type
=== 'checkbox' && e
.checked
) {
236 slide
= e
.parentNode
.parentNode
;
237 slide
.classList
.remove('zero_opacity');
242 Lightbox
.prototype.deleteSelection = function() {
244 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
245 e
= this.form
.elements
[i
];
246 if (e
.type
=== 'checkbox' && e
.checked
) {
247 slide
= e
.parentNode
.parentNode
;
248 slide
.classList
.add('zero_width');
252 // if you change this, delay you should also change this css rule :
253 // .lightbox span { transition: width 1s
254 setTimeout(function(){self
._removeSelection();}, 1000);
257 Lightbox
.prototype._removeSelection = function() {
260 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
261 e
= this.form
.elements
[i
];
262 if (e
.type
=== 'checkbox' && e
.checked
) {
263 toRemove
.push(e
.parentNode
.parentNode
);
266 for (i
=0 ; i
<toRemove
.length
; i
++) {
268 slide
.parentNode
.removeChild(slide
);
270 this.cbIndex
= undefined;
273 Lightbox
.prototype.getCBIndex = function(cb
) {
275 // build checkbox index
278 var nodes
= this.grid
.childNodes
;
279 for (i
=0 ; i
<nodes
.length
; i
++) {
281 if (node
.nodeName
=== 'SPAN') {
282 c
= node
.getElementsByTagName('input')[0];
283 c
.index
= this.cbIndex
.length
;
284 this.cbIndex
[this.cbIndex
.length
] = c
;
291 Lightbox
.prototype.selectCBRange = function(evt
) {
292 var target
= getTargetedObject(evt
);
293 evt
= getEventObject(evt
);
294 var shift
= evt
.shiftKey
;
295 if (shift
&& this.lastCBChecked
) {
296 var from = this.getCBIndex(this.lastCBChecked
);
297 var to
= this.getCBIndex(target
);
298 var start
= Math
.min(from, to
);
299 var stop
= Math
.max(from, to
);
301 for (i
=start
; i
<stop
; i
++ ) {
302 this.cbIndex
[i
].setAttribute('checked', 'checked');
305 else if (target
.checked
) {
306 this.lastCBChecked
= target
;
309 this.lastCBChecked
= undefined;
313 Lightbox
.prototype.refreshGrid = function() {
314 var req
= new XMLHttpRequest();
316 req
.onreadystatechange = function() {
317 switch (req
.readyState
) {
323 if (req
.status
=== 200) {
324 self
._refreshGrid(req
)
330 var url
= absolute_url() +
331 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
333 req
.open('GET', url
, true);
337 Lightbox
.prototype._refreshGrid = function(req
) {
338 var doc
= req
.responseXML
.documentElement
;
340 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
341 node
= doc
.childNodes
[i
];
342 if (node
.nodeType
=== 1) {
343 this.slides
[i
] = this.grid
.replaceChild(getCopyOfNode(node
), this.slides
[i
]);
348 Lightbox
.prototype.fetchTail = function() {
349 var req
= new XMLHttpRequest();
351 req
.onreadystatechange = function() {
352 switch (req
.readyState
) {
358 if (req
.status
=== 200) {
359 self
._appendTail(req
)
365 var url
= absolute_url() +
366 '/portfolio_thumbnails_tail?start:int=' +
367 String(this.slides
.length
+ 1 ) +
369 req
.open('GET', url
, true);
373 Lightbox
.prototype._appendTail = function(req
) {
374 var doc
= req
.responseXML
.documentElement
;
376 for (i
=0 ; i
<doc
.childNodes
.length
; i
++) {
377 node
= doc
.childNodes
[i
];
378 if (node
.nodeType
=== 1) {
379 this.lastSlide
= this.grid
.appendChild(getCopyOfNode(node
));
380 this.slides
.push(this.lastSlide
);
383 this.fetchingDisabled
= false;
384 if (doc
.getAttribute('nomore')) {
385 this.complete
= true;
386 console
.info('complete');
388 this.windowScrollGridHandler();
392 var _outlineSelectedSlide
;
393 if (browser
.isGecko
) {
394 _outlineSelectedSlide = function(slide
) {
395 slide
.className
= 'selected';
399 _outlineSelectedSlide = function(slide
) {
400 if (slide
.className
&&
401 !reSelected
.test(slide
.className
)) {
402 slide
.className
= slide
.className
+ ' selected';