ac20cd33bd04c6de8eb1165ce6b1187ccf1a59ad
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
= this.fm
= new FormManager(this.form
);
38 addListener(this.form
, 'change', function(evt
){self
.onChangeHandler(evt
);});
39 fm
.onBeforeSubmit = function(fm_
, evt
) {return self
.onBeforeSubmit(fm_
, evt
);};
40 fm
.onResponseLoad = function(req
) {return self
.onResponseLoad(req
);};
44 Lightbox
.prototype.windowScrollHandler = function(evt
) {
45 if (this.toolbar
.offsetTop
< window
.scrollY
&& !this.toolbarFixed
) {
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.onChangeHandler = function(evt
) {
147 var target
= getTargetedObject(evt
);
148 if (target
.name
=== 'sort_on') {
149 this.fm
.submitButton
= {'name' : 'set_sorting', 'value' : 'ok'};
154 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
155 switch(fm
.submitButton
.name
) {
157 this.hideSelection();
162 Lightbox
.prototype.onResponseLoad = function(req
) {
163 switch(req
.responseXML
.documentElement
.nodeName
) {
165 this.deleteSelection();
168 this.showSelection();
171 this.fm
.submitButton
= undefined;
177 Lightbox
.prototype.switchToolBarPositioning = function(fixed
) {
178 var tbs
= this.toolbar
.style
;
180 this.toolbar
.defaultCssText
= this.toolbar
.style
.cssText
;
181 tbs
.width
= String(this.toolbar
.offsetWidth
) + 'px';
182 tbs
.height
= String(this.toolbar
.offsetHeight
) + 'px';
183 tbs
.position
= 'fixed';
185 this.toolbarPlaceholder
= document
.createElement('div');
186 var phs
= this.toolbarPlaceholder
.style
;
187 phs
.cssText
= tbs
.cssText
;
188 phs
.position
= 'relative';
189 this.toolbar
.parentNode
.insertBefore(this.toolbarPlaceholder
, this.toolbar
);
192 this.toolbarPlaceholder
.parentNode
.removeChild(this.toolbarPlaceholder
);
193 tbs
.cssText
= this.toolbar
.defaultCssText
;
198 Lightbox
.prototype.hideSelection = function() {
200 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
201 e
= this.form
.elements
[i
];
202 if (e
.type
=== 'checkbox' && e
.checked
) {
203 slide
= e
.parentNode
.parentNode
;
204 slide
.classList
.add('zero_opacity');
209 Lightbox
.prototype.showSelection = function() {
211 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
212 e
= this.form
.elements
[i
];
213 if (e
.type
=== 'checkbox' && e
.checked
) {
214 slide
= e
.parentNode
.parentNode
;
215 slide
.classList
.remove('zero_opacity');
220 Lightbox
.prototype.deleteSelection = 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_width');
230 // if you change this, delay you should also change this css rule :
231 // .lightbox span { transition: width 1s
232 setTimeout(function(){self
._removeSelection();}, 1000);
235 Lightbox
.prototype._removeSelection = function() {
238 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
239 e
= this.form
.elements
[i
];
240 if (e
.type
=== 'checkbox' && e
.checked
) {
241 toRemove
.push(e
.parentNode
.parentNode
);
244 for (i
=0 ; i
<toRemove
.length
; i
++) {
246 slide
.parentNode
.removeChild(slide
);
248 this.cbIndex
= undefined;
251 Lightbox
.prototype.getCBIndex = function(cb
) {
253 // build checkbox index
256 var nodes
= this.grid
.childNodes
;
257 for (i
=0 ; i
<nodes
.length
; i
++) {
259 if (node
.nodeName
=== 'SPAN') {
260 c
= node
.getElementsByTagName('input')[0];
261 c
.index
= this.cbIndex
.length
;
262 this.cbIndex
[this.cbIndex
.length
] = c
;
269 Lightbox
.prototype.selectCBRange = function(evt
) {
270 var target
= getTargetedObject(evt
);
271 evt
= getEventObject(evt
);
272 var shift
= evt
.shiftKey
;
273 if (shift
&& this.lastCBChecked
) {
274 var from = this.getCBIndex(this.lastCBChecked
);
275 var to
= this.getCBIndex(target
);
276 var start
= Math
.min(from, to
);
277 var stop
= Math
.max(from, to
);
279 for (i
=start
; i
<stop
; i
++ ) {
280 this.cbIndex
[i
].setAttribute('checked', 'checked');
283 else if (target
.checked
) {
284 this.lastCBChecked
= target
;
287 this.lastCBChecked
= undefined;
291 Lightbox
.prototype.refreshGrid = function() {
292 if (!this.uidIndex
) {
293 // build checkbox index
295 var i
, node
, length
=0;
296 var nodes
= this.grid
.childNodes
;
297 for (i
=0 ; i
<nodes
.length
; i
++) {
299 if (node
.nodeName
=== 'SPAN') {
300 this.uidIndex
[node
.name
] = node
;
304 this.uidIndex
.length
= length
;
306 var req
= new XMLHttpRequest();
308 req
.onreadystatechange = function() {
309 switch (req
.readyState
) {
315 if (req
.status
=== 200) {
316 self
._refreshGrid(req
)
322 var url
= absolute_url() +
323 '/portfolio_thumbnails_tail?start:int=0&size:int=' +
324 this.uidIndex
.length
;
325 req
.open('GET', url
, true);
329 Lightbox
.prototype._refreshGrid = function(req
) {
334 var _outlineSelectedSlide
;
335 if (browser
.isGecko
) {
336 _outlineSelectedSlide = function(slide
) {
337 slide
.className
= 'selected';
341 _outlineSelectedSlide = function(slide
) {
342 if (slide
.className
&&
343 !reSelected
.test(slide
.className
)) {
344 slide
.className
= slide
.className
+ ' selected';