Mise à jour de la balise <base> du document pour que les liens relatifs fonctionnent...
[Plinn.git] / skins / ajax_scripts / folder_contents_script.js
index b896cb7..45ec76a 100644 (file)
@@ -1,40 +1,66 @@
 // (c) Benoît PIN 2006-2007
 // http://plinn.org
 // Licence GPL
-// $Id: folder_contents_script.js 1375 2008-09-12 12:29:12Z pin $
-// $URL: http://svn.cri.ensmp.fr/svn/Plinn/branches/CMF-2.1/skins/ajax_scripts/folder_contents_script.js $
+// 
+// 
+
+var FolderDDropControler;
+var DropTarget;
+var loadListing;
+var DDFolderUploader;
+
+(function(){
 
 function getTargetRow(evt){
        var target = getTargetedObject(evt);
-       while (target.nodeName != "TR")
-               target = target.parentNode;
+       while (target.nodeName !== "TR") {
+               target = target.parentNode; }
        return target;
 }
 
-function FolderDDropControler(listing) {
+FolderDDropControler = function(listing, orderable, firstItemPos) {
        this.folderUrl = document.getElementById("FolderUrl").innerHTML;
        this.targetRow = null;
        this.lastOverPosition = null;
        this.prevDirUp = null;
        this.noOver = true;
        this.listing = listing;
+       this.firstItemPos = firstItemPos;
+       this._updatePositions();
+       this.lastCBChecked = undefined;
        var thisControler = this;
-       this.listing.onmousedown        = function(evt) {thisControler.drag(evt);};
-       this.listing.onmouseover        = function(evt) {thisControler.moveRow(evt);};
-       this.listing.onmouseup          = function(evt) {thisControler.drop(evt);};
-       this.listing.onclick            = function(evt) {thisControler.disableClickAfterDrop(evt);};
+       if (orderable) {
+               this.listing.onmousedown        = function(evt) {thisControler.drag(evt);};
+               this.listing.onmouseover        = function(evt) {thisControler.moveRow(evt);};
+               this.listing.onmouseup          = function(evt) {thisControler.drop(evt);};
+               addListener(this.listing, 'click', function(evt) {thisControler.disableClickAfterDrop(evt);});
+       }
+       addListener(this.listing, 'click', function(evt) {thisControler.selectCBRange(evt);});
        
        if (browser.isIE) {
                this.listing.ondragstart = function() { window.event.returnValue = false;};
        }
-}
+};
+
+FolderDDropControler.prototype._updatePositions = function() {
+       var rows = this.listing.getElementsByTagName("TR");
+       var i, row;
+       for (i=0 ; i < rows.length ; i++ ) {
+               row = rows[i];
+               row.pos = i + this.firstItemPos;
+               if (i % 2 === 0) {
+                       row.className = "even"; }
+               else {
+                       row.className = "odd"; }
+       }
+};
 
 FolderDDropControler.prototype.drag =  function(evt){
        var target = getTargetedObject(evt);
-       if (target.nodeName == "INPUT") return true;
+       if (target.nodeName === "INPUT") { return true; }
        disableDefault(evt);
        var targetRow = getTargetRow(evt);
-       targetRow.style.backgroundColor = base_properties["highLightColor"];
+       targetRow.style.backgroundColor = base_properties.highLightColor;
        this.listing.style.cursor = "move";
        this.targetRow = targetRow;
        this.lastOverPosition = targetRow.pos;
@@ -42,13 +68,13 @@ FolderDDropControler.prototype.drag =  function(evt){
 
 FolderDDropControler.prototype.moveRow =  function(evt){
        var targetRow = this.targetRow;
-       if (targetRow != null) {
+       if (targetRow !== null) {
                this.noOver = false;
-               if (browser.isIE) document.selection.clear();
+               if (browser.isIE) {document.selection.clear();}
                var overRow = getTargetRow(evt);
 
-               if (overRow.pos == targetRow.pos) return;
-       
+               if (overRow.pos === targetRow.pos) {return;}
+
                if (this.lastOverPosition < overRow.pos) { // move up
                        this.listing.insertBefore(targetRow, overRow.nextSibling);
                        this.prevDirUp = true;
@@ -74,45 +100,36 @@ FolderDDropControler.prototype.moveRow =  function(evt){
 
 FolderDDropControler.prototype.drop =  function(evt){
        var targetRow = this.targetRow;
-       if (targetRow != null) {
+       if (targetRow !== null) {
                targetRow.style.backgroundColor="";
                this.listing.style.cursor = "";
+               var thisControler = this;
                if (this.noOver) {
-                       var thisControler = this;
                        setTimeout(function(){thisControler.reset();}, 50);
                        return;
                }
-               if (this.lastOverPosition != null) {
+               if (this.lastOverPosition !== null) {
                        // get new object position.
                        var trim = 0;
-                       if (targetRow.pos < this.lastOverPosition && !this.prevDirUp)
-                               trim = -1;
-                       else if (targetRow.pos > this.lastOverPosition && this.prevDirUp)
-                               trim = 1;
+                       if (targetRow.pos < this.lastOverPosition && !this.prevDirUp) {
+                               trim = -1; }
+                       else if (targetRow.pos > this.lastOverPosition && this.prevDirUp) {
+                               trim = 1; }
                
                        // construct url
-                       var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
-                       var url = this.folderUrl + "/moveObjectIdToPosition";
-                       var form ="object_id=" + object_id + "&position:int=" +
-                                          String(this.lastOverPosition - 1 + trim);
+                       var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
+                       var url = this.folderUrl + "/moveObjectIdToPosition";
+                       var form ="object_id=" + object_id + "&position:int=" +
+                                          String(this.lastOverPosition - 1 + trim);
 
                        // reinitialize positions
-                       var rows = this.listing.getElementsByTagName("TR"), row;
-                       for (var i = 0 ; i < rows.length ; i++) {
-                               row = rows[i];
-                               row.pos = i+1;
-                               if (i % 2 == 0)
-                                       row.className = "even";
-                               else
-                                       row.className = "odd";
-                       }
-
-                       // send request                 
+                       this._updatePositions();
+
+                       // send request                         
                        var req = new XMLHttpRequest();
                        req.open("POST", url, true);
                        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
                        req.send(form);
-                       var thisControler = this;
                        setTimeout(function(){thisControler.reset();}, 50);
                }
        }
@@ -127,47 +144,80 @@ FolderDDropControler.prototype.disableClickAfterDrop = function(evt) {
        this.reset();
 };
 
+FolderDDropControler.prototype.selectCBRange = function(evt) {
+       var target = getTargetedObject(evt);
+       if (target.tagName === 'INPUT' && target.type === 'checkbox') {
+               evt = getEventObject(evt);
+               var shift = evt.shiftKey;
+               if (shift && this.lastCBChecked) {
+                       var from = this.getCBIndex(this.lastCBChecked);
+                       var to = this.getCBIndex(target);
+                       var rows = this.listing.getElementsByTagName('TR');
+                       var start = Math.min(from, to);
+                       var stop = Math.max(from, to);
+                       var i;
+                       for (i=start ; i<stop ; i++ ) {
+                               rows[i].getElementsByTagName('INPUT')[0].checked = true;
+                       }
+               }
+               else if (target.checked) {
+                       this.lastCBChecked = target;
+               }
+               else {
+                       this.lastCBChecked = undefined;
+               }
+       }
+};
+
+FolderDDropControler.prototype.getCBIndex = function(cb) {
+       var row = cb.parentNode;
+       while(row.tagName !== 'TR') {
+               row = row.parentNode;
+       }
+       return row.pos - this.firstItemPos;
+};
+
 FolderDDropControler.prototype.reset = function() {
        this.targetRow = null;
        this.lastOverPosition = null;
        this.prevDirUp = null;
        this.noOver = true;
-}
+};
 
 
 
-function DropTarget(node, folderDDControler) {
+DropTarget = function(node, folderDDControler) {
        this.folderDDControler = folderDDControler;
-       this.batchSize = parseInt(document.getElementById("BatchNavigationSize").innerHTML);
+       this.batchSize = parseInt(document.getElementById("BatchNavigationSize").innerHTML, 10);
        var thisControler = this;
        node.onmouseup = function(evt){thisControler.drop(evt);};
        node.onmouseover = function(evt) {thisControler.highlightTarget(evt);};
        node.onmouseout = function(evt) {
                var target = getTargetedObject(evt);
-               if (target.nodeName == "A" && target.className == "dropPageTarget")
-                       target.className = "";
+               if (target.nodeName === "A" && target.className === "dropPageTarget"){
+                       target.className = "";}
        };
-}
+};
 
 DropTarget.prototype.drop = function(evt) {
        var target = getTargetedObject(evt);
-       if (target.nodeName == "A" &&
-               target.className != "previous" && 
-               target.className != "next") {
-               var pageNumber = parseInt(target.innerHTML);
+       if (target.nodeName === "A" &&
+               target.className !== "previous" && 
+               target.className !== "next") {
+               var pageNumber = parseInt(target.innerHTML, 10);
                var targetRow = this.folderDDControler.targetRow;
                if ( !isNaN(pageNumber) && targetRow) {
                        this.folderDDControler.reset();
-               var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
-               var url = this.folderDDControler.folderUrl + "/moveObjectIdToPosition";
-               var form ="object_id=" + object_id + "&position:int=" + String(this.batchSize * (pageNumber-1));
+                       var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
+                       var url = this.folderDDControler.folderUrl + "/moveObjectIdToPosition";
+                       var form ="object_id=" + object_id + "&position:int=" + String(this.batchSize * (pageNumber-1));
                        // send request
                        var req = new XMLHttpRequest();
                        req.open("POST", url, true);
                        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
                        req.send(form);
                        req.onreadystatechange = function() {
-                               if (req.readyState == 4) {
+                               if (req.readyState === 4) {
                                        switch (req.status) {
                                                case 200:
                                                case 204:
@@ -176,7 +226,7 @@ DropTarget.prototype.drop = function(evt) {
                                                        break;
                                                default:
                                                        alert('Error: ' + req.status);
-                                       };
+                                       }
                                }
                        };
                }
@@ -184,34 +234,35 @@ DropTarget.prototype.drop = function(evt) {
 };
 
 DropTarget.prototype.highlightTarget = function(evt){
-       if (browser.isIE) document.selection.clear();
+       if (browser.isIE) {document.selection.clear();}
        var target = getTargetedObject(evt);
        if (this.folderDDControler.targetRow &&
-               target.nodeName == "A" &&
-               target.className != "previous" &&
-               target.className != "next")
-               target.className = "dropPageTarget";
+               target.nodeName === "A" &&
+               target.className !== "previous" &&
+               target.className !== "next"){
+               target.className = "dropPageTarget";}
 };
 
 
 
 
-function loadListing(evt) {
+loadListing = function(evt) {
        var target = getTargetedObject(evt);
        disableDefault(evt);
        disablePropagation(evt);
+       var url;
        switch (target.nodeName) {
                case "A" :
-                       var parts = target.href.split('?')
-                       var url = parts[0];
+                       var parts = target.href.split('?');
+                       url = parts[0];
                        var query = '';
-                       if (parts.length == 2)
-                               query = parts[1];
+                       if (parts.length === 2){
+                               query = parts[1];}
                        
                        var urlParts = url.split("/");
-                       var url = urlParts.slice(0,urlParts.length-1).join("/");
-                       if (query.search("template") == -1)
-                               query += "&template=folder_contents_macros&macro=FolderListing&fragmentId=FolderListing";
+                       url = urlParts.slice(0,urlParts.length-1).join("/");
+                       if (query.search("template") === -1){
+                               query += "&template=folder_contents_macros&macro=FolderListing&fragmentId=FolderListing";}
                        url = url + "/folder_contents?" + query;
                        
                        var fi = new FragmentImporter(url);
@@ -219,9 +270,9 @@ function loadListing(evt) {
                        break;
 
                case "IMG" :
-                       if (target.id == 'SetSortingAsDefault') {
+                       if (target.id === 'SetSortingAsDefault') {
                                var parent = target.parentNode;
-                               var url = parent.href;
+                               url = parent.href;
                                url = url.replace("folder_contents", "folder_sort_control");
                                parent.parentNode.removeChild(parent);
                                
@@ -232,4 +283,93 @@ function loadListing(evt) {
                        break;
        }
        return false;
-}
+};
+
+DDFolderUploader = function(dropbox, uploadUrl, listing) {
+       DDFileUploaderBase.apply(this, [dropbox, uploadUrl]);
+       this.listing = listing;
+       this.progressBarMaxSize = listing.clientWidth;
+       var thead = listing;
+       do {
+               thead = thead.previousSibling;
+       } while (thead.tagName !== 'THEAD');
+
+       var cells = thead.getElementsByTagName('th');
+       var cell, i;
+       this.tableSpan = 0;
+       for (i=0 ; i < cells.length ; i++) {
+               cell = cells[i];
+               this.tableSpan += cell.getAttribute('colspan') ? Number(cell.getAttribute('colspan')) : 1;
+       }
+       var lastRow = listing.lastChild;
+       while(lastRow && lastRow.tagName !== 'TR') {
+               lastRow = lastRow.previousSibling;
+       }
+       this.lastRowClassName = lastRow ? lastRow.className : 'even';
+};
+
+copyPrototype(DDFolderUploader, DDFileUploaderBase);
+
+
+DDFolderUploader.prototype.createRow = function(file) {
+       var row = document.createElement('tr');
+       row.file = file;
+       row.className = this.lastRowClassName === 'even' ? 'odd' : 'even';
+       this.lastRowClassName = row.className;
+       var td = document.createElement('td');
+       td.setAttribute('colspan', this.tableSpan);
+       var relSpan = document.createElement('span');
+       relSpan.style.position = 'relative';
+       td.appendChild(relSpan);
+       var progressBar = document.createElement('span');
+       progressBar.className = 'upload-progress';
+       row.progressBar = progressBar;
+       relSpan.appendChild(progressBar);
+       var fileNameSpan = document.createElement('span');
+       fileNameSpan.innerHTML = file.name;
+       td.appendChild(fileNameSpan);
+       row.appendChild(td);
+       this.listing.appendChild(row);
+       this.progressBarMaxSize = row.clientWidth;
+       return row;
+};
+
+// Methods about upload
+DDFolderUploader.prototype.handleFiles = function(files) {
+       var file, i, row;
+       for (i = 0; i < files.length; i++) {
+               file = files[i];
+               row = this.createRow(file);
+               this.uploadQueuePush(row);
+       }
+};
+
+DDFolderUploader.prototype.beforeUpload = function(item) {
+       this.uploadedItem = item;
+       this.progressBar = item.progressBar;
+};
+
+DDFolderUploader.prototype.uploadCompleteHandlerCB = function(req) {
+       var item = this.uploadedItem;
+       var row = getCopyOfNode(req.responseXML.documentElement.firstChild);
+       row.className = item.className;
+
+       if (req.status === 200) {
+               // update
+               console.log('todo');
+       }
+       else if(req.status === 201) {
+               // creation
+               this.listing.replaceChild(row, item);
+               this.progressBarMaxSize = row.clientWidth;
+       }
+};
+
+DDFolderUploader.prototype.progressHandlerCB = function(progress) {
+       // 0 <= progress <= 1
+       var size = this.progressBarMaxSize * progress;
+       size = Math.round(size);
+       this.progressBar.style.width = size + 'px';
+};
+
+}());
\ No newline at end of file