bugfix : on vérifie que le parent implémente bien l'interface d'ordonancement.
[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 function getTargetRow(evt){
8 var target = getTargetedObject(evt);
9 while (target.nodeName != "TR")
10 target = target.parentNode;
11 return target;
12 }
13
14 function FolderDDropControler(listing) {
15 this.folderUrl = document.getElementById("FolderUrl").innerHTML;
16 this.targetRow = null;
17 this.lastOverPosition = null;
18 this.prevDirUp = null;
19 this.noOver = true;
20 this.listing = listing;
21 var thisControler = this;
22 this.listing.onmousedown = function(evt) {thisControler.drag(evt);};
23 this.listing.onmouseover = function(evt) {thisControler.moveRow(evt);};
24 this.listing.onmouseup = function(evt) {thisControler.drop(evt);};
25 this.listing.onclick = function(evt) {thisControler.disableClickAfterDrop(evt);};
26
27 if (browser.isIE) {
28 this.listing.ondragstart = function() { window.event.returnValue = false;};
29 }
30 }
31
32 FolderDDropControler.prototype.drag = function(evt){
33 var target = getTargetedObject(evt);
34 if (target.nodeName == "INPUT") return true;
35 disableDefault(evt);
36 var targetRow = getTargetRow(evt);
37 targetRow.style.backgroundColor = base_properties["highLightColor"];
38 this.listing.style.cursor = "move";
39 this.targetRow = targetRow;
40 this.lastOverPosition = targetRow.pos;
41 };
42
43 FolderDDropControler.prototype.moveRow = function(evt){
44 var targetRow = this.targetRow;
45 if (targetRow != null) {
46 this.noOver = false;
47 if (browser.isIE) document.selection.clear();
48 var overRow = getTargetRow(evt);
49
50 if (overRow.pos == targetRow.pos) return;
51
52 if (this.lastOverPosition < overRow.pos) { // move up
53 this.listing.insertBefore(targetRow, overRow.nextSibling);
54 this.prevDirUp = true;
55 this.lastOverPosition = overRow.pos;
56 }
57 else if (this.lastOverPosition > overRow.pos) { // move down
58 this.listing.insertBefore(targetRow, overRow);
59 this.prevDirUp = false;
60 this.lastOverPosition = overRow.pos;
61 }
62 else {
63 if (this.prevDirUp) {
64 this.prevDirUp = false;
65 this.listing.insertBefore(targetRow, overRow);
66 }
67 else {
68 this.prevDirUp = true;
69 this.listing.insertBefore(targetRow, overRow.nextSibling);
70 }
71 }
72 }
73 };
74
75 FolderDDropControler.prototype.drop = function(evt){
76 var targetRow = this.targetRow;
77 if (targetRow != null) {
78 targetRow.style.backgroundColor="";
79 this.listing.style.cursor = "";
80 if (this.noOver) {
81 var thisControler = this;
82 setTimeout(function(){thisControler.reset();}, 50);
83 return;
84 }
85 if (this.lastOverPosition != null) {
86 // get new object position.
87 var trim = 0;
88 if (targetRow.pos < this.lastOverPosition && !this.prevDirUp)
89 trim = -1;
90 else if (targetRow.pos > this.lastOverPosition && this.prevDirUp)
91 trim = 1;
92
93 // construct url
94 var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
95 var url = this.folderUrl + "/moveObjectIdToPosition";
96 var form ="object_id=" + object_id + "&position:int=" +
97 String(this.lastOverPosition - 1 + trim);
98
99 // reinitialize positions
100 var rows = this.listing.getElementsByTagName("TR"), row;
101 for (var i = 0 ; i < rows.length ; i++) {
102 row = rows[i];
103 row.pos = i+1;
104 if (i % 2 == 0)
105 row.className = "even";
106 else
107 row.className = "odd";
108 }
109
110 // send request
111 var req = new XMLHttpRequest();
112 req.open("POST", url, true);
113 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
114 req.send(form);
115 var thisControler = this;
116 setTimeout(function(){thisControler.reset();}, 50);
117 }
118 }
119 };
120
121 FolderDDropControler.prototype.disableClickAfterDrop = function(evt) {
122 var target = getTargetedObject(evt);
123 if (!this.noOver) {
124 disablePropagation(evt);
125 disableDefault(evt);
126 }
127 this.reset();
128 };
129
130 FolderDDropControler.prototype.reset = function() {
131 this.targetRow = null;
132 this.lastOverPosition = null;
133 this.prevDirUp = null;
134 this.noOver = true;
135 }
136
137
138
139 function DropTarget(node, folderDDControler) {
140 this.folderDDControler = folderDDControler;
141 this.batchSize = parseInt(document.getElementById("BatchNavigationSize").innerHTML);
142 var thisControler = this;
143 node.onmouseup = function(evt){thisControler.drop(evt);};
144 node.onmouseover = function(evt) {thisControler.highlightTarget(evt);};
145 node.onmouseout = function(evt) {
146 var target = getTargetedObject(evt);
147 if (target.nodeName == "A" && target.className == "dropPageTarget")
148 target.className = "";
149 };
150 }
151
152 DropTarget.prototype.drop = function(evt) {
153 var target = getTargetedObject(evt);
154 if (target.nodeName == "A" &&
155 target.className != "previous" &&
156 target.className != "next") {
157 var pageNumber = parseInt(target.innerHTML);
158 var targetRow = this.folderDDControler.targetRow;
159 if ( !isNaN(pageNumber) && targetRow) {
160 this.folderDDControler.reset();
161 var object_id = targetRow.getElementsByTagName("INPUT")[0].getAttribute("value");
162 var url = this.folderDDControler.folderUrl + "/moveObjectIdToPosition";
163 var form ="object_id=" + object_id + "&position:int=" + String(this.batchSize * (pageNumber-1));
164 // send request
165 var req = new XMLHttpRequest();
166 req.open("POST", url, true);
167 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
168 req.send(form);
169 req.onreadystatechange = function() {
170 if (req.readyState == 4) {
171 switch (req.status) {
172 case 200:
173 case 204:
174 case 1223:
175 raiseMouseEvent(target, "click");
176 break;
177 default:
178 alert('Error: ' + req.status);
179 };
180 }
181 };
182 }
183 }
184 };
185
186 DropTarget.prototype.highlightTarget = function(evt){
187 if (browser.isIE) document.selection.clear();
188 var target = getTargetedObject(evt);
189 if (this.folderDDControler.targetRow &&
190 target.nodeName == "A" &&
191 target.className != "previous" &&
192 target.className != "next")
193 target.className = "dropPageTarget";
194 };
195
196
197
198
199 function loadListing(evt) {
200 var target = getTargetedObject(evt);
201 disableDefault(evt);
202 disablePropagation(evt);
203 switch (target.nodeName) {
204 case "A" :
205 var parts = target.href.split('?')
206 var url = parts[0];
207 var query = '';
208 if (parts.length == 2)
209 query = parts[1];
210
211 var urlParts = url.split("/");
212 var url = urlParts.slice(0,urlParts.length-1).join("/");
213 if (query.search("template") == -1)
214 query += "&template=folder_contents_macros&macro=FolderListing&fragmentId=FolderListing";
215 url = url + "/folder_contents?" + query;
216
217 var fi = new FragmentImporter(url);
218 fi.load();
219 break;
220
221 case "IMG" :
222 if (target.id == 'SetSortingAsDefault') {
223 var parent = target.parentNode;
224 var url = parent.href;
225 url = url.replace("folder_contents", "folder_sort_control");
226 parent.parentNode.removeChild(parent);
227
228 var req = new XMLHttpRequest();
229 req.open("GET", url, true);
230 req.send(null);
231 }
232 break;
233 }
234 return false;
235 }