shrink de l'image si écran rikiki.
[Portfolio.git] / skins / photo_film_viewer.js
index 0b1fde3..16ffb88 100644 (file)
@@ -5,7 +5,7 @@ Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
 */
 
 var FilmSlider;
-
+var s;
 (function(){
 
 var keyLeft = 37, keyRight = 39;
@@ -16,6 +16,7 @@ var DEFAULT_IMAGE_SIZES = [500, 600, 800];
 
 FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
        var thisSlider = this;
+       s = this;
        this.filmBar = filmBar;
        this.filmBarWidth = getObjectWidth(this.filmBar);
        var film = filmBar.firstChild;
@@ -146,10 +147,11 @@ FilmSlider.prototype._checkSize = function(evt) {
 FilmSlider.prototype._checkSizeStability = function(evt) {
        var self = this;
        var i;
+       var checkAgain = function(evt){self._checkSizeStability();};
        for (i=0 ; i<this._barSizes.length - 1 ; i++) {
                if (this._barSizes[i] !== this._barSizes[i+1]) {
                        this._barSizes = [];
-                       setTimeout(function(evt){self._checkSizeStability();}, 250);
+                       setTimeout(checkAgain, 250);
                        return;
                }
        }
@@ -166,11 +168,12 @@ FilmSlider.prototype.fitToScreen = function(evt) {
 
 FilmSlider.prototype._fitToScreen = function(evt) {
        var wh = getWindowHeight();
+       var rb;
        if (!browser.isMobile) {
-               var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
+               rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
        }
        else {
-               var rb = getObjectTop(this.filmBar) + getObjectHeight(this.filmBar); // film bottom
+               rb = getObjectTop(this.filmBar) + getObjectHeight(this.filmBar); // film bottom
        }
        var delta = wh - rb;
        var sh = getObjectHeight(this.stretchable);
@@ -184,6 +187,7 @@ FilmSlider.prototype._fitToScreen = function(evt) {
                var src = this.image.src.replace(imgRequestedSize, 'size=' + bestFitSize);
                this.pendingImage.src = src;
        }
+       this.shrinkImage(this.image);
 };
 
 FilmSlider.prototype.getBestFitSize = function(ratio) {
@@ -208,6 +212,27 @@ FilmSlider.prototype.getBestFitSize = function(ratio) {
        return DEFAULT_IMAGE_SIZES[i];
 };
 
+FilmSlider.prototype.shrinkImage = function(img) {
+       var dispWidth = parseInt(this.stretchable.style.width);
+       var imgWidth = img.width;
+       var dispHeight = parseInt(this.stretchable.style.height);
+       var imgHeight = img.height;
+       var ratio;
+
+       if (imgHeight > dispHeight) {
+               ratio = dispHeight / imgHeight;
+               imgWidth = imgWidth * ratio;
+               imgHeight = dispHeight;
+       }
+       if (imgWidth > dispWidth) {
+               ratio = dispWidth / imgWidth;
+               imgHeight = imgHeight * ratio;
+               imgWidth = dispWidth;
+       }
+       img.width = imgWidth;
+       img.height = imgHeight;
+};
+
 if (!browser.isMobile) {
        FilmSlider.prototype.centerSlide = function(slideIndex) {
                if (this.sliderRatio > 1) { return; }
@@ -289,6 +314,7 @@ FilmSlider.prototype.addEventListeners = function() {
        addListener(this.toolbar, 'click', function(evt){thisSlider.toolbarClickHandler(evt);});
        addListener(window, 'load', function(evt){thisSlider.fitToScreen(evt);});
        addListener(window, 'load', function(evt){thisSlider._checkSizeAfterLoad(evt);});
+       addListener(window, 'load', function(evt){thisSlider.startThumbnailsLoadQueue(evt);});
        
        // dd listeners
        addListener(this.slider, 'mousedown', this.ddHandlers.down);
@@ -354,6 +380,18 @@ FilmSlider.prototype.mouseOutHandler = function(evt) {
        }
 };
 
+FilmSlider.prototype.translateImgUrl = function(url) {
+       var canonicalImgUrl;
+       if (this.ctxUrlTranslation[0]) {
+               canonicalImgUrl = url.replace(this.ctxUrlTranslation[0],
+                                                                         this.ctxUrlTranslation[1]);
+       }
+       else {
+               canonicalImgUrl = url;
+       }
+       return canonicalImgUrl;
+};
+
 FilmSlider.prototype.thumbnailClickHandler = function(evt) {
        var target = getTargetedObject(evt);
        while (target.tagName !== 'A' && target !== this.filmBar) { target = target.parentNode; }
@@ -370,12 +408,7 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                history.pushState(target.href, '', target.href);
                
                var imgBaseUrl = target.href;
-               var canonicalImgUrl;
-               if (this.ctxUrlTranslation[0]) {
-                       canonicalImgUrl = imgBaseUrl.replace(this.ctxUrlTranslation[0],
-                                                                                                this.ctxUrlTranslation[1]);
-               }
-               else { canonicalImgUrl = imgBaseUrl; }
+               var canonicalImgUrl = this.translateImgUrl(imgBaseUrl);
                
                var ajaxUrl = imgBaseUrl + '/photo_view_ajax';
                var thisFS = this;
@@ -622,36 +655,36 @@ FilmSlider.prototype.touchMoveHandler = function(evt) {
        var delta = this.touchStartX - evt.changedTouches[0].screenX;
        var posX = this.filmStartX - delta;
        this.setFilmPosition(posX);
-    this.lastMoveTime = (new Date()).getTime();
+       this.lastMoveTime = (new Date()).getTime();
 };
 
 FilmSlider.prototype.touchEndHandler = function(evt) {
-    var x = evt.changedTouches[0].screenX;
+       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;
+               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
+                       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();
-        }
+                       var animate = function() {
+                               // 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;
 };
@@ -711,10 +744,27 @@ FilmSlider.prototype.populateViewer = function(req) {
 };
 
 FilmSlider.prototype.refreshImage = function() {
+       var dispWidth = getObjectWidth(this.stretchable);
+       var imgWidth = this.pendingImage.width;
+       var dispHeight = getObjectHeight(this.stretchable);
+       var imgHeight = this.pendingImage.height;
+       var ratio;
+
+       if (imgHeight > dispHeight) {
+               ratio = dispHeight / imgHeight;
+               imgWidth = imgWidth * ratio;
+               imgHeight = dispHeight;
+       }
+       if (imgWidth > dispWidth) {
+               ratio = dispWidth / imgWidth;
+               imgHeight = imgHeight * ratio;
+               imgWidth = dispWidth;
+       }
+       
        this.image.style.visibility = 'hidden';
        this.image.src = this.pendingImage.src;
-       this.image.width = this.pendingImage.width;
-       this.image.height = this.pendingImage.height;
+       this.image.width = imgWidth;
+       this.image.height = imgHeight;
        this.image.style.visibility = 'visible';
        if (this.selectedSlideInSelection) { this.image.parentNode.className = 'selected'; }
        else { this.image.parentNode.className = ''; }
@@ -722,11 +772,47 @@ FilmSlider.prototype.refreshImage = function() {
 
 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;
        }
 };
 
+FilmSlider.prototype.startThumbnailsLoadQueue = function(evt) {
+       var thumbnails = this.film.getElementsByTagName('img');
+       if (thumbnails.length === 1) { return; }
+       this.thumbnailsLoadingOrder = [];
+       var leftSize = this.center;
+       var rightSize = thumbnails.length - this.center - 1;
+       var i;
+       for (i=1 ; i<=Math.min(leftSize, rightSize) ; i++) {
+               this.thumbnailsLoadingOrder.push(thumbnails[this.center + i]);
+               this.thumbnailsLoadingOrder.push(thumbnails[this.center - i]);
+       }
+       if (leftSize > rightSize) {
+               for (i = this.center - rightSize - 1 ; i >= 0 ; i--) {
+                       this.thumbnailsLoadingOrder.push(thumbnails[i]);
+               }
+       }
+       else if (leftSize < rightSize) {
+               for (i = this.center + leftSize + 1 ; i < thumbnails.length ; i++) {
+                       this.thumbnailsLoadingOrder.push(thumbnails[i]);
+               }
+       }
+       var next = this.thumbnailsLoadingOrder.shift();
+       var self = this;
+       addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
+       next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
+};
+
+FilmSlider.prototype._loadNextThumb = function(evt) {
+       var next = this.thumbnailsLoadingOrder.shift();
+       if (!next) {return;}
+       var self = this;
+       addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
+       next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
+};
+
+
 FilmSlider.prototype.startSlideShow = function() {
        this.slideShowSlide = this.pendingSlideShowSlide = this.selectedSlide;
        return this.slideShowSlide.href;