2 * © 2008-2014 Benoît Pin – Centre de recherche en informatique – MINES ParisTech
4 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
13 var keyLeft
= 37, keyRight
= 39;
14 var isTextMime
= /^text\/.+/i;
15 var isAddToSelection
= /.*\/add_to_selection$/;
16 var imgRequestedSize
= /size=(\d+)/;
17 var DEFAULT_IMAGE_SIZES
= [500, 600, 800];
19 FilmSlider = function(filmBar
, slider
, ctxInfos
, image
, toolbar
, breadcrumbs
) {
20 var thisSlider
= this;
21 this.filmBar
= filmBar
;
22 var film
= filmBar
.firstChild
;
23 if (film
.nodeType
== 3)
24 film
= film
.nextSibling
;
27 this.rail
= slider
.parentNode
;
28 this.sliderSpeedRatio
= undefined;
29 this.sliderRatio
= undefined;
30 this.selectedSlide
= undefined;
31 this.selectedSlideInSelection
= undefined;
32 this.cartSlide
= document
.getElementById('cart_slide');
34 this.stretchable
= image
.parentNode
;
35 this.viewMode
= 'medium';
37 this.buttons
= new Array();
38 this.toolbar
= toolbar
;
40 var bcElements
= breadcrumbs
.getElementsByTagName('a');
41 this.lastBCElement
= bcElements
[bcElements
.length
-1];
42 var imgSrcParts
= image
.src
.split('/');
43 this.lastBCElement
.innerHTML
= imgSrcParts
[imgSrcParts
.length
-2];
44 this.hasBreadcrumbs
= true;
47 this.hasBreadcrumbs
= false;
50 var buttons
= toolbar
.getElementsByTagName('img');
52 for (var i
=0 ; i
<buttons
.length
; i
++) {
54 name
= b
.getAttribute('name');
56 this.buttons
[name
] = b
;
59 this.pendingImage
= new Image();
60 this.pendingImage
.onload = function() {
61 thisSlider
.refreshImage();
63 this.initialized
= false;
65 with(this.slider
.style
) {
69 with(this.film
.style
) {
74 this.filmLength
= ctxInfos
.filmLength
;
75 this.center
= ctxInfos
.center
;
76 this.slideSize
= ctxInfos
.slideSize
;
77 this.ctxUrlTranslation
= ctxInfos
.ctxUrlTranslation
;
79 this.ddHandlers
= {'down' : function(evt
){thisSlider
.mouseDownHandler(evt
);},
80 'move' : function(evt
){thisSlider
.mouseMoveHandler(evt
);},
81 'up' : function(evt
){thisSlider
.mouseUpHandler(evt
);},
82 'out' : function(evt
){thisSlider
.mouseOutHandler(evt
);}
86 this.addEventListeners()
90 FilmSlider
.prototype.resizeSlider = function(evt
) {
91 var filmBarWidth
= getObjectWidth(this.filmBar
);
93 var thisSlider
= this;
94 addListener(window
, 'load', function(evt
){thisSlider
.resizeSlider(evt
);});
98 var filmWidth
= this.slideSize
* this.filmLength
;
99 var sliderRatio
= this.sliderRatio
= filmBarWidth
/ filmWidth
;
100 var sliderWidth
= filmBarWidth
* sliderRatio
;
101 this.rail
.style
.width
= filmBarWidth
+ 'px';
102 this.rail
.style
.display
= 'block';
103 this.rail
.style
.visibility
= 'visible';
104 if (sliderRatio
< 1) {
105 this.slider
.style
.width
= Math
.round(sliderWidth
) + 'px';
106 this.slider
.style
.visibility
= 'visible';
109 this.slider
.style
.visibility
= 'hidden';
112 this.winSize
= {'width' : getWindowWidth(),
113 'height' : getWindowHeight()};
114 this.maxRightPosition
= filmBarWidth
- sliderWidth
115 this.sliderSpeedRatio
= - (filmBarWidth
- sliderWidth
) / (filmWidth
- filmBarWidth
);
116 if (!this.initialized
) {
117 this.centerSlide(this.center
);
118 this.selectedSlide
= this.filmBar
.getElementsByTagName('img')[this.center
].parentNode
;
119 this.initialized
= true;
123 FilmSlider
.prototype.fitToScreen = function(evt
) {
125 var thisSlider
= this;
126 addListener(window
, 'resize', function(evt
){thisSlider
._fitToScreen();});
129 FilmSlider
.prototype._fitToScreen = function(evt
) {
130 var wh
= getWindowHeight();
131 var rb
= getObjectTop(this.rail
) + getObjectHeight(this.rail
); // rail bottom
133 var sh
= getObjectHeight(this.stretchable
);
134 var newSize
= sh
+ delta
;
135 this.stretchable
.style
.height
= newSize
+ 'px';
137 var ratio
= this.image
.height
/ this.image
.width
;
138 var bestFitSize
= this.getBestFitSize(ratio
);
139 var currentSize
= parseInt(imgRequestedSize
.exec(this.image
.src
)[1]);
140 if (currentSize
!= bestFitSize
) {
141 var src
= this.image
.src
.replace(imgRequestedSize
, 'size=' + bestFitSize
);
142 this.pendingImage
.src
= src
;
146 FilmSlider
.prototype.getBestFitSize = function(ratio
) {
147 var fw
= getObjectWidth(this.stretchable
) - 1;
148 var fh
= getObjectHeight(this.stretchable
) - 1;
152 for (i
=DEFAULT_IMAGE_SIZES
.length
-1 ; i
>0 ; i
--) {
153 irw
= DEFAULT_IMAGE_SIZES
[i
];
155 if (irw
<= fw
&& irh
<= fh
)
160 for (i
=DEFAULT_IMAGE_SIZES
.length
-1 ; i
>0 ; i
--) {
161 irh
= DEFAULT_IMAGE_SIZES
[i
];
163 if (irw
<= fw
&& irh
<= fh
)
167 return DEFAULT_IMAGE_SIZES
[i
];
170 FilmSlider
.prototype.centerSlide = function(slideIndex
) {
171 if (this.sliderRatio
> 1)
173 var filmBarWidth
= getObjectWidth(this.filmBar
);
174 var x
= slideIndex
* this.slideSize
175 x
= x
- (filmBarWidth
- this.slideSize
) / 2.0;
176 x
= x
* this.sliderSpeedRatio
;
177 var p
= new Point( -x
, 0 )
178 this.setSliderPosition(p
);
181 FilmSlider
.prototype.setSliderPosition = function(point
) {
184 if (point
.x
> this.maxRightPosition
)
185 point
.x
= this.maxRightPosition
;
186 this.slider
.style
.left
= point
.x
+ 'px';
187 this.setFilmPosition(point
);
190 FilmSlider
.prototype.setFilmPosition = function(point
) {
191 this.film
.style
.left
= point
.x
/ this.sliderSpeedRatio
+ 'px';
194 FilmSlider
.prototype.getSliderPosition = function() {
195 var x
= parseInt(this.slider
.style
.left
);
196 var y
= parseInt(this.slider
.style
.top
);
197 var p
= new Point(x
, y
);
201 FilmSlider
.prototype.getFilmPosition = function() {
202 var x
= parseInt(this.film
.style
.left
);
203 var y
= parseInt(this.film
.style
.top
);
204 var p
= new Point(x
, y
);
208 FilmSlider
.prototype.loadSibling = function(previous
) {
211 slide
= this.selectedSlide
.parentNode
.previousSibling
;
212 if (slide
&& slide
.nodeType
==3)
213 slide
= slide
.previousSibling
;
216 slide
= this.selectedSlide
.parentNode
.nextSibling
;
217 if (slide
&& slide
.nodeType
==3)
218 slide
= slide
.nextSibling
;
224 var target
= slide
.getElementsByTagName('a')[0];
225 raiseMouseEvent(target
, 'click');
226 var index
= parseInt(target
.getAttribute('portfolio:position'));
227 this.centerSlide(index
);
231 FilmSlider
.prototype.addEventListeners = function() {
232 var thisSlider
= this;
233 addListener(window
, 'resize', function(evt
){thisSlider
.resizeSlider(evt
);});
234 addListener(this.filmBar
, 'click', function(evt
){thisSlider
.thumbnailClickHandler(evt
);});
235 addListener(this.toolbar
, 'click', function(evt
){thisSlider
.toolbarClickHandler(evt
);});
236 addListener(window
, 'load', function(evt
){thisSlider
.fitToScreen(evt
);});
239 addListener(this.slider
, 'mousedown', this.ddHandlers
['down']);
240 if(browser
.isDOM2Event
){
241 if (browser
.isAppleWebKit
) {
242 this.filmBar
.addEventListener('mousewheel', function(evt
){thisSlider
.mouseWheelHandler(evt
);}, false);
245 addListener(this.filmBar
, 'DOMMouseScroll', function(evt
){thisSlider
.mouseWheelHandler(evt
);});
248 else if (browser
.isIE6up
) {
249 addListener(this.filmBar
, 'mousewheel', function(evt
){thisSlider
.mouseWheelHandler(evt
);});
252 addListener(document
, 'keydown', function(evt
){thisSlider
.keyDownHandler(evt
);});
253 addListener(document
, 'keypress', function(evt
){thisSlider
.keyPressHandler(evt
);});
257 FilmSlider
.prototype.mouseDownHandler = function(evt
) {
258 this.initialClickPoint
= new Point(evt
.clientX
, evt
.clientY
);
259 this.initialPosition
= this.getSliderPosition();
260 this.dragInProgress
= true;
261 addListener(document
, 'mousemove', this.ddHandlers
['move']);
262 addListener(document
, 'mouseup', this.ddHandlers
['up']);
263 addListener(document
.body
, 'mouseout', this.ddHandlers
['out'])
268 FilmSlider
.prototype.mouseMoveHandler = function(evt
) {
269 if(!this.dragInProgress
)
273 evt
= getEventObject(evt
);
274 var currentPoint
= new Point(evt
.clientX
, evt
.clientY
);
275 var displacement
= currentPoint
.diff(this.initialClickPoint
);
276 this.setSliderPosition(this.initialPosition
.add(displacement
));
279 FilmSlider
.prototype.mouseUpHandler = function(evt
) {
280 this.dragInProgress
= false;
281 evt
= getEventObject(evt
);
282 this.mouseMoveHandler(evt
);
286 FilmSlider
.prototype.mouseOutHandler = function(evt
) {
287 evt
= getEventObject(evt
);
291 x
> this.winSize
['width'] ||
293 y
> this.winSize
['height']
295 this.mouseUpHandler(evt
);
299 FilmSlider
.prototype.thumbnailClickHandler = function(evt
) {
300 var target
= getTargetedObject(evt
);
301 while (target
.tagName
!= 'A' && target
!= this.filmBar
)
302 target
= target
.parentNode
;
303 if (target
.tagName
!= 'A')
306 if (this.viewMode
== 'full') {
307 this.mosaique
.unload();
308 this.mosaique
= null;
309 this.viewMode
= 'medium';
312 disablePropagation(evt
);
314 history
.pushState(target
.href
, '', target
.href
);
316 var imgBaseUrl
= target
.href
;
318 if (this.ctxUrlTranslation
[0])
319 canonicalImgUrl
= imgBaseUrl
.replace(this.ctxUrlTranslation
[0],
320 this.ctxUrlTranslation
[1]);
322 canonicalImgUrl
= imgBaseUrl
;
324 var ajaxUrl
= imgBaseUrl
+ '/photo_view_ajax';
327 //this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=600';
328 var thumbnail
= target
.getElementsByTagName('IMG')[0];
329 var bestFitSize
= this.getBestFitSize(thumbnail
.height
/thumbnail
.width
);
330 this.pendingImage
.src
= canonicalImgUrl
+ '/getResizedImage?size=' + bestFitSize
;
333 var fullScreenLink
= this.buttons
['full_screen'].parentNode
;
334 fullScreenLink
.href
= canonicalImgUrl
+ '/zoom_view';
336 var toggleSelectionBtn
= this.buttons
['toggle_selection'];
337 var toggleSelectionLink
= toggleSelectionBtn
.parentNode
;
338 this.selectedSlideInSelection
= (target
.className
=='selected');
339 if (this.selectedSlideInSelection
) {
340 toggleSelectionBtn
.src
= portal_url() + '/unselect_flag_btn.gif';
341 toggleSelectionBtn
.alt
= toggleSelectionLink
.title
= 'Retirer de la sélection';
342 toggleSelectionLink
.href
= canonicalImgUrl
+ '/remove_to_selection';
345 toggleSelectionBtn
.src
= portal_url() + '/select_flag_btn.gif';
346 toggleSelectionBtn
.alt
= toggleSelectionLink
.title
= 'Ajouter à la sélection';
347 toggleSelectionLink
.href
= canonicalImgUrl
+ '/add_to_selection';
350 var showBuyableButtonLink
= this.buttons
['show_buyable'].parentNode
;
351 showBuyableButtonLink
.href
= canonicalImgUrl
+ '/get_slide_buyable_items';
352 this.cartSlide
.innerHTML
= '';
353 this.cartSlide
.style
.visibility
='hidden';
356 var metadataButton
= this.buttons
['edit_metadata']
357 if (metadataButton
) {
358 var metadataEditLink
= metadataButton
.parentNode
;
359 metadataEditLink
.href
= canonicalImgUrl
+ '/photo_edit_form'
363 var req
= new XMLHttpRequest();
364 req
.onreadystatechange = function() {
365 switch (req
.readyState
) {
371 if (! isTextMime
.exec(req
.getResponseHeader('Content-Type'))) {
372 req
.onreadystatechange
= null;
375 window
.location
.href
= thisFS
._fallBackUrl
;
382 if (req
.status
== '200')
383 thisFS
.populateViewer(req
);
385 //window.location.href = target.href;
386 console
.error(ajaxUrl
);
391 req
.open("GET", ajaxUrl
, true);
394 // update old displayed slide className
395 var className
= this.selectedSlide
.className
;
396 var classes
= className
.split(' ');
397 var newClasses
= new Array();
402 if (name
== 'displayed')
405 newClasses
.push(name
);
408 this.selectedSlide
.className
= newClasses
.join(' ')
410 // hightlight new displayed slide
411 this.selectedSlide
= target
;
412 className
= this.selectedSlide
.className
;
413 classes
= className
.split(' ');
414 classes
.push('displayed');
415 this.selectedSlide
.className
= classes
.join(' ');
419 FilmSlider
.prototype.toolbarClickHandler = function(evt
) {
420 var target
= getTargetedObject(evt
);
421 if(target
.tagName
== 'IMG' && target
.getAttribute('name')){
422 switch(target
.getAttribute('name')) {
425 disablePropagation(evt
);
427 var link
= button
.parentNode
;
429 this.loadSibling(true);
433 disablePropagation(evt
);
435 var link
= button
.parentNode
;
437 this.loadSibling(false);
441 disablePropagation(evt
);
442 target
.parentNode
.blur();
443 if (this.viewMode
== 'full') {
444 this.mosaique
.unload();
445 this.mosaique
= null;
446 this.viewMode
= 'medium';
449 var main
= document
.getElementById('photo_viewer');
450 var url
= target
.parentNode
.href
;
451 url
= url
.substring(0, url
.length
- '/zoom_view'.length
);
452 var margins
= {'top':0, 'right':-1, 'bottom':0, 'left':0};
453 this.mosaique
= new Mosaique(main
, url
, margins
);
454 this.viewMode
= 'full';
457 case 'toggle_selection':
459 disablePropagation(evt
);
461 var link
= button
.parentNode
;
464 var req
= new XMLHttpRequest();
466 req
.open("POST", url
, true);
467 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
471 var parts
= url
.split('/');
472 var canonicalImgUrl
= parts
.slice(0, parts
.length
-1).join('/');
474 if (isAddToSelection
.test(url
)) {
475 button
.src
= portal_url() + '/unselect_flag_btn.gif';
476 button
.alt
= link
.title
= 'Retirer de la sélection';
477 link
.href
= canonicalImgUrl
+ '/remove_to_selection';
478 this.selectedSlide
.className
= 'selected displayed';
479 this.image
.parentNode
.className
= 'selected';
480 this.selectedSlideInSelection
= true;
483 button
.src
= portal_url() + '/select_flag_btn.gif';
484 button
.alt
= link
.title
= 'Ajouter à la sélection';
485 link
.href
= canonicalImgUrl
+ '/add_to_selection';
486 this.selectedSlide
.className
= 'displayed';
487 this.image
.parentNode
.className
= '';
488 this.selectedSlideInSelection
= false;
494 disablePropagation(evt
);
496 var link
= button
.parentNode
;
498 var slide
= this.cartSlide
;
499 slide
.innerHTML
= '';
500 slide
.style
.visibility
= 'visible';
501 var cw
= new CartWidget(slide
, link
.href
);
502 cw
.onCancel = function() {
503 CartWidget
.prototype.onCancel
.apply(this);
504 slide
.style
.visibility
= 'hidden';
506 cw
.onAfterConfirm = function() {
507 slide
.style
.visibility
= 'hidden';
515 case 'edit_metadata' :
517 disablePropagation(evt);
519 if (this.viewMode == 'full') {
520 this.mosaique.unload();
521 this.mosaique = null;
522 this.viewMode = 'medium';
525 var fi = new FragmentImporter(absolute_url());
526 fi.useMacro('metadata_edit_form_macros', 'iptc', 'image_metadata');
534 if(browser
.isDOM2Event
){
535 if (browser
.isAppleWebKit
) {
536 FilmSlider
.prototype.mouseWheelHandler = function(evt
) {
538 var pos
= this.getSliderPosition();
539 pos
.x
-= evt
.wheelDelta
/ 40;
540 this.setSliderPosition(pos
);
544 FilmSlider
.prototype.mouseWheelHandler = function(evt
) {
546 var pos
= this.getSliderPosition();
547 pos
.x
+= evt
.detail
* 3;
548 this.setSliderPosition(pos
);
552 else if (browser
.isIE6up
) {
553 FilmSlider
.prototype.mouseWheelHandler = function() {
554 var evt
= window
.event
;
555 evt
.returnValue
= false;
556 var pos
= this.getSliderPosition();
557 pos
.x
-= evt
.wheelDelta
/ 40;
558 this.setSliderPosition(pos
);
562 FilmSlider
.prototype.keyDownHandler = function(evt
) {
563 var evt
= getEventObject(evt
);
564 switch (evt
.keyCode
) {
566 this.loadSibling(true);
569 this.loadSibling(false);
577 FilmSlider
.prototype.keyPressHandler = function(evt
) {
578 var target
= getTargetedObject(evt
);
579 if (target
.tagName
== 'INPUT' || target
.tagName
== 'TEXTAREA')
581 var evt
= getEventObject(evt
);
582 var charPress
= String
.fromCharCode((evt
.keyCode
) ? evt
.keyCode
: evt
.which
);
586 raiseMouseEvent(this.buttons
['full_screen'], 'click');
591 FilmSlider
.prototype.populateViewer = function(req
) {
592 var elements
= req
.responseXML
.documentElement
.childNodes
;
593 for(var i
=0 ; i
< elements
.length
; i
++ ) {
594 element
= elements
[i
];
595 switch (element
.nodeName
) {
597 var dest
= document
.getElementById(element
.getAttribute('id'));
598 if (dest
) { dest
.innerHTML
= element
.firstChild
.nodeValue
; }
600 case 'imageattributes' :
601 var link
= this.buttons
['back_to_portfolio'].parentNode
;
602 link
.href
= element
.getAttribute('backToContextUrl');
603 link
= this.buttons
['show_buyable'].parentNode
;
604 var buyable
= element
.getAttribute('buyable');
605 if(buyable
== 'True')
606 link
.className
= null;
607 else if(buyable
== 'False')
608 link
.className
= 'hidden';
609 this.image
.alt
= element
.getAttribute('alt');
610 this.updateBreadcrumbs(element
.getAttribute('lastBcUrl'),
611 element
.getAttribute('img_id'));
617 FilmSlider
.prototype.refreshImage = function() {
618 this.image
.style
.visibility
= 'hidden';
619 this.image
.src
= this.pendingImage
.src
;
620 this.image
.width
= this.pendingImage
.width
;
621 this.image
.height
= this.pendingImage
.height
;
622 this.image
.style
.visibility
= 'visible';
623 if (this.selectedSlideInSelection
)
624 this.image
.parentNode
.className
= 'selected';
626 this.image
.parentNode
.className
= '';
629 FilmSlider
.prototype.updateBreadcrumbs = function(url
, title
) {
630 if (this.hasBreadcrumbs
) {
631 this.lastBCElement
.href
= element
.getAttribute('lastBcUrl');
632 this.lastBCElement
.innerHTML
= element
.getAttribute('img_id');
636 FilmSlider
.prototype.startSlideShow = function() {
637 this.slideShowSlide
= this.pendingSlideShowSlide
= this.selectedSlide
;
638 return this.slideShowSlide
.href
;
641 FilmSlider
.prototype.slideShowNext = function() {
642 var nextSlide
= this.slideShowSlide
.parentNode
.nextSibling
;
643 if (nextSlide
&& nextSlide
.nodeType
==3)
644 nextSlide
= nextSlide
.nextSibling
;
647 nextSlide
= nextSlide
.getElementsByTagName('a')[0];
648 this.pendingSlideShowSlide
= nextSlide
;
649 return this.pendingSlideShowSlide
.href
;
652 var row
= this.slideShowSlide
.parentNode
.parentNode
;
653 var first
= row
.firstChild
;
654 if (first
.nodeType
==3)
655 first
= first
.nextSibling
;
656 this.pendingSlideShowSlide
= first
.getElementsByTagName('a')[0];
657 return this.pendingSlideShowSlide
.href
;
661 FilmSlider
.prototype.slideShowPrevious = function() {
662 var previousSlide
= this.slideShowSlide
.parentNode
.previousSibling
;
663 if (previousSlide
&& previousSlide
.nodeType
==3)
664 previousSlide
= previousSlide
.previousSibling
;
667 previousSlide
= previousSlide
.getElementsByTagName('a')[0];
668 this.pendingSlideShowSlide
= previousSlide
;
669 return this.pendingSlideShowSlide
.href
;
672 var row
= this.slideShowSlide
.parentNode
.parentNode
;
673 var last
= row
.lastChild
;
674 if (last
.nodeType
==3)
675 last
= last
.previousSibling
;
676 this.pendingSlideShowSlide
= last
.getElementsByTagName('a')[0];
677 return this.pendingSlideShowSlide
.href
;
681 FilmSlider
.prototype.slideShowImageLoaded = function() {
682 this.slideShowSlide
= this.pendingSlideShowSlide
;
685 FilmSlider
.prototype.stopSlideShow = function() {
686 raiseMouseEvent(this.slideShowSlide
, 'click');
687 var index
= parseInt(this.selectedSlide
.getAttribute('portfolio:position'));
688 this.centerSlide(index
);
693 function Point(x
, y
) {
694 this.x
= Math
.round(x
);
695 this.y
= Math
.round(y
);
697 Point
.prototype.diff = function(point
) { return new Point(this.x
- point
.x
, this.y
- point
.y
); };
698 Point
.prototype.add = function(point
) { return new Point(this.x
+ point
.x
, this.y
+ point
.y
); };
699 Point
.prototype.mul = function(k
) { return new Point(this.x
* k
, this.y
*k
)};
700 Point
.prototype.toString = function() { return "(" + String(this.x
) + ", " + String(this.y
) + ")"; };