1
|
// Copyright 2006 Stepan Riha
|
2
|
// www.nonplus.net
|
3
|
// $Id: dragdrop.js 65 2017-03-03 21:38:16Z manu $
|
4
|
/**
|
5
|
* -----------------------------------------------------------------------------------------
|
6
|
* MODIFICATON FOR THE JSADMIN MODULE
|
7
|
* -----------------------------------------------------------------------------------------
|
8
|
* MODIFICATION HISTORY:
|
9
|
* Swen Uth; 01/24/2008
|
10
|
* +INCLUDE VARIABLE buttonCell FOR ADAPTATION TO LATER LAYOUTS
|
11
|
*
|
12
|
**/
|
13
|
JsAdmin.DD = {};
|
14
|
JsAdmin.movable_rows = {};
|
15
|
|
16
|
JsAdmin.init_drag_drop = function() {
|
17
|
|
18
|
// There seems to be many different ways the ordering is set up
|
19
|
// pages/index.php has UL/LI containing tables with single row
|
20
|
// pages/sections.php has a TABLE with many rows
|
21
|
// pages/modify.php for manuals is completely weird...
|
22
|
// So we only want to deal with pages & sections...
|
23
|
var StashAllULIds =(function () {
|
24
|
// Stash all UL ids
|
25
|
var ids = {};
|
26
|
var lists = document.getElementsByTagName('ul.draggable');
|
27
|
for(var i = 0; i < lists.length; i++) {
|
28
|
if(lists[i].id) {
|
29
|
ids[lists[i].id] = true;
|
30
|
}
|
31
|
}
|
32
|
// Now fix all LIs
|
33
|
var items = document.getElementsByTagName('li');
|
34
|
for(var i = 0; i < items.length; i++) {
|
35
|
var item = items[i];
|
36
|
// Fix duplicate ID
|
37
|
if(ids[item.id]) {
|
38
|
item.id = JsAdmin.util.getUniqueId();
|
39
|
}
|
40
|
// Fix UL parented by UL
|
41
|
var ul = JsAdmin.util.getNextSiblingNode(item, 'ul');
|
42
|
if(ul) {
|
43
|
var lis = ul.getElementsByTagName('li');
|
44
|
if(!lis || lis.length == 0) {
|
45
|
// Remove list without items
|
46
|
ul.parentNode.removeChild(ul);
|
47
|
} else {
|
48
|
// Make list child of list item
|
49
|
item.appendChild(ul);
|
50
|
}
|
51
|
}
|
52
|
}
|
53
|
});
|
54
|
|
55
|
var page_type = '';
|
56
|
var is_tree = false;
|
57
|
var is_list = false;
|
58
|
if(document.URL.indexOf(JsAdmin.ADMIN_DIRECTORY + "/pages/index.php") > -1) {
|
59
|
is_list = document.querySelector('ul.draggable');
|
60
|
is_tree = ((is_list!==null)?true:false);
|
61
|
page_type = 'pages';
|
62
|
// is_tree = true;
|
63
|
// This page uses duplicate IDs and incorrectly nested lists:
|
64
|
// <ul id="p1">
|
65
|
// <li id="p1"><table /></li>
|
66
|
// <ul>... sub items ...</ul>
|
67
|
// </ul>
|
68
|
//
|
69
|
// We need to fix that to the following:
|
70
|
// <ul id="p1">
|
71
|
// <li id="uniqueID"><table />
|
72
|
// <ul>... sub items ...</ul>
|
73
|
// </li>
|
74
|
// </ul>
|
75
|
|
76
|
} else if(document.URL.indexOf(JsAdmin.ADMIN_DIRECTORY + "/pages/sections.php") > 0) {
|
77
|
is_list = document.querySelector('ul.draggable');
|
78
|
is_tree = ((is_list!==null)?true:false);
|
79
|
page_type = 'sections';
|
80
|
} else if(document.URL.indexOf(JsAdmin.ADMIN_DIRECTORY + "/pages/modify.php") > 0) {
|
81
|
is_list = document.querySelector('ul.draggable');
|
82
|
is_tree = ((is_list!==null)?true:false);
|
83
|
page_type = 'modules';
|
84
|
} else if(document.URL.indexOf(JsAdmin.ADMIN_DIRECTORY + "/admintools/tool.php") > 0) {
|
85
|
is_list = document.querySelector('ul.draggable');
|
86
|
is_tree = ((is_list!==null)?true:false);
|
87
|
page_type = 'tool';
|
88
|
} else {
|
89
|
// We don't do any other pages
|
90
|
return false;
|
91
|
}
|
92
|
console.log(is_list);
|
93
|
console.log('is_tree: '+is_tree);
|
94
|
if (is_list){StashAllULIds();}
|
95
|
// var linkType = ((page_type==='modules')?'button':'a');
|
96
|
// var linkType = ((page_type==='modules')?'a':'a');
|
97
|
var linkType = 'a';
|
98
|
var links = document.getElementsByTagName(linkType);
|
99
|
var reImg = /(.*)move_(down|up)\.php(.*)/;
|
100
|
for(var i = 0; i < links.length; i++) {
|
101
|
var link = links[i];
|
102
|
var href = link.href || '';
|
103
|
var match = href.match(reImg);
|
104
|
if(!match) {
|
105
|
continue;
|
106
|
}
|
107
|
var url = match[1];
|
108
|
var op = match[2];
|
109
|
var params = match[3];
|
110
|
var tr = JsAdmin.util.getAncestorNode(link, 'tr');
|
111
|
var item = is_tree ? JsAdmin.util.getAncestorNode(tr, 'li') : tr;
|
112
|
if(!item) {
|
113
|
continue;
|
114
|
}
|
115
|
// Make sure we have a unique id
|
116
|
if(!item.id || YAHOO.util.Dom.get(item.id) != item) {
|
117
|
item.id = JsAdmin.util.getUniqueId();
|
118
|
}
|
119
|
if(is_tree) {
|
120
|
var parent = JsAdmin.util.getAncestorNode(item, 'ul');
|
121
|
new JsAdmin.DD.liDDSwap(item.id, (parent && parent.id) ? parent.id : 'top');
|
122
|
} else {
|
123
|
new JsAdmin.DD.trDDSwap(item.id);
|
124
|
}
|
125
|
item.className += " jsadmin_drag";
|
126
|
this.movable_rows[item.id] = { item: item, tr : tr, url : url, params : params };
|
127
|
}
|
128
|
};
|
129
|
|
130
|
//==========================================================================
|
131
|
// Drag-drop utils
|
132
|
//==========================================================================
|
133
|
|
134
|
JsAdmin.DD.dragee = null;
|
135
|
|
136
|
JsAdmin.DD.addMoveButton = function(tr, cell, op) {
|
137
|
if(op === 'down') {
|
138
|
cell++;
|
139
|
}
|
140
|
console.log(tr);
|
141
|
if (tr) {
|
142
|
var item = JsAdmin.movable_rows[tr.id];
|
143
|
} else { return;}
|
144
|
if(!JsAdmin.util.isNodeType(tr, 'tr')) {
|
145
|
var rows = tr.getElementsByTagName('tr');
|
146
|
tr = rows[0];
|
147
|
}
|
148
|
|
149
|
var html = '<a href="' + item.url + 'move_' + op + '.php' + item.params
|
150
|
+ '"><img src="' + JsAdminTheme.THEME_URL + '/images/' + op
|
151
|
+ '_16.png" border="0" alt="' + op + '" /></a>';
|
152
|
tr.cells[cell].innerHTML = html;
|
153
|
//console.log(html);
|
154
|
|
155
|
};
|
156
|
|
157
|
JsAdmin.DD.deleteMoveButton = function(tr, cell, op) {
|
158
|
if(op == 'down') {
|
159
|
cell++;
|
160
|
}
|
161
|
if(!JsAdmin.util.isNodeType(tr, 'tr')) {
|
162
|
var rows = tr.getElementsByTagName('tr');
|
163
|
tr = rows[0];
|
164
|
}
|
165
|
|
166
|
tr.cells[cell].innerHTML = "";
|
167
|
};
|
168
|
|
169
|
//==========================================================================
|
170
|
// Drag-drop handling for table rows
|
171
|
//==========================================================================
|
172
|
|
173
|
JsAdmin.DD.trDDSwap = function(id, sGroup) {
|
174
|
this.init(id, sGroup);
|
175
|
this.addInvalidHandleType('a');
|
176
|
this.addInvalidHandleType('input');
|
177
|
this.addInvalidHandleType('select');
|
178
|
this.initFrame();
|
179
|
this.buttonCell = buttonCell;//, by Swen Uth
|
180
|
|
181
|
// For Connection
|
182
|
this.scope = this;
|
183
|
};
|
184
|
//console.log('buttonCell: '+buttonCell);
|
185
|
|
186
|
JsAdmin.DD.trDDSwap.prototype = new YAHOO.util.DDProxy();
|
187
|
|
188
|
JsAdmin.DD.trDDSwap.prototype.startDrag = function(x, y) {
|
189
|
if (JsAdmin.DD.dragee != this) {
|
190
|
this.rowIndex = this.getEl().rowIndex;
|
191
|
this.numRows = this.getEl().parentNode.rows.length;
|
192
|
this.opacity = YAHOO.util.Dom.getStyle(this.getEl(), "opacity");
|
193
|
this.background = YAHOO.util.Dom.getStyle(this.getEl(), "background");
|
194
|
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
|
195
|
YAHOO.util.Dom.setStyle(this.getEl(), "background", "transparent");
|
196
|
}
|
197
|
JsAdmin.DD.dragee = this;
|
198
|
};
|
199
|
|
200
|
JsAdmin.DD.trDDSwap.prototype.onDragEnter = function(e, id) {
|
201
|
var elt = id ? YAHOO.util.Dom.get(id) : null;
|
202
|
var item = JsAdmin.movable_rows[this.getEl().id];
|
203
|
var rows = item.tr.parentNode.rows;
|
204
|
var wasFirst = item.tr.rowIndex == 1;
|
205
|
var wasLast = item.tr.rowIndex == this.numRows - 2;
|
206
|
if(elt.rowIndex < item.tr.rowIndex) {
|
207
|
elt.parentNode.insertBefore(item.tr, elt);
|
208
|
} else {
|
209
|
elt.parentNode.insertBefore(elt, item.tr);
|
210
|
}
|
211
|
// Fixup buttons
|
212
|
var isFirst = item.tr.rowIndex == 1;
|
213
|
var isLast = item.tr.rowIndex == this.numRows - 2;
|
214
|
//console.log(this.numRows);
|
215
|
if(wasFirst != isFirst) {
|
216
|
if(isFirst) {
|
217
|
JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'up');
|
218
|
//console.log(JsAdmin.util.getNextSiblingNode(item.tr));
|
219
|
JsAdmin.DD.addMoveButton(JsAdmin.util.getNextSiblingNode(item.tr), this.buttonCell, 'up');
|
220
|
} else {
|
221
|
JsAdmin.DD.addMoveButton(item.tr, this.buttonCell, 'up');
|
222
|
JsAdmin.DD.deleteMoveButton(rows[1], this.buttonCell, 'up');
|
223
|
}
|
224
|
}
|
225
|
if(wasLast != isLast) {
|
226
|
if(isLast) {
|
227
|
JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'down');
|
228
|
JsAdmin.DD.addMoveButton(JsAdmin.util.getPreviousSiblingNode(item.tr), this.buttonCell, 'down');
|
229
|
} else {
|
230
|
JsAdmin.DD.addMoveButton(item.tr, this.buttonCell, 'down');
|
231
|
JsAdmin.DD.deleteMoveButton(rows[rows.length-2], this.buttonCell, 'down');
|
232
|
}
|
233
|
}
|
234
|
|
235
|
this.DDM.refreshCache(this.groups);
|
236
|
};
|
237
|
|
238
|
JsAdmin.DD.trDDSwap.prototype.endDrag = function(e) {
|
239
|
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", this.opacity);
|
240
|
YAHOO.util.Dom.setStyle(this.getEl(), "background", "#f0f0f0");
|
241
|
|
242
|
JsAdmin.DD.dragee = null;
|
243
|
|
244
|
var newIndex = this.getEl().rowIndex;
|
245
|
if(newIndex != this.rowIndex) {
|
246
|
|
247
|
var url = JsAdmin.WB_URL + "/modules/"+JsAdmin.ModuleUrl+"/move_to.php";
|
248
|
url += JsAdmin.movable_rows[this.getEl().id].params + "&newposition=" + newIndex;
|
249
|
//console.info(url);
|
250
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_busy";
|
251
|
YAHOO.util.Connect.asyncRequest('GET', url, this, null);
|
252
|
}
|
253
|
};
|
254
|
|
255
|
JsAdmin.DD.trDDSwap.prototype.success = function(o) {
|
256
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_success";
|
257
|
};
|
258
|
|
259
|
JsAdmin.DD.trDDSwap.prototype.failure = function(o) {
|
260
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/, "$1") + " jsadmin_failure";
|
261
|
};
|
262
|
|
263
|
//==========================================================================
|
264
|
// Drag-drop handling for list items
|
265
|
//==========================================================================
|
266
|
|
267
|
JsAdmin.DD.liDDSwap = function(id, sGroup) {
|
268
|
this.init(id, sGroup);
|
269
|
this.addInvalidHandleType('a');
|
270
|
this.addInvalidHandleType('input');
|
271
|
this.addInvalidHandleType('select');
|
272
|
this.initFrame();
|
273
|
this.buttonCell = buttonCell;//, by Swen Uth
|
274
|
this.counter = 0;
|
275
|
};
|
276
|
|
277
|
JsAdmin.DD.liDDSwap.prototype = new YAHOO.util.DDProxy();
|
278
|
|
279
|
JsAdmin.DD.liDDSwap.prototype.startDrag = function(x, y) {
|
280
|
// On IE, startDrag is sometimes called twice
|
281
|
if(JsAdmin.DD.dragee && JsAdmin.DD.dragee != this) {
|
282
|
JsAdmin.DD.dragee.endDrag(null);
|
283
|
}
|
284
|
if(JsAdmin.DD.dragee != this) {
|
285
|
this.rowIndex = JsAdmin.util.getItemIndex(this.getEl());
|
286
|
this.opacity = YAHOO.util.Dom.getStyle(this.getEl(), "opacity");
|
287
|
this.background = YAHOO.util.Dom.getStyle(this.getEl(), "background");
|
288
|
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
|
289
|
|
290
|
this.list = JsAdmin.util.getAncestorNode(this.getEl(), "ul");
|
291
|
this.list.className += " jsadmin_drag_area";
|
292
|
}
|
293
|
JsAdmin.DD.dragee = this;
|
294
|
};
|
295
|
|
296
|
JsAdmin.DD.liDDSwap.prototype.onDragEnter = function(e, id) {
|
297
|
// Swap with other element
|
298
|
var elt = id ? YAHOO.util.Dom.get(id) : null;
|
299
|
var item = JsAdmin.movable_rows[this.getEl().id];
|
300
|
var eltRowIndex = JsAdmin.util.getItemIndex(elt);
|
301
|
var rowIndex = JsAdmin.util.getItemIndex(this.getEl());
|
302
|
var wasFirst = !JsAdmin.util.getPreviousSiblingNode(this.getEl());
|
303
|
var wasLast = !JsAdmin.util.getNextSiblingNode(this.getEl());
|
304
|
|
305
|
if(eltRowIndex < rowIndex) {
|
306
|
elt.parentNode.insertBefore(this.getEl(), elt);
|
307
|
} else {
|
308
|
elt.parentNode.insertBefore(elt, this.getEl());
|
309
|
}
|
310
|
// Fixup buttons
|
311
|
var isFirst = !JsAdmin.util.getPreviousSiblingNode(this.getEl());
|
312
|
var isLast = !JsAdmin.util.getNextSiblingNode(this.getEl());
|
313
|
|
314
|
if(wasFirst != isFirst) {
|
315
|
if(isFirst) {
|
316
|
JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'up');
|
317
|
JsAdmin.DD.addMoveButton(JsAdmin.util.getNextSiblingNode(item.item), this.buttonCell, 'up');
|
318
|
} else {
|
319
|
JsAdmin.DD.addMoveButton(item.item, this.buttonCell, 'up');
|
320
|
var first, prev = JsAdmin.util.getPreviousSiblingNode(item.item);
|
321
|
while(prev) {
|
322
|
first = prev;
|
323
|
prev = JsAdmin.util.getPreviousSiblingNode(prev);
|
324
|
}
|
325
|
JsAdmin.DD.deleteMoveButton(JsAdmin.movable_rows[first.id].tr, this.buttonCell, 'up');
|
326
|
}
|
327
|
}
|
328
|
if(wasLast != isLast) {
|
329
|
if(isLast) {
|
330
|
JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'down');
|
331
|
JsAdmin.DD.addMoveButton(JsAdmin.util.getPreviousSiblingNode(item.item), this.buttonCell, 'down');
|
332
|
} else {
|
333
|
JsAdmin.DD.addMoveButton(item.item, this.buttonCell, 'down');
|
334
|
var last, next = JsAdmin.util.getNextSiblingNode(item.item);
|
335
|
while(next) {
|
336
|
last = next;
|
337
|
next = JsAdmin.util.getNextSiblingNode(next);
|
338
|
}
|
339
|
JsAdmin.DD.deleteMoveButton(JsAdmin.movable_rows[last.id].tr, this.buttonCell, 'down');
|
340
|
}
|
341
|
}
|
342
|
|
343
|
this.DDM.refreshCache(this.groups);
|
344
|
};
|
345
|
|
346
|
JsAdmin.DD.liDDSwap.prototype.endDrag = function(e) {
|
347
|
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", this.opacity);
|
348
|
this.list.className = String(this.list.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1");
|
349
|
JsAdmin.DD.dragee = null;
|
350
|
var newIndex = JsAdmin.util.getItemIndex(this.getEl());
|
351
|
if(newIndex != this.rowIndex) {
|
352
|
var url = JsAdmin.WB_URL + "/modules/"+JsAdmin.ModuleUrl+"/move_to.php";
|
353
|
url += JsAdmin.movable_rows[this.getEl().id].params + "&newposition=" + (newIndex+1);
|
354
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_busy";
|
355
|
YAHOO.util.Connect.asyncRequest('GET', url, this, null);
|
356
|
}
|
357
|
};
|
358
|
|
359
|
JsAdmin.DD.liDDSwap.prototype.success = function(o) {
|
360
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_success";
|
361
|
};
|
362
|
|
363
|
JsAdmin.DD.liDDSwap.prototype.failure = function(o) {
|
364
|
document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/, "$1") + " jsadmin_failure";
|
365
|
};
|