Project

General

Profile

1
// Copyright 2006 Stepan Riha
2
// www.nonplus.net
3
// $Id: dragdrop.js 2 2006-04-18 03:04:39Z stepan $
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

    
27
	if(document.URL.indexOf(JsAdmin.ADMIN_URL + "/pages/index.php") > -1) {
28
		page_type = 'pages';
29
		is_tree = true;
30

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

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

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

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

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

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

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

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

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

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

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

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

    
124
JsAdmin.DD.dragee = null;
125

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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