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.lastSlide
= this.grid
.children
[this.grid
.children
.length
-1];
18 this.toolbar
= toolbar
;
20 this.toolbarFixed
= false;
21 addListener(window
, 'scroll', function(evt
){self
.windowScrollHandler(evt
);});
23 this.lastCBChecked
= undefined;
24 this.form
= undefined;
25 var parent
= this.grid
.parentNode
;
27 parent
= parent
.parentNode
;
28 if (parent
.tagName
=== 'FORM') {
32 else if (parent
.tagName
=== 'BODY') {
36 addListener(this.grid
, 'click', function(evt
){self
.mouseClickHandler(evt
);});
38 var fm
= this.fm
= new FormManager(this.form
);
39 addListener(this.form
, 'change', function(evt
){self
.onChangeHandler(evt
);});
40 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
41 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
45 Lightbox
.prototype.windowScrollHandler = function(evt
) {
46 if (this.toolbar
.offsetTop
< window
.scrollY
&& !this.toolbarFixed
) {
47 this.toolbarFixed
= true;
48 this.backThreshold
= this.toolbar
.offsetTop
;
49 this.switchToolBarPositioning(true);
51 else if (this.toolbarFixed
&& window
.scrollY
< this.backThreshold
) {
52 this.toolbarFixed
= false;
53 this.switchToolBarPositioning(false);
55 if (window
.scrollY
> this.lastSlide
.firstElementChild
.offsetTop
- getWindowHeight()) {
56 console
.log('À boire !');
60 Lightbox
.prototype.mouseClickHandler = function(evt
) {
61 var target
= getTargetedObject(evt
);
62 if (target
.tagName
=== 'IMG') {
64 var link
= target
.parentNode
;
65 var button
= link
.parentNode
;
66 var slide
= button
.parentNode
;
68 if (link
.tagName
=== 'A') {
69 switch(link
.getAttribute('name')) {
70 case 'add_to_selection':
73 req
= new XMLHttpRequest();
75 req
.open("POST", url
, true);
76 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
79 slide
.className
= 'selected';
81 link
.setAttribute('name', 'remove_to_selection');
82 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
83 link
.title
= img
.alt
= 'Retirer de la sélection';
84 button
.className
= "button slide-deselect";
87 case 'remove_to_selection':
90 req
= new XMLHttpRequest();
92 req
.open("POST", url
, true);
93 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
95 slide
.className
= null;
96 link
.setAttribute('name', 'add_to_selection');
97 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
98 link
.title
= img
.alt
= 'Ajouter à la sélection';
99 button
.className
= "button slide-select";
104 slide
.widget
= new CartWidget(slide
, link
.href
);
107 case 'hide_for_anonymous':
110 req
= new XMLHttpRequest();
112 req
.open("POST", url
, true);
113 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
115 slide
.className
= 'hidden-slide';
116 link
.setAttribute('name', 'show_for_anonymous');
117 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
118 link
.title
= img
.alt
= 'Montrer au anonymes';
119 button
.className
= "button slide-show";
122 case 'show_for_anonymous':
125 req
= new XMLHttpRequest();
127 req
.open("POST", url
, true);
128 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
130 slide
.className
= null;
131 link
.setAttribute('name', 'hide_for_anonymous');
132 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
133 link
.title
= img
.alt
= 'Masquer pour les anonymes';
134 button
.className
= "button slide-hide";
138 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
141 cb
.setAttribute('checked', 'checked');
144 cb
.removeAttribute('checked');
146 this.selectCBRange(evt
);
150 Lightbox
.prototype.onChangeHandler = function(evt
) {
151 var target
= getTargetedObject(evt
);
152 if (target
.name
=== 'sort_on') {
153 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
158 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
159 switch(fm
.submitButton
.name
) {
161 this.hideSelection();
166 Lightbox
.prototype.onResponseLoad = function(req
) {
167 switch(req
.responseXML
.documentElement
.nodeName
) {
169 this.deleteSelection();
172 this.showSelection();
175 this.fm
.submitButton
= undefined;
181 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
182 var tbs
= this.toolbar
.style
;
184 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
185 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
186 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
187 tbs
.position
= 'fixed';
189 this.toolbarPlaceholder
= document
.createElement('div');
190 var phs
= this.toolbarPlaceholder
.style
;
191 phs
.cssText
= tbs
.cssText
;
192 phs
.position
= 'relative';
193 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
196 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
197 tbs
.cssText
= this.toolbar
.defaultCssText
;
202 Lightbox
.prototype.hideSelection = function() {
204 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
205 e
= this.form
.elements
[i
];
206 if (e
.type
=== 'checkbox' && e
.checked
) {
207 slide
= e
.parentNode
.parentNode
;
208 slide
.classList
.add('zero_opacity');
213 Lightbox
.prototype.showSelection = function() {
215 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
216 e
= this.form
.elements
[i
];
217 if (e
.type
=== 'checkbox' && e
.checked
) {
218 slide
= e
.parentNode
.parentNode
;
219 slide
.classList
.remove('zero_opacity');
224 Lightbox
.prototype.deleteSelection = 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 slide
= e
.parentNode
.parentNode
;
230 slide
.classList
.add('zero_width');
234 // if you change this, delay you should also change this css rule :
235 // .lightbox span { transition: width 1s
236 setTimeout(function(){self
._removeSelection();}, 1000);
239 Lightbox
.prototype._removeSelection = 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 toRemove
.push(e
.parentNode
.parentNode
);
248 for (i
=0 ; i
<toRemove
.length
; i
++) {
250 slide
.parentNode
.removeChild(slide
);
252 this.cbIndex
= undefined;
255 Lightbox
.prototype.getCBIndex = function(cb
) {
257 // build checkbox index
260 var nodes
= this.grid
.childNodes
;
261 for (i
=0 ; i
<nodes
.length
; i
++) {
263 if (node
.nodeName
=== 'SPAN') {
264 c
= node
.getElementsByTagName('input')[0];
265 c
.index
= this.cbIndex
.length
;
266 this.cbIndex
[this.cbIndex
.length
] = c
;
273 Lightbox
.prototype.selectCBRange = function(evt
) {
274 var target
= getTargetedObject(evt
);
275 evt
= getEventObject(evt
);
276 var shift
= evt
.shiftKey
;
277 if (shift
&& this.lastCBChecked
) {
278 var from = this.getCBIndex(this.lastCBChecked
);
279 var to
= this.getCBIndex(target
);
280 var start
= Math
.min(from, to
);
281 var stop
= Math
.max(from, to
);
283 for (i
=start
; i
<stop
; i
++ ) {
284 this.cbIndex
[i
].setAttribute('checked', 'checked');
287 else if (target
.checked
) {
288 this.lastCBChecked
= target
;
291 this.lastCBChecked
= undefined;
295 Lightbox
.prototype.refreshGrid = function() {
296 var req
= new XMLHttpRequest();
298 req
.onreadystatechange = function() {
299 switch (req
.readyState
) {
305 if (req
.status
=== 200) {
306 self
._refreshGrid(req
)
312 var url
= absolute_url() +
313 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
314 this.grid
.children
.length
;
315 req
.open('GET', url
, true);
319 Lightbox
.prototype._refreshGrid = function(req
) {
320 var doc
= req
.responseXML
.documentElement
;
322 var slides
= this.grid
.children
;
323 for (i
=0 ; i
<doc
.children
.length
; i
++) {
324 this.grid
.replaceChild(getCopyOfNode(doc
.children
[i
]), slides
[i
]);
329 var _outlineSelectedSlide
;
330 if (browser
.isGecko
) {
331 _outlineSelectedSlide = function(slide
) {
332 slide
.className
= 'selected';
336 _outlineSelectedSlide = function(slide
) {
337 if (slide
.className
&&
338 !reSelected
.test(slide
.className
)) {
339 slide
.className
= slide
.className
+ ' selected';