bugfix.
[Portfolio.git] / skins / photo_film_viewer.js
index ddd73e1..0a1cab9 100644 (file)
@@ -73,38 +73,56 @@ FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
                                           'out' :      function(evt){thisSlider.mouseOutHandler(evt);}
                                          };
 
                                           'out' :      function(evt){thisSlider.mouseOutHandler(evt);}
                                          };
 
-       this.resizeSlider();
+       if (browser.isMobile) {
+               this.rail.className = 'hidden';
+       }
+       else { 
+               this.resizeSlider();
+       }
        this.addEventListeners();
 };
 
        this.addEventListeners();
 };
 
-
-FilmSlider.prototype.resizeSlider = function(evt) {
-       var filmBarWidth = this.filmBarWidth;
-       if (!filmBarWidth) { return; }
-       var filmWidth = this.slideSize * this.filmLength;
-       var sliderRatio = this.sliderRatio =  filmBarWidth / filmWidth;
-       var sliderWidth = filmBarWidth * sliderRatio;
-       this.rail.style.width = filmBarWidth + 'px';
-       this.rail.style.display = 'block';
-       this.rail.style.visibility = 'visible';
-       if (sliderRatio < 1) {
-               this.slider.style.width = Math.round(sliderWidth) + 'px';
-               this.slider.style.visibility = 'visible';
-       }
-       else {
-               this.slider.style.visibility = 'hidden';
-       }
+if (!browser.isMobile) {
+       FilmSlider.prototype.resizeSlider = function(evt) {
+               var filmBarWidth = this.filmBarWidth;
+               if (!filmBarWidth) { return; }
+               var filmWidth = this.slideSize * this.filmLength;
+               var sliderRatio = this.sliderRatio =  filmBarWidth / filmWidth;
+               var sliderWidth = filmBarWidth * sliderRatio;
+               this.rail.style.width = filmBarWidth + 'px';
+               this.rail.style.display = 'block';
+               this.rail.style.visibility = 'visible';
+               if (sliderRatio < 1) {
+                       this.slider.style.width = Math.round(sliderWidth) + 'px';
+                       this.slider.style.visibility = 'visible';
+               }
+               else {
+                       this.slider.style.visibility = 'hidden';
+               }
        
        
-       this.winSize = {'width'  : getWindowWidth(),
-                                       'height' : getWindowHeight()};
-       this.maxRightPosition = filmBarWidth - sliderWidth;
-       this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
-       if (!this.initialized) {
-               this.centerSlide(this.center);
-               this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
-               this.initialized = true;
-       }
-};
+               this.winSize = {'width'  : getWindowWidth(),
+                                               'height' : getWindowHeight()};
+               this.maxRightPosition = filmBarWidth - sliderWidth;
+               this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
+               if (!this.initialized) {
+                       this.centerSlide(this.center);
+                       this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
+                       this.initialized = true;
+               }
+       };
+}
+
+else {
+       // pas de barre de scroll horizontal pour les tablettes
+       FilmSlider.prototype.resizeSlider = function(evt) {
+               this.filmMaxX = - (getObjectWidth(this.film) - this.filmBarWidth);
+               if (!this.initialized) {
+                       this.centerSlide(this.center);
+                       this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
+                       this.initialized = true;
+               }
+       };
+}
 
 FilmSlider.prototype._checkSizeAfterLoad = function(evt) {
        this._barSizes = [];
 
 FilmSlider.prototype._checkSizeAfterLoad = function(evt) {
        this._barSizes = [];
@@ -148,7 +166,12 @@ FilmSlider.prototype.fitToScreen = function(evt) {
 
 FilmSlider.prototype._fitToScreen = function(evt) {
        var wh = getWindowHeight();
 
 FilmSlider.prototype._fitToScreen = function(evt) {
        var wh = getWindowHeight();
-       var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
+       if (!browser.isMobile) {
+               var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
+       }
+       else {
+               var rb = getObjectTop(this.filmBar) + getObjectHeight(this.filmBar); // film bottom
+       }
        var delta = wh - rb;
        var sh = getObjectHeight(this.stretchable);
        var newSize = sh + delta;
        var delta = wh - rb;
        var sh = getObjectHeight(this.stretchable);
        var newSize = sh + delta;
@@ -185,15 +208,25 @@ FilmSlider.prototype.getBestFitSize = function(ratio) {
        return DEFAULT_IMAGE_SIZES[i];
 };
 
        return DEFAULT_IMAGE_SIZES[i];
 };
 
-FilmSlider.prototype.centerSlide = function(slideIndex) {
-       if (this.sliderRatio > 1) { return; }
-       var filmBarWidth = getObjectWidth(this.filmBar);
-       var x = slideIndex * this.slideSize;
-       x = x - (filmBarWidth - this.slideSize) / 2.0;
-       x = x * this.sliderSpeedRatio;
-       var p = new Point( -x, 0 );
-       this.setSliderPosition(p);
-};
+if (!browser.isMobile) {
+       FilmSlider.prototype.centerSlide = function(slideIndex) {
+               if (this.sliderRatio > 1) { return; }
+               var filmBarWidth = getObjectWidth(this.filmBar);
+               var x = slideIndex * this.slideSize;
+               x = x - (filmBarWidth - this.slideSize) / 2.0;
+               x = x * this.sliderSpeedRatio;
+               var p = new Point( -x, 0 );
+               this.setSliderPosition(p);
+       };
+}
+else {
+       FilmSlider.prototype.centerSlide = function(slideIndex) {
+               var filmBarWidth = getObjectWidth(this.filmBar);
+               var x = slideIndex * this.slideSize;
+               x = x - (filmBarWidth - this.slideSize) / 2.0;
+               this.setFilmPosition(-x);
+       };
+}
 
 FilmSlider.prototype.setSliderPosition = function(point) {
        if(point.x < 0) { point.x = 0; }
 
 FilmSlider.prototype.setSliderPosition = function(point) {
        if(point.x < 0) { point.x = 0; }
@@ -202,9 +235,18 @@ FilmSlider.prototype.setSliderPosition = function(point) {
        this.setFilmPosition(point);
 };
 
        this.setFilmPosition(point);
 };
 
-FilmSlider.prototype.setFilmPosition = function(point) {
-       this.film.style.left = point.x / this.sliderSpeedRatio + 'px';
-};
+if (!browser.isMobile) {
+       FilmSlider.prototype.setFilmPosition = function(point) {
+               this.film.style.left = point.x / this.sliderSpeedRatio + 'px';
+       };
+}
+else {
+       FilmSlider.prototype.setFilmPosition = function(x) {
+               x = Math.min(0, x);
+               x = Math.max(this.filmMaxX, x);
+               this.film.style.left = String(x) + 'px';
+       };
+}
 
 FilmSlider.prototype.getSliderPosition = function() {
        var x = parseInt(this.slider.style.left, 10);
 
 FilmSlider.prototype.getSliderPosition = function() {
        var x = parseInt(this.slider.style.left, 10);
@@ -261,6 +303,11 @@ FilmSlider.prototype.addEventListeners = function() {
        else if (browser.isIE6up) {
                addListener(this.filmBar, 'mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);});
        }
        else if (browser.isIE6up) {
                addListener(this.filmBar, 'mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);});
        }
+       if (browser.isMobile) {
+               this.filmBar.addEventListener('touchstart', function(evt){thisSlider.touchStartHandler(evt);}, false);
+               this.filmBar.addEventListener('touchmove', function(evt){thisSlider.touchMoveHandler(evt);}, false);
+               this.filmBar.addEventListener('touchend', function(evt){thisSlider.touchEndHandler(evt);}, false);
+       }
        
        addListener(document, 'keydown', function(evt){thisSlider.keyDownHandler(evt);});
        addListener(document, 'keypress', function(evt){thisSlider.keyPressHandler(evt);});
        
        addListener(document, 'keydown', function(evt){thisSlider.keyDownHandler(evt);});
        addListener(document, 'keypress', function(evt){thisSlider.keyPressHandler(evt);});
@@ -564,6 +611,52 @@ else if (browser.isIE6up) {
        };
 }
 
        };
 }
 
+FilmSlider.prototype.touchStartHandler = function(evt) {
+       this.filmStartX = parseInt(this.film.style.left, 10);
+       this.touchStartX = evt.changedTouches[0].screenX;
+       this.touchStartTime = (new Date()).getTime();
+};
+
+FilmSlider.prototype.touchMoveHandler = function(evt) {
+       disableDefault(evt);
+       var delta = this.touchStartX - evt.changedTouches[0].screenX;
+       var posX = this.filmStartX - delta;
+       this.setFilmPosition(posX);
+    this.lastMoveTime = (new Date()).getTime();
+};
+
+FilmSlider.prototype.touchEndHandler = function(evt) {
+    var x = evt.changedTouches[0].screenX;
+       var delta = x - this.touchStartX;
+       if (delta) {
+               disableDefault(evt);
+        var now = (new Date()).getTime();
+        if (now - this.lastMoveTime < 100) {
+            // au delà de 100 ms de maintient, on annule l'inertie
+               var speed = delta / (now - this.touchStartTime)
+               var x0 = parseInt(this.film.style.left, 10);
+               var t0 = (new Date()).getTime();
+               var d = 500; // milisecondes
+            var delta = 0;
+            var dt = 25
+               var self = this;
+       
+               function animate() {
+                // inertie
+                       var t = (new Date()).getTime() - t0;
+                       if (t < d) {
+                               setTimeout(animate, dt);
+                               delta = delta + (1-t/d) * speed * dt; // décelleration linéaire
+                               self.setFilmPosition(x0 + delta);
+                       }
+               }
+               animate();
+        }
+       }
+       this.touchStartX = undefined;
+};
+
+
 FilmSlider.prototype.keyDownHandler = function(evt) {
        evt = getEventObject(evt);
        switch (evt.keyCode) {
 FilmSlider.prototype.keyDownHandler = function(evt) {
        evt = getEventObject(evt);
        switch (evt.keyCode) {
@@ -604,13 +697,13 @@ FilmSlider.prototype.populateViewer = function(req) {
                                break;
                        case 'imageattributes' :
                                var link = this.buttons.back_to_portfolio.parentNode;
                                break;
                        case 'imageattributes' :
                                var link = this.buttons.back_to_portfolio.parentNode;
-                               link.href = element.getAttribute('backToContextUrl');
+                               link.href = element.getAttribute('back_to_context_url');
                                link = this.buttons.show_buyable.parentNode;
                                var buyable = element.getAttribute('buyable');
                                if(buyable === 'True') { link.className = null; }
                                else if(buyable === 'False') { link.className = 'hidden'; }
                                this.image.alt = element.getAttribute('alt');
                                link = this.buttons.show_buyable.parentNode;
                                var buyable = element.getAttribute('buyable');
                                if(buyable === 'True') { link.className = null; }
                                else if(buyable === 'False') { link.className = 'hidden'; }
                                this.image.alt = element.getAttribute('alt');
-                               this.updateBreadcrumbs(element.getAttribute('lastBcUrl'),
+                               this.updateBreadcrumbs(element.getAttribute('last_bc_url'),
                                                                           element.getAttribute('img_id'));
                                break;
                }
                                                                           element.getAttribute('img_id'));
                                break;
                }
@@ -629,8 +722,8 @@ FilmSlider.prototype.refreshImage = function() {
 
 FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
        if (this.hasBreadcrumbs) {
 
 FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
        if (this.hasBreadcrumbs) {
-               this.lastBCElement.href = element.getAttribute('lastBcUrl');
-               this.lastBCElement.innerHTML = element.getAttribute('img_id');
+               this.lastBCElement.href = url;
+               this.lastBCElement.innerHTML = title;
        }
 };
 
        }
 };