7edda7dadf86af84f4fcfa90b33c8b143b89c425
[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 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);});
37
38 if (browser.isIE) {
39 this.listing.ondragstart = function() { window.event.returnValue = false;};
40 }
41 };
42
43 FolderDDropControler.prototype._updatePositions = function() {
44 var rows = this.listing.getElementsByTagName("TR");
45 var i, row;
46 for (i=0 ; i < rows.length ; i++ ) {
47 row = rows[i];
48 row.pos = i + this.firstItemPos;
49 if (i % 2 === 0) {
50 row.className = "even"; }
51 else {
52 row.className = "odd"; }
53 }
54 };
55
56 FolderDDropControler.prototype.drag = function(evt){
57 var target = getTargetedObject(evt);
58 if (target.nodeName === "INPUT") { return true; }
59 disableDefault(evt);
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;
65 };
66
67 FolderDDropControler.prototype.moveRow = function(evt){
68 var targetRow = this.targetRow;
69 if (targetRow !== null) {
70 this.noOver = false;
71 if (browser.isIE) {document.selection.clear();}
72 var overRow = getTargetRow(evt);
73
74 if (overRow.pos === targetRow.pos) {return;}
75
76 if (this.lastOverPosition < overRow.pos) { // move up
77 this.listing.insertBefore(targetRow, overRow.nextSibling);
78 this.prevDirUp = true;
79 this.lastOverPosition = overRow.pos;
80 }
81 else if (this.lastOverPosition > overRow.pos) { // move down
82 this.listing.insertBefore(targetRow, overRow);
83 this.prevDirUp = false;
84 this.lastOverPosition = overRow.pos;
85 }
86 else {
87 if (this.prevDirUp) {
88 this.prevDirUp = false;
89 this.listing.insertBefore(targetRow, overRow);
90 }
91 else {
92 this.prevDirUp = true;
93 this.listing.insertBefore(targetRow, overRow.nextSibling);
94 }
95 }
96 }
97 };
98
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;
105 if (this.noOver) {
106 setTimeout(function(){thisControler.reset();}, 50);
107 return;
108 }
109 if (this.lastOverPosition !== null) {
110 // get new object position.
111 var trim = 0;
112 if (targetRow.pos < this.lastOverPosition && !this.prevDirUp) {
113 trim = -1; }
114 else if (targetRow.pos > this.lastOverPosition && this.prevDirUp) {
115 trim = 1; }
116
117 // construct url
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);
122
123 // reinitialize positions
124 this._updatePositions();
125
126 // send request
127 var req = new XMLHttpRequest();
128 req.open("POST", url, true);
129 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
130 req.send(form);
131 setTimeout(function(){thisControler.reset();}, 50);
132 }
133 }
134 };
135
136 FolderDDropControler.prototype.disableClickAfterDrop = function(evt) {
137 var target = getTargetedObject(evt);
138 if (!this.noOver) {
139 disablePropagation(evt);
140 disableDefault(evt);
141 }
142 this.reset();
143 };
144
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);
156 var i;
157 for (i=start ; i<stop ; i++ ) {
158 rows[i].getElementsByTagName('INPUT')[0].checked = true;
159 }
160 }
161 else if (target.checked) {
162 this.lastCBChecked = target;
163 }
164 else {
165 this.lastCBChecked = undefined;
166 }
167 }
168 };
169
170 FolderDDropControler.prototype.getCBIndex = function(cb) {
171 var row = cb.parentNode;
172 while(row.tagName !== 'TR') {
173 row = row.parentNode;
174 }
175 return row.pos - this.firstItemPos;
176 };
177
178 FolderDDropControler.prototype.reset = function() {
179 this.targetRow = null;
180 this.lastOverPosition = null;
181 this.prevDirUp = null;
182 this.noOver = true;
183 };
184
185
186
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 = "";}
197 };
198 };
199
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));
212 // send request
213 var req = new XMLHttpRequest();
214 req.open("POST", url, true);
215 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
216 req.send(form);
217 req.onreadystatechange = function() {
218 if (req.readyState === 4) {
219 switch (req.status) {
220 case 200:
221 case 204:
222 case 1223:
223 raiseMouseEvent(target, "click");
224 break;
225 default:
226 alert('Error: ' + req.status);
227 }
228 }
229 };
230 }
231 }
232 };
233
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";}
242 };
243
244
245
246
247 loadListing = function(evt) {
248 var target = getTargetedObject(evt);
249 disableDefault(evt);
250 disablePropagation(evt);
251 var url;
252 switch (target.nodeName) {
253 case "A" :
254 var parts = target.href.split('?');
255 url = parts[0];
256 var query = '';
257 if (parts.length === 2){
258 query = parts[1];}
259
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&macro=FolderListing&fragmentId=FolderListing";}
264 url = url + "/folder_contents?" + query;
265
266 var fi = new FragmentImporter(url);
267 fi.load();
268 break;
269
270 case "IMG" :
271 if (target.id === 'SetSortingAsDefault') {
272 var parent = target.parentNode;
273 url = parent.href;
274 url = url.replace("folder_contents", "folder_sort_control");
275 parent.parentNode.removeChild(parent);
276
277 var req = new XMLHttpRequest();
278 req.open("GET", url, true);
279 req.send(null);
280 }
281 break;
282 }
283 return false;
284 };
285
286 DDFolderUploader = function(dropbox, uploadUrl, listing) {
287 DDFileUploaderBase.apply(this, [dropbox, uploadUrl]);
288 this.listing = listing;
289 this.progressBarMaxSize = listing.clientWidth;
290 var thead = listing;
291 do {
292 thead = thead.previousSibling;
293 } while (thead.tagName !== 'THEAD');
294
295 var cells = thead.getElementsByTagName('th');
296 var cell, i;
297 this.tableSpan = 0;
298 for (i=0 ; i < cells.length ; i++) {
299 cell = cells[i];
300 this.tableSpan += cell.getAttribute('colspan') ? Number(cell.getAttribute('colspan')) : 1;
301 }
302 var lastRow = listing.lastChild;
303 while(lastRow && lastRow.tagName !== 'TR') {
304 lastRow = lastRow.previousSibling;
305 }
306 this.lastRowClassName = lastRow ? lastRow.className : 'even';
307 };
308
309 copyPrototype(DDFolderUploader, DDFileUploaderBase);
310
311
312 DDFolderUploader.prototype.createRow = function(file) {
313 var row = document.createElement('tr');
314 row.file = file;
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);
329 row.appendChild(td);
330 this.listing.appendChild(row);
331 this.progressBarMaxSize = row.clientWidth;
332 return row;
333 };
334
335 // Methods about upload
336 DDFolderUploader.prototype.handleFiles = function(files) {
337 var file, i, row;
338 for (i = 0; i < files.length; i++) {
339 file = files[i];
340 row = this.createRow(file);
341 this.uploadQueuePush(row);
342 }
343 };
344
345 DDFolderUploader.prototype.beforeUpload = function(item) {
346 this.uploadedItem = item;
347 this.progressBar = item.progressBar;
348 };
349
350 DDFolderUploader.prototype.uploadCompleteHandlerCB = function(req) {
351 var item = this.uploadedItem;
352 var row = getCopyOfNode(req.responseXML.documentElement.firstChild);
353 row.className = item.className;
354
355 if (req.status === 200) {
356 // update
357 console.log('todo');
358 }
359 else if(req.status === 201) {
360 // creation
361 this.listing.replaceChild(row, item);
362 this.progressBarMaxSize = row.clientWidth;
363 }
364 };
365
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';
371 };
372
373 }());