7edda7dadf86af84f4fcfa90b33c8b143b89c425
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 this.lastCBChecked
= undefined;
31 var thisControler
= this;
32 this.listing
.onmousedown = function(evt
) {thisControler
.drag(evt
);};
33 this.listing
.onmouseover = function(evt
) {thisControler
.moveRow(evt
);};
34 this.listing
.onmouseup = function(evt
) {thisControler
.drop(evt
);};
35 addListener(this.listing
, 'click', function(evt
) {thisControler
.disableClickAfterDrop(evt
);});
36 addListener(this.listing
, 'click', function(evt
) {thisControler
.selectCBRange(evt
);});
39 this.listing
.ondragstart = function() { window
.event
.returnValue
= false;};
43 FolderDDropControler
.prototype._updatePositions = function() {
44 var rows
= this.listing
.getElementsByTagName("TR");
46 for (i
=0 ; i
< rows
.length
; i
++ ) {
48 row
.pos
= i
+ this.firstItemPos
;
50 row
.className
= "even"; }
52 row
.className
= "odd"; }
56 FolderDDropControler
.prototype.drag = function(evt
){
57 var target
= getTargetedObject(evt
);
58 if (target
.nodeName
=== "INPUT") { return true; }
60 var targetRow
= getTargetRow(evt
);
61 targetRow
.style
.backgroundColor
= base_properties
.highLightColor
;
62 this.listing
.style
.cursor
= "move";
63 this.targetRow
= targetRow
;
64 this.lastOverPosition
= targetRow
.pos
;
67 FolderDDropControler
.prototype.moveRow = function(evt
){
68 var targetRow
= this.targetRow
;
69 if (targetRow
!== null) {
71 if (browser
.isIE
) {document
.selection
.clear();}
72 var overRow
= getTargetRow(evt
);
74 if (overRow
.pos
=== targetRow
.pos
) {return;}
76 if (this.lastOverPosition
< overRow
.pos
) { // move up
77 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
78 this.prevDirUp
= true;
79 this.lastOverPosition
= overRow
.pos
;
81 else if (this.lastOverPosition
> overRow
.pos
) { // move down
82 this.listing
.insertBefore(targetRow
, overRow
);
83 this.prevDirUp
= false;
84 this.lastOverPosition
= overRow
.pos
;
88 this.prevDirUp
= false;
89 this.listing
.insertBefore(targetRow
, overRow
);
92 this.prevDirUp
= true;
93 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
99 FolderDDropControler
.prototype.drop = function(evt
){
100 var targetRow
= this.targetRow
;
101 if (targetRow
!== null) {
102 targetRow
.style
.backgroundColor
="";
103 this.listing
.style
.cursor
= "";
104 var thisControler
= this;
106 setTimeout(function(){thisControler
.reset();}, 50);
109 if (this.lastOverPosition
!== null) {
110 // get new object position.
112 if (targetRow
.pos
< this.lastOverPosition
&& !this.prevDirUp
) {
114 else if (targetRow
.pos
> this.lastOverPosition
&& this.prevDirUp
) {
118 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
119 var url
= this.folderUrl
+ "/moveObjectIdToPosition";
120 var form
="object_id=" + object_id
+ "&position:int=" +
121 String(this.lastOverPosition
- 1 + trim
);
123 // reinitialize positions
124 this._updatePositions();
127 var req
= new XMLHttpRequest();
128 req
.open("POST", url
, true);
129 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
131 setTimeout(function(){thisControler
.reset();}, 50);
136 FolderDDropControler
.prototype.disableClickAfterDrop = function(evt
) {
137 var target
= getTargetedObject(evt
);
139 disablePropagation(evt
);
145 FolderDDropControler
.prototype.selectCBRange = function(evt
) {
146 var target
= getTargetedObject(evt
);
147 if (target
.tagName
=== 'INPUT' && target
.type
=== 'checkbox') {
148 evt
= getEventObject(evt
);
149 var shift
= evt
.shiftKey
;
150 if (shift
&& this.lastCBChecked
) {
151 var from = this.getCBIndex(this.lastCBChecked
);
152 var to
= this.getCBIndex(target
);
153 var rows
= this.listing
.getElementsByTagName('TR');
154 var start
= Math
.min(from, to
);
155 var stop
= Math
.max(from, to
);
157 for (i
=start
; i
<stop
; i
++ ) {
158 rows
[i
].getElementsByTagName('INPUT')[0].checked
= true;
161 else if (target
.checked
) {
162 this.lastCBChecked
= target
;
165 this.lastCBChecked
= undefined;
170 FolderDDropControler
.prototype.getCBIndex = function(cb
) {
171 var row
= cb
.parentNode
;
172 while(row
.tagName
!== 'TR') {
173 row
= row
.parentNode
;
175 return row
.pos
- this.firstItemPos
;
178 FolderDDropControler
.prototype.reset = function() {
179 this.targetRow
= null;
180 this.lastOverPosition
= null;
181 this.prevDirUp
= null;
187 DropTarget = function(node
, folderDDControler
) {
188 this.folderDDControler
= folderDDControler
;
189 this.batchSize
= parseInt(document
.getElementById("BatchNavigationSize").innerHTML
, 10);
190 var thisControler
= this;
191 node
.onmouseup = function(evt
){thisControler
.drop(evt
);};
192 node
.onmouseover = function(evt
) {thisControler
.highlightTarget(evt
);};
193 node
.onmouseout = function(evt
) {
194 var target
= getTargetedObject(evt
);
195 if (target
.nodeName
=== "A" && target
.className
=== "dropPageTarget"){
196 target
.className
= "";}
200 DropTarget
.prototype.drop = function(evt
) {
201 var target
= getTargetedObject(evt
);
202 if (target
.nodeName
=== "A" &&
203 target
.className
!== "previous" &&
204 target
.className
!== "next") {
205 var pageNumber
= parseInt(target
.innerHTML
, 10);
206 var targetRow
= this.folderDDControler
.targetRow
;
207 if ( !isNaN(pageNumber
) && targetRow
) {
208 this.folderDDControler
.reset();
209 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
210 var url
= this.folderDDControler
.folderUrl
+ "/moveObjectIdToPosition";
211 var form
="object_id=" + object_id
+ "&position:int=" + String(this.batchSize
* (pageNumber
-1));
213 var req
= new XMLHttpRequest();
214 req
.open("POST", url
, true);
215 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
217 req
.onreadystatechange = function() {
218 if (req
.readyState
=== 4) {
219 switch (req
.status
) {
223 raiseMouseEvent(target
, "click");
226 alert('Error: ' + req
.status
);
234 DropTarget
.prototype.highlightTarget = function(evt
){
235 if (browser
.isIE
) {document
.selection
.clear();}
236 var target
= getTargetedObject(evt
);
237 if (this.folderDDControler
.targetRow
&&
238 target
.nodeName
=== "A" &&
239 target
.className
!== "previous" &&
240 target
.className
!== "next"){
241 target
.className
= "dropPageTarget";}
247 loadListing = function(evt
) {
248 var target
= getTargetedObject(evt
);
250 disablePropagation(evt
);
252 switch (target
.nodeName
) {
254 var parts
= target
.href
.split('?');
257 if (parts
.length
=== 2){
260 var urlParts
= url
.split("/");
261 url
= urlParts
.slice(0,urlParts
.length
-1).join("/");
262 if (query
.search("template") === -1){
263 query
+= "&template=folder_contents_macros¯o=FolderListing&fragmentId=FolderListing";}
264 url
= url
+ "/folder_contents?" + query
;
266 var fi
= new FragmentImporter(url
);
271 if (target
.id
=== 'SetSortingAsDefault') {
272 var parent
= target
.parentNode
;
274 url
= url
.replace("folder_contents", "folder_sort_control");
275 parent
.parentNode
.removeChild(parent
);
277 var req
= new XMLHttpRequest();
278 req
.open("GET", url
, true);
286 DDFolderUploader = function(dropbox
, uploadUrl
, listing
) {
287 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
288 this.listing
= listing
;
289 this.progressBarMaxSize
= listing
.clientWidth
;
292 thead
= thead
.previousSibling
;
293 } while (thead
.tagName
!== 'THEAD');
295 var cells
= thead
.getElementsByTagName('th');
298 for (i
=0 ; i
< cells
.length
; i
++) {
300 this.tableSpan
+= cell
.getAttribute('colspan') ? Number(cell
.getAttribute('colspan')) : 1;
302 var lastRow
= listing
.lastChild
;
303 while(lastRow
&& lastRow
.tagName
!== 'TR') {
304 lastRow
= lastRow
.previousSibling
;
306 this.lastRowClassName
= lastRow
? lastRow
.className
: 'even';
309 copyPrototype(DDFolderUploader
, DDFileUploaderBase
);
312 DDFolderUploader
.prototype.createRow = function(file
) {
313 var row
= document
.createElement('tr');
315 row
.className
= this.lastRowClassName
=== 'even' ? 'odd' : 'even';
316 this.lastRowClassName
= row
.className
;
317 var td
= document
.createElement('td');
318 td
.setAttribute('colspan', this.tableSpan
);
319 var relSpan
= document
.createElement('span');
320 relSpan
.style
.position
= 'relative';
321 td
.appendChild(relSpan
);
322 var progressBar
= document
.createElement('span');
323 progressBar
.className
= 'upload-progress';
324 row
.progressBar
= progressBar
;
325 relSpan
.appendChild(progressBar
);
326 var fileNameSpan
= document
.createElement('span');
327 fileNameSpan
.innerHTML
= file
.name
;
328 td
.appendChild(fileNameSpan
);
330 this.listing
.appendChild(row
);
331 this.progressBarMaxSize
= row
.clientWidth
;
335 // Methods about upload
336 DDFolderUploader
.prototype.handleFiles = function(files
) {
338 for (i
= 0; i
< files
.length
; i
++) {
340 row
= this.createRow(file
);
341 this.uploadQueuePush(row
);
345 DDFolderUploader
.prototype.beforeUpload = function(item
) {
346 this.uploadedItem
= item
;
347 this.progressBar
= item
.progressBar
;
350 DDFolderUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
351 var item
= this.uploadedItem
;
352 var row
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
353 row
.className
= item
.className
;
355 if (req
.status
=== 200) {
359 else if(req
.status
=== 201) {
361 this.listing
.replaceChild(row
, item
);
362 this.progressBarMaxSize
= row
.clientWidth
;
366 DDFolderUploader
.prototype.progressHandlerCB = function(progress
) {
367 // 0 <= progress <= 1
368 var size
= this.progressBarMaxSize
* progress
;
369 size
= Math
.round(size
);
370 this.progressBar
.style
.width
= size
+ 'px';