jslint
[Portfolio.git] / skins / photo_film_viewer.js
index cf55fd9..efda660 100644 (file)
@@ -1,9 +1,9 @@
 /*
-* © 2008 Benoît Pin – Centre de recherche en informatique – École des mines de Paris
+* © 2008-2014 Benoît Pin – Centre de recherche en informatique – MINES ParisTech
 * http://plinn.org
 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
-* $Id: photo_film_viewer.js 1303 2009-08-21 22:19:05Z pin $
-* $URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/skins/photo_film_viewer.js $
+* 
+* 
 */
 
 var FilmSlider;
@@ -20,8 +20,7 @@ FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
        var thisSlider = this;
        this.filmBar = filmBar;
        var film = filmBar.firstChild;
-       if (film.nodeType == 3)
-               film = film.nextSibling;
+       if (film.nodeType === 3) { film = film.nextSibling; }
        this.film = film;
        this.slider = slider;
        this.rail = slider.parentNode;
@@ -34,20 +33,25 @@ FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
        this.stretchable = image.parentNode;
        this.viewMode = 'medium';
        
-       this.buttons = new Array();
+       this.buttons = [];
        this.toolbar = toolbar;
-       var bcElements = breadcrumbs.getElementsByTagName('a');
-       this.lastBCElement = bcElements[bcElements.length-1];
-       var imgSrcParts = image.src.split('/');
-       this.lastBCElement.innerHTML = imgSrcParts[imgSrcParts.length-2];
+       if (breadcrumbs) {
+               var bcElements = breadcrumbs.getElementsByTagName('a');
+               this.lastBCElement = bcElements[bcElements.length-1];
+               var imgSrcParts = image.src.split('/');
+               this.lastBCElement.innerHTML = imgSrcParts[imgSrcParts.length-2];
+               this.hasBreadcrumbs = true;
+       }
+       else {
+               this.hasBreadcrumbs = false;
+       }
        
        var buttons = toolbar.getElementsByTagName('img');
-       var b, name;
-       for (var i=0 ; i<buttons.length ; i++) {
+       var b, name, i;
+       for (i=0 ; i<buttons.length ; i++) {
                b = buttons[i];
                name = b.getAttribute('name');
-               if (name)
-                       this.buttons[name] = b;
+               if (name) { this.buttons[name] = b; }
        }
        
        this.pendingImage = new Image();
@@ -56,14 +60,8 @@ FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
        };
        this.initialized = false;
        
-       with(this.slider.style) {
-               left='0';
-               top='0';
-       } 
-       with(this.film.style) {
-               left='0';
-               top='0';
-       }
+       this.film.style.left='0';
+       this.film.style.top='0';
        
        this.filmLength = ctxInfos.filmLength;
        this.center = ctxInfos.center;
@@ -72,18 +70,18 @@ FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
        
        this.ddHandlers = {'down' : function(evt){thisSlider.mouseDownHandler(evt);},
                                           'move' : function(evt){thisSlider.mouseMoveHandler(evt);},
-                                          'up' :   function(evt){thisSlider.mouseUpHandler(evt);},
-                                          'out' :  function(evt){thisSlider.mouseOutHandler(evt);}
+                                          'up' :       function(evt){thisSlider.mouseUpHandler(evt);},
+                                          'out' :      function(evt){thisSlider.mouseOutHandler(evt);}
                                          };
 
        this.resizeSlider();
-       this.addEventListeners()
+       this.addEventListeners();
 };
 
 
 FilmSlider.prototype.resizeSlider = function(evt) {
        var filmBarWidth = getObjectWidth(this.filmBar);
-       if (!filmBarWidth){
+       if (!filmBarWidth) {
                var thisSlider = this;
                addListener(window, 'load', function(evt){thisSlider.resizeSlider(evt);});
                return;
@@ -92,20 +90,20 @@ FilmSlider.prototype.resizeSlider = function(evt) {
        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.rail.style.width = filmBarWidth + 'px';
                this.slider.style.width = Math.round(sliderWidth) + 'px';
-               this.rail.style.display = 'block';
-               this.rail.style.visibility = 'visible';
+               this.slider.style.visibility = 'visible';
        }
        else {
-               this.rail.style.display = 'none';
-               this.rail.style.visibility = 'hidden';
+               this.slider.style.visibility = 'hidden';
        }
        
-       this.winSize = {'width'  : getWindowWidth(),
+       this.winSize = {'width'  : getWindowWidth(),
                                        'height' : getWindowHeight()};
-       this.maxRightPosition = filmBarWidth - sliderWidth
+       this.maxRightPosition = filmBarWidth - sliderWidth;
        this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
        if (!this.initialized) {
                this.centerSlide(this.center);
@@ -122,15 +120,16 @@ FilmSlider.prototype.fitToScreen = function(evt) {
 
 FilmSlider.prototype._fitToScreen = function(evt) {
        var wh = getWindowHeight();
-       var ch = document.body.clientHeight;
+       var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
+       var delta = wh - rb;
        var sh = getObjectHeight(this.stretchable);
-       var newSize = sh + wh - ch;
+       var newSize = sh + delta;
        this.stretchable.style.height = newSize + 'px';
        
        var ratio = this.image.height / this.image.width;
        var bestFitSize = this.getBestFitSize(ratio);
-       var currentSize = parseInt(imgRequestedSize.exec(this.image.src)[1]);
-       if (currentSize != bestFitSize) {
+       var currentSize = parseInt(imgRequestedSize.exec(this.image.src)[1], 10);
+       if (currentSize !== bestFitSize) {
                var src = this.image.src.replace(imgRequestedSize, 'size=' + bestFitSize);
                this.pendingImage.src = src;
        }
@@ -145,37 +144,32 @@ FilmSlider.prototype.getBestFitSize = function(ratio) {
                for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
                        irw = DEFAULT_IMAGE_SIZES[i];
                        irh = irw * ratio;
-                       if (irw <= fw && irh <= fh)
-                               break;
+                       if (irw <= fw && irh <= fh) { break; }
                }
        }
        else {
                for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
                        irh = DEFAULT_IMAGE_SIZES[i];
                        irw = irh / ratio;
-                       if (irw <= fw && irh <= fh)
-                               break;
+                       if (irw <= fw && irh <= fh) { break; }
                }
        }
        return DEFAULT_IMAGE_SIZES[i];
 };
 
 FilmSlider.prototype.centerSlide = function(slideIndex) {
-       if (this.sliderRatio > 1)
-               return;
+       if (this.sliderRatio > 1) { return; }
        var filmBarWidth = getObjectWidth(this.filmBar);
-       var x = slideIndex * this.slideSize
+       var x = slideIndex * this.slideSize;
        x = x - (filmBarWidth - this.slideSize) / 2.0;
        x = x * this.sliderSpeedRatio;
-       var p = new Point( -x, 0 )
+       var p = new Point( -x, 0 );
        this.setSliderPosition(p);
 };
 
 FilmSlider.prototype.setSliderPosition = function(point) {
-       if(point.x < 0)
-               point.x = 0;
-       if (point.x > this.maxRightPosition)
-               point.x = this.maxRightPosition;
+       if(point.x < 0) { point.x = 0; }
+       if (point.x > this.maxRightPosition) { point.x = this.maxRightPosition; }
        this.slider.style.left = point.x + 'px';
        this.setFilmPosition(point);
 };
@@ -185,15 +179,15 @@ FilmSlider.prototype.setFilmPosition = function(point) {
 };
 
 FilmSlider.prototype.getSliderPosition = function() {
-       var x = parseInt(this.slider.style.left);
-       var y = parseInt(this.slider.style.top);
+       var x = parseInt(this.slider.style.left, 10);
+       var y = parseInt(this.slider.style.top, 10);
        var p = new Point(x, y);
        return p;
 };
 
 FilmSlider.prototype.getFilmPosition = function() {
-       var x = parseInt(this.film.style.left);
-       var y = parseInt(this.film.style.top);
+       var x = parseInt(this.film.style.left, 10);
+       var y = parseInt(this.film.style.top, 10);
        var p = new Point(x, y);
        return p;
 };
@@ -202,21 +196,18 @@ FilmSlider.prototype.loadSibling = function(previous) {
        var slide = null;
        if (previous) {
                slide = this.selectedSlide.parentNode.previousSibling;
-               if (slide && slide.nodeType==3)
-                       slide = slide.previousSibling;
+               if (slide && slide.nodeType===3) { slide = slide.previousSibling; }
        }
        else {
                slide = this.selectedSlide.parentNode.nextSibling;
-               if (slide && slide.nodeType==3)
-                       slide = slide.nextSibling;
+               if (slide && slide.nodeType===3) { slide = slide.nextSibling; }
        }
        
-       if (!slide)
-               return;
+       if (!slide) { return; }
        else {
                var target = slide.getElementsByTagName('a')[0];
                raiseMouseEvent(target, 'click');
-               var index = parseInt(target.getAttribute('portfolio:position'));
+               var index = parseInt(target.getAttribute('portfolio:position'), 10);
                this.centerSlide(index);
        }
 };
@@ -229,7 +220,7 @@ FilmSlider.prototype.addEventListeners = function() {
        addListener(window, 'load', function(evt){thisSlider.fitToScreen(evt);});
        
        // dd listeners
-       addListener(this.slider, 'mousedown', this.ddHandlers['down']);
+       addListener(this.slider, 'mousedown', this.ddHandlers.down);
        if(browser.isDOM2Event){
                if (browser.isAppleWebKit) {
                        this.filmBar.addEventListener('mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);}, false);
@@ -251,16 +242,14 @@ FilmSlider.prototype.mouseDownHandler = function(evt) {
        this.initialClickPoint = new Point(evt.clientX, evt.clientY);
        this.initialPosition = this.getSliderPosition();
        this.dragInProgress = true;
-       addListener(document, 'mousemove', this.ddHandlers['move']);
-       addListener(document, 'mouseup', this.ddHandlers['up']);
-       addListener(document.body, 'mouseout', this.ddHandlers['out'])
-       
+       addListener(document, 'mousemove', this.ddHandlers.move);
+       addListener(document, 'mouseup', this.ddHandlers.up);
+       addListener(document.body, 'mouseout', this.ddHandlers.out);
 };
 
 
 FilmSlider.prototype.mouseMoveHandler = function(evt) {
-       if(!this.dragInProgress)
-               return;
+       if(!this.dragInProgress) { return; }
 
        clearSelection();
        evt = getEventObject(evt);
@@ -281,22 +270,20 @@ FilmSlider.prototype.mouseOutHandler = function(evt) {
        var x = evt.clientX;
        var y = evt.clientY;
        if (x < 0 ||
-               x > this.winSize['width'] ||
+               x > this.winSize.width ||
                y < 0 ||
-               y > this.winSize['height']
-               ){
+               y > this.winSize.height
+               ) {
                this.mouseUpHandler(evt);
        }
 };
 
 FilmSlider.prototype.thumbnailClickHandler = function(evt) {
        var target = getTargetedObject(evt);
-       while (target.tagName != 'A' && target != this.filmBar)
-               target = target.parentNode;
-       if (target.tagName != 'A')
-               return;
+       while (target.tagName !== 'A' && target !== this.filmBar) { target = target.parentNode; }
+       if (target.tagName !== 'A') { return; }
        else {
-               if (this.viewMode == 'full') {
+               if (this.viewMode === 'full') {
                        this.mosaique.unload();
                        this.mosaique = null;
                        this.viewMode = 'medium';
@@ -304,14 +291,15 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                disableDefault(evt);
                disablePropagation(evt);
                target.blur();
+               history.pushState(target.href, '', target.href);
                
                var imgBaseUrl = target.href;
                var canonicalImgUrl;
-               if (this.ctxUrlTranslation[0])
+               if (this.ctxUrlTranslation[0]) {
                        canonicalImgUrl = imgBaseUrl.replace(this.ctxUrlTranslation[0],
-                                                                                                this.ctxUrlTranslation[1]);
-               else
-                       canonicalImgUrl = imgBaseUrl;
+                                                                                                this.ctxUrlTranslation[1]);
+               }
+               else { canonicalImgUrl = imgBaseUrl; }
                
                var ajaxUrl = imgBaseUrl + '/photo_view_ajax';
                var thisFS = this;
@@ -322,12 +310,12 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=' + bestFitSize;
                
                // update buttons
-               var fullScreenLink = this.buttons['full_screen'].parentNode;
+               var fullScreenLink = this.buttons.full_screen.parentNode;
                fullScreenLink.href = canonicalImgUrl + '/zoom_view';
 
-               var toggleSelectionBtn = this.buttons['toggle_selection'];
+               var toggleSelectionBtn = this.buttons.toggle_selection;
                var toggleSelectionLink = toggleSelectionBtn.parentNode;
-               this.selectedSlideInSelection = (target.className=='selected');
+               this.selectedSlideInSelection = (target.className==='selected');
                if (this.selectedSlideInSelection) {
                        toggleSelectionBtn.src = portal_url() + '/unselect_flag_btn.gif';
                        toggleSelectionBtn.alt = toggleSelectionLink.title = 'Retirer de la sélection';
@@ -339,7 +327,7 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                        toggleSelectionLink.href = canonicalImgUrl + '/add_to_selection';
                }
 
-               var showBuyableButtonLink = this.buttons['show_buyable'].parentNode;
+               var showBuyableButtonLink = this.buttons.show_buyable.parentNode;
                showBuyableButtonLink.href = canonicalImgUrl + '/get_slide_buyable_items';
                this.cartSlide.innerHTML = '';
                this.cartSlide.style.visibility='hidden';
@@ -371,11 +359,10 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                                        break;
                                case 4 :
                                        hideProgressImage();
-                                       if (req.status == '200')
-                                               thisFS.populateViewer(req);
-                                       else
-                                               //window.location.href = target.href;
-                                               console.error(ajaxUrl);
+                                       if (req.status === '200') { thisFS.populateViewer(req); }
+                                       // else
+                                       //      //window.location.href = target.href;
+                                       //      console.error(ajaxUrl);
 
                        };
                };
@@ -386,15 +373,13 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
                // update old displayed slide className
                var className = this.selectedSlide.className;
                var classes = className.split(' ');
-               var newClasses = new Array();
-               var name;
+               var newClasses = [];
+               var name, i;
 
                for (i in classes) {
                        name = classes[i];
-                       if (name == 'displayed')
-                               continue;
-                       else
-                               newClasses.push(name);
+                       if (name === 'displayed') { continue; }
+                       else { newClasses.push(name); }
                }
                
                this.selectedSlide.className = newClasses.join(' ')
@@ -410,7 +395,7 @@ FilmSlider.prototype.thumbnailClickHandler = function(evt) {
 
 FilmSlider.prototype.toolbarClickHandler = function(evt) {
        var target = getTargetedObject(evt);
-       if(target.tagName == 'IMG' && target.getAttribute('name')){
+       if(target.tagName === 'IMG' && target.getAttribute('name')) {
                switch(target.getAttribute('name')) {
                        case 'previous' :
                                disableDefault(evt);
@@ -432,7 +417,7 @@ FilmSlider.prototype.toolbarClickHandler = function(evt) {
                                disableDefault(evt);
                                disablePropagation(evt);
                                target.parentNode.blur();
-                               if (this.viewMode == 'full') {
+                               if (this.viewMode === 'full') {
                                        this.mosaique.unload();
                                        this.mosaique = null;
                                        this.viewMode = 'medium';
@@ -508,7 +493,7 @@ FilmSlider.prototype.toolbarClickHandler = function(evt) {
                                disableDefault(evt);
                                disablePropagation(evt);
                                target.blur();
-                               if (this.viewMode == 'full') {
+                               if (this.viewMode === 'full') {
                                        this.mosaique.unload();
                                        this.mosaique = null;
                                        this.viewMode = 'medium';
@@ -523,7 +508,7 @@ FilmSlider.prototype.toolbarClickHandler = function(evt) {
 };
 
 
-if(browser.isDOM2Event){
+if(browser.isDOM2Event) {
        if (browser.isAppleWebKit) {
                FilmSlider.prototype.mouseWheelHandler = function(evt) {
                        disableDefault(evt);
@@ -568,10 +553,8 @@ FilmSlider.prototype.keyDownHandler = function(evt) {
 
 FilmSlider.prototype.keyPressHandler = function(evt) {
        var target = getTargetedObject(evt);
-       if (target.tagName == 'INPUT' || target.tagName== 'TEXTAREA')
-               return;
-       var evt = evt = getEventObject(evt);
-       evt = getEventObject(evt);
+       if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') { return; }
+       var evt = getEventObject(evt);
        var charPress = String.fromCharCode((evt.keyCode) ? evt.keyCode : evt.which);
        switch(charPress) {
                case 'f':
@@ -588,20 +571,18 @@ FilmSlider.prototype.populateViewer = function(req) {
                switch (element.nodeName) {
                        case 'fragment' :
                                var dest = document.getElementById(element.getAttribute('id'));
-                               dest.innerHTML = element.firstChild.nodeValue;
+                               if (dest) { dest.innerHTML = element.firstChild.nodeValue; }
                                break;
                        case 'imageattributes' :
                                var link = this.buttons['back_to_portfolio'].parentNode;
                                link.href = element.getAttribute('backToContextUrl');
                                link = this.buttons['show_buyable'].parentNode;
                                var buyable = element.getAttribute('buyable');
-                               if(buyable == 'True')
-                                       link.className = null;
-                               else if(buyable == 'False')
-                                       link.className = 'hidden';
+                               if(buyable === 'True') { link.className = null; }
+                               else if(buyable === 'False') { link.className = 'hidden'; }
                                this.image.alt = element.getAttribute('alt');
-                               this.lastBCElement.href = element.getAttribute('lastBcUrl');
-                               this.lastBCElement.innerHTML = element.getAttribute('img_id');
+                               this.updateBreadcrumbs(element.getAttribute('lastBcUrl'),
+                                                                          element.getAttribute('img_id'));
                                break;
                }
        }
@@ -613,39 +594,63 @@ FilmSlider.prototype.refreshImage = function() {
        this.image.width = this.pendingImage.width;
        this.image.height = this.pendingImage.height;
        this.image.style.visibility = 'visible';
-       if (this.selectedSlideInSelection)
-               this.image.parentNode.className = 'selected';
-       else
-               this.image.parentNode.className = '';
+       if (this.selectedSlideInSelection) { this.image.parentNode.className = 'selected'; }
+       else { this.image.parentNode.className = ''; }
+};
+
+FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
+       if (this.hasBreadcrumbs) {
+               this.lastBCElement.href = element.getAttribute('lastBcUrl');
+               this.lastBCElement.innerHTML = element.getAttribute('img_id');
+       }
 };
 
 FilmSlider.prototype.startSlideShow = function() {
-       this.slideShowSlide = this.selectedSlide;
-       this.nextSlideShowSlide = this.selectedSlide;
+       this.slideShowSlide = this.pendingSlideShowSlide = this.selectedSlide;
        return this.slideShowSlide.href;
 };
 
 FilmSlider.prototype.slideShowNext = function() {
-       this.slideShowSlide = this.nextSlideShowSlide;
        var nextSlide = this.slideShowSlide.parentNode.nextSibling;
-       if (nextSlide && nextSlide.nodeType==3)
-               nextSlide = nextSlide.nextSibling;      
+       if (nextSlide && nextSlide.nodeType===3) { nextSlide = nextSlide.nextSibling; }
 
        if (nextSlide) {
                nextSlide = nextSlide.getElementsByTagName('a')[0];
-               this.nextSlideShowSlide = nextSlide;
-               return nextSlide.href;
+               this.pendingSlideShowSlide = nextSlide;
+               return this.pendingSlideShowSlide.href;
        }
        else {
                var row = this.slideShowSlide.parentNode.parentNode;
                var first = row.firstChild;
-               if (first.nodeType==3)
+               if (first.nodeType===3)
                        first = first.nextSibling;
-               this.nextSlideShowSlide = first.getElementsByTagName('a')[0];
-               return this.nextSlideShowSlide.href;
+               this.pendingSlideShowSlide = first.getElementsByTagName('a')[0];
+               return this.pendingSlideShowSlide.href;
        }
 };
 
+FilmSlider.prototype.slideShowPrevious = function() {
+       var previousSlide = this.slideShowSlide.parentNode.previousSibling;
+       if (previousSlide && previousSlide.nodeType===3) { previousSlide = previousSlide.previousSibling; }
+
+       if (previousSlide) {
+               previousSlide = previousSlide.getElementsByTagName('a')[0];
+               this.pendingSlideShowSlide = previousSlide;
+               return this.pendingSlideShowSlide.href;
+       }
+       else {
+               var row = this.slideShowSlide.parentNode.parentNode;
+               var last = row.lastChild;
+               if (last.nodeType===3) { last = last.previousSibling; }
+               this.pendingSlideShowSlide = last.getElementsByTagName('a')[0];
+               return this.pendingSlideShowSlide.href;
+       }
+};
+
+FilmSlider.prototype.slideShowImageLoaded = function() {
+       this.slideShowSlide = this.pendingSlideShowSlide;
+};
+
 FilmSlider.prototype.stopSlideShow = function() {
        raiseMouseEvent(this.slideShowSlide, 'click');
        var index = parseInt(this.selectedSlide.getAttribute('portfolio:position'));