800a746acd06149955513e1c585dd1baa3367b1e
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
++ ) {
47 row
.pos
= i
+ this.firstItemPos
;
49 row
.className
= "even"; }
51 row
.className
= "odd"; }
55 FolderDDropControler
.prototype.drag = function(evt
){
56 var target
= getTargetedObject(evt
);
57 if (target
.nodeName
=== "INPUT") { return true; }
59 var targetRow
= getTargetRow(evt
);
60 targetRow
.style
.backgroundColor
= base_properties
.highLightColor
;
61 this.listing
.style
.cursor
= "move";
62 this.targetRow
= targetRow
;
63 this.lastOverPosition
= targetRow
.pos
;
66 FolderDDropControler
.prototype.moveRow = function(evt
){
67 var targetRow
= this.targetRow
;
68 if (targetRow
!== null) {
70 if (browser
.isIE
) {document
.selection
.clear();}
71 var overRow
= getTargetRow(evt
);
73 if (overRow
.pos
=== targetRow
.pos
) {return;}
75 if (this.lastOverPosition
< overRow
.pos
) { // move up
76 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
77 this.prevDirUp
= true;
78 this.lastOverPosition
= overRow
.pos
;
80 else if (this.lastOverPosition
> overRow
.pos
) { // move down
81 this.listing
.insertBefore(targetRow
, overRow
);
82 this.prevDirUp
= false;
83 this.lastOverPosition
= overRow
.pos
;
87 this.prevDirUp
= false;
88 this.listing
.insertBefore(targetRow
, overRow
);
91 this.prevDirUp
= true;
92 this.listing
.insertBefore(targetRow
, overRow
.nextSibling
);
98 FolderDDropControler
.prototype.drop = function(evt
){
99 var targetRow
= this.targetRow
;
100 if (targetRow
!== null) {
101 targetRow
.style
.backgroundColor
="";
102 this.listing
.style
.cursor
= "";
103 var thisControler
= this;
105 setTimeout(function(){thisControler
.reset();}, 50);
108 if (this.lastOverPosition
!== null) {
109 // get new object position.
111 if (targetRow
.pos
< this.lastOverPosition
&& !this.prevDirUp
) {
113 else if (targetRow
.pos
> this.lastOverPosition
&& this.prevDirUp
) {
117 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
118 var url
= this.folderUrl
+ "/moveObjectIdToPosition";
119 var form
="object_id=" + object_id
+ "&position:int=" +
120 String(this.lastOverPosition
- 1 + trim
);
122 // reinitialize positions
123 this._updatePositions()
126 var req
= new XMLHttpRequest();
127 req
.open("POST", url
, true);
128 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
130 setTimeout(function(){thisControler
.reset();}, 50);
135 FolderDDropControler
.prototype.disableClickAfterDrop = function(evt
) {
136 var target
= getTargetedObject(evt
);
138 disablePropagation(evt
);
144 FolderDDropControler
.prototype.selectCBRange = function(evt
) {
148 FolderDDropControler
.prototype.reset = function() {
149 this.targetRow
= null;
150 this.lastOverPosition
= null;
151 this.prevDirUp
= null;
157 DropTarget = function(node
, folderDDControler
) {
158 this.folderDDControler
= folderDDControler
;
159 this.batchSize
= parseInt(document
.getElementById("BatchNavigationSize").innerHTML
, 10);
160 var thisControler
= this;
161 node
.onmouseup = function(evt
){thisControler
.drop(evt
);};
162 node
.onmouseover = function(evt
) {thisControler
.highlightTarget(evt
);};
163 node
.onmouseout = function(evt
) {
164 var target
= getTargetedObject(evt
);
165 if (target
.nodeName
=== "A" && target
.className
=== "dropPageTarget"){
166 target
.className
= "";}
170 DropTarget
.prototype.drop = function(evt
) {
171 var target
= getTargetedObject(evt
);
172 if (target
.nodeName
=== "A" &&
173 target
.className
!== "previous" &&
174 target
.className
!== "next") {
175 var pageNumber
= parseInt(target
.innerHTML
, 10);
176 var targetRow
= this.folderDDControler
.targetRow
;
177 if ( !isNaN(pageNumber
) && targetRow
) {
178 this.folderDDControler
.reset();
179 var object_id
= targetRow
.getElementsByTagName("INPUT")[0].getAttribute("value");
180 var url
= this.folderDDControler
.folderUrl
+ "/moveObjectIdToPosition";
181 var form
="object_id=" + object_id
+ "&position:int=" + String(this.batchSize
* (pageNumber
-1));
183 var req
= new XMLHttpRequest();
184 req
.open("POST", url
, true);
185 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
187 req
.onreadystatechange = function() {
188 if (req
.readyState
=== 4) {
189 switch (req
.status
) {
193 raiseMouseEvent(target
, "click");
196 alert('Error: ' + req
.status
);
204 DropTarget
.prototype.highlightTarget = function(evt
){
205 if (browser
.isIE
) {document
.selection
.clear();}
206 var target
= getTargetedObject(evt
);
207 if (this.folderDDControler
.targetRow
&&
208 target
.nodeName
=== "A" &&
209 target
.className
!== "previous" &&
210 target
.className
!== "next"){
211 target
.className
= "dropPageTarget";}
217 loadListing = function(evt
) {
218 var target
= getTargetedObject(evt
);
220 disablePropagation(evt
);
222 switch (target
.nodeName
) {
224 var parts
= target
.href
.split('?');
227 if (parts
.length
=== 2){
230 var urlParts
= url
.split("/");
231 url
= urlParts
.slice(0,urlParts
.length
-1).join("/");
232 if (query
.search("template") === -1){
233 query
+= "&template=folder_contents_macros¯o=FolderListing&fragmentId=FolderListing";}
234 url
= url
+ "/folder_contents?" + query
;
236 var fi
= new FragmentImporter(url
);
241 if (target
.id
=== 'SetSortingAsDefault') {
242 var parent
= target
.parentNode
;
244 url
= url
.replace("folder_contents", "folder_sort_control");
245 parent
.parentNode
.removeChild(parent
);
247 var req
= new XMLHttpRequest();
248 req
.open("GET", url
, true);
256 DDFolderUploader = function(dropbox
, uploadUrl
, listing
) {
257 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
258 this.listing
= listing
;
259 this.progressBarMaxSize
= listing
.clientWidth
;
262 thead
= thead
.previousSibling
;
263 } while (thead
.tagName
!== 'THEAD');
265 var cells
= thead
.getElementsByTagName('th');
268 for (i
=0 ; i
< cells
.length
; i
++) {
270 this.tableSpan
+= cell
.getAttribute('colspan') ? Number(cell
.getAttribute('colspan')) : 1;
272 var lastRow
= listing
.lastChild
;
273 while(lastRow
&& lastRow
.tagName
!== 'TR') {
274 lastRow
= lastRow
.previousSibling
;
276 this.lastRowClassName
= lastRow
? lastRow
.className
: 'even';
279 copyPrototype(DDFolderUploader
, DDFileUploaderBase
);
282 DDFolderUploader
.prototype.createRow = function(file
) {
283 var row
= document
.createElement('tr');
285 row
.className
= this.lastRowClassName
=== 'even' ? 'odd' : 'even';
286 this.lastRowClassName
= row
.className
;
287 var td
= document
.createElement('td');
288 td
.setAttribute('colspan', this.tableSpan
);
289 var relSpan
= document
.createElement('span');
290 relSpan
.style
.position
= 'relative';
291 td
.appendChild(relSpan
);
292 var progressBar
= document
.createElement('span');
293 progressBar
.className
= 'upload-progress';
294 row
.progressBar
= progressBar
;
295 relSpan
.appendChild(progressBar
);
296 var fileNameSpan
= document
.createElement('span');
297 fileNameSpan
.innerHTML
= file
.name
;
298 td
.appendChild(fileNameSpan
);
300 this.listing
.appendChild(row
);
301 this.progressBarMaxSize
= row
.clientWidth
;
305 // Methods about upload
306 DDFolderUploader
.prototype.handleFiles = function(files
) {
308 for (i
= 0; i
< files
.length
; i
++) {
310 row
= this.createRow(file
);
311 this.uploadQueuePush(row
);
315 DDFolderUploader
.prototype.beforeUpload = function(item
) {
316 this.uploadedItem
= item
;
317 this.progressBar
= item
.progressBar
;
320 DDFolderUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
321 var item
= this.uploadedItem
;
322 var row
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
323 row
.className
= item
.className
;
325 if (req
.status
=== 200) {
329 else if(req
.status
=== 201) {
331 this.listing
.replaceChild(row
, item
);
332 this.progressBarMaxSize
= row
.clientWidth
;
336 DDFolderUploader
.prototype.progressHandlerCB = function(progress
) {
337 // 0 <= progress <= 1
338 var size
= this.progressBarMaxSize
* progress
;
339 size
= Math
.round(size
);
340 this.progressBar
.style
.width
= size
+ 'px';