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