1 // (c) Benoît PIN 2006-2007
7 var FolderDDropControler
;
14 function getTargetRow(evt
){
15 var target
= getTargetedObject(evt
);
16 while (target
.nodeName
!== "TR") {
17 target
= target
.parentNode
; }
21 FolderDDropControler = function(listing
, firstItemPos
) {
22 this.folderUrl
= document
.getElementById("FolderUrl").innerHTML
;
23 this.targetRow
= null;
24 this.lastOverPosition
= null;
25 this.prevDirUp
= null;
27 this.listing
= listing
;
28 this.firstItemPos
= firstItemPos
;
29 this._updatePositions();
30 var thisControler
= this;
31 this.listing
.onmousedown = function(evt
) {thisControler
.drag(evt
);};
32 this.listing
.onmouseover = function(evt
) {thisControler
.moveRow(evt
);};
33 this.listing
.onmouseup = function(evt
) {thisControler
.drop(evt
);};
34 addListener(this.listing
, 'click', function(evt
) {thisControler
.disableClickAfterDrop(evt
);});
35 addListener(this.listing
, 'click', function(evt
) {thisControler
.selectCBRange(evt
);});
38 this.listing
.ondragstart = function() { window
.event
.returnValue
= false;};
42 FolderDDropControler
.prototype._updatePositions = function() {
43 var rows
= this.listing
.getElementsByTagName("TR");
45 for (i
=0 ; i
< rows
.length
; i
++ ) {
46 rows
[i
].pos
= i
+ this.firstItemPos
; }
50 FolderDDropControler
.prototype.drag = function(evt
){
51 var target
= getTargetedObject(evt
);
52 if (target
.nodeName
=== "INPUT") { return true; }
54 var targetRow
= getTargetRow(evt
);
55 targetRow
.style
.backgroundColor
= base_properties
.highLightColor
;
56 this.listing
.style
.cursor
= "move";
57 this.targetRow
= targetRow
;
58 this.lastOverPosition
= targetRow
.pos
;
61 FolderDDropControler
.prototype.moveRow = function(evt
){
62 var targetRow
= this.targetRow
;
63 if (targetRow
!== null) {
65 if (browser
.isIE
) {document
.selection
.clear();}
66 var overRow
= getTargetRow(evt
);
68 if (overRow
.pos
=== targetRow
.pos
) {return;}
70 if (this.lastOverPosition
< overRow
.pos
) { // move up
71 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
72 this.prevDirUp
= true;
73 this.lastOverPosition
= overRow
.pos
;
75 else if (this.lastOverPosition
> overRow
.pos
) { // move down
76 this.listing
.insertBefore(targetRow
, overRow
);
77 this.prevDirUp
= false;
78 this.lastOverPosition
= overRow
.pos
;
82 this.prevDirUp
= false;
83 this.listing
.insertBefore(targetRow
, overRow
);
86 this.prevDirUp
= true;
87 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
93 FolderDDropControler
.prototype.drop = function(evt
){
94 var targetRow
= this.targetRow
;
95 if (targetRow
!== null) {
96 targetRow
.style
.backgroundColor
="";
97 this.listing
.style
.cursor
= "";
98 var thisControler
= this;
100 setTimeout(function(){thisControler
.reset();}, 50);
103 if (this.lastOverPosition
!== null) {
104 // get new object position.
106 if (targetRow
.pos
< this.lastOverPosition
&& !this.prevDirUp
) {
108 else if (targetRow
.pos
> this.lastOverPosition
&& this.prevDirUp
) {
112 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
113 var url
= this.folderUrl
+ "/moveObjectIdToPosition";
114 var form
="object_id=" + object_id
+ "&position:int=" +
115 String(this.lastOverPosition
- 1 + trim
);
117 // reinitialize positions
118 var rows
= this.listing
.getElementsByTagName("TR"), row
;
120 for (i
= 0 ; i
< rows
.length
; i
++) {
124 row
.className
= "even";}
126 row
.className
= "odd";}
130 var req
= new XMLHttpRequest();
131 req
.open("POST", url
, true);
132 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
134 setTimeout(function(){thisControler
.reset();}, 50);
139 FolderDDropControler
.prototype.disableClickAfterDrop = function(evt
) {
140 var target
= getTargetedObject(evt
);
142 disablePropagation(evt
);
148 FolderDDropControler
.prototype.selectCBRange = function(evt
) {
152 FolderDDropControler
.prototype.reset = function() {
153 this.targetRow
= null;
154 this.lastOverPosition
= null;
155 this.prevDirUp
= null;
161 DropTarget = function(node
, folderDDControler
) {
162 this.folderDDControler
= folderDDControler
;
163 this.batchSize
= parseInt(document
.getElementById("BatchNavigationSize").innerHTML
, 10);
164 var thisControler
= this;
165 node
.onmouseup = function(evt
){thisControler
.drop(evt
);};
166 node
.onmouseover = function(evt
) {thisControler
.highlightTarget(evt
);};
167 node
.onmouseout = function(evt
) {
168 var target
= getTargetedObject(evt
);
169 if (target
.nodeName
=== "A" && target
.className
=== "dropPageTarget"){
170 target
.className
= "";}
174 DropTarget
.prototype.drop = function(evt
) {
175 var target
= getTargetedObject(evt
);
176 if (target
.nodeName
=== "A" &&
177 target
.className
!== "previous" &&
178 target
.className
!== "next") {
179 var pageNumber
= parseInt(target
.innerHTML
, 10);
180 var targetRow
= this.folderDDControler
.targetRow
;
181 if ( !isNaN(pageNumber
) && targetRow
) {
182 this.folderDDControler
.reset();
183 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
184 var url
= this.folderDDControler
.folderUrl
+ "/moveObjectIdToPosition";
185 var form
="object_id=" + object_id
+ "&position:int=" + String(this.batchSize
* (pageNumber
-1));
187 var req
= new XMLHttpRequest();
188 req
.open("POST", url
, true);
189 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
191 req
.onreadystatechange = function() {
192 if (req
.readyState
=== 4) {
193 switch (req
.status
) {
197 raiseMouseEvent(target
, "click");
200 alert('Error: ' + req
.status
);
208 DropTarget
.prototype.highlightTarget = function(evt
){
209 if (browser
.isIE
) {document
.selection
.clear();}
210 var target
= getTargetedObject(evt
);
211 if (this.folderDDControler
.targetRow
&&
212 target
.nodeName
=== "A" &&
213 target
.className
!== "previous" &&
214 target
.className
!== "next"){
215 target
.className
= "dropPageTarget";}
221 loadListing = function(evt
) {
222 var target
= getTargetedObject(evt
);
224 disablePropagation(evt
);
226 switch (target
.nodeName
) {
228 var parts
= target
.href
.split('?');
231 if (parts
.length
=== 2){
234 var urlParts
= url
.split("/");
235 url
= urlParts
.slice(0,urlParts
.length
-1).join("/");
236 if (query
.search("template") === -1){
237 query
+= "&template=folder_contents_macros¯o=FolderListing&fragmentId=FolderListing";}
238 url
= url
+ "/folder_contents?" + query
;
240 var fi
= new FragmentImporter(url
);
245 if (target
.id
=== 'SetSortingAsDefault') {
246 var parent
= target
.parentNode
;
248 url
= url
.replace("folder_contents", "folder_sort_control");
249 parent
.parentNode
.removeChild(parent
);
251 var req
= new XMLHttpRequest();
252 req
.open("GET", url
, true);
260 DDFolderUploader = function(dropbox
, uploadUrl
, listing
) {
261 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
262 this.listing
= listing
;
263 this.progressBarMaxSize
= listing
.clientWidth
;
266 thead
= thead
.previousSibling
;
267 } while (thead
.tagName
!== 'THEAD');
269 var cells
= thead
.getElementsByTagName('th');
272 for (i
=0 ; i
< cells
.length
; i
++) {
274 this.tableSpan
+= cell
.getAttribute('colspan') ? Number(cell
.getAttribute('colspan')) : 1;
276 var lastRow
= listing
.lastChild
;
277 while(lastRow
&& lastRow
.tagName
!== 'TR') {
278 lastRow
= lastRow
.previousSibling
;
280 this.lastRowClassName
= lastRow
? lastRow
.className
: 'even';
283 copyPrototype(DDFolderUploader
, DDFileUploaderBase
);
286 DDFolderUploader
.prototype.createRow = function(file
) {
287 var row
= document
.createElement('tr');
289 row
.className
= this.lastRowClassName
=== 'even' ? 'odd' : 'even';
290 this.lastRowClassName
= row
.className
;
291 var td
= document
.createElement('td');
292 td
.setAttribute('colspan', this.tableSpan
);
293 var relSpan
= document
.createElement('span');
294 relSpan
.style
.position
= 'relative';
295 td
.appendChild(relSpan
);
296 var progressBar
= document
.createElement('span');
297 progressBar
.className
= 'upload-progress';
298 row
.progressBar
= progressBar
;
299 relSpan
.appendChild(progressBar
);
300 var fileNameSpan
= document
.createElement('span');
301 fileNameSpan
.innerHTML
= file
.name
;
302 td
.appendChild(fileNameSpan
);
304 this.listing
.appendChild(row
);
305 this.progressBarMaxSize
= row
.clientWidth
;
309 // Methods about upload
310 DDFolderUploader
.prototype.handleFiles = function(files
) {
312 for (i
= 0; i
< files
.length
; i
++) {
314 row
= this.createRow(file
);
315 this.uploadQueuePush(row
);
319 DDFolderUploader
.prototype.beforeUpload = function(item
) {
320 this.uploadedItem
= item
;
321 this.progressBar
= item
.progressBar
;
324 DDFolderUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
325 var item
= this.uploadedItem
;
326 var row
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
327 row
.className
= item
.className
;
329 if (req
.status
=== 200) {
333 else if(req
.status
=== 201) {
335 this.listing
.replaceChild(row
, item
);
336 this.progressBarMaxSize
= row
.clientWidth
;
340 DDFolderUploader
.prototype.progressHandlerCB = function(progress
) {
341 // 0 <= progress <= 1
342 var size
= this.progressBarMaxSize
* progress
;
343 size
= Math
.round(size
);
344 this.progressBar
.style
.width
= size
+ 'px';