9
9
*
10
10
* @author Samit Badle
11
11
* @param listBox - element with list box (must have an id)
12
- * @param listItemsRa - array with string items to show and reorder in the listbox
12
+ * @param listItemsRa - array with string items to show and reorder in the listbox. Can also accept array of objects, but then the mapFn should be provided
13
+ * @param mapFn - an optional function to map the items in the listItemsRa to displayable text
14
+ * @param keepInOrder - arrange the listItemsRa in the order of the items in the list
15
+ * @param dropFn
13
16
*/
14
- function DnDReorderedListbox ( listBox , listItemsRa ) {
17
+ function DnDReorderedListbox ( listBox , listItemsRa , mapFn , keepInOrder , dropFn ) {
15
18
this . listBox = listBox ;
16
- this . listBoxDnD = new ListboxDnDReorder ( this . listBox , 'application/x-' + listBox . id . toLowerCase ( ) ) ;
17
19
this . listItems = listItemsRa ;
20
+ this . mapFn = mapFn || function ( item ) { return item ; } ;
21
+ this . keepInOrder = keepInOrder ? true : false ;
22
+ var self = this ;
23
+ this . listBoxDnD = new ListboxDnDReorder (
24
+ listBox ,
25
+ 'application/x-' + listBox . id . toLowerCase ( ) ,
26
+ keepInOrder ? function ( srcIndex , destIndex ) {
27
+ if ( dropFn ) {
28
+ dropFn ( srcIndex , destIndex ) ;
29
+ }
30
+ self . _reorder ( srcIndex , destIndex ) ;
31
+ } : dropFn
32
+ ) ;
18
33
this . reload ( ) ;
19
34
}
20
35
@@ -35,11 +50,34 @@ DnDReorderedListbox.prototype.reload = function(listItemsRa) {
35
50
//load the listbox with the list
36
51
var listItems = this . listItems ;
37
52
for ( var j = 0 ; j < listItems . length ; j ++ ) {
38
- list . appendItem ( listItems [ j ] , j ) ;
53
+ list . appendItem ( this . mapFn ( listItems [ j ] ) , j ) ;
39
54
}
40
55
} ;
41
56
57
+ DnDReorderedListbox . prototype . updateItem = function ( index ) {
58
+ this . listBox . getItemAtIndex ( index ) . label = this . mapFn ( this . listItems [ index ] ) ;
59
+ } ;
60
+
61
+ DnDReorderedListbox . prototype . appendItem = function ( item ) {
62
+ var newIndex = this . listItems . length ;
63
+ this . listItems . push ( item ) ;
64
+ this . listBox . appendItem ( this . mapFn ( item ) , newIndex ) ;
65
+ return newIndex ;
66
+ } ;
67
+
68
+ DnDReorderedListbox . prototype . removeItem = function ( index ) {
69
+ this . listBox . removeItemAt ( index ) ;
70
+ return this . listItems . splice ( index , 1 ) [ 0 ] ;
71
+ } ;
72
+
73
+ DnDReorderedListbox . prototype . selectItem = function ( index ) {
74
+ this . listBox . selectedIndex = index ;
75
+ } ;
76
+
42
77
DnDReorderedListbox . prototype . getListItems = function ( ) {
78
+ if ( this . keepInOrder ) {
79
+ return this . listItems ;
80
+ }
43
81
var listItems = this . listItems ;
44
82
var newRa = new Array ( listItems . length ) ;
45
83
var list = this . listBox ;
@@ -49,6 +87,12 @@ DnDReorderedListbox.prototype.getListItems = function() {
49
87
return newRa ;
50
88
} ;
51
89
90
+ DnDReorderedListbox . prototype . _reorder = function ( srcIndex , destIndex ) {
91
+ //TODO
92
+ var item = this . listItems . splice ( srcIndex , 1 ) ;
93
+ this . listItems . splice ( destIndex , 0 , item [ 0 ] ) ;
94
+ } ;
95
+
52
96
/**
53
97
* Drag and drop reordering behaviour for the list Box
54
98
* The list box must be loaded before this controller object is created, i.e. call during or after the load event
@@ -57,8 +101,10 @@ DnDReorderedListbox.prototype.getListItems = function() {
57
101
* @author Samit Badle
58
102
* @param listBox
59
103
* @param dataType
104
+ * @param onDrop
60
105
*/
61
- function ListboxDnDReorder ( listBox , dataType ) {
106
+ function ListboxDnDReorder ( listBox , dataType , onDrop ) {
107
+ this . onDrop = onDrop ;
62
108
this . dataType = dataType ; //for e.g. 'application/x-something'
63
109
this . listBox = listBox ;
64
110
var self = this ;
@@ -76,7 +122,7 @@ ListboxDnDReorder.prototype.dragStart = function(event) {
76
122
if ( this . itemCount > 1 ) {
77
123
// You cannot rearrange a single or no items no matter how hard you try
78
124
//cache some useful elements first
79
- this . selectedItem = this . listBox . selectedItem ;
125
+ this . draggedItem = this . listBox . selectedItem ;
80
126
this . lastListItem = this . listBox . getItemAtIndex ( this . itemCount - 1 ) ;
81
127
var rect = this . lastListItem . getBoundingClientRect ( ) ;
82
128
this . flipYPos = rect . top + ( rect . height / 2 ) ;
@@ -113,7 +159,7 @@ ListboxDnDReorder.prototype.dragLeave = function(event) {
113
159
114
160
ListboxDnDReorder . prototype . dragEnd = function ( event ) {
115
161
this . _clearDropPosition ( ) ;
116
- this . selectedItem = null ;
162
+ this . draggedItem = null ;
117
163
this . curItem = null ;
118
164
} ;
119
165
@@ -122,16 +168,23 @@ ListboxDnDReorder.prototype.drop = function(event) {
122
168
if ( this . dropAfter ) {
123
169
dropIndex ++ ;
124
170
}
125
- var selectedIndex = this . listBox . getIndexOfItem ( this . selectedItem ) ;
171
+ var selectedIndex = this . listBox . getIndexOfItem ( this . draggedItem ) ;
126
172
this . _clearDropPosition ( ) ;
127
173
if ( selectedIndex < dropIndex ) {
128
174
dropIndex -- ;
129
175
}
130
176
if ( selectedIndex != dropIndex ) {
131
- var lbl = this . selectedItem . label ;
132
- var userData = this . selectedItem . value ;
177
+ var selectDropped = selectedIndex == this . listBox . selectedIndex ;
178
+ if ( this . onDrop ) {
179
+ this . onDrop ( selectedIndex , dropIndex ) ;
180
+ }
181
+ var lbl = this . draggedItem . label ;
182
+ var userData = this . draggedItem . value ;
133
183
this . listBox . removeItemAt ( selectedIndex ) ;
134
184
this . listBox . insertItemAt ( dropIndex , lbl , userData ) ;
185
+ if ( selectDropped ) {
186
+ this . listBox . selectedIndex = dropIndex ;
187
+ }
135
188
}
136
189
} ;
137
190
0 commit comments