Project

General

Profile

1
// Copyright 2006 Stepan Riha
2
// www.nonplus.net
3
// $Id: dragdrop.js 603 2008-01-26 16:02:19Z Ruebenwurzel $
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

    
24
	var page_type = '';
25
	var is_tree = false;
26
	if(document.URL.indexOf("/admin/pages/index.php") > 0) {
27
		page_type = 'pages';
28
		is_tree = true;
29

    
30
		// This page uses duplicate IDs and incorrectly nested lists:
31
		// <ul id="p1">
32
		//		<li id="p1"><table /></li>
33
		//		<ul>... sub items ...</ul>
34
		// </ul>
35
		//
36
		// We need to fix that to the following:
37
		// <ul id="p1">
38
		//		<li id="uniqueID"><table />
39
		//		<ul>... sub items ...</ul>
40
		//		</li>
41
		// </ul>
42

    
43
		// Stash all UL ids
44
		var ids = {};
45
		var lists = document.getElementsByTagName('ul');
46
		for(var i = 0; i < lists.length; i++) {
47
			if(lists[i].id) {
48
				ids[lists[i].id] = true;
49
			}
50
		}
51

    
52
		// Now fix all LIs
53
		var items = document.getElementsByTagName('li');
54
 		for(var i = 0; i < items.length; i++) {
55
			var item = items[i];
56

    
57
			// Fix duplicate ID
58
			if(ids[item.id]) {
59
				item.id =  JsAdmin.util.getUniqueId();
60
			}
61

    
62
			// Fix UL parented by UL
63
			var ul = JsAdmin.util.getNextSiblingNode(item, 'ul');
64
			if(ul) {
65
				var lis = ul.getElementsByTagName('li');
66
 				if(!lis || lis.length == 0) {
67
					// Remove list without items
68
					ul.parentNode.removeChild(ul);
69
				} else {
70
					// Make list child of list item
71
					item.appendChild(ul);
72
				}
73
			}
74
		}
75

    
76
	} else if(document.URL.indexOf("/admin/pages/sections.php") > 0) {
77
		page_type = 'sections';
78
	} else {
79
		// We don't do any other pages
80
		return;
81
	}
82

    
83
	var links = document.getElementsByTagName('a');
84
	var reImg = /(.*)move_(down|up)\.php(.*)/;
85

    
86
	for(var i = 0; i < links.length; i++) {
87
		var link = links[i];
88
		var href = link.href || '';
89
		var match = href.match(reImg);
90
		if(!match) {
91
			continue;
92
		}
93
		var url = match[1];
94
		var op = match[2];
95
		var params = match[3];
96
		var tr = JsAdmin.util.getAncestorNode(link, 'tr');
97
		var item = is_tree ? JsAdmin.util.getAncestorNode(tr, 'li') : tr;
98
		if(!item) {
99
			continue;
100
		}
101

    
102
		// Make sure we have a unique id
103
		if(!item.id || YAHOO.util.Dom.get(item.id) != item) {
104
			item.id = JsAdmin.util.getUniqueId();
105
		}
106

    
107
		if(is_tree) {
108
			var parent = JsAdmin.util.getAncestorNode(item, 'ul');
109
			new JsAdmin.DD.liDDSwap(item.id, (parent && parent.id) ? parent.id : 'top');
110
		} else {
111
			new JsAdmin.DD.trDDSwap(item.id);
112
		}
113
		item.className += " jsadmin_drag";
114

    
115
		this.movable_rows[item.id] = { item: item, tr : tr, url : url, params : params };
116
	}
117
};
118

    
119
//==========================================================================
120
// Drag-drop utils
121
//==========================================================================
122

    
123
JsAdmin.DD.dragee = null;
124

    
125
JsAdmin.DD.addMoveButton = function(tr, cell, op) {
126
	if(op == 'down') {
127
		cell++;
128
	}
129
	var item = JsAdmin.movable_rows[tr.id];
130
	if(!JsAdmin.util.isNodeType(tr, 'tr')) {
131
		var rows = tr.getElementsByTagName('tr');
132
		tr = rows[0];
133
	}
134
	
135
	var html = '<a href="' + item.url + 'move_' + op + '.php' + item.params
136
				+ '"><img src="' + JsAdmin.WB_URL + '/admin/images/' + op
137
				+ '_16.png" border="0" alt="' + op + '" /></a>';
138
	tr.cells[cell].innerHTML = html;
139
};
140

    
141
JsAdmin.DD.deleteMoveButton = function(tr, cell, op) {
142
	if(op == 'down') {
143
		cell++;
144
	}
145
	if(!JsAdmin.util.isNodeType(tr, 'tr')) {
146
		var rows = tr.getElementsByTagName('tr');
147
		tr = rows[0];
148
	}
149
	
150
	tr.cells[cell].innerHTML = "";
151
};
152

    
153
//==========================================================================
154
// Drag-drop handling for table rows
155
//==========================================================================
156

    
157
JsAdmin.DD.trDDSwap = function(id, sGroup) {
158
    this.init(id, sGroup);
159
	this.addInvalidHandleType('a');
160
	this.addInvalidHandleType('input');
161
	this.addInvalidHandleType('select');
162
    this.initFrame();
163
	this.buttonCell = buttonCell;//, by Swen Uth
164
	
165
	// For Connection
166
	this.scope = this;
167
};
168

    
169
JsAdmin.DD.trDDSwap.prototype = new YAHOO.util.DDProxy();
170

    
171
JsAdmin.DD.trDDSwap.prototype.startDrag = function(x, y) {
172
	if (JsAdmin.DD.dragee != this) {
173
		this.rowIndex = this.getEl().rowIndex;
174
		this.numRows = this.getEl().parentNode.rows.length;
175
		this.opacity = YAHOO.util.Dom.getStyle(this.getEl(), "opacity");
176
		this.background = YAHOO.util.Dom.getStyle(this.getEl(), "background");
177
		YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
178
		YAHOO.util.Dom.setStyle(this.getEl(), "background", "#dde");
179
	}
180
	JsAdmin.DD.dragee = this;
181
};
182

    
183
JsAdmin.DD.trDDSwap.prototype.onDragEnter = function(e, id) {
184
  var elt = id ? YAHOO.util.Dom.get(id) : null;
185
	var item = JsAdmin.movable_rows[this.getEl().id];
186
	var rows = item.tr.parentNode.rows;
187
	var wasFirst = item.tr.rowIndex == 1;
188
	var wasLast = item.tr.rowIndex == this.numRows - 2;
189
	if(elt.rowIndex < item.tr.rowIndex) {
190
		elt.parentNode.insertBefore(item.tr, elt);
191
	} else {
192
		elt.parentNode.insertBefore(elt, item.tr);
193
	}
194
	// Fixup buttons
195
	var isFirst = item.tr.rowIndex == 1;
196
	var isLast = item.tr.rowIndex == this.numRows - 2;
197

    
198
	if(wasFirst != isFirst) {
199
		if(isFirst) {
200
			JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'up');
201
			JsAdmin.DD.addMoveButton(JsAdmin.util.getNextSiblingNode(item.tr), this.buttonCell, 'up');
202
		} else {
203
			JsAdmin.DD.addMoveButton(item.tr, this.buttonCell, 'up');
204
			JsAdmin.DD.deleteMoveButton(rows[1], this.buttonCell, 'up');
205
		}
206
	}
207
	if(wasLast != isLast) {
208
		if(isLast) {
209
			JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'down');
210
			JsAdmin.DD.addMoveButton(JsAdmin.util.getPreviousSiblingNode(item.tr), this.buttonCell, 'down');
211
		} else {
212
			JsAdmin.DD.addMoveButton(item.tr, this.buttonCell, 'down');
213
			JsAdmin.DD.deleteMoveButton(rows[rows.length-2], this.buttonCell, 'down');
214
		}
215
	}
216

    
217
	this.DDM.refreshCache(this.groups);
218
};
219

    
220
JsAdmin.DD.trDDSwap.prototype.endDrag = function(e) {
221
	YAHOO.util.Dom.setStyle(this.getEl(), "opacity", this.opacity);
222
	YAHOO.util.Dom.setStyle(this.getEl(), "background", "white");
223
	
224
	JsAdmin.DD.dragee = null;
225

    
226
	var newIndex = this.getEl().rowIndex;
227
	if(newIndex != this.rowIndex) {
228
		var url = JsAdmin.WB_URL + "/modules/jsadmin/move_to.php";
229
		url += JsAdmin.movable_rows[this.getEl().id].params + "&position=" + newIndex;
230
		document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_busy";
231
		YAHOO.util.Connect.asyncRequest('GET', url, this, null);
232
	}
233
};
234

    
235
JsAdmin.DD.trDDSwap.prototype.success = function(o) {
236
	document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_success";
237
};
238

    
239
JsAdmin.DD.trDDSwap.prototype.failure = function(o) {
240
	document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/, "$1") + " jsadmin_failure";
241
};
242

    
243
//==========================================================================
244
// Drag-drop handling for list items
245
//==========================================================================
246

    
247
JsAdmin.DD.liDDSwap = function(id, sGroup) {
248
    this.init(id, sGroup);
249
	this.addInvalidHandleType('a');
250
	this.addInvalidHandleType('input');
251
	this.addInvalidHandleType('select');
252
    this.initFrame();
253
 this.buttonCell = buttonCell;//, by Swen Uth
254
	this.counter = 0;
255
};
256

    
257
JsAdmin.DD.liDDSwap.prototype = new YAHOO.util.DDProxy();
258

    
259
JsAdmin.DD.liDDSwap.prototype.startDrag = function(x, y) {
260
	// On IE, startDrag is sometimes called twice
261
	if(JsAdmin.DD.dragee && JsAdmin.DD.dragee != this) {
262
		JsAdmin.DD.dragee.endDrag(null);
263
	}
264
	if(JsAdmin.DD.dragee != this) {
265
		this.rowIndex = JsAdmin.util.getItemIndex(this.getEl());
266
		this.opacity = YAHOO.util.Dom.getStyle(this.getEl(), "opacity");
267
		this.background = YAHOO.util.Dom.getStyle(this.getEl(), "background");
268
		YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
269

    
270
		this.list = JsAdmin.util.getAncestorNode(this.getEl(), "ul");
271
		this.list.className += " jsadmin_drag_area";
272
	}
273
	JsAdmin.DD.dragee = this;
274
};
275

    
276
JsAdmin.DD.liDDSwap.prototype.onDragEnter = function(e, id) {
277
	// Swap with other element
278
	var elt = id ? YAHOO.util.Dom.get(id) : null;
279
	var item = JsAdmin.movable_rows[this.getEl().id];
280
	var eltRowIndex = JsAdmin.util.getItemIndex(elt);
281
	var rowIndex = JsAdmin.util.getItemIndex(this.getEl());
282
	var wasFirst = !JsAdmin.util.getPreviousSiblingNode(this.getEl());
283
	var wasLast = !JsAdmin.util.getNextSiblingNode(this.getEl());
284

    
285
	if(eltRowIndex < rowIndex) {
286
		elt.parentNode.insertBefore(this.getEl(), elt);
287
	} else {
288
		elt.parentNode.insertBefore(elt, this.getEl());
289
	}
290
	// Fixup buttons
291
	var isFirst = !JsAdmin.util.getPreviousSiblingNode(this.getEl());
292
	var isLast = !JsAdmin.util.getNextSiblingNode(this.getEl());
293

    
294
	if(wasFirst != isFirst) {
295
		if(isFirst) {
296
			JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'up');
297
			JsAdmin.DD.addMoveButton(JsAdmin.util.getNextSiblingNode(item.item), this.buttonCell, 'up');
298
		} else {
299
			JsAdmin.DD.addMoveButton(item.item, this.buttonCell, 'up');
300
			var first, prev = JsAdmin.util.getPreviousSiblingNode(item.item);
301
			while(prev) {
302
				first = prev;
303
				prev = JsAdmin.util.getPreviousSiblingNode(prev);
304
			}
305
			JsAdmin.DD.deleteMoveButton(JsAdmin.movable_rows[first.id].tr, this.buttonCell, 'up');
306
		}
307
	}
308
	if(wasLast != isLast) {
309
		if(isLast) {
310
			JsAdmin.DD.deleteMoveButton(item.tr, this.buttonCell, 'down');
311
			JsAdmin.DD.addMoveButton(JsAdmin.util.getPreviousSiblingNode(item.item), this.buttonCell, 'down');
312
		} else {
313
			JsAdmin.DD.addMoveButton(item.item, this.buttonCell, 'down');
314
			var last, next = JsAdmin.util.getNextSiblingNode(item.item);
315
			while(next) {
316
				last = next;
317
				next = JsAdmin.util.getNextSiblingNode(next);
318
			}
319
			JsAdmin.DD.deleteMoveButton(JsAdmin.movable_rows[last.id].tr, this.buttonCell, 'down');
320
		}
321
	}
322

    
323
	this.DDM.refreshCache(this.groups);
324
};
325

    
326
JsAdmin.DD.liDDSwap.prototype.endDrag = function(e) {
327
	YAHOO.util.Dom.setStyle(this.getEl(), "opacity", this.opacity);
328
	this.list.className = String(this.list.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1");
329
	JsAdmin.DD.dragee = null;
330
	var newIndex = JsAdmin.util.getItemIndex(this.getEl());
331
	if(newIndex != this.rowIndex) {
332
		var url = JsAdmin.WB_URL + "/modules/jsadmin/move_to.php";
333
		url += JsAdmin.movable_rows[this.getEl().id].params + "&position=" + (newIndex+1);
334
		document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_busy";
335
		YAHOO.util.Connect.asyncRequest('GET', url, this, null);
336
	}
337
};
338

    
339
JsAdmin.DD.liDDSwap.prototype.success = function(o) {
340
	document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/g, "$1") + " jsadmin_success";
341
};
342

    
343
JsAdmin.DD.liDDSwap.prototype.failure = function(o) {
344
	document.body.className = String(document.body.className).replace(/(\s*)jsadmin_([a-z]+)/, "$1") + " jsadmin_failure";
345
};
(1-1/4)