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
) {
16 this.lastCBChecked
= undefined;
17 this.form
= undefined;
18 var parent
= this.grid
.parentNode
;
20 parent
= parent
.parentNode
;
21 if (parent
.tagName
=== 'FORM') {
25 else if (parent
.tagName
=== 'BODY') {
30 addListener(this.grid
, 'click', function(evt
){thisLightbox
.mouseClickHandler(evt
);});
32 var fm
= new FormManager(this.form
);
33 fm
.onBeforeSubmit = function(fm_
, evt
) {return thisLightbox
.onBeforeSubmit(fm_
, evt
);};
34 fm
.onResponseLoad = function(req
) {return thisLightbox
.onResponseLoad(req
);};
38 Lightbox
.prototype.mouseClickHandler = function(evt
) {
39 var target
= getTargetedObject(evt
);
40 if (target
.tagName
=== 'IMG') {
42 var link
= target
.parentNode
;
43 var button
= link
.parentNode
;
44 var slide
= button
.parentNode
;
46 if (link
.tagName
=== 'A') {
47 switch(link
.getAttribute('name')) {
48 case 'add_to_selection':
51 req
= new XMLHttpRequest();
53 req
.open("POST", url
, true);
54 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
57 slide
.className
= 'selected';
59 link
.setAttribute('name', 'remove_to_selection');
60 link
.href
= url
.replace(/(.*\/)add_to_selection$/, '$1remove_to_selection');
61 link
.title
= img
.alt
= 'Retirer de la sélection';
62 button
.className
= "button slide-deselect";
65 case 'remove_to_selection':
68 req
= new XMLHttpRequest();
70 req
.open("POST", url
, true);
71 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
73 slide
.className
= null;
74 link
.setAttribute('name', 'add_to_selection');
75 link
.href
= url
.replace(/(.*\/)remove_to_selection$/, '$1add_to_selection');
76 link
.title
= img
.alt
= 'Ajouter à la sélection';
77 button
.className
= "button slide-select";
82 slide
.widget
= new CartWidget(slide
, link
.href
);
85 case 'hide_for_anonymous':
88 req
= new XMLHttpRequest();
90 req
.open("POST", url
, true);
91 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
93 slide
.className
= 'hidden-slide';
94 link
.setAttribute('name', 'show_for_anonymous');
95 link
.href
= url
.replace(/(.*\/)hideForAnonymous$/, '$1resetHide');
96 link
.title
= img
.alt
= 'Montrer au anonymes';
97 button
.className
= "button slide-show";
100 case 'show_for_anonymous':
103 req
= new XMLHttpRequest();
105 req
.open("POST", url
, true);
106 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
108 slide
.className
= null;
109 link
.setAttribute('name', 'hide_for_anonymous');
110 link
.href
= url
.replace(/(.*\/)resetHide$/, '$1hideForAnonymous');
111 link
.title
= img
.alt
= 'Masquer pour les anonymes';
112 button
.className
= "button slide-hide";
116 } else if(target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
119 cb
.setAttribute('checked', 'checked');
122 cb
.removeAttribute('checked');
124 this.selectCBRange(evt
);
128 Lightbox
.prototype.onBeforeSubmit = function(fm
, evt
) {
129 switch(fm
.submitButton
.name
) {
131 this.hideSelection();
136 Lightbox
.prototype.onResponseLoad = function(req
) {
137 switch(req
.responseXML
.documentElement
.nodeName
) {
139 this.deleteSelection();
142 this.showSelection();
147 Lightbox
.prototype.hideSelection = function() {
149 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
150 e
= this.form
.elements
[i
];
151 if (e
.type
=== 'checkbox' && e
.checked
) {
152 slide
= e
.parentNode
.parentNode
;
153 slide
.classList
.add('zero_opacity');
158 Lightbox
.prototype.showSelection = function() {
160 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
161 e
= this.form
.elements
[i
];
162 if (e
.type
=== 'checkbox' && e
.checked
) {
163 slide
= e
.parentNode
.parentNode
;
164 slide
.classList
.remove('zero_opacity');
169 Lightbox
.prototype.deleteSelection = function() {
171 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
172 e
= this.form
.elements
[i
];
173 if (e
.type
=== 'checkbox' && e
.checked
) {
174 slide
= e
.parentNode
.parentNode
;
175 slide
.classList
.add('zero_width');
179 // if you change this, delay you should also change this css rule :
180 // .lightbox span { transition: width 1s
181 setTimeout(function(){self
._removeSelection();}, 1000);
184 Lightbox
.prototype._removeSelection = function() {
185 console
.info('_removeSelection');
188 for (i
=0 ; i
<this.form
.elements
.length
; i
++) {
189 e
= this.form
.elements
[i
];
190 if (e
.type
=== 'checkbox' && e
.checked
) {
191 toRemove
.push(e
.parentNode
.parentNode
);
194 for (i
=0 ; i
<toRemove
.length
; i
++) {
196 slide
.parentNode
.removeChild(slide
);
198 this.cbIndex
= undefined;
201 Lightbox
.prototype.getCBIndex = function(cb
) {
203 // build checkbox index
206 var nodes
= this.grid
.childNodes
;
207 for (i
=0 ; i
<nodes
.length
; i
++) {
209 if (node
.nodeName
=== 'SPAN') {
210 c
= node
.getElementsByTagName('input')[0];
211 c
.index
= this.cbIndex
.length
;
212 this.cbIndex
[this.cbIndex
.length
] = c
;
219 Lightbox
.prototype.selectCBRange = function(evt
) {
220 var target
= getTargetedObject(evt
);
221 evt
= getEventObject(evt
);
222 var shift
= evt
.shiftKey
;
223 if (shift
&& this.lastCBChecked
) {
224 var from = this.getCBIndex(this.lastCBChecked
);
225 var to
= this.getCBIndex(target
);
226 var start
= Math
.min(from, to
);
227 var stop
= Math
.max(from, to
);
229 for (i
=start
; i
<stop
; i
++ ) {
230 this.cbIndex
[i
].setAttribute('checked', 'checked');
233 else if (target
.checked
) {
234 this.lastCBChecked
= target
;
237 this.lastCBChecked
= undefined;
242 var _outlineSelectedSlide
;
243 if (browser
.isGecko
) {
244 _outlineSelectedSlide = function(slide
) {
245 slide
.className
= 'selected';
249 _outlineSelectedSlide = function(slide
) {
250 if (slide
.className
&&
251 !reSelected
.test(slide
.className
)) {
252 slide
.className
= slide
.className
+ ' selected';