Si la création du type adoc n’est pas possible (pas autorisée) on crée alors un fichi...
[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 this.progressBarMaxSize = listing.clientWidth;
248 var thead = listing;
249 do {
250 thead = thead.previousSibling;
251 } while (thead.tagName !== 'THEAD')
252
253 var cells = thead.getElementsByTagName('th');
254 var cell;
255 this.tableSpan = 0;
256 for (var i = 0 ; i < cells.length ; i++) {
257 cell = cells[i];
258 this.tableSpan += cell.getAttribute('colspan') ? Number(cell.getAttribute('colspan')) : 1;
259 }
260 var lastRow = listing.lastChild;
261 while(lastRow && lastRow.tagName !== 'TR') {
262 lastRow = lastRow.previousSibling;
263 }
264 this.lastRowClassName = lastRow ? lastRow.className : 'even';
265 };
266
267 copyPrototype(DDFolderUploader, DDFileUploaderBase);
268
269
270 DDFolderUploader.prototype.createRow = function(file) {
271 var row = document.createElement('tr');
272 row.file = file;
273 row.className = this.lastRowClassName === 'even' ? 'odd' : 'even';
274 this.lastRowClassName = row.className;
275 var td = document.createElement('td');
276 td.setAttribute('colspan', this.tableSpan);
277 var relSpan = document.createElement('span');
278 relSpan.style.position = 'relative';
279 td.appendChild(relSpan);
280 var progressBar = document.createElement('span');
281 progressBar.className = 'upload-progress';
282 row.progressBar = progressBar;
283 relSpan.appendChild(progressBar);
284 var fileNameSpan = document.createElement('span');
285 fileNameSpan.innerHTML = file.name;
286 td.appendChild(fileNameSpan);
287 row.appendChild(td);
288 this.listing.appendChild(row);
289 this.progressBarMaxSize = row.clientWidth;
290 return row;
291 };
292
293 // Methods about upload
294 DDFolderUploader.prototype.handleFiles = function(files) {
295 var file, i, row;
296 for (i = 0; i < files.length; i++) {
297 file = files[i];
298 row = this.createRow(file);
299 this.uploadQueuePush(row);
300 }
301 };
302
303 DDFolderUploader.prototype.beforeUpload = function(item) {
304 this.uploadedItem = item;
305 this.progressBar = item.progressBar;
306 };
307
308 DDFolderUploader.prototype.uploadCompleteHandlerCB = function(req) {
309 var item = this.uploadedItem;
310 var row = getCopyOfNode(req.responseXML.documentElement.firstChild);
311 row.className = item.className;
312
313 if (req.status === 200) {
314 // update
315 console.log('todo');
316 }
317 else if(req.status === 201) {
318 // creation
319 this.listing.replaceChild(row, item);
320 this.progressBarMaxSize = row.clientWidth;
321 }
322 };
323
324 DDFolderUploader.prototype.progressHandlerCB = function(progress) {
325 // 0 <= progress <= 1
326 var size = this.progressBarMaxSize * progress;
327 size = Math.round(size);
328 this.progressBar.style.width = size + 'px';
329 };
330
331 }());