Skip to content

Commit 16d8694

Browse files
committed
Added support for map function, drop callback and useful functions to the listbox with drag and drop support
1 parent d240380 commit 16d8694

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

ide/main/src/content/dndReorderedListbox.js

+63-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,27 @@
99
*
1010
* @author Samit Badle
1111
* @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
1316
*/
14-
function DnDReorderedListbox(listBox, listItemsRa) {
17+
function DnDReorderedListbox(listBox, listItemsRa, mapFn, keepInOrder, dropFn) {
1518
this.listBox = listBox;
16-
this.listBoxDnD = new ListboxDnDReorder(this.listBox, 'application/x-' + listBox.id.toLowerCase());
1719
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+
);
1833
this.reload();
1934
}
2035

@@ -35,11 +50,34 @@ DnDReorderedListbox.prototype.reload = function(listItemsRa) {
3550
//load the listbox with the list
3651
var listItems = this.listItems;
3752
for (var j = 0; j < listItems.length; j++) {
38-
list.appendItem(listItems[j], j);
53+
list.appendItem(this.mapFn(listItems[j]), j);
3954
}
4055
};
4156

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+
4277
DnDReorderedListbox.prototype.getListItems = function() {
78+
if (this.keepInOrder) {
79+
return this.listItems;
80+
}
4381
var listItems = this.listItems;
4482
var newRa = new Array(listItems.length);
4583
var list = this.listBox;
@@ -49,6 +87,12 @@ DnDReorderedListbox.prototype.getListItems = function() {
4987
return newRa;
5088
};
5189

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+
5296
/**
5397
* Drag and drop reordering behaviour for the list Box
5498
* 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() {
57101
* @author Samit Badle
58102
* @param listBox
59103
* @param dataType
104+
* @param onDrop
60105
*/
61-
function ListboxDnDReorder(listBox, dataType) {
106+
function ListboxDnDReorder(listBox, dataType, onDrop) {
107+
this.onDrop = onDrop;
62108
this.dataType = dataType; //for e.g. 'application/x-something'
63109
this.listBox = listBox;
64110
var self = this;
@@ -76,7 +122,7 @@ ListboxDnDReorder.prototype.dragStart = function(event) {
76122
if (this.itemCount > 1) {
77123
// You cannot rearrange a single or no items no matter how hard you try
78124
//cache some useful elements first
79-
this.selectedItem = this.listBox.selectedItem;
125+
this.draggedItem = this.listBox.selectedItem;
80126
this.lastListItem = this.listBox.getItemAtIndex(this.itemCount - 1);
81127
var rect = this.lastListItem.getBoundingClientRect();
82128
this.flipYPos = rect.top + (rect.height / 2);
@@ -113,7 +159,7 @@ ListboxDnDReorder.prototype.dragLeave = function(event) {
113159

114160
ListboxDnDReorder.prototype.dragEnd = function(event) {
115161
this._clearDropPosition();
116-
this.selectedItem = null;
162+
this.draggedItem = null;
117163
this.curItem = null;
118164
};
119165

@@ -122,16 +168,23 @@ ListboxDnDReorder.prototype.drop = function(event) {
122168
if (this.dropAfter) {
123169
dropIndex++;
124170
}
125-
var selectedIndex = this.listBox.getIndexOfItem(this.selectedItem);
171+
var selectedIndex = this.listBox.getIndexOfItem(this.draggedItem);
126172
this._clearDropPosition();
127173
if (selectedIndex < dropIndex) {
128174
dropIndex--;
129175
}
130176
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;
133183
this.listBox.removeItemAt(selectedIndex);
134184
this.listBox.insertItemAt(dropIndex, lbl, userData);
185+
if (selectDropped) {
186+
this.listBox.selectedIndex = dropIndex;
187+
}
135188
}
136189
};
137190

0 commit comments

Comments
 (0)