Callback ajax après déplacement.
[Portfolio.git] / skins / photo_lightbox_viewer.js
index ac20cd3..f4c4498 100644 (file)
@@ -11,14 +11,20 @@ var Lightbox;
 
 var reSelected = /.*selected.*/;
 
-Lightbox = function(grid, toolbar) {
+Lightbox = function(grid, toolbar, complete, container_type) {
        var self = this;
        this.grid = grid;
+       this._buildSlidesIndex(); // set this.slides and this.lastSlide;
+       this.fetchingDisabled = false;
+       this.complete = complete;
+       this.container_type = container_type;
        this.toolbar = toolbar;
        if (toolbar) {
                this.toolbarFixed = false;
-               addListener(window, 'scroll', function(evt){self.windowScrollHandler(evt);});
+               addListener(window, 'scroll', function(evt){self.windowScrollToolbarlHandler(evt);});
        }
+       addListener(window, 'scroll', function(evt){self.windowScrollGridHandler(evt);});
+       registerStartupFunction(function(){ self.windowScrollGridHandler();});
        this.lastCBChecked = undefined;
        this.form = undefined;
        var parent = this.grid.parentNode;
@@ -35,29 +41,57 @@ Lightbox = function(grid, toolbar) {
        addListener(this.grid, 'click', function(evt){self.mouseClickHandler(evt);});
        if (this.form) {
                var fm = this.fm = new FormManager(this.form);
-        addListener(this.form, 'change', function(evt){self.onChangeHandler(evt);});
+               addListener(this.form, 'change', function(evt){self.onChangeHandler(evt);});
                fm.onBeforeSubmit = function(fm_, evt) {return self.onBeforeSubmit(fm_, evt);};
                fm.onResponseLoad = function(req) {return self.onResponseLoad(req);};
        }
+
+       // drag and drop
+       this.disableDefaultDragging();
+       addListener(this.grid, 'dragstart', function(evt){self.onDragStart(evt);});
+       addListener(this.grid, 'dragover', function(evt){self.onDragOver(evt);});
+       addListener(this.grid, 'dragend', function(evt){self.onDragEnd(evt);});
 };
 
-Lightbox.prototype.windowScrollHandler = function(evt) {
-       if (this.toolbar.offsetTop < window.scrollY && !this.toolbarFixed) {
+Lightbox.prototype._buildSlidesIndex = function() {
+       this.slides = [];
+       var node, i;
+       for (i=0 ; i<this.grid.childNodes.length ; i++) {
+               node = this.grid.childNodes[i];
+               if (node.nodeType === 1) { // is element
+                       this.slides.push(node);
+               }
+       }
+       this.lastSlide = this.slides[this.slides.length-1];
+};
+
+Lightbox.prototype.windowScrollToolbarlHandler = function(evt) {
+       if (this.toolbar.offsetTop < getWindowScrollY() && !this.toolbarFixed) {
                this.toolbarFixed = true;
                this.backThreshold = this.toolbar.offsetTop;
                this.switchToolBarPositioning(true);
        }
-       else if (this.toolbarFixed && window.scrollY < this.backThreshold) {
+       else if (this.toolbarFixed && getWindowScrollY() < this.backThreshold) {
                this.toolbarFixed = false;
                this.switchToolBarPositioning(false);
        }
 };
+Lightbox.prototype.windowScrollGridHandler = function(evt) {
+       if (!this.complete &&
+               !this.fetchingDisabled &&
+               getWindowScrollY() >
+                       (this.lastSlide.firstElementChild || this.lastSlide.children[0]).offsetTop
+                        - getWindowHeight()) {
+               this.fetchingDisabled = true;
+               this.fetchTail();
+       }
+};
 
 Lightbox.prototype.mouseClickHandler = function(evt) {
        var target = getTargetedObject(evt);
        if (target.tagName === 'IMG') {
                var img = target;
-               var link =  target.parentNode;
+               var link =      target.parentNode;
                var button =  link.parentNode;
                var slide = button.parentNode;
                var req, url;
@@ -144,11 +178,11 @@ Lightbox.prototype.mouseClickHandler = function(evt) {
 };
 
 Lightbox.prototype.onChangeHandler = function(evt) {
-    var target = getTargetedObject(evt);
-    if (target.name === 'sort_on') {
-        this.fm.submitButton = {'name' : 'set_sorting', 'value' : 'ok'};
-        this.fm.submit(evt);
-    }
+       var target = getTargetedObject(evt);
+       if (target.name === 'sort_on') {
+               this.fm.submitButton = {'name' : 'set_sorting', 'value' : 'ok'};
+               this.fm.submit(evt);
+       }
 };
 
 Lightbox.prototype.onBeforeSubmit = function(fm, evt) {
@@ -167,10 +201,13 @@ Lightbox.prototype.onResponseLoad = function(req) {
                case 'error' :
                        this.showSelection();
                        break;
-        case 'sorted' :
-            this.fm.submitButton = undefined;
-            this.refreshGrid();
-            break;
+               case 'sorted' :
+                       this.fm.submitButton = undefined;
+                       this.refreshGrid();
+                       break;
+               default :
+                       this.fm.loadResponse(req);
+                       break;
        }
 };
 
@@ -233,7 +270,7 @@ Lightbox.prototype.deleteSelection = function() {
 };
 
 Lightbox.prototype._removeSelection = function() {
-       var i, e, slide;
+       var i, e;
        var toRemove = [];
        for (i=0 ; i<this.form.elements.length ; i++) {
                e = this.form.elements[i];
@@ -242,10 +279,11 @@ Lightbox.prototype._removeSelection = function() {
                }
        }
        for (i=0 ; i<toRemove.length ; i++) {
-               slide = toRemove[i];
-               slide.parentNode.removeChild(slide);
+               this.grid.removeChild(toRemove[i]);
        }
+       this._buildSlidesIndex();
        this.cbIndex = undefined;
+       this.windowScrollGridHandler();
 };
 
 Lightbox.prototype.getCBIndex = function(cb) {
@@ -253,14 +291,11 @@ Lightbox.prototype.getCBIndex = function(cb) {
                // build checkbox index
                this.cbIndex = [];
                var i, node, c;
-               var nodes = this.grid.childNodes;
-               for (i=0 ; i<nodes.length ; i++) {
-                       node = nodes[i];
-                       if (node.nodeName === 'SPAN') {
-                               c = node.getElementsByTagName('input')[0];
-                               c.index = this.cbIndex.length;
-                               this.cbIndex[this.cbIndex.length] = c;
-                       }
+               for (i=0 ; i<this.slides.length ; i++) {
+                       node = this.slides[i];
+                       c = node.getElementsByTagName('input')[0];
+                       c.index = this.cbIndex.length;
+                       this.cbIndex.push(c);
                }
        }
        return cb.index;
@@ -289,20 +324,6 @@ Lightbox.prototype.selectCBRange = function(evt) {
 };
 
 Lightbox.prototype.refreshGrid = function() {
-       if (!this.uidIndex) {
-               // build checkbox index
-               this.uidIndex = {};
-               var i, node, length=0;
-               var nodes = this.grid.childNodes;
-               for (i=0 ; i<nodes.length ; i++) {
-                       node = nodes[i];
-                       if (node.nodeName === 'SPAN') {
-                               this.uidIndex[node.name] = node;
-                               length++;
-                       }
-               }
-               this.uidIndex.length = length;
-       }
        var req = new XMLHttpRequest();
        self = this;
        req.onreadystatechange = function() {
@@ -313,7 +334,7 @@ Lightbox.prototype.refreshGrid = function() {
                        case 4 :
                                hideProgressImage();
                                if (req.status === 200) {
-                                       self._refreshGrid(req)
+                                       self._refreshGrid(req);
                                }
                                break;
                }
@@ -321,13 +342,77 @@ Lightbox.prototype.refreshGrid = function() {
        
        var url = absolute_url() +
                          '/portfolio_thumbnails_tail?start:int=0&size:int=' +
-                         this.uidIndex.length;
+                         this.slides.length;
        req.open('GET', url, true);
        req.send();
 };
 
 Lightbox.prototype._refreshGrid = function(req) {
-       console.log(req);
+       var doc = req.responseXML.documentElement;
+       var i, node;
+       var j = 0;
+       for (i=0 ; i<doc.childNodes.length ; i++) {
+               node = doc.childNodes[i];
+               if (node.nodeType === 1) {
+                       node = getCopyOfNode(node);
+                       this.disableDefaultDragging(node);
+                       this.grid.replaceChild(node, this.slides[j]);
+                       this.slides[j] = node;
+                       j++;
+               }
+       }
+       this.cbIndex = undefined;
+};
+
+Lightbox.prototype.fetchTail = function() {
+       var req = new XMLHttpRequest();
+       var self = this;
+       req.onreadystatechange = function() {
+               switch (req.readyState) {
+                       case 1 :
+                               showProgressImage();
+                               break;
+                       case 4 :
+                               hideProgressImage();
+                               if (req.status === 200) {
+                                       self._appendTail(req);
+                               }
+                               break;
+               }
+       };
+       
+       var url = absolute_url() +
+                         '/portfolio_thumbnails_tail?start:int=' +
+                         String(this.slides.length) +
+                         '&size:int=10' +
+                         '&container_type=' +
+                         this.container_type;
+       req.open('GET', url, true);
+       req.send();
+};
+
+Lightbox.prototype._appendTail = function(req) {
+       var doc = req.responseXML.documentElement;
+       var i, node, c;
+       for (i=0 ; i<doc.childNodes.length ; i++) {
+               node = doc.childNodes[i];
+               if (node.nodeType === 1) {
+                       this.lastSlide = this.grid.appendChild(getCopyOfNode(node));
+                       this.disableDefaultDragging(this.lastSlide);
+                       this.slides.push(this.lastSlide);
+                       if (this.cbIndex) {
+                               c = this.lastSlide.getElementsByTagName('input')[0];
+                               c.index = this.cbIndex.length;
+                               this.cbIndex.push(c);
+                               
+                       }
+               }
+       }
+       this.fetchingDisabled = false;
+       if (doc.getAttribute('nomore')) {
+               this.complete = true;
+       }
+       this.windowScrollGridHandler();
 };
 
 
@@ -346,4 +431,118 @@ else {
        };
 }
 
+if (browser.isGecko) {
+       Lightbox.prototype.disableDefaultDragging = function(element) {
+               if (!element) {
+                       element = this.grid;
+               }
+               var i, j, name, elements;
+               var elementsNames = ['a', 'img'];
+               for (i=0 ; i < elementsNames.length ; i++) {
+                       name = elementsNames[i];
+                       elements = element.getElementsByTagName(name);
+                       for (j=0 ; j < elements.length ; j++) {
+                               elements[j].draggable=false;
+                       }
+               }
+       };
+}
+else {
+       Lightbox.prototype.disableDefaultDragging = function() {};
+}
+
+Lightbox.prototype.getSelectedSlides = function() {
+       var i, e, slide;
+       var slides = [];
+       for (i=0 ; i<this.form.elements.length ; i++) {
+               e = this.form.elements[i];
+               if (e.type === 'checkbox' && e.checked) {
+                       slide = e.parentNode.parentNode;
+                       slides.push(slide);
+               }
+       }
+       return slides;
+};
+
+Lightbox.prototype.onDragStart = function(evt) {
+       var target = getTargetedObject(evt);
+       this.dragged = target;
+       this.draggedSelection = this.getSelectedSlides();
+       if (this.draggedSelection.indexOf(target) === -1) {
+               this.draggedSelection.push(target);
+       }
+       evt.dataTransfer.setData('text', '');
+       var i, slide;
+       for(i=0 ; i<this.draggedSelection.length ; i++) {
+               slide = this.draggedSelection[i];
+               slide.style.opacity = 0;
+               slide.style.width = 0;
+       }
+};
+
+Lightbox.prototype.onDragOver = function(evt) {
+       var target = getTargetedObject(evt);
+       if (!target) {return;}
+       while(target.className !== 'slide') {
+               target = target.parentNode;
+       }
+       target = target.parentNode;
+       if (target !== this.dragged) {
+               target.classList.add('dragover');
+       }
+       if (this.lastDropTarget && this.lastDropTarget !== target) {
+               this.lastDropTarget.classList.remove('dragover');
+       }
+       this.lastDropTarget = target;
+};
+
+Lightbox.prototype.onDragEnd = function(evt) {
+       if (this.lastDropTarget) {
+               this.lastDropTarget.classList.remove('dragover');
+               var i, slide;
+               for(i=this.draggedSelection.length -1 ; i>=0 ; i--) {
+                       slide = this.draggedSelection[i].cloneNode(true);
+                       this.grid.insertBefore(slide, this.lastDropTarget.nextSibling);
+                       slide.style.opacity = 1;
+                       slide.style.width = '';
+               }
+               this.moveSelectedPhotos();
+       }
+       // this.draggedSelection = this.lastDropTarget
+       this.dragged = undefined;
+};
+
+Lightbox.prototype.moveSelectedPhotos = function() {
+       var req = new XMLHttpRequest();
+       self = this;
+       req.onreadystatechange = function() {
+               switch (req.readyState) {
+                       case 1 :
+                               showProgressImage();
+                               break;
+                       case 4 :
+                               hideProgressImage();
+                               if (req.status === 200) {
+                                       console.log(req.responseText);
+                                       // self._refreshGrid(req);
+                               }
+                               break;
+               }
+       };
+       
+       var url = absolute_url() + '/portfolio_move_photos';
+       req.open("POST", url, true);
+       req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
+       var query = 'container_type=' + this.container_type;
+       var i;
+       for (i=0 ; i<this.draggedSelection.length ; i++) {
+               query += '&uids:list=' + this.draggedSelection[i].getAttribute('name');
+       }
+       query += '&afterUid=' + this.lastDropTarget.getAttribute('name');
+       req.send(query);
+};
+
+Lightbox.prototype.moveSelectedPhoto = function() {
+};
+
 }());
\ No newline at end of file