coquille--
[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) {
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 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 this.listing.onclick = function(evt) {thisControler.disableClickAfterDrop(evt);};
33
34 if (browser.isIE) {
35 this.listing.ondragstart = function() { window.event.returnValue = false;};
36 }
37 }
38
39 FolderDDropControler.prototype.drag = function(evt){
40 var target = getTargetedObject(evt);
41 if (target.nodeName == "INPUT") return true;
42 disableDefault(evt);
43 var targetRow = getTargetRow(evt);
44 targetRow.style.backgroundColor = base_properties["highLightColor"];
45 this.listing.style.cursor = "move";
46 this.targetRow = targetRow;
47 this.lastOverPosition = targetRow.pos;
48 };
49
50 FolderDDropControler.prototype.moveRow = function(evt){
51 var targetRow = this.targetRow;
52 if (targetRow != null) {
53 this.noOver = false;
54 if (browser.isIE) document.selection.clear();
55 var overRow = getTargetRow(evt);
56
57 if (overRow.pos == targetRow.pos) return;
58
59 if (this.lastOverPosition < overRow.pos) { // move up
60 this.listing.insertBefore(targetRow, overRow.nextSibling);
61 this.prevDirUp = true;
62 this.lastOverPosition = overRow.pos;
63 }
64 else if (this.lastOverPosition > overRow.pos) { // move down
65 this.listing.insertBefore(targetRow, overRow);
66 this.prevDirUp = false;
67 this.lastOverPosition = overRow.pos;
68 }
69 else {
70 if (this.prevDirUp) {
71 this.prevDirUp = false;
72 this.listing.insertBefore(targetRow, overRow);
73 }
74 else {
75 this.prevDirUp = true;
76 this.listing.insertBefore(targetRow, overRow.nextSibling);
77 }
78 }
79 }
80 };
81
82 FolderDDropControler.prototype.drop = function(evt){
83 var targetRow = this.targetRow;
84 if (targetRow != null) {
85 targetRow.style.backgroundColor="";
86 this.listing.style.cursor = "";
87 if (this.noOver) {
88 var thisControler = this;
89 setTimeout(function(){thisControler.reset();}, 50);
90 return;
91 }
92 if (this.lastOverPosition != null) {
93 // get new object position.
94 var trim = 0;
95 if (targetRow.pos < this.lastOverPosition && !this.prevDirUp)
96 trim = -1;
97 else if (targetRow.pos > this.lastOverPosition && this.prevDirUp)
98 trim = 1;
99
100 // construct url
101 var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
102 var url = this.folderUrl + "/moveObjectIdToPosition";
103 var form ="object_id=" + object_id + "&position:int=" +
104 String(this.lastOverPosition - 1 + trim);
105
106 // reinitialize positions
107 var rows = this.listing.getElementsByTagName("TR"), row;
108 for (var i = 0 ; i < rows.length ; i++) {
109 row = rows[i];
110 row.pos = i+1;
111 if (i % 2 == 0)
112 row.className = "even";
113 else
114 row.className = "odd";
115 }
116
117 // send request
118 var req = new XMLHttpRequest();
119 req.open("POST", url, true);
120 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
121 req.send(form);
122 var thisControler = this;
123 setTimeout(function(){thisControler.reset();}, 50);
124 }
125 }
126 };
127
128 FolderDDropControler.prototype.disableClickAfterDrop = function(evt) {
129 var target = getTargetedObject(evt);
130 if (!this.noOver) {
131 disablePropagation(evt);
132 disableDefault(evt);
133 }
134 this.reset();
135 };
136
137 FolderDDropControler.prototype.reset = function() {
138 this.targetRow = null;
139 this.lastOverPosition = null;
140 this.prevDirUp = null;
141 this.noOver = true;
142 }
143
144
145
146 DropTarget = function(node, folderDDControler) {
147 this.folderDDControler = folderDDControler;
148 this.batchSize = parseInt(document.getElementById("BatchNavigationSize").innerHTML);
149 var thisControler = this;
150 node.onmouseup = function(evt){thisControler.drop(evt);};
151 node.onmouseover = function(evt) {thisControler.highlightTarget(evt);};
152 node.onmouseout = function(evt) {
153 var target = getTargetedObject(evt);
154 if (target.nodeName == "A" && target.className == "dropPageTarget")
155 target.className = "";
156 };
157 }
158
159 DropTarget.prototype.drop = function(evt) {
160 var target = getTargetedObject(evt);
161 if (target.nodeName == "A" &&
162 target.className != "previous" &&
163 target.className != "next") {
164 var pageNumber = parseInt(target.innerHTML);
165 var targetRow = this.folderDDControler.targetRow;
166 if ( !isNaN(pageNumber) && targetRow) {
167 this.folderDDControler.reset();
168 var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
169 var url = this.folderDDControler.folderUrl + "/moveObjectIdToPosition";
170 var form ="object_id=" + object_id + "&position:int=" + String(this.batchSize * (pageNumber-1));
171 // send request
172 var req = new XMLHttpRequest();
173 req.open("POST", url, true);
174 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
175 req.send(form);
176 req.onreadystatechange = function() {
177 if (req.readyState == 4) {
178 switch (req.status) {
179 case 200:
180 case 204:
181 case 1223:
182 raiseMouseEvent(target, "click");
183 break;
184 default:
185 alert('Error: ' + req.status);
186 };
187 }
188 };
189 }
190 }
191 };
192
193 DropTarget.prototype.highlightTarget = function(evt){
194 if (browser.isIE) document.selection.clear();
195 var target = getTargetedObject(evt);
196 if (this.folderDDControler.targetRow &&
197 target.nodeName == "A" &&
198 target.className != "previous" &&
199 target.className != "next")
200 target.className = "dropPageTarget";
201 };
202
203
204
205
206 loadListing = function(evt) {
207 var target = getTargetedObject(evt);
208 disableDefault(evt);
209 disablePropagation(evt);
210 switch (target.nodeName) {
211 case "A" :
212 var parts = target.href.split('?')
213 var url = parts[0];
214 var query = '';
215 if (parts.length == 2)
216 query = parts[1];
217
218 var urlParts = url.split("/");
219 var url = urlParts.slice(0,urlParts.length-1).join("/");
220 if (query.search("template") == -1)
221 query += "&template=folder_contents_macros&macro=FolderListing&fragmentId=FolderListing";
222 url = url + "/folder_contents?" + query;
223
224 var fi = new FragmentImporter(url);
225 fi.load();
226 break;
227
228 case "IMG" :
229 if (target.id == 'SetSortingAsDefault') {
230 var parent = target.parentNode;
231 var url = parent.href;
232 url = url.replace("folder_contents", "folder_sort_control");
233 parent.parentNode.removeChild(parent);
234
235 var req = new XMLHttpRequest();
236 req.open("GET", url, true);
237 req.send(null);
238 }
239 break;
240 }
241 return false;
242 }
243
244 DDFolderUploader = function(dropbox, uploadUrl, listing) {
245 DDFileUploaderBase.apply(this, [dropbox, uploadUrl]);
246 this.listing = listing;
247 var thead = listing;
248 do {
249 thead = thead.previousSibling;
250 } while (thead.tagName !== 'THEAD')
251
252 var cells = thead.getElementsByTagName('th');
253 var cell;
254 this.tableSpan = 0;
255 for (var i = 0 ; i < cells.length ; i++) {
256 cell = cells[i];
257 this.tableSpan += cell.getAttribute('colspan') ? Number(cell.getAttribute('colspan')) : 1;
258 }
259 var lastRow = listing.lastChild;
260 while(lastRow && lastRow.tagName !== 'TR') {
261 lastRow = lastRow.previousSibling;
262 }
263 this.lastRowClassName = lastRow ? lastRow.className : 'even';
264 };
265
266 copyPrototype(DDFolderUploader, DDFileUploaderBase);
267
268
269 DDFolderUploader.prototype.createRow = function(file) {
270 var row = document.createElement('tr');
271 row.file = file;
272 row.className = this.lastRowClassName === 'even' ? 'odd' : 'even';
273 this.lastRowClassName = row.className;
274 var td = document.createElement('td');
275 td.setAttribute('colspan', this.tableSpan);
276 td.innerHTML = file.name;
277 row.appendChild(td);
278 this.listing.appendChild(row);
279 return row;
280 };
281
282 // Methods about upload
283 DDFolderUploader.prototype.handleFiles = function(files) {
284 var file, i, row;
285 for (i = 0; i < files.length; i++) {
286 file = files[i];
287 row = this.createRow(file);
288 this.uploadQueuePush(row);
289 }
290 };
291
292 DDFolderUploader.prototype.beforeUpload = function(item) {
293 // To be implemented by decendant.
294 };
295
296 DDFolderUploader.prototype.uploadCompleteHandlerCB = function(req) {
297 // To be implemented by descendant.
298 };
299
300 DDFolderUploader.prototype.progressHandlerCB = function(progress) {
301 // To be implemented by descendant.
302 // 0 <= progress <= 1
303 };
304
305 }());