-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoxSelector.js
273 lines (237 loc) · 7.41 KB
/
BoxSelector.js
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
/**
* # BoxSelector
* Copyright(c) 2019 Stefano Balietti
* MIT Licensed
*
* Creates a simple box that opens a menu of items to choose from
*
* www.nodegame.org
*/
(function(node) {
"use strict";
var NDDB = node.NDDB;
node.widgets.register('BoxSelector', BoxSelector);
// ## Meta-data
BoxSelector.version = '1.0.0';
BoxSelector.description = 'Creates a simple box that opens a menu ' +
'of items to choose from.';
BoxSelector.panel = false;
BoxSelector.title = false;
BoxSelector.className = 'boxselector';
// ## Dependencies
BoxSelector.dependencies = {
JSUS: {}
};
/**
* ## BoxSelector constructor
*
* `BoxSelector` is a simple configurable chat
*
* @see BoxSelector.init
*/
function BoxSelector() {
/**
* ### BoxSelector.button
*
* The button that if pressed shows the items
*
* @see BoxSelector.ul
*/
this.button = null;
/**
* ### BoxSelector.buttonText
*
* The text on the button
*
* @see BoxSelector.button
*/
this.buttonText = '';
/**
* ### BoxSelector.items
*
* List of items to choose from
*/
this.items = [];
/**
* ### BoxSelector.onclick
*
* A callback to call when an item from the list is clicked
*
* Callback is executed with the BoxSelector instance as context.
*
* Optional. If not specified, items won't be clickable.
*
* @see BoxSelector.items
*/
this.onclick = null;
/**
* ### BoxSelector.getDescr
*
* A callback that renders an element into a text
*/
this.getDescr = null;
/**
* ### BoxSelector.getId
*
* A callback that returns the id of an item
*
* Default: returns item.id.
*/
this.getId = function(item) { return item.id; };
/**
* ### BoxSelector.ul
*
* The HTML UL element displaying the list of items
*
* @see BoxSelector.items
*/
this.ul = null;
}
// ## BoxSelector methods
/**
* ### BoxSelector.init
*
* Initializes the widget
*
* @param {object} options Configuration options.
*/
BoxSelector.prototype.init = function(options) {
if (options.onclick) {
if ('function' !== typeof options.onclick) {
throw new Error('BoxSelector.init: options.getId must be ' +
'function or undefined. Found: ' +
options.getId);
}
this.onclick = options.onclick;
}
if ('function' !== typeof options.getDescr) {
throw new Error('BoxSelector.init: options.getDescr must be ' +
'function. Found: ' + options.getDescr);
}
this.getDescr = options.getDescr;
if (options.getId && 'function' !== typeof options.getId) {
throw new Error('BoxSelector.init: options.getId must be ' +
'function or undefined. Found: ' + options.getId);
}
this.getId = options.getId;
};
BoxSelector.prototype.append = function() {
var that, ul, btn, btnGroup, toggled;
btnGroup = W.add('div', this.bodyDiv);
btnGroup.role = 'group';
btnGroup['aria-label'] = 'Select Items';
btnGroup.className = 'btn-group dropup';
// Here we create the Button holding the treatment.
btn = this.button = W.add('button', btnGroup);
btn.className = 'btn btn-default btn dropdown-toggle';
btn['data-toggle'] = 'dropdown';
btn['aria-haspopup'] = 'true';
btn['aria-expanded'] = 'false';
btn.innerHTML = this.buttonText + ' ';
W.add('span', btn, { className: 'caret' });
// Here the create the UL of treatments.
// It will be populated later.
ul = this.ul = W.add('ul', btnGroup);
ul.className = 'dropdown-menu';
ul.style.display = 'none';
// Variable toggled controls if the dropdown menu
// is displayed (we are not using bootstrap js files)
// and we redo the job manually here.
toggled = false;
btn.onclick = function() {
if (toggled) {
ul.style.display = 'none';
toggled = false;
}
else {
ul.style.display = 'block';
toggled = true;
}
};
if (this.onclick) {
that = this;
ul.onclick = function(eventData) {
var id, i, len;
id = eventData.target;
// When '' is hidden by bootstrap class.
ul.style.display = '';
toggled = false;
id = id.parentNode.id;
// Clicked on description?
if (!id) id = eventData.target.parentNode.parentNode.id;
// Nothing relevant clicked (e.g., header).
if (!id) return;
len = that.items.length;
// Call the onclick.
for ( i = 0 ; i < len ; i++) {
if (that.getId(that.items[i]) === id) {
that.onclick.call(that, that.items[i], id);
break;
}
}
};
}
};
/**
* ### BoxSelector.addItem
*
* Adds an item to the list and renders it
*
* @param {mixed} item The item to add
*/
BoxSelector.prototype.addItem = function(item) {
var ul, li, a, tmp;
ul = this.ul;
li = document.createElement('li');
// Text.
tmp = this.getDescr(item);
if (!tmp || 'string' !== typeof tmp) {
throw new Error('BoxSelector.addItem: getDescr did not return a ' +
'string. Found: ' + tmp + '. Item: ' + item);
}
if (this.onclick) {
a = document.createElement('a');
a.href = '#';
a.innerHTML = tmp;
li.appendChild(a);
}
else {
li.innerHTML = tmp;
}
// Id.
tmp = this.getId(item);
if (!tmp || 'string' !== typeof tmp) {
throw new Error('BoxSelector.addItem: getId did not return a ' +
'string. Found: ' + tmp + '. Item: ' + item);
}
li.id = tmp;
li.className = 'dropdown-header';
ul.appendChild(li);
this.items.push(item);
};
/**
* ### BoxSelector.removeItem
*
* Removes an item with given id from the list and the dom
*
* @param {mixed} item The item to add
*
* @return {mixed|boolean} The removed item or false if not found
*/
BoxSelector.prototype.removeItem = function(id) {
var i, len, elem;
len = this.items.length;
for ( i = 0 ; i < len ; i++) {
if (this.getId(this.items[i]) === id) {
elem = W.gid(id);
this.ul.removeChild(elem);
return this.items.splice(i, 1);
}
}
return false;
};
BoxSelector.prototype.getValues = function() {
return this.items;
};
// ## Helper functions.
})(node);