800a746acd06149955513e1c585dd1baa3367b1e
[Plinn.git] / skins / ajax_scripts / folder_contents_script.js
1 // (c) BenoƮt PIN 2006-2007
2 // http://plinn.org
3 // Licence GPL
4 //
5 //
6
7 var FolderDDropControler;
8 var DropTarget;
9 var loadListing;
10 var DDFolderUploader;
11
12 (function(){
13
14 function getTargetRow(evt){
15 var target = getTargetedObject(evt);
16 while (target.nodeName !== "TR") {
17 target = target.parentNode; }
18 return target;
19 }
20
21 FolderDDropControler = function(listing, firstItemPos) {
22 this.folderUrl = document.getElementById("FolderUrl").innerHTML;
23 this.targetRow = null;
24 this.lastOverPosition = null;
25 this.prevDirUp = null;
26 this.noOver = true;
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);});
36
37 if (browser.isIE) {
38 this.listing.ondragstart = function() { window.event.returnValue = false;};
39 }
40 };
41
42 FolderDDropControler.prototype._updatePositions = function() {
43 var rows = this.listing.getElementsByTagName("TR");
44 var i, row;
45 for (i=0 ; i < rows.length ; i++ ) {
46 row = rows[i];
47 row.pos = i + this.firstItemPos;
48 if (i % 2 === 0) {
49 row.className = "even"; }
50 else {
51 row.className = "odd"; }
52 }
53 };
54
55 FolderDDropControler.prototype.drag = function(evt){
56 var target = getTargetedObject(evt);
57 if (target.nodeName === "INPUT") { return true; }
58 disableDefault(evt);
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;
64 };
65
66 FolderDDropControler.prototype.moveRow = function(evt){
67 var targetRow = this.targetRow;
68 if (targetRow !== null) {
69 this.noOver = false;
70 if (browser.isIE) {document.selection.clear();}
71 var overRow = getTargetRow(evt);
72
73 if (overRow.pos === targetRow.pos) {return;}
74
75 if (this.lastOverPosition < overRow.pos) { // move up
76 this.listing.insertBefore(targetRow, overRow.nextSibling);
77 this.prevDirUp = true;
78 this.lastOverPosition = overRow.pos;
79 }
80 else if (this.lastOverPosition > overRow.pos) { // move down
81 this.listing.insertBefore(targetRow, overRow);
82 this.prevDirUp = false;
83 this.lastOverPosition = overRow.pos;
84 }
85 else {
86 if (this.prevDirUp) {
87 this.prevDirUp = false;
88 this.listing.insertBefore(targetRow, overRow);
89 }
90 else {
91 this.prevDirUp = true;
92 this.listing.insertBefore(targetRow, overRow.nextSibling);
93 }
94 }
95 }
96 };
97
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;
104 if (this.noOver) {
105 setTimeout(function(){thisControler.reset();}, 50);
106 return;
107 }
108 if (this.lastOverPosition !== null) {
109 // get new object position.
110 var trim = 0;
111 if (targetRow.pos < this.lastOverPosition && !this.prevDirUp) {
112 trim = -1; }
113 else if (targetRow.pos > this.lastOverPosition && this.prevDirUp) {
114 trim = 1; }
115
116 // construct url
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);
121
122 // reinitialize positions
123 this._updatePositions()
124
125 // send request
126 var req = new XMLHttpRequest();
127 req.open("POST", url, true);
128 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
129 req.send(form);
130 setTimeout(function(){thisControler.reset();}, 50);
131 }
132 }
133 };
134
135 FolderDDropControler.prototype.disableClickAfterDrop = function(evt) {
136 var target = getTargetedObject(evt);
137 if (!this.noOver) {
138 disablePropagation(evt);
139 disableDefault(evt);
140 }
141 this.reset();
142 };
143
144 FolderDDropControler.prototype.selectCBRange = function(evt) {
145 };
146
147
148 FolderDDropControler.prototype.reset = function() {
149 this.targetRow = null;
150 this.lastOverPosition = null;
151 this.prevDirUp = null;
152 this.noOver = true;
153 };
154
155
156
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 = "";}
167 };
168 };
169
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));
182 // send request
183 var req = new XMLHttpRequest();
184 req.open("POST", url, true);
185 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
186 req.send(form);
187 req.onreadystatechange = function() {
188 if (req.readyState === 4) {
189 switch (req.status) {
190 case 200:
191 case 204:
192 case 1223:
193 raiseMouseEvent(target, "click");
194 break;
195 default:
196 alert('Error: ' + req.status);
197 }
198 }
199 };
200 }
201 }
202 };
203
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";}
212 };
213
214
215
216
217 loadListing = function(evt) {
218 var target = getTargetedObject(evt);
219 disableDefault(evt);
220 disablePropagation(evt);
221 var url;
222 switch (target.nodeName) {
223 case "A" :
224 var parts = target.href.split('?');
225 url = parts[0];
226 var query = '';
227 if (parts.length === 2){
228 query = parts[1];}
229
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&macro=FolderListing&fragmentId=FolderListing";}
234 url = url + "/folder_contents?" + query;
235
236 var fi = new FragmentImporter(url);
237 fi.load();
238 break;
239
240 case "IMG" :
241 if (target.id === 'SetSortingAsDefault') {
242 var parent = target.parentNode;
243 url = parent.href;
244 url = url.replace("folder_contents", "folder_sort_control");
245 parent.parentNode.removeChild(parent);
246
247 var req = new XMLHttpRequest();
248 req.open("GET", url, true);
249 req.send(null);
250 }
251 break;
252 }
253 return false;
254 };
255
256 DDFolderUploader = function(dropbox, uploadUrl, listing) {
257 DDFileUploaderBase.apply(this, [dropbox, uploadUrl]);
258 this.listing = listing;
259 this.progressBarMaxSize = listing.clientWidth;
260 var thead = listing;
261 do {
262 thead = thead.previousSibling;
263 } while (thead.tagName !== 'THEAD');
264
265 var cells = thead.getElementsByTagName('th');
266 var cell, i;
267 this.tableSpan = 0;
268 for (i=0 ; i < cells.length ; i++) {
269 cell = cells[i];
270 this.tableSpan += cell.getAttribute('colspan') ? Number(cell.getAttribute('colspan')) : 1;
271 }
272 var lastRow = listing.lastChild;
273 while(lastRow && lastRow.tagName !== 'TR') {
274 lastRow = lastRow.previousSibling;
275 }
276 this.lastRowClassName = lastRow ? lastRow.className : 'even';
277 };
278
279 copyPrototype(DDFolderUploader, DDFileUploaderBase);
280
281
282 DDFolderUploader.prototype.createRow = function(file) {
283 var row = document.createElement('tr');
284 row.file = file;
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);
299 row.appendChild(td);
300 this.listing.appendChild(row);
301 this.progressBarMaxSize = row.clientWidth;
302 return row;
303 };
304
305 // Methods about upload
306 DDFolderUploader.prototype.handleFiles = function(files) {
307 var file, i, row;
308 for (i = 0; i < files.length; i++) {
309 file = files[i];
310 row = this.createRow(file);
311 this.uploadQueuePush(row);
312 }
313 };
314
315 DDFolderUploader.prototype.beforeUpload = function(item) {
316 this.uploadedItem = item;
317 this.progressBar = item.progressBar;
318 };
319
320 DDFolderUploader.prototype.uploadCompleteHandlerCB = function(req) {
321 var item = this.uploadedItem;
322 var row = getCopyOfNode(req.responseXML.documentElement.firstChild);
323 row.className = item.className;
324
325 if (req.status === 200) {
326 // update
327 console.log('todo');
328 }
329 else if(req.status === 201) {
330 // creation
331 this.listing.replaceChild(row, item);
332 this.progressBarMaxSize = row.clientWidth;
333 }
334 };
335
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';
341 };
342
343 }());