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
, orderable
, 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 this.lastCBChecked
= undefined;
31 var thisControler
= this;
33 this.listing
.onmousedown = function(evt
) {thisControler
.drag(evt
);};
34 this.listing
.onmouseover = function(evt
) {thisControler
.moveRow(evt
);};
35 this.listing
.onmouseup = function(evt
) {thisControler
.drop(evt
);};
36 addListener(this.listing
, 'click', function(evt
) {thisControler
.disableClickAfterDrop(evt
);});
38 addListener(this.listing
, 'click', function(evt
) {thisControler
.selectCBRange(evt
);});
41 this.listing
.ondragstart = function() { window
.event
.returnValue
= false;};
45 FolderDDropControler
.prototype._updatePositions = function() {
46 var rows
= this.listing
.getElementsByTagName("TR");
48 for (i
=0 ; i
< rows
.length
; i
++ ) {
50 row
.pos
= i
+ this.firstItemPos
;
52 row
.className
= "even"; }
54 row
.className
= "odd"; }
58 FolderDDropControler
.prototype.drag = function(evt
){
59 var target
= getTargetedObject(evt
);
60 if (target
.nodeName
=== "INPUT") { return true; }
62 var targetRow
= getTargetRow(evt
);
63 targetRow
.style
.backgroundColor
= base_properties
.highLightColor
;
64 this.listing
.style
.cursor
= "move";
65 this.targetRow
= targetRow
;
66 this.lastOverPosition
= targetRow
.pos
;
69 FolderDDropControler
.prototype.moveRow = function(evt
){
70 var targetRow
= this.targetRow
;
71 if (targetRow
!== null) {
73 if (browser
.isIE
) {document
.selection
.clear();}
74 var overRow
= getTargetRow(evt
);
76 if (overRow
.pos
=== targetRow
.pos
) {return;}
78 if (this.lastOverPosition
< overRow
.pos
) { // move up
79 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
80 this.prevDirUp
= true;
81 this.lastOverPosition
= overRow
.pos
;
83 else if (this.lastOverPosition
> overRow
.pos
) { // move down
84 this.listing
.insertBefore(targetRow
, overRow
);
85 this.prevDirUp
= false;
86 this.lastOverPosition
= overRow
.pos
;
90 this.prevDirUp
= false;
91 this.listing
.insertBefore(targetRow
, overRow
);
94 this.prevDirUp
= true;
95 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
101 FolderDDropControler
.prototype.drop = function(evt
){
102 var targetRow
= this.targetRow
;
103 if (targetRow
!== null) {
104 targetRow
.style
.backgroundColor
="";
105 this.listing
.style
.cursor
= "";
106 var thisControler
= this;
108 setTimeout(function(){thisControler
.reset();}, 50);
111 if (this.lastOverPosition
!== null) {
112 // get new object position.
114 if (targetRow
.pos
< this.lastOverPosition
&& !this.prevDirUp
) {
116 else if (targetRow
.pos
> this.lastOverPosition
&& this.prevDirUp
) {
120 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
121 var url
= this.folderUrl
+ "/moveObjectIdToPosition";
122 var form
="object_id=" + object_id
+ "&position:int=" +
123 String(this.lastOverPosition
- 1 + trim
);
125 // reinitialize positions
126 this._updatePositions();
129 var req
= new XMLHttpRequest();
130 req
.open("POST", url
, true);
131 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
133 setTimeout(function(){thisControler
.reset();}, 50);
138 FolderDDropControler
.prototype.disableClickAfterDrop = function(evt
) {
139 var target
= getTargetedObject(evt
);
141 disablePropagation(evt
);
147 FolderDDropControler
.prototype.selectCBRange = function(evt
) {
148 var target
= getTargetedObject(evt
);
149 if (target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
150 evt
= getEventObject(evt
);
151 var shift
= evt
.shiftKey
;
152 if (shift
&& this.lastCBChecked
) {
153 var from = this.getCBIndex(this.lastCBChecked
);
154 var to
= this.getCBIndex(target
);
155 var rows
= this.listing
.getElementsByTagName('TR');
156 var start
= Math
.min(from, to
);
157 var stop
= Math
.max(from, to
);
159 for (i
=start
; i
<stop
; i
++ ) {
160 rows
[i
].getElementsByTagName('INPUT')[0].checked
= true;
163 else if (target
.checked
) {
164 this.lastCBChecked
= target
;
167 this.lastCBChecked
= undefined;
172 FolderDDropControler
.prototype.getCBIndex = function(cb
) {
173 var row
= cb
.parentNode
;
174 while(row
.tagName
!== 'TR') {
175 row
= row
.parentNode
;
177 return row
.pos
- this.firstItemPos
;
180 FolderDDropControler
.prototype.reset = function() {
181 this.targetRow
= null;
182 this.lastOverPosition
= null;
183 this.prevDirUp
= null;
189 DropTarget = function(node
, folderDDControler
) {
190 this.folderDDControler
= folderDDControler
;
191 this.batchSize
= parseInt(document
.getElementById("BatchNavigationSize").innerHTML
, 10);
192 var thisControler
= this;
193 node
.onmouseup = function(evt
){thisControler
.drop(evt
);};
194 node
.onmouseover = function(evt
) {thisControler
.highlightTarget(evt
);};
195 node
.onmouseout = function(evt
) {
196 var target
= getTargetedObject(evt
);
197 if (target
.nodeName
=== "A" && target
.className
=== "dropPageTarget"){
198 target
.className
= "";}
202 DropTarget
.prototype.drop = function(evt
) {
203 var target
= getTargetedObject(evt
);
204 if (target
.nodeName
=== "A" &&
205 target
.className
!== "previous" &&
206 target
.className
!== "next") {
207 var pageNumber
= parseInt(target
.innerHTML
, 10);
208 var targetRow
= this.folderDDControler
.targetRow
;
209 if ( !isNaN(pageNumber
) && targetRow
) {
210 this.folderDDControler
.reset();
211 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
212 var url
= this.folderDDControler
.folderUrl
+ "/moveObjectIdToPosition";
213 var form
="object_id=" + object_id
+ "&position:int=" + String(this.batchSize
* (pageNumber
-1));
215 var req
= new XMLHttpRequest();
216 req
.open("POST", url
, true);
217 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
219 req
.onreadystatechange = function() {
220 if (req
.readyState
=== 4) {
221 switch (req
.status
) {
225 raiseMouseEvent(target
, "click");
228 alert('Error: ' + req
.status
);
236 DropTarget
.prototype.highlightTarget = function(evt
){
237 if (browser
.isIE
) {document
.selection
.clear();}
238 var target
= getTargetedObject(evt
);
239 if (this.folderDDControler
.targetRow
&&
240 target
.nodeName
=== "A" &&
241 target
.className
!== "previous" &&
242 target
.className
!== "next"){
243 target
.className
= "dropPageTarget";}
249 loadListing = function(evt
) {
250 var target
= getTargetedObject(evt
);
252 disablePropagation(evt
);
254 switch (target
.nodeName
) {
256 var parts
= target
.href
.split('?');
259 if (parts
.length
=== 2){
262 var urlParts
= url
.split("/");
263 url
= urlParts
.slice(0,urlParts
.length
-1).join("/");
264 if (query
.search("template") === -1){
265 query
+= "&template=folder_contents_macros¯o=FolderListing&fragmentId=FolderListing";}
266 url
= url
+ "/folder_contents?" + query
;
268 var fi
= new FragmentImporter(url
);
273 if (target
.id
=== 'SetSortingAsDefault') {
274 var parent
= target
.parentNode
;
276 url
= url
.replace("folder_contents", "folder_sort_control");
277 parent
.parentNode
.removeChild(parent
);
279 var req
= new XMLHttpRequest();
280 req
.open("GET", url
, true);
288 DDFolderUploader = function(dropbox
, uploadUrl
, listing
) {
289 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
290 this.listing
= listing
;
291 this.progressBarMaxSize
= listing
.clientWidth
;
294 thead
= thead
.previousSibling
;
295 } while (thead
.tagName
!== 'THEAD');
297 var cells
= thead
.getElementsByTagName('th');
300 for (i
=0 ; i
< cells
.length
; i
++) {
302 this.tableSpan
+= cell
.getAttribute('colspan') ? Number(cell
.getAttribute('colspan')) : 1;
304 var lastRow
= listing
.lastChild
;
305 while(lastRow
&& lastRow
.tagName
!== 'TR') {
306 lastRow
= lastRow
.previousSibling
;
308 this.lastRowClassName
= lastRow
? lastRow
.className
: 'even';
311 copyPrototype(DDFolderUploader
, DDFileUploaderBase
);
314 DDFolderUploader
.prototype.createRow = function(file
) {
315 var row
= document
.createElement('tr');
317 row
.className
= this.lastRowClassName
=== 'even' ? 'odd' : 'even';
318 this.lastRowClassName
= row
.className
;
319 var td
= document
.createElement('td');
320 td
.setAttribute('colspan', this.tableSpan
);
321 var relSpan
= document
.createElement('span');
322 relSpan
.style
.position
= 'relative';
323 td
.appendChild(relSpan
);
324 var progressBar
= document
.createElement('span');
325 progressBar
.className
= 'upload-progress';
326 row
.progressBar
= progressBar
;
327 relSpan
.appendChild(progressBar
);
328 var fileNameSpan
= document
.createElement('span');
329 fileNameSpan
.innerHTML
= file
.name
;
330 td
.appendChild(fileNameSpan
);
332 this.listing
.appendChild(row
);
333 this.progressBarMaxSize
= row
.clientWidth
;
337 // Methods about upload
338 DDFolderUploader
.prototype.handleFiles = function(files
) {
340 for (i
= 0; i
< files
.length
; i
++) {
342 row
= this.createRow(file
);
343 this.uploadQueuePush(row
);
347 DDFolderUploader
.prototype.beforeUpload = function(item
) {
348 this.uploadedItem
= item
;
349 this.progressBar
= item
.progressBar
;
352 DDFolderUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
353 var item
= this.uploadedItem
;
354 var row
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
355 row
.className
= item
.className
;
357 if (req
.status
=== 200) {
361 else if(req
.status
=== 201) {
363 this.listing
.replaceChild(row
, item
);
364 this.progressBarMaxSize
= row
.clientWidth
;
368 DDFolderUploader
.prototype.progressHandlerCB = function(progress
) {
369 // 0 <= progress <= 1
370 var size
= this.progressBarMaxSize
* progress
;
371 size
= Math
.round(size
);
372 this.progressBar
.style
.width
= size
+ 'px';