2 * © 2008 Benoît Pin – Centre de recherche en informatique – École des mines de Paris
4 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
5 * $Id: photo_film_viewer.js 1303 2009-08-21 22:19:05Z pin $
6 * $URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/skins/photo_film_viewer.js $
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
;
39 var bcElements
= breadcrumbs
.getElementsByTagName('a');
40 this.lastBCElement
= bcElements
[bcElements
.length
-1];
41 var imgSrcParts
= image
.src
.split('/');
42 this.lastBCElement
.innerHTML
= imgSrcParts
[imgSrcParts
.length
-2];
44 var buttons
= toolbar
.getElementsByTagName('img');
46 for (var i
=0 ; i
<buttons
.length
; i
++) {
48 name
= b
.getAttribute('name');
50 this.buttons
[name
] = b
;
53 this.pendingImage
= new Image();
54 this.pendingImage
.onload = function() {
55 thisSlider
.refreshImage();
57 this.initialized
= false;
59 with(this.slider
.style
) {
63 with(this.film
.style
) {
68 this.filmLength
= ctxInfos
.filmLength
;
69 this.center
= ctxInfos
.center
;
70 this.slideSize
= ctxInfos
.slideSize
;
71 this.ctxUrlTranslation
= ctxInfos
.ctxUrlTranslation
;
73 this.ddHandlers
= {'down' : function(evt
){thisSlider
.mouseDownHandler(evt
);},
74 'move' : function(evt
){thisSlider
.mouseMoveHandler(evt
);},
75 'up' : function(evt
){thisSlider
.mouseUpHandler(evt
);},
76 'out' : function(evt
){thisSlider
.mouseOutHandler(evt
);}
80 this.addEventListeners()
84 FilmSlider
.prototype.resizeSlider = function(evt
) {
85 var filmBarWidth
= getObjectWidth(this.filmBar
);
87 var thisSlider
= this;
88 addListener(window
, 'load', function(evt
){thisSlider
.resizeSlider(evt
);});
92 var filmWidth
= this.slideSize
* this.filmLength
;
93 var sliderRatio
= this.sliderRatio
= filmBarWidth
/ filmWidth
;
94 var sliderWidth
= filmBarWidth
* sliderRatio
;
95 if (sliderRatio
< 1) {
96 this.rail
.style
.width
= filmBarWidth
+ 'px';
97 this.slider
.style
.width
= Math
.round(sliderWidth
) + 'px';
98 this.rail
.style
.display
= 'block';
99 this.rail
.style
.visibility
= 'visible';
102 this.rail
.style
.display
= 'none';
103 this.rail
.style
.visibility
= 'hidden';
106 this.winSize
= {'width' : getWindowWidth(),
107 'height' : getWindowHeight()};
108 this.maxRightPosition
= filmBarWidth
- sliderWidth
109 this.sliderSpeedRatio
= - (filmBarWidth
- sliderWidth
) / (filmWidth
- filmBarWidth
);
110 if (!this.initialized
) {
111 this.centerSlide(this.center
);
112 this.selectedSlide
= this.filmBar
.getElementsByTagName('img')[this.center
].parentNode
;
113 this.initialized
= true;
117 FilmSlider
.prototype.fitToScreen = function(evt
) {
119 var thisSlider
= this;
120 addListener(window
, 'resize', function(evt
){thisSlider
._fitToScreen();});
123 FilmSlider
.prototype._fitToScreen = function(evt
) {
124 var wh
= getWindowHeight();
125 var ch
= document
.body
.clientHeight
;
126 var sh
= getObjectHeight(this.stretchable
);
127 var newSize
= sh
+ wh
- ch
;
128 this.stretchable
.style
.height
= newSize
+ 'px';
130 var ratio
= this.image
.height
/ this.image
.width
;
131 var bestFitSize
= this.getBestFitSize(ratio
);
132 var currentSize
= parseInt(imgRequestedSize
.exec(this.image
.src
)[1]);
133 if (currentSize
!= bestFitSize
) {
134 var src
= this.image
.src
.replace(imgRequestedSize
, 'size=' + bestFitSize
);
135 this.pendingImage
.src
= src
;
139 FilmSlider
.prototype.getBestFitSize = function(ratio
) {
140 var fw
= getObjectWidth(this.stretchable
) - 1;
141 var fh
= getObjectHeight(this.stretchable
) - 1;
145 for (i
=DEFAULT_IMAGE_SIZES
.length
-1 ; i
>0 ; i
--) {
146 irw
= DEFAULT_IMAGE_SIZES
[i
];
148 if (irw
<= fw
&& irh
<= fh
)
153 for (i
=DEFAULT_IMAGE_SIZES
.length
-1 ; i
>0 ; i
--) {
154 irh
= DEFAULT_IMAGE_SIZES
[i
];
156 if (irw
<= fw
&& irh
<= fh
)
160 return DEFAULT_IMAGE_SIZES
[i
];
163 FilmSlider
.prototype.centerSlide = function(slideIndex
) {
164 if (this.sliderRatio
> 1)
166 var filmBarWidth
= getObjectWidth(this.filmBar
);
167 var x
= slideIndex
* this.slideSize
168 x
= x
- (filmBarWidth
- this.slideSize
) / 2.0;
169 x
= x
* this.sliderSpeedRatio
;
170 var p
= new Point( -x
, 0 )
171 this.setSliderPosition(p
);
174 FilmSlider
.prototype.setSliderPosition = function(point
) {
177 if (point
.x
> this.maxRightPosition
)
178 point
.x
= this.maxRightPosition
;
179 this.slider
.style
.left
= point
.x
+ 'px';
180 this.setFilmPosition(point
);
183 FilmSlider
.prototype.setFilmPosition = function(point
) {
184 this.film
.style
.left
= point
.x
/ this.sliderSpeedRatio
+ 'px';
187 FilmSlider
.prototype.getSliderPosition = function() {
188 var x
= parseInt(this.slider
.style
.left
);
189 var y
= parseInt(this.slider
.style
.top
);
190 var p
= new Point(x
, y
);
194 FilmSlider
.prototype.getFilmPosition = function() {
195 var x
= parseInt(this.film
.style
.left
);
196 var y
= parseInt(this.film
.style
.top
);
197 var p
= new Point(x
, y
);
201 FilmSlider
.prototype.loadSibling = function(previous
) {
204 slide
= this.selectedSlide
.parentNode
.previousSibling
;
205 if (slide
&& slide
.nodeType
==3)
206 slide
= slide
.previousSibling
;
209 slide
= this.selectedSlide
.parentNode
.nextSibling
;
210 if (slide
&& slide
.nodeType
==3)
211 slide
= slide
.nextSibling
;
217 var target
= slide
.getElementsByTagName('a')[0];
218 raiseMouseEvent(target
, 'click');
219 var index
= parseInt(target
.getAttribute('portfolio:position'));
220 this.centerSlide(index
);
224 FilmSlider
.prototype.addEventListeners = function() {
225 var thisSlider
= this;
226 addListener(window
, 'resize', function(evt
){thisSlider
.resizeSlider(evt
);});
227 addListener(this.filmBar
, 'click', function(evt
){thisSlider
.thumbnailClickHandler(evt
);});
228 addListener(this.toolbar
, 'click', function(evt
){thisSlider
.toolbarClickHandler(evt
);});
229 addListener(window
, 'load', function(evt
){thisSlider
.fitToScreen(evt
);});
232 addListener(this.slider
, 'mousedown', this.ddHandlers
['down']);
233 if(browser
.isDOM2Event
){
234 if (browser
.isAppleWebKit
) {
235 this.filmBar
.addEventListener('mousewheel', function(evt
){thisSlider
.mouseWheelHandler(evt
);}, false);
238 addListener(this.filmBar
, 'DOMMouseScroll', function(evt
){thisSlider
.mouseWheelHandler(evt
);});
241 else if (browser
.isIE6up
) {
242 addListener(this.filmBar
, 'mousewheel', function(evt
){thisSlider
.mouseWheelHandler(evt
);});
245 addListener(document
, 'keydown', function(evt
){thisSlider
.keyDownHandler(evt
);});
246 addListener(document
, 'keypress', function(evt
){thisSlider
.keyPressHandler(evt
);});
250 FilmSlider
.prototype.mouseDownHandler = function(evt
) {
251 this.initialClickPoint
= new Point(evt
.clientX
, evt
.clientY
);
252 this.initialPosition
= this.getSliderPosition();
253 this.dragInProgress
= true;
254 addListener(document
, 'mousemove', this.ddHandlers
['move']);
255 addListener(document
, 'mouseup', this.ddHandlers
['up']);
256 addListener(document
.body
, 'mouseout', this.ddHandlers
['out'])
261 FilmSlider
.prototype.mouseMoveHandler = function(evt
) {
262 if(!this.dragInProgress
)
266 evt
= getEventObject(evt
);
267 var currentPoint
= new Point(evt
.clientX
, evt
.clientY
);
268 var displacement
= currentPoint
.diff(this.initialClickPoint
);
269 this.setSliderPosition(this.initialPosition
.add(displacement
));
272 FilmSlider
.prototype.mouseUpHandler = function(evt
) {
273 this.dragInProgress
= false;
274 evt
= getEventObject(evt
);
275 this.mouseMoveHandler(evt
);
279 FilmSlider
.prototype.mouseOutHandler = function(evt
) {
280 evt
= getEventObject(evt
);
284 x
> this.winSize
['width'] ||
286 y
> this.winSize
['height']
288 this.mouseUpHandler(evt
);
292 FilmSlider
.prototype.thumbnailClickHandler = function(evt
) {
293 var target
= getTargetedObject(evt
);
294 while (target
.tagName
!= 'A' && target
!= this.filmBar
)
295 target
= target
.parentNode
;
296 if (target
.tagName
!= 'A')
299 if (this.viewMode
== 'full') {
300 this.mosaique
.unload();
301 this.mosaique
= null;
302 this.viewMode
= 'medium';
305 disablePropagation(evt
);
308 var imgBaseUrl
= target
.href
;
310 if (this.ctxUrlTranslation
[0])
311 canonicalImgUrl
= imgBaseUrl
.replace(this.ctxUrlTranslation
[0],
312 this.ctxUrlTranslation
[1]);
314 canonicalImgUrl
= imgBaseUrl
;
316 var ajaxUrl
= imgBaseUrl
+ '/photo_view_ajax';
319 //this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=600';
320 var thumbnail
= target
.getElementsByTagName('IMG')[0];
321 var bestFitSize
= this.getBestFitSize(thumbnail
.height
/thumbnail
.width
);
322 this.pendingImage
.src
= canonicalImgUrl
+ '/getResizedImage?size=' + bestFitSize
;
325 var fullScreenLink
= this.buttons
['full_screen'].parentNode
;
326 fullScreenLink
.href
= canonicalImgUrl
+ '/zoom_view';
328 var toggleSelectionBtn
= this.buttons
['toggle_selection'];
329 var toggleSelectionLink
= toggleSelectionBtn
.parentNode
;
330 this.selectedSlideInSelection
= (target
.className
=='selected');
331 if (this.selectedSlideInSelection
) {
332 toggleSelectionBtn
.src
= portal_url() + '/unselect_flag_btn.gif';
333 toggleSelectionBtn
.alt
= toggleSelectionLink
.title
= 'Retirer de la sélection';
334 toggleSelectionLink
.href
= canonicalImgUrl
+ '/remove_to_selection';
337 toggleSelectionBtn
.src
= portal_url() + '/select_flag_btn.gif';
338 toggleSelectionBtn
.alt
= toggleSelectionLink
.title
= 'Ajouter à la sélection';
339 toggleSelectionLink
.href
= canonicalImgUrl
+ '/add_to_selection';
342 var showBuyableButtonLink
= this.buttons
['show_buyable'].parentNode
;
343 showBuyableButtonLink
.href
= canonicalImgUrl
+ '/get_slide_buyable_items';
344 this.cartSlide
.innerHTML
= '';
345 this.cartSlide
.style
.visibility
='hidden';
348 var metadataButton
= this.buttons
['edit_metadata']
349 if (metadataButton
) {
350 var metadataEditLink
= metadataButton
.parentNode
;
351 metadataEditLink
.href
= canonicalImgUrl
+ '/photo_edit_form'
355 var req
= new XMLHttpRequest();
356 req
.onreadystatechange = function() {
357 switch (req
.readyState
) {
363 if (! isTextMime
.exec(req
.getResponseHeader('Content-Type'))) {
364 req
.onreadystatechange
= null;
367 window
.location
.href
= thisFS
._fallBackUrl
;
374 if (req
.status
== '200')
375 thisFS
.populateViewer(req
);
377 //window.location.href = target.href;
378 console
.error(ajaxUrl
);
383 req
.open("GET", ajaxUrl
, true);
386 // update old displayed slide className
387 var className
= this.selectedSlide
.className
;
388 var classes
= className
.split(' ');
389 var newClasses
= new Array();
394 if (name
== 'displayed')
397 newClasses
.push(name
);
400 this.selectedSlide
.className
= newClasses
.join(' ')
402 // hightlight new displayed slide
403 this.selectedSlide
= target
;
404 className
= this.selectedSlide
.className
;
405 classes
= className
.split(' ');
406 classes
.push('displayed');
407 this.selectedSlide
.className
= classes
.join(' ');
411 FilmSlider
.prototype.toolbarClickHandler = function(evt
) {
412 var target
= getTargetedObject(evt
);
413 if(target
.tagName
== 'IMG' && target
.getAttribute('name')){
414 switch(target
.getAttribute('name')) {
417 disablePropagation(evt
);
419 var link
= button
.parentNode
;
421 this.loadSibling(true);
425 disablePropagation(evt
);
427 var link
= button
.parentNode
;
429 this.loadSibling(false);
433 disablePropagation(evt
);
434 target
.parentNode
.blur();
435 if (this.viewMode
== 'full') {
436 this.mosaique
.unload();
437 this.mosaique
= null;
438 this.viewMode
= 'medium';
441 var main
= document
.getElementById('photo_viewer');
442 var url
= target
.parentNode
.href
;
443 url
= url
.substring(0, url
.length
- '/zoom_view'.length
);
444 var margins
= {'top':0, 'right':-1, 'bottom':0, 'left':0};
445 this.mosaique
= new Mosaique(main
, url
, margins
);
446 this.viewMode
= 'full';
449 case 'toggle_selection':
451 disablePropagation(evt
);
453 var link
= button
.parentNode
;
456 var req
= new XMLHttpRequest();
458 req
.open("POST", url
, true);
459 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
463 var parts
= url
.split('/');
464 var canonicalImgUrl
= parts
.slice(0, parts
.length
-1).join('/');
466 if (isAddToSelection
.test(url
)) {
467 button
.src
= portal_url() + '/unselect_flag_btn.gif';
468 button
.alt
= link
.title
= 'Retirer de la sélection';
469 link
.href
= canonicalImgUrl
+ '/remove_to_selection';
470 this.selectedSlide
.className
= 'selected displayed';
471 this.image
.parentNode
.className
= 'selected';
472 this.selectedSlideInSelection
= true;
475 button
.src
= portal_url() + '/select_flag_btn.gif';
476 button
.alt
= link
.title
= 'Ajouter à la sélection';
477 link
.href
= canonicalImgUrl
+ '/add_to_selection';
478 this.selectedSlide
.className
= 'displayed';
479 this.image
.parentNode
.className
= '';
480 this.selectedSlideInSelection
= false;
486 disablePropagation(evt
);
488 var link
= button
.parentNode
;
490 var slide
= this.cartSlide
;
491 slide
.innerHTML
= '';
492 slide
.style
.visibility
= 'visible';
493 var cw
= new CartWidget(slide
, link
.href
);
494 cw
.onCancel = function() {
495 CartWidget
.prototype.onCancel
.apply(this);
496 slide
.style
.visibility
= 'hidden';
498 cw
.onAfterConfirm = function() {
499 slide
.style
.visibility
= 'hidden';
507 case 'edit_metadata' :
509 disablePropagation(evt);
511 if (this.viewMode == 'full') {
512 this.mosaique.unload();
513 this.mosaique = null;
514 this.viewMode = 'medium';
517 var fi = new FragmentImporter(absolute_url());
518 fi.useMacro('metadata_edit_form_macros', 'iptc', 'image_metadata');
526 if(browser
.isDOM2Event
){
527 if (browser
.isAppleWebKit
) {
528 FilmSlider
.prototype.mouseWheelHandler = function(evt
) {
530 var pos
= this.getSliderPosition();
531 pos
.x
-= evt
.wheelDelta
/ 40;
532 this.setSliderPosition(pos
);
536 FilmSlider
.prototype.mouseWheelHandler = function(evt
) {
538 var pos
= this.getSliderPosition();
539 pos
.x
+= evt
.detail
* 3;
540 this.setSliderPosition(pos
);
544 else if (browser
.isIE6up
) {
545 FilmSlider
.prototype.mouseWheelHandler = function() {
546 var evt
= window
.event
;
547 evt
.returnValue
= false;
548 var pos
= this.getSliderPosition();
549 pos
.x
-= evt
.wheelDelta
/ 40;
550 this.setSliderPosition(pos
);
554 FilmSlider
.prototype.keyDownHandler = function(evt
) {
555 var evt
= getEventObject(evt
);
556 switch (evt
.keyCode
) {
558 this.loadSibling(true);
561 this.loadSibling(false);
569 FilmSlider
.prototype.keyPressHandler = function(evt
) {
570 var target
= getTargetedObject(evt
);
571 if (target
.tagName
== 'INPUT' || target
.tagName
== 'TEXTAREA')
573 var evt
= evt
= getEventObject(evt
);
574 evt
= getEventObject(evt
);
575 var charPress
= String
.fromCharCode((evt
.keyCode
) ? evt
.keyCode
: evt
.which
);
579 raiseMouseEvent(this.buttons
['full_screen'], 'click');
584 FilmSlider
.prototype.populateViewer = function(req
) {
585 var elements
= req
.responseXML
.documentElement
.childNodes
;
586 for(var i
=0 ; i
< elements
.length
; i
++ ) {
587 element
= elements
[i
];
588 switch (element
.nodeName
) {
590 var dest
= document
.getElementById(element
.getAttribute('id'));
591 dest
.innerHTML
= element
.firstChild
.nodeValue
;
593 case 'imageattributes' :
594 var link
= this.buttons
['back_to_portfolio'].parentNode
;
595 link
.href
= element
.getAttribute('backToContextUrl');
596 link
= this.buttons
['show_buyable'].parentNode
;
597 var buyable
= element
.getAttribute('buyable');
598 if(buyable
== 'True')
599 link
.className
= null;
600 else if(buyable
== 'False')
601 link
.className
= 'hidden';
602 this.image
.alt
= element
.getAttribute('alt');
603 this.lastBCElement
.href
= element
.getAttribute('lastBcUrl');
604 this.lastBCElement
.innerHTML
= element
.getAttribute('img_id');
610 FilmSlider
.prototype.refreshImage = function() {
611 this.image
.style
.visibility
= 'hidden';
612 this.image
.src
= this.pendingImage
.src
;
613 this.image
.width
= this.pendingImage
.width
;
614 this.image
.height
= this.pendingImage
.height
;
615 this.image
.style
.visibility
= 'visible';
616 if (this.selectedSlideInSelection
)
617 this.image
.parentNode
.className
= 'selected';
619 this.image
.parentNode
.className
= '';
622 FilmSlider
.prototype.startSlideShow = function() {
623 this.slideShowSlide
= this.selectedSlide
;
624 this.nextSlideShowSlide
= this.selectedSlide
;
625 return this.slideShowSlide
.href
;
628 FilmSlider
.prototype.slideShowNext = function() {
629 this.slideShowSlide
= this.nextSlideShowSlide
;
630 var nextSlide
= this.slideShowSlide
.parentNode
.nextSibling
;
631 if (nextSlide
&& nextSlide
.nodeType
==3)
632 nextSlide
= nextSlide
.nextSibling
;
635 nextSlide
= nextSlide
.getElementsByTagName('a')[0];
636 this.nextSlideShowSlide
= nextSlide
;
637 return nextSlide
.href
;
640 var row
= this.slideShowSlide
.parentNode
.parentNode
;
641 var first
= row
.firstChild
;
642 if (first
.nodeType
==3)
643 first
= first
.nextSibling
;
644 this.nextSlideShowSlide
= first
.getElementsByTagName('a')[0];
645 return this.nextSlideShowSlide
.href
;
649 FilmSlider
.prototype.stopSlideShow = function() {
650 raiseMouseEvent(this.slideShowSlide
, 'click');
651 var index
= parseInt(this.selectedSlide
.getAttribute('portfolio:position'));
652 this.centerSlide(index
);
657 function Point(x
, y
) {
658 this.x
= Math
.round(x
);
659 this.y
= Math
.round(y
);
661 Point
.prototype.diff = function(point
) { return new Point(this.x
- point
.x
, this.y
- point
.y
); };
662 Point
.prototype.add = function(point
) { return new Point(this.x
+ point
.x
, this.y
+ point
.y
); };
663 Point
.prototype.mul = function(k
) { return new Point(this.x
* k
, this.y
*k
)};
664 Point
.prototype.toString = function() { return "(" + String(this.x
) + ", " + String(this.y
) + ")"; };