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