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
) {
22 this.folderUrl
= document
.getElementById("FolderUrl").innerHTML
;
23 this.targetRow
= null;
24 this.lastOverPosition
= null;
25 this.prevDirUp
= null;
27 this.listing
= listing
;
28 var thisControler
= this;
29 this.listing
.onmousedown = function(evt
) {thisControler
.drag(evt
);};
30 this.listing
.onmouseover = function(evt
) {thisControler
.moveRow(evt
);};
31 this.listing
.onmouseup = function(evt
) {thisControler
.drop(evt
);};
32 addListener(this.listing
, 'click', function(evt
) {thisControler
.disableClickAfterDrop(evt
);});
33 addListener(this.listing
, 'click', function(evt
) {thisControler
.selectCBRange(evt
);});
36 this.listing
.ondragstart = function() { window
.event
.returnValue
= false;};
40 FolderDDropControler
.prototype.drag = function(evt
){
41 var target
= getTargetedObject(evt
);
42 if (target
.nodeName
== "INPUT") return true;
44 var targetRow
= getTargetRow(evt
);
45 targetRow
.style
.backgroundColor
= base_properties
["highLightColor"];
46 this.listing
.style
.cursor
= "move";
47 this.targetRow
= targetRow
;
48 this.lastOverPosition
= targetRow
.pos
;
51 FolderDDropControler
.prototype.moveRow = function(evt
){
52 var targetRow
= this.targetRow
;
53 if (targetRow
!= null) {
55 if (browser
.isIE
) document
.selection
.clear();
56 var overRow
= getTargetRow(evt
);
58 if (overRow
.pos
== targetRow
.pos
) return;
60 if (this.lastOverPosition
< overRow
.pos
) { // move up
61 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
62 this.prevDirUp
= true;
63 this.lastOverPosition
= overRow
.pos
;
65 else if (this.lastOverPosition
> overRow
.pos
) { // move down
66 this.listing
.insertBefore(targetRow
, overRow
);
67 this.prevDirUp
= false;
68 this.lastOverPosition
= overRow
.pos
;
72 this.prevDirUp
= false;
73 this.listing
.insertBefore(targetRow
, overRow
);
76 this.prevDirUp
= true;
77 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
83 FolderDDropControler
.prototype.drop = function(evt
){
84 var targetRow
= this.targetRow
;
85 if (targetRow
!= null) {
86 targetRow
.style
.backgroundColor
="";
87 this.listing
.style
.cursor
= "";
89 var thisControler
= this;
90 setTimeout(function(){thisControler
.reset();}, 50);
93 if (this.lastOverPosition
!= null) {
94 // get new object position.
96 if (targetRow
.pos
< this.lastOverPosition
&& !this.prevDirUp
)
98 else if (targetRow
.pos
> this.lastOverPosition
&& this.prevDirUp
)
102 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
103 var url
= this.folderUrl
+ "/moveObjectIdToPosition";
104 var form
="object_id=" + object_id
+ "&position:int=" +
105 String(this.lastOverPosition
- 1 + trim
);
107 // reinitialize positions
108 var rows
= this.listing
.getElementsByTagName("TR"), row
;
109 for (var i
= 0 ; i
< rows
.length
; i
++) {
113 row
.className
= "even";
115 row
.className
= "odd";
119 var req
= new XMLHttpRequest();
120 req
.open("POST", url
, true);
121 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
123 var thisControler
= this;
124 setTimeout(function(){thisControler
.reset();}, 50);
129 FolderDDropControler
.prototype.disableClickAfterDrop = function(evt
) {
130 var target
= getTargetedObject(evt
);
132 disablePropagation(evt
);
138 FolderDDropControler
.prototype.selectCBRange = function(evt
) {
142 FolderDDropControler
.prototype.reset = function() {
143 this.targetRow
= null;
144 this.lastOverPosition
= null;
145 this.prevDirUp
= null;
151 DropTarget = function(node
, folderDDControler
) {
152 this.folderDDControler
= folderDDControler
;
153 this.batchSize
= parseInt(document
.getElementById("BatchNavigationSize").innerHTML
);
154 var thisControler
= this;
155 node
.onmouseup = function(evt
){thisControler
.drop(evt
);};
156 node
.onmouseover = function(evt
) {thisControler
.highlightTarget(evt
);};
157 node
.onmouseout = function(evt
) {
158 var target
= getTargetedObject(evt
);
159 if (target
.nodeName
== "A" && target
.className
== "dropPageTarget")
160 target
.className
= "";
164 DropTarget
.prototype.drop = function(evt
) {
165 var target
= getTargetedObject(evt
);
166 if (target
.nodeName
== "A" &&
167 target
.className
!= "previous" &&
168 target
.className
!= "next") {
169 var pageNumber
= parseInt(target
.innerHTML
);
170 var targetRow
= this.folderDDControler
.targetRow
;
171 if ( !isNaN(pageNumber
) && targetRow
) {
172 this.folderDDControler
.reset();
173 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
174 var url
= this.folderDDControler
.folderUrl
+ "/moveObjectIdToPosition";
175 var form
="object_id=" + object_id
+ "&position:int=" + String(this.batchSize
* (pageNumber
-1));
177 var req
= new XMLHttpRequest();
178 req
.open("POST", url
, true);
179 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
181 req
.onreadystatechange = function() {
182 if (req
.readyState
== 4) {
183 switch (req
.status
) {
187 raiseMouseEvent(target
, "click");
190 alert('Error: ' + req
.status
);
198 DropTarget
.prototype.highlightTarget = function(evt
){
199 if (browser
.isIE
) document
.selection
.clear();
200 var target
= getTargetedObject(evt
);
201 if (this.folderDDControler
.targetRow
&&
202 target
.nodeName
== "A" &&
203 target
.className
!= "previous" &&
204 target
.className
!= "next")
205 target
.className
= "dropPageTarget";
211 loadListing = function(evt
) {
212 var target
= getTargetedObject(evt
);
214 disablePropagation(evt
);
215 switch (target
.nodeName
) {
217 var parts
= target
.href
.split('?')
220 if (parts
.length
== 2)
223 var urlParts
= url
.split("/");
224 var url
= urlParts
.slice(0,urlParts
.length
-1).join("/");
225 if (query
.search("template") == -1)
226 query
+= "&template=folder_contents_macros¯o=FolderListing&fragmentId=FolderListing";
227 url
= url
+ "/folder_contents?" + query
;
229 var fi
= new FragmentImporter(url
);
234 if (target
.id
== 'SetSortingAsDefault') {
235 var parent
= target
.parentNode
;
236 var url
= parent
.href
;
237 url
= url
.replace("folder_contents", "folder_sort_control");
238 parent
.parentNode
.removeChild(parent
);
240 var req
= new XMLHttpRequest();
241 req
.open("GET", url
, true);
249 DDFolderUploader = function(dropbox
, uploadUrl
, listing
) {
250 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
251 this.listing
= listing
;
252 this.progressBarMaxSize
= listing
.clientWidth
;
255 thead
= thead
.previousSibling
;
256 } while (thead
.tagName
!== 'THEAD')
258 var cells
= thead
.getElementsByTagName('th');
261 for (var i
= 0 ; i
< cells
.length
; i
++) {
263 this.tableSpan
+= cell
.getAttribute('colspan') ? Number(cell
.getAttribute('colspan')) : 1;
265 var lastRow
= listing
.lastChild
;
266 while(lastRow
&& lastRow
.tagName
!== 'TR') {
267 lastRow
= lastRow
.previousSibling
;
269 this.lastRowClassName
= lastRow
? lastRow
.className
: 'even';
272 copyPrototype(DDFolderUploader
, DDFileUploaderBase
);
275 DDFolderUploader
.prototype.createRow = function(file
) {
276 var row
= document
.createElement('tr');
278 row
.className
= this.lastRowClassName
=== 'even' ? 'odd' : 'even';
279 this.lastRowClassName
= row
.className
;
280 var td
= document
.createElement('td');
281 td
.setAttribute('colspan', this.tableSpan
);
282 var relSpan
= document
.createElement('span');
283 relSpan
.style
.position
= 'relative';
284 td
.appendChild(relSpan
);
285 var progressBar
= document
.createElement('span');
286 progressBar
.className
= 'upload-progress';
287 row
.progressBar
= progressBar
;
288 relSpan
.appendChild(progressBar
);
289 var fileNameSpan
= document
.createElement('span');
290 fileNameSpan
.innerHTML
= file
.name
;
291 td
.appendChild(fileNameSpan
);
293 this.listing
.appendChild(row
);
294 this.progressBarMaxSize
= row
.clientWidth
;
298 // Methods about upload
299 DDFolderUploader
.prototype.handleFiles = function(files
) {
301 for (i
= 0; i
< files
.length
; i
++) {
303 row
= this.createRow(file
);
304 this.uploadQueuePush(row
);
308 DDFolderUploader
.prototype.beforeUpload = function(item
) {
309 this.uploadedItem
= item
;
310 this.progressBar
= item
.progressBar
;
313 DDFolderUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
314 var item
= this.uploadedItem
;
315 var row
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
316 row
.className
= item
.className
;
318 if (req
.status
=== 200) {
322 else if(req
.status
=== 201) {
324 this.listing
.replaceChild(row
, item
);
325 this.progressBarMaxSize
= row
.clientWidth
;
329 DDFolderUploader
.prototype.progressHandlerCB = function(progress
) {
330 // 0 <= progress <= 1
331 var size
= this.progressBarMaxSize
* progress
;
332 size
= Math
.round(size
);
333 this.progressBar
.style
.width
= size
+ 'px';