shrink de l'image si écran rikiki.
[Portfolio.git] / skins / photo_film_viewer.js
1 /*
2 copyright 2008-2014 Benoit Pin - Centre de recherche en informatique - MINES ParisTech
3 http://plinn.org
4 Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
5 */
6
7 var FilmSlider;
8 var s;
9 (function(){
10
11 var keyLeft = 37, keyRight = 39;
12 var isTextMime = /^text\/.+/i;
13 var isAddToSelection = /.*\/add_to_selection$/;
14 var imgRequestedSize = /size=(\d+)/;
15 var DEFAULT_IMAGE_SIZES = [500, 600, 800];
16
17 FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
18 var thisSlider = this;
19 s = this;
20 this.filmBar = filmBar;
21 this.filmBarWidth = getObjectWidth(this.filmBar);
22 var film = filmBar.firstChild;
23 if (film.nodeType === 3) { film = film.nextSibling; }
24 this.film = film;
25 this.slider = slider;
26 this.rail = slider.parentNode;
27 this.sliderSpeedRatio = undefined;
28 this.sliderRatio = undefined;
29 this.selectedSlide = undefined;
30 this.selectedSlideInSelection = undefined;
31 this.cartSlide = document.getElementById('cart_slide');
32 this.image = image;
33 this.stretchable = image.parentNode;
34 this.viewMode = 'medium';
35
36 this.buttons = [];
37 this.toolbar = toolbar;
38 if (breadcrumbs) {
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];
43 this.hasBreadcrumbs = true;
44 }
45 else {
46 this.hasBreadcrumbs = false;
47 }
48
49 var buttons = toolbar.getElementsByTagName('img');
50 var b, name, i;
51 for (i=0 ; i<buttons.length ; i++) {
52 b = buttons[i];
53 name = b.getAttribute('name');
54 if (name) { this.buttons[name] = b; }
55 }
56
57 this.pendingImage = new Image();
58 this.pendingImage.onload = function() {
59 thisSlider.refreshImage();
60 };
61 this.initialized = false;
62
63 this.film.style.left='0';
64 this.film.style.top='0';
65
66 this.filmLength = ctxInfos.filmLength;
67 this.center = ctxInfos.center;
68 this.slideSize = ctxInfos.slideSize;
69 this.ctxUrlTranslation = ctxInfos.ctxUrlTranslation;
70
71 this.ddHandlers = {'down' : function(evt){thisSlider.mouseDownHandler(evt);},
72 'move' : function(evt){thisSlider.mouseMoveHandler(evt);},
73 'up' : function(evt){thisSlider.mouseUpHandler(evt);},
74 'out' : function(evt){thisSlider.mouseOutHandler(evt);}
75 };
76
77 if (browser.isMobile) {
78 this.rail.className = 'hidden';
79 }
80 else {
81 this.resizeSlider();
82 }
83 this.addEventListeners();
84 };
85
86 if (!browser.isMobile) {
87 FilmSlider.prototype.resizeSlider = function(evt) {
88 var filmBarWidth = this.filmBarWidth;
89 if (!filmBarWidth) { return; }
90 var filmWidth = this.slideSize * this.filmLength;
91 var sliderRatio = this.sliderRatio = filmBarWidth / filmWidth;
92 var sliderWidth = filmBarWidth * sliderRatio;
93 this.rail.style.width = filmBarWidth + 'px';
94 this.rail.style.display = 'block';
95 this.rail.style.visibility = 'visible';
96 if (sliderRatio < 1) {
97 this.slider.style.width = Math.round(sliderWidth) + 'px';
98 this.slider.style.visibility = 'visible';
99 }
100 else {
101 this.slider.style.visibility = 'hidden';
102 }
103
104 this.winSize = {'width' : getWindowWidth(),
105 'height' : getWindowHeight()};
106 this.maxRightPosition = filmBarWidth - sliderWidth;
107 this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
108 if (!this.initialized) {
109 this.centerSlide(this.center);
110 this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
111 this.initialized = true;
112 }
113 };
114 }
115
116 else {
117 // pas de barre de scroll horizontal pour les tablettes
118 FilmSlider.prototype.resizeSlider = function(evt) {
119 this.filmMaxX = - (getObjectWidth(this.film) - this.filmBarWidth);
120 if (!this.initialized) {
121 this.centerSlide(this.center);
122 this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
123 this.initialized = true;
124 }
125 };
126 }
127
128 FilmSlider.prototype._checkSizeAfterLoad = function(evt) {
129 this._barSizes = [];
130 this.filmBarWidth = this._barSizes[this._barSizes.length] = getObjectWidth(this.filmBar);
131 this.resizeSlider();
132 var self = this;
133 this._checkSizeIntervalId = setInterval(function(evt){self._checkSize(evt);}, 25);
134 setTimeout(function(evt){self._checkSizeStability();}, 250);
135 };
136
137 FilmSlider.prototype._checkSize = function(evt) {
138 this._barSizes[this._barSizes.length] = getObjectWidth(this.filmBar);
139 if (this._barSizes.length >= 2 &&
140 this._barSizes[this._barSizes.length-2] !== this._barSizes[this._barSizes.length-1]) {
141 this.filmBarWidth = this._barSizes[this._barSizes.length-1];
142 this.initialized = false;
143 this.resizeSlider();
144 }
145 };
146
147 FilmSlider.prototype._checkSizeStability = function(evt) {
148 var self = this;
149 var i;
150 var checkAgain = function(evt){self._checkSizeStability();};
151 for (i=0 ; i<this._barSizes.length - 1 ; i++) {
152 if (this._barSizes[i] !== this._barSizes[i+1]) {
153 this._barSizes = [];
154 setTimeout(checkAgain, 250);
155 return;
156 }
157 }
158 clearInterval(this._checkSizeIntervalId);
159 delete this._barSizes;
160 delete this._checkSizeIntervalId;
161 };
162
163 FilmSlider.prototype.fitToScreen = function(evt) {
164 this._fitToScreen();
165 var thisSlider = this;
166 addListener(window, 'resize', function(evt){thisSlider._fitToScreen();});
167 };
168
169 FilmSlider.prototype._fitToScreen = function(evt) {
170 var wh = getWindowHeight();
171 var rb;
172 if (!browser.isMobile) {
173 rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
174 }
175 else {
176 rb = getObjectTop(this.filmBar) + getObjectHeight(this.filmBar); // film bottom
177 }
178 var delta = wh - rb;
179 var sh = getObjectHeight(this.stretchable);
180 var newSize = sh + delta;
181 this.stretchable.style.height = newSize + 'px';
182
183 var ratio = this.image.height / this.image.width;
184 var bestFitSize = this.getBestFitSize(ratio);
185 var currentSize = parseInt(imgRequestedSize.exec(this.image.src)[1], 10);
186 if (currentSize !== bestFitSize) {
187 var src = this.image.src.replace(imgRequestedSize, 'size=' + bestFitSize);
188 this.pendingImage.src = src;
189 }
190 this.shrinkImage(this.image);
191 };
192
193 FilmSlider.prototype.getBestFitSize = function(ratio) {
194 var fw = getObjectWidth(this.stretchable) - 1;
195 var fh = getObjectHeight(this.stretchable) - 1;
196
197 var i, irw, irh;
198 if (ratio < 1) {
199 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
200 irw = DEFAULT_IMAGE_SIZES[i];
201 irh = irw * ratio;
202 if (irw <= fw && irh <= fh) { break; }
203 }
204 }
205 else {
206 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
207 irh = DEFAULT_IMAGE_SIZES[i];
208 irw = irh / ratio;
209 if (irw <= fw && irh <= fh) { break; }
210 }
211 }
212 return DEFAULT_IMAGE_SIZES[i];
213 };
214
215 FilmSlider.prototype.shrinkImage = function(img) {
216 var dispWidth = parseInt(this.stretchable.style.width);
217 var imgWidth = img.width;
218 var dispHeight = parseInt(this.stretchable.style.height);
219 var imgHeight = img.height;
220 var ratio;
221
222 if (imgHeight > dispHeight) {
223 ratio = dispHeight / imgHeight;
224 imgWidth = imgWidth * ratio;
225 imgHeight = dispHeight;
226 }
227 if (imgWidth > dispWidth) {
228 ratio = dispWidth / imgWidth;
229 imgHeight = imgHeight * ratio;
230 imgWidth = dispWidth;
231 }
232 img.width = imgWidth;
233 img.height = imgHeight;
234 };
235
236 if (!browser.isMobile) {
237 FilmSlider.prototype.centerSlide = function(slideIndex) {
238 if (this.sliderRatio > 1) { return; }
239 var filmBarWidth = getObjectWidth(this.filmBar);
240 var x = slideIndex * this.slideSize;
241 x = x - (filmBarWidth - this.slideSize) / 2.0;
242 x = x * this.sliderSpeedRatio;
243 var p = new Point( -x, 0 );
244 this.setSliderPosition(p);
245 };
246 }
247 else {
248 FilmSlider.prototype.centerSlide = function(slideIndex) {
249 var filmBarWidth = getObjectWidth(this.filmBar);
250 var x = slideIndex * this.slideSize;
251 x = x - (filmBarWidth - this.slideSize) / 2.0;
252 this.setFilmPosition(-x);
253 };
254 }
255
256 FilmSlider.prototype.setSliderPosition = function(point) {
257 if(point.x < 0) { point.x = 0; }
258 if (point.x > this.maxRightPosition) { point.x = this.maxRightPosition; }
259 this.slider.style.left = point.x + 'px';
260 this.setFilmPosition(point);
261 };
262
263 if (!browser.isMobile) {
264 FilmSlider.prototype.setFilmPosition = function(point) {
265 this.film.style.left = point.x / this.sliderSpeedRatio + 'px';
266 };
267 }
268 else {
269 FilmSlider.prototype.setFilmPosition = function(x) {
270 x = Math.min(0, x);
271 x = Math.max(this.filmMaxX, x);
272 this.film.style.left = String(x) + 'px';
273 };
274 }
275
276 FilmSlider.prototype.getSliderPosition = function() {
277 var x = parseInt(this.slider.style.left, 10);
278 var y = parseInt(this.slider.style.top, 10);
279 var p = new Point(x, y);
280 return p;
281 };
282
283 FilmSlider.prototype.getFilmPosition = function() {
284 var x = parseInt(this.film.style.left, 10);
285 var y = parseInt(this.film.style.top, 10);
286 var p = new Point(x, y);
287 return p;
288 };
289
290 FilmSlider.prototype.loadSibling = function(previous) {
291 var slide = null;
292 if (previous) {
293 slide = this.selectedSlide.parentNode.previousSibling;
294 if (slide && slide.nodeType===3) { slide = slide.previousSibling; }
295 }
296 else {
297 slide = this.selectedSlide.parentNode.nextSibling;
298 if (slide && slide.nodeType===3) { slide = slide.nextSibling; }
299 }
300
301 if (!slide) { return; }
302 else {
303 var target = slide.getElementsByTagName('a')[0];
304 raiseMouseEvent(target, 'click');
305 var index = parseInt(target.getAttribute('portfolio:position'), 10);
306 this.centerSlide(index);
307 }
308 };
309
310 FilmSlider.prototype.addEventListeners = function() {
311 var thisSlider = this;
312 addListener(window, 'resize', function(evt){thisSlider.resizeSlider(evt);});
313 addListener(this.filmBar, 'click', function(evt){thisSlider.thumbnailClickHandler(evt);});
314 addListener(this.toolbar, 'click', function(evt){thisSlider.toolbarClickHandler(evt);});
315 addListener(window, 'load', function(evt){thisSlider.fitToScreen(evt);});
316 addListener(window, 'load', function(evt){thisSlider._checkSizeAfterLoad(evt);});
317 addListener(window, 'load', function(evt){thisSlider.startThumbnailsLoadQueue(evt);});
318
319 // dd listeners
320 addListener(this.slider, 'mousedown', this.ddHandlers.down);
321 if(browser.isDOM2Event){
322 if (browser.isAppleWebKit) {
323 this.filmBar.addEventListener('mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);}, false);
324 }
325 else {
326 addListener(this.filmBar, 'DOMMouseScroll', function(evt){thisSlider.mouseWheelHandler(evt);});
327 }
328 }
329 else if (browser.isIE6up) {
330 addListener(this.filmBar, 'mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);});
331 }
332 if (browser.isMobile) {
333 this.filmBar.addEventListener('touchstart', function(evt){thisSlider.touchStartHandler(evt);}, false);
334 this.filmBar.addEventListener('touchmove', function(evt){thisSlider.touchMoveHandler(evt);}, false);
335 this.filmBar.addEventListener('touchend', function(evt){thisSlider.touchEndHandler(evt);}, false);
336 }
337
338 addListener(document, 'keydown', function(evt){thisSlider.keyDownHandler(evt);});
339 addListener(document, 'keypress', function(evt){thisSlider.keyPressHandler(evt);});
340 };
341
342
343 FilmSlider.prototype.mouseDownHandler = function(evt) {
344 this.initialClickPoint = new Point(evt.clientX, evt.clientY);
345 this.initialPosition = this.getSliderPosition();
346 this.dragInProgress = true;
347 addListener(document, 'mousemove', this.ddHandlers.move);
348 addListener(document, 'mouseup', this.ddHandlers.up);
349 addListener(document.body, 'mouseout', this.ddHandlers.out);
350 };
351
352
353 FilmSlider.prototype.mouseMoveHandler = function(evt) {
354 if(!this.dragInProgress) { return; }
355
356 clearSelection();
357 evt = getEventObject(evt);
358 var currentPoint = new Point(evt.clientX, evt.clientY);
359 var displacement = currentPoint.diff(this.initialClickPoint);
360 this.setSliderPosition(this.initialPosition.add(displacement));
361 };
362
363 FilmSlider.prototype.mouseUpHandler = function(evt) {
364 this.dragInProgress = false;
365 evt = getEventObject(evt);
366 this.mouseMoveHandler(evt);
367 };
368
369
370 FilmSlider.prototype.mouseOutHandler = function(evt) {
371 evt = getEventObject(evt);
372 var x = evt.clientX;
373 var y = evt.clientY;
374 if (x < 0 ||
375 x > this.winSize.width ||
376 y < 0 ||
377 y > this.winSize.height
378 ) {
379 this.mouseUpHandler(evt);
380 }
381 };
382
383 FilmSlider.prototype.translateImgUrl = function(url) {
384 var canonicalImgUrl;
385 if (this.ctxUrlTranslation[0]) {
386 canonicalImgUrl = url.replace(this.ctxUrlTranslation[0],
387 this.ctxUrlTranslation[1]);
388 }
389 else {
390 canonicalImgUrl = url;
391 }
392 return canonicalImgUrl;
393 };
394
395 FilmSlider.prototype.thumbnailClickHandler = function(evt) {
396 var target = getTargetedObject(evt);
397 while (target.tagName !== 'A' && target !== this.filmBar) { target = target.parentNode; }
398 if (target.tagName !== 'A') { return; }
399 else {
400 if (this.viewMode === 'full') {
401 this.mosaique.unload();
402 this.mosaique = null;
403 this.viewMode = 'medium';
404 }
405 disableDefault(evt);
406 disablePropagation(evt);
407 target.blur();
408 history.pushState(target.href, '', target.href);
409
410 var imgBaseUrl = target.href;
411 var canonicalImgUrl = this.translateImgUrl(imgBaseUrl);
412
413 var ajaxUrl = imgBaseUrl + '/photo_view_ajax';
414 var thisFS = this;
415
416 //this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=600';
417 var thumbnail = target.getElementsByTagName('IMG')[0];
418 var bestFitSize = this.getBestFitSize(thumbnail.height/thumbnail.width);
419 this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=' + bestFitSize;
420
421 // update buttons
422 var fullScreenLink = this.buttons.full_screen.parentNode;
423 fullScreenLink.href = canonicalImgUrl + '/zoom_view';
424
425 var toggleSelectionBtn = this.buttons.toggle_selection;
426 var toggleSelectionLink = toggleSelectionBtn.parentNode;
427 this.selectedSlideInSelection = (target.className==='selected');
428 if (this.selectedSlideInSelection) {
429 toggleSelectionBtn.src = portal_url() + '/unselect_flag_btn.gif';
430 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Retirer de la sélection';
431 toggleSelectionLink.href = canonicalImgUrl + '/remove_to_selection';
432 }
433 else {
434 toggleSelectionBtn.src = portal_url() + '/select_flag_btn.gif';
435 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Ajouter à la sélection';
436 toggleSelectionLink.href = canonicalImgUrl + '/add_to_selection';
437 }
438
439 var showBuyableButtonLink = this.buttons.show_buyable.parentNode;
440 showBuyableButtonLink.href = canonicalImgUrl + '/get_slide_buyable_items';
441 this.cartSlide.innerHTML = '';
442 this.cartSlide.style.visibility='hidden';
443
444
445 var metadataButton = this.buttons.edit_metadata;
446 if (metadataButton) {
447 var metadataEditLink = metadataButton.parentNode;
448 metadataEditLink.href = canonicalImgUrl + '/photo_edit_form';
449 }
450
451
452 var req = new XMLHttpRequest();
453 req.onreadystatechange = function() {
454 switch (req.readyState) {
455 case 1 :
456 showProgressImage();
457 break;
458 case 2 :
459 try {
460 if (! isTextMime.exec(req.getResponseHeader('Content-Type'))) {
461 req.onreadystatechange = null;
462 req.abort();
463 hideProgressImage();
464 window.location.href = thisFS._fallBackUrl;
465 }
466 }
467 catch(e){}
468 break;
469 case 4 :
470 hideProgressImage();
471 if (req.status === 200) { thisFS.populateViewer(req); }
472 break;
473 }
474 };
475
476 req.open("GET", ajaxUrl, true);
477 req.send(null);
478
479 // update old displayed slide className
480 var className = this.selectedSlide.className;
481 var classes = className.split(' ');
482 var newClasses = [];
483 var name, i;
484
485 for (i=0 ; i<classes.length ; i++) {
486 name = classes[i];
487 if (name !== 'displayed') {
488 newClasses.push(name);
489 }
490 }
491
492 this.selectedSlide.className = newClasses.join(' ');
493
494 // hightlight new displayed slide
495 this.selectedSlide = target;
496 className = this.selectedSlide.className;
497 classes = className.split(' ');
498 classes.push('displayed');
499 this.selectedSlide.className = classes.join(' ');
500 }
501 };
502
503 FilmSlider.prototype.toolbarClickHandler = function(evt) {
504 var target = getTargetedObject(evt);
505 var button, link, url;
506 if(target.tagName === 'IMG' && target.getAttribute('name')) {
507 switch(target.getAttribute('name')) {
508 case 'previous' :
509 disableDefault(evt);
510 disablePropagation(evt);
511 button = target;
512 link = button.parentNode;
513 link.blur();
514 this.loadSibling(true);
515 break;
516 case 'next' :
517 disableDefault(evt);
518 disablePropagation(evt);
519 button = target;
520 link = button.parentNode;
521 link.blur();
522 this.loadSibling(false);
523 break;
524 case 'full_screen':
525 disableDefault(evt);
526 disablePropagation(evt);
527 target.parentNode.blur();
528 if (this.viewMode === 'full') {
529 this.mosaique.unload();
530 this.mosaique = null;
531 this.viewMode = 'medium';
532 return;
533 }
534 var main = document.getElementById('photo_viewer');
535 url = target.parentNode.href;
536 url = url.substring(0, url.length - '/zoom_view'.length);
537 var margins = {'top':0, 'right':-1, 'bottom':0, 'left':0};
538 this.mosaique = new Mosaique(main, url, margins);
539 this.viewMode = 'full';
540 break;
541
542 case 'toggle_selection':
543 disableDefault(evt);
544 disablePropagation(evt);
545 button = target;
546 link = button.parentNode;
547 link.blur();
548
549 var req = new XMLHttpRequest();
550 url = link.href;
551 req.open("POST", url, true);
552 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
553 req.send("ajax=1");
554
555 // toggle button
556 var parts = url.split('/');
557 var canonicalImgUrl = parts.slice(0, parts.length-1).join('/');
558
559 if (isAddToSelection.test(url)) {
560 button.src = portal_url() + '/unselect_flag_btn.gif';
561 button.alt = link.title = 'Retirer de la sélection';
562 link.href = canonicalImgUrl + '/remove_to_selection';
563 this.selectedSlide.className = 'selected displayed';
564 this.image.parentNode.className = 'selected';
565 this.selectedSlideInSelection = true;
566 }
567 else {
568 button.src = portal_url() + '/select_flag_btn.gif';
569 button.alt = link.title = 'Ajouter à la sélection';
570 link.href = canonicalImgUrl + '/add_to_selection';
571 this.selectedSlide.className = 'displayed';
572 this.image.parentNode.className = '';
573 this.selectedSlideInSelection = false;
574 }
575 break;
576
577 case 'show_buyable':
578 disableDefault(evt);
579 disablePropagation(evt);
580 button = target;
581 link = button.parentNode;
582 link.blur();
583 var slide = this.cartSlide;
584 slide.innerHTML = '';
585 slide.style.visibility = 'visible';
586 var cw = new CartWidget(slide, link.href);
587 cw.onCancel = function() {
588 CartWidget.prototype.onCancel.apply(this);
589 slide.style.visibility = 'hidden';
590 };
591 cw.onAfterConfirm = function() {
592 slide.style.visibility = 'hidden';
593 };
594 break;
595
596
597
598
599 /*
600 case 'edit_metadata' :
601 disableDefault(evt);
602 disablePropagation(evt);
603 target.blur();
604 if (this.viewMode === 'full') {
605 this.mosaique.unload();
606 this.mosaique = null;
607 this.viewMode = 'medium';
608 return;
609 }
610 var fi = new FragmentImporter(absolute_url());
611 fi.useMacro('metadata_edit_form_macros', 'iptc', 'image_metadata');
612 break;
613 */
614 }
615 }
616 };
617
618
619 if(browser.isDOM2Event) {
620 if (browser.isAppleWebKit) {
621 FilmSlider.prototype.mouseWheelHandler = function(evt) {
622 disableDefault(evt);
623 var pos = this.getSliderPosition();
624 pos.x -= evt.wheelDelta / 40;
625 this.setSliderPosition(pos);
626 };
627 }
628 else {
629 FilmSlider.prototype.mouseWheelHandler = function(evt) {
630 disableDefault(evt);
631 var pos = this.getSliderPosition();
632 pos.x += evt.detail * 3;
633 this.setSliderPosition(pos);
634 };
635 }
636 }
637 else if (browser.isIE6up) {
638 FilmSlider.prototype.mouseWheelHandler = function() {
639 var evt = window.event;
640 evt.returnValue = false;
641 var pos = this.getSliderPosition();
642 pos.x -= evt.wheelDelta / 40;
643 this.setSliderPosition(pos);
644 };
645 }
646
647 FilmSlider.prototype.touchStartHandler = function(evt) {
648 this.filmStartX = parseInt(this.film.style.left, 10);
649 this.touchStartX = evt.changedTouches[0].screenX;
650 this.touchStartTime = (new Date()).getTime();
651 };
652
653 FilmSlider.prototype.touchMoveHandler = function(evt) {
654 disableDefault(evt);
655 var delta = this.touchStartX - evt.changedTouches[0].screenX;
656 var posX = this.filmStartX - delta;
657 this.setFilmPosition(posX);
658 this.lastMoveTime = (new Date()).getTime();
659 };
660
661 FilmSlider.prototype.touchEndHandler = function(evt) {
662 var x = evt.changedTouches[0].screenX;
663 var delta = x - this.touchStartX;
664 if (delta) {
665 disableDefault(evt);
666 var now = (new Date()).getTime();
667 if (now - this.lastMoveTime < 100) {
668 // au delà de 100 ms de maintient, on annule l'inertie
669 var speed = delta / (now - this.touchStartTime);
670 var x0 = parseInt(this.film.style.left, 10);
671 var t0 = (new Date()).getTime();
672 var d = 500; // milisecondes
673 delta = 0;
674 var dt = 25;
675 var self = this;
676
677 var animate = function() {
678 // inertie
679 var t = (new Date()).getTime() - t0;
680 if (t < d) {
681 setTimeout(animate, dt);
682 delta = delta + (1-t/d) * speed * dt; // décelleration linéaire
683 self.setFilmPosition(x0 + delta);
684 }
685 };
686 animate();
687 }
688 }
689 this.touchStartX = undefined;
690 };
691
692
693 FilmSlider.prototype.keyDownHandler = function(evt) {
694 evt = getEventObject(evt);
695 switch (evt.keyCode) {
696 case keyLeft :
697 this.loadSibling(true);
698 break;
699 case keyRight :
700 this.loadSibling(false);
701 break;
702 default:
703 return;
704 }
705 };
706
707
708 FilmSlider.prototype.keyPressHandler = function(evt) {
709 var target = getTargetedObject(evt);
710 if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') { return; }
711 evt = getEventObject(evt);
712 var charPress = String.fromCharCode((evt.keyCode) ? evt.keyCode : evt.which);
713 switch(charPress) {
714 case 'f':
715 case 'F':
716 raiseMouseEvent(this.buttons.full_screen, 'click');
717 break;
718 }
719 };
720
721 FilmSlider.prototype.populateViewer = function(req) {
722 var elements = req.responseXML.documentElement.childNodes;
723 var i;
724 for(i=0 ; i < elements.length ; i++ ) {
725 element = elements[i];
726 switch (element.nodeName) {
727 case 'fragment' :
728 var dest = document.getElementById(element.getAttribute('id'));
729 if (dest) { dest.innerHTML = element.firstChild.nodeValue; }
730 break;
731 case 'imageattributes' :
732 var link = this.buttons.back_to_portfolio.parentNode;
733 link.href = element.getAttribute('back_to_context_url');
734 link = this.buttons.show_buyable.parentNode;
735 var buyable = element.getAttribute('buyable');
736 if(buyable === 'True') { link.className = null; }
737 else if(buyable === 'False') { link.className = 'hidden'; }
738 this.image.alt = element.getAttribute('alt');
739 this.updateBreadcrumbs(element.getAttribute('last_bc_url'),
740 element.getAttribute('img_id'));
741 break;
742 }
743 }
744 };
745
746 FilmSlider.prototype.refreshImage = function() {
747 var dispWidth = getObjectWidth(this.stretchable);
748 var imgWidth = this.pendingImage.width;
749 var dispHeight = getObjectHeight(this.stretchable);
750 var imgHeight = this.pendingImage.height;
751 var ratio;
752
753 if (imgHeight > dispHeight) {
754 ratio = dispHeight / imgHeight;
755 imgWidth = imgWidth * ratio;
756 imgHeight = dispHeight;
757 }
758 if (imgWidth > dispWidth) {
759 ratio = dispWidth / imgWidth;
760 imgHeight = imgHeight * ratio;
761 imgWidth = dispWidth;
762 }
763
764 this.image.style.visibility = 'hidden';
765 this.image.src = this.pendingImage.src;
766 this.image.width = imgWidth;
767 this.image.height = imgHeight;
768 this.image.style.visibility = 'visible';
769 if (this.selectedSlideInSelection) { this.image.parentNode.className = 'selected'; }
770 else { this.image.parentNode.className = ''; }
771 };
772
773 FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
774 if (this.hasBreadcrumbs) {
775 this.lastBCElement.href = url;
776 this.lastBCElement.innerHTML = title;
777 }
778 };
779
780 FilmSlider.prototype.startThumbnailsLoadQueue = function(evt) {
781 var thumbnails = this.film.getElementsByTagName('img');
782 if (thumbnails.length === 1) { return; }
783 this.thumbnailsLoadingOrder = [];
784 var leftSize = this.center;
785 var rightSize = thumbnails.length - this.center - 1;
786 var i;
787 for (i=1 ; i<=Math.min(leftSize, rightSize) ; i++) {
788 this.thumbnailsLoadingOrder.push(thumbnails[this.center + i]);
789 this.thumbnailsLoadingOrder.push(thumbnails[this.center - i]);
790 }
791 if (leftSize > rightSize) {
792 for (i = this.center - rightSize - 1 ; i >= 0 ; i--) {
793 this.thumbnailsLoadingOrder.push(thumbnails[i]);
794 }
795 }
796 else if (leftSize < rightSize) {
797 for (i = this.center + leftSize + 1 ; i < thumbnails.length ; i++) {
798 this.thumbnailsLoadingOrder.push(thumbnails[i]);
799 }
800 }
801 var next = this.thumbnailsLoadingOrder.shift();
802 var self = this;
803 addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
804 next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
805 };
806
807 FilmSlider.prototype._loadNextThumb = function(evt) {
808 var next = this.thumbnailsLoadingOrder.shift();
809 if (!next) {return;}
810 var self = this;
811 addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
812 next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
813 };
814
815
816 FilmSlider.prototype.startSlideShow = function() {
817 this.slideShowSlide = this.pendingSlideShowSlide = this.selectedSlide;
818 return this.slideShowSlide.href;
819 };
820
821 FilmSlider.prototype.slideShowNext = function() {
822 var nextSlide = this.slideShowSlide.parentNode.nextSibling;
823 if (nextSlide && nextSlide.nodeType===3) { nextSlide = nextSlide.nextSibling; }
824
825 if (nextSlide) {
826 nextSlide = nextSlide.getElementsByTagName('a')[0];
827 this.pendingSlideShowSlide = nextSlide;
828 return this.pendingSlideShowSlide.href;
829 }
830 else {
831 var row = this.slideShowSlide.parentNode.parentNode;
832 var first = row.firstChild;
833 if (first.nodeType===3) { first = first.nextSibling; }
834 this.pendingSlideShowSlide = first.getElementsByTagName('a')[0];
835 return this.pendingSlideShowSlide.href;
836 }
837 };
838
839 FilmSlider.prototype.slideShowPrevious = function() {
840 var previousSlide = this.slideShowSlide.parentNode.previousSibling;
841 if (previousSlide && previousSlide.nodeType===3) { previousSlide = previousSlide.previousSibling; }
842
843 if (previousSlide) {
844 previousSlide = previousSlide.getElementsByTagName('a')[0];
845 this.pendingSlideShowSlide = previousSlide;
846 return this.pendingSlideShowSlide.href;
847 }
848 else {
849 var row = this.slideShowSlide.parentNode.parentNode;
850 var last = row.lastChild;
851 if (last.nodeType===3) { last = last.previousSibling; }
852 this.pendingSlideShowSlide = last.getElementsByTagName('a')[0];
853 return this.pendingSlideShowSlide.href;
854 }
855 };
856
857 FilmSlider.prototype.slideShowImageLoaded = function() {
858 this.slideShowSlide = this.pendingSlideShowSlide;
859 };
860
861 FilmSlider.prototype.stopSlideShow = function() {
862 raiseMouseEvent(this.slideShowSlide, 'click');
863 var index = parseInt(this.selectedSlide.getAttribute('portfolio:position'), 10);
864 this.centerSlide(index);
865 };
866
867
868 /* UTILS */
869 function Point(x, y) {
870 this.x = Math.round(x);
871 this.y = Math.round(y);
872 }
873 Point.prototype.diff = function(point) { return new Point(this.x - point.x, this.y - point.y); };
874 Point.prototype.add = function(point) { return new Point(this.x + point.x, this.y + point.y); };
875 Point.prototype.mul = function(k) { return new Point(this.x * k, this.y *k); };
876 Point.prototype.toString = function() { return "(" + String(this.x) + ", " + String(this.y) + ")"; };
877
878 }());