-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboBoxControl.js
More file actions
342 lines (287 loc) · 10.1 KB
/
ComboBoxControl.js
File metadata and controls
342 lines (287 loc) · 10.1 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
define(function (require)
{
require("jquery");
require("jquery-ui");
function ComboBoxControl(container,options)
{
// make sure container is a jQuery object
var container = $(container);
var control;
var self = this;
self.OriginalElement = container;
self.InputElement = null;
self.FieldName = null;
var _input;
var _selectedValue;
var _cache = {};
var _userSource;
if (!(self instanceof ComboBoxControl)) { throw new TypeError("ComboBoxControl must be called using New."); }
var defaults =
{
autocomplete : {
delay: 200,
minLength: 2,
source: [],
autoFocus:true
},
wrapperCSS: {position: "relative", display: "inline-block","white-space":"nowrap"},
toggleCSS: {
position: "relative",
top: "-1px",
bottom: 0,
"margin-left": "-1px",
/*"font-size":"1em",*/
"width":"auto",
"padding": "5px 6px",
"border": "1px solid gray"
},
inputCSS: {
margin: 0,
padding: "2px 5px",
"font-weight" : "normal",
"background" : "white",
"color": "black",
"border": "1px solid gray",
"border-right": "0",
"min-width": "300px",
"height":"17px",
"position": "relative"
},
autocompleteMenuCSS: { "font-size":"1em", "box-shadow": "8px 6px 6px gray","text-align":"left"},
useCache: true
};
var settings = $.extend(true,{},defaults,options);
/**
* Initialize Control. Called at end.
* @constructor
*/
var Init = function()
{
control = CreateControl();
};
/**
* Creates the control
* @constructor
*/
var CreateControl = function()
{
var control = $("<span>").addClass("custom-combobox").css(settings.wrapperCSS);
$.each(container.data,function(key,value){
self.data(key,value);
container.removeAttr("data-" + key);
});
if (container.data("field-name"))
{
self.FieldName = container.data("field-name");
container.data("field-name",null);
container.removeAttr("data-field-name");
}
var autocomplete = CreateAutoComplete();
var dropdown = CreateDropDownButton();
control.append(autocomplete,dropdown);
if (container.is("select") || container.is("input"))
{
control.insertAfter(container);
container.hide();
}
else
{
container.append(control);
}
return control;
};
/**
* Creates the autocomplete box
* @returns {*|jQuery}
* @constructor
*/
var CreateAutoComplete = function()
{
_input = $("<input type='text'>")
.css(settings.inputCSS)
.attr("title", "")
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
.tooltip({tooltipClass: "ui-state-highlight"});
self.InputElement = _input;
if (self.FieldName)
{
_input.attr("data-field-name",self.FieldName);
_input.data("field-name",self.FieldName);
}
settings.autocomplete.select = Select;
_userSource = settings.autocomplete.source;
settings.autocomplete.source = Source;
settings.autocomplete.appendTo = (container.is("select") || container.is("input")) ? container.parent() : container;
_input.autocomplete(settings.autocomplete);
_input.autocomplete("widget").css(settings.autocompleteMenuCSS);
_input.data("ComboBox",self);
container.data("ComboBox",self);
return _input;
};
/**
* Creates the drop down button
* @returns {*|jQuery}
* @constructor
*/
var CreateDropDownButton = function ()
{
var wasOpen = false;
var dropDownButton = $("<a>")
.css(settings.toggleCSS)
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.tooltip()
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("custom-combobox-toggle ui-corner-right")
.mousedown(function () {
var input = control.find("input");
wasOpen = input.autocomplete("widget").is(":visible");
})
.click(function () {
var input = control.find("input");
input.focus();
// Close if already visible
if (wasOpen) { return; }
// save minLength value
var minLength = input.autocomplete( "option", "minLength" );
// set minLength to 0 so we can show all
input.autocomplete( "option", "minLength", 0 );
// Pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
// set minLength back to previous value
input.autocomplete( "option", "minLength", minLength );
});
return dropDownButton;
};
/**
* Remove text from the input if not an item in the list. Not currently used.
* @param event
* @param ui
* @constructor
*/
var RemoveIfInvalid = function (event, ui)
{
// Selected an item, nothing to do
if (ui.item) {
return;
}
// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children("option").each(function () {
if ($(this).text().toLowerCase() === valueLowerCase) {
this.selected = valid = true;
return false;
}
});
// Found a match, nothing to do
if (valid) {
return;
}
// Remove invalid value
this.input
.val("")
.attr("title", value + " didn't match any item")
.tooltip("open");
this.element.val("");
this._delay(function () {
this.input.tooltip("close").attr("title", "");
}, 2500);
this.input.autocomplete("instance").term = "";
};
/**
* Caching function to use if user's source is a function
* @param request
* @param response
* @constructor
*/
var Source = function (request, response)
{
if (settings.useCache && _cache[request.term])
{
response(_cache[request.term]);
}else{
if (typeof _userSource === "function")
{
_userSource.call(self,request, function (userSourceResponseData) {
_cache[request.term] = userSourceResponseData;
response(userSourceResponseData)
});
}else if (typeof _userSource === "string"){
$.get(_userSource,request,function(getResponse)
{
if (!getResponse.status) {
_cache[request.term] = getResponse;
response(getResponse);
}
});
}
}
};
/**
* Triggered when an dropdown item is selected. sets value of original input/select
* @param event
* @param ui
* @returns {boolean}
* @constructor
*/
var Select = function( event, ui )
{
_input.val( ui.item.label );
_selectedValue = ui.item.value;
if ($(container).is("select"))
{
// remove existing options from select
container.find("option").remove();
// add new option to select
container.append(String.format("<option value='{0}'>{1}</option>",ui.item.value,ui.item.label))
}
// set original element value and trigger change event on original input
container.val(_selectedValue).change();
// let others know a select happened
$(self).trigger("select");
// resize to fit
self.Autosize();
return false;
};
/**
* Set combobox text and value
* @param displayText
* @param value
* @constructor
*/
this.SetValue = function(displayText,value)
{
Select(null,{item:{label:displayText,value:value}});
};
/**
* Remove the control and restore the select (if we started with a select)
*/
this.Destroy = function()
{
control.remove();
container.show();
};
/**
* Automatically size the input to be wide enough to show entire text
* @constructor
*/
this.Autosize = function()
{
var span = $("<span></span>").text(_input.val()).css("visibility","hidden");
span.appendTo("body");
_input.css("width",span.width() + 40);
span.remove();
};
// do it!
Init();
}
return ComboBoxControl;
});