summaryrefslogtreecommitdiffstats
path: root/usr/local/www/draglist.js
blob: a8651ec13a6cb0fab0e71a43bc12b8a935b537fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ----------------------------------------------------------------------------
// (c) Copyright, DTLink, LLC 1997-2005
//     http://www.dtlink.com
//
// DragList - Drag and Drop Ordered Lists
//
// Javascript Support file for formVista <draglist> fvml tag.
//
// For more information please see:
//
//    http://www.formvista.com/otherprojects/draglist.html
//
// For questions or comments please contact us at:
//
//     http://www.formvista.com/contact.html
//
// LICENSE: This file is governed by the new BSD license. For more information
// please see the LICENSE.txt file accompanying this package. 
//
// REVISION HISTORY:
//
// 2004-11-12 YmL:
//	.	initial revision.
//
// 2005-05-28 YmL:
//	.	pulled out of formVista, relicensed and packaged as a standalone implementation.
//
// 2005-06-02 mtmosier:
//	.	added horizontal dragging support.
//
// ------------------------

/**
* constructor for dragList class
*/

function fv_dragList( name )
	{

	// name of this dragList. Must match the id of the root DIV tag.

	this.dragListRootId = name;

	// array of item offsets

	this.offsetsX = new Array();
	this.offsetsY = new Array();

	}

// ----------------------------------------------

/**
* setup the draglist prior to use
*
* @param string orientation defaults to vert. if set to "horz" renders horizontally.
* @param string itemTagName. if null defaults to "div". Can be "span".
*/

fv_dragList.prototype.setup = function( orientation, itemTagName )
	{

	var horizontal;

	if ( orientation == "horz" )
		horizontal = true;
	else
		horizontal = false;

	this.listRoot = document.getElementById( this.dragListRootId );
	this.listItems = this.getListItems( itemTagName );

	for (var i = 0; i < this.listItems.length; i++) 
		{

		if ( this.listItems[i] == undefined )
			continue;

		if ( horizontal )
			{
			Drag.init(this.listItems[i], null, null, null, 0, 0);
			}
		else
			{
			Drag.init(this.listItems[i], null, 0, 0, null, null);
			}

		// ---------------------------------------------------
		// on drag method

		this.listItems[i].onDrag = function( x, y, thisElem ) 
			{

			x = thisElem.offsetLeft;
			y = thisElem.offsetTop;

			// this is a callback from the dom-drag code. From within this
			// function "this" does not refer to the fv_draglist function.

			draglist = getDragList( thisElem );

			draglist.recalcOffsets( itemTagName );

			var pos = draglist.getCurrentOffset( thisElem, itemTagName );

			//var listItems = this.getListItems( itemTagName );

			// if bottom edge is below top of lower item.

			var testMoveUp;
			var testMoveDown;
			if ( horizontal )
				{
				testMoveUp = (x + draglist.getDivWidth(thisElem) > draglist.offsetsX[pos + 1] + draglist.getDivWidth( draglist.listItems[pos + 1] ));
				testMoveDown = x < draglist.offsetsX[pos - 1];
				}
			else
				{
				testMoveUp = (y + draglist.getDivHeight(thisElem) > draglist.offsetsY[pos + 1] + draglist.getDivHeight( draglist.listItems[pos + 1] ));
				testMoveDown = y < draglist.offsetsY[pos - 1];
				}

			if (( pos != draglist.listItems.length - 1) && testMoveUp )
				{ 
				draglist.listRoot.removeChild(thisElem);

				if ( pos + 1 == draglist.listItems.length )
					{
					draglist.listRoot.appendChild( thisElem );
					}
				else
					{
					draglist.listRoot.insertBefore(thisElem, draglist.listItems[pos+1]);
					}

				thisElem.style["top"] = "0px";
				thisElem.style["left"] = "0px";
				}
			else if ( pos != 0 && testMoveDown ) 
				{ 
				draglist.listRoot.removeChild(thisElem);
				draglist.listRoot.insertBefore(thisElem, draglist.listItems[pos-1]);
				thisElem.style["top"] = "0px";
				thisElem.style["left"] = "0px";
				}

			};

		this.listItems[i].onDragEnd = function(x,y,thisElem) 
			{
			thisElem.style["top"] = "0px";
			thisElem.style["left"] = "0px";
			}

		}	// end of for loop.

	this.recalcOffsets( itemTagName );

	}	// end of setup.

// ----------------------------------------------


/**
* update the order value fields and submit the form.
*/

fv_dragList.prototype.do_submit = function( formName, dragListRootId )
	{

	var listOrderItems = this.listRoot.getElementsByTagName("input");

	for (var i = 0; i < listOrderItems.length; i++) 
		{
		listOrderItems[i].value = i;
		}

	expr = "document." + formName + ".submit()";

	eval( expr );
	}

// ----------------------------------------------
// "Private" methods.
// ----------------------------------------------

fv_dragList.prototype.recalcOffsets = function( itemTagName ) 
	{
	var listItems = this.getListItems( itemTagName );

	for (var i = 0; i < listItems.length; i++) 
		{
		this.offsetsX[i] = listItems[i].offsetLeft;
		this.offsetsY[i] = listItems[i].offsetTop;
		}
	}

fv_dragList.prototype.getCurrentOffset = function(elem, itemTagName) 
	{ 
	var listItems = this.getListItems( itemTagName );

	for (var i = 0; i < listItems.length; i++) 
		{
		if (listItems[i] == elem) 
			{ 
			return i;
			}
		}
	}

fv_dragList.prototype.getDivWidth = function(elem) 								  
	{

	if (( elem == undefined) || ( elem.offsetWidth == undefined ))
		return( 0 );

	value = elem.offsetWidth;
	if (isNaN(value))
		{
		value = 0;
		}

	return( value );
	}

fv_dragList.prototype.getDivHeight = function(elem) 
	{

	if (( elem == undefined) || ( elem.offsetHeight == undefined ))
		return( 0 );

	value = elem.offsetHeight;
	if (isNaN(value))
		{
		value = 25;
		}

	return( value );
	}

/**
* return list of draggable items
*/

fv_dragList.prototype.getListItems = function( itemTagName )
	{
	if ( itemTagName == undefined )
		{
		itemTagName = "div";
		}

	var listItems = this.listRoot.getElementsByTagName( itemTagName );

	return( listItems );
	}

// end of draglist class definition.

// -------------------------------------

/**
* add a new dragList to the list of draglists on this page
*
* This implementatoin supports multiple managed draglists on
* a single page. The index is contained in a global dragListIndex
* array that must be declared in the page.
*/

function addDragList( draglist )
	{
	dragListIndex[ draglist.dragListRootId ] = draglist;
	}

// -------------------------------------------------------

/**
* given a draggable div element, return the draglist it belongs to
*
* @see fv_draglist.prototype.setup
* @todo this should probably be a method inside the draglist class.
*/

function getDragList( elem )
	{

	// given a list item return the drag list it belongs to.

	var draglistContainer = elem.parentNode;

	var draglist = dragListIndex[ draglistContainer.id ];

	return( draglist );
	}

// END
OpenPOWER on IntegriCloud