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