-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathselect.js
More file actions
238 lines (195 loc) · 5.45 KB
/
select.js
File metadata and controls
238 lines (195 loc) · 5.45 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
/**
* Select editor
*
* Renders a <select> with given options
*
* Requires an 'options' value on the schema.
* Can be an array of options, a function that calls back with the array of options, a string of HTML
* or a Backbone collection. If a collection, the models must implement a toString() method
*/
Form.editors.Select = Form.editors.Base.extend({
tagName: 'select',
previousValue: '',
events: {
'keyup': 'determineChange',
'keypress': function(event) {
var self = this;
setTimeout(function() {
self.determineChange();
}, 0);
},
'change': function(event) {
this.trigger('change', this);
},
'focus': function(event) {
this.trigger('focus', this);
},
'blur': function(event) {
this.trigger('blur', this);
}
},
initialize: function(options) {
Form.editors.Base.prototype.initialize.call(this, options);
if (!this.schema || !this.schema.options) throw new Error("Missing required 'schema.options'");
},
render: function() {
this.setOptions(this.schema.options);
return this;
},
/**
* Sets the options that populate the <select>
*
* @param {Mixed} options
*/
setOptions: function(options) {
var self = this;
//If a collection was passed, check if it needs fetching
if (options instanceof Backbone.Collection) {
var collection = options;
//Don't do the fetch if it's already populated
if (collection.length > 0) {
this.renderOptions(options);
} else {
collection.fetch({
success: function(collection) {
self.renderOptions(options);
}
});
}
}
//If a function was passed, run it to get the options
else if (_.isFunction(options)) {
options(function(result) {
self.renderOptions(result);
}, self);
}
//Otherwise, ready to go straight to renderOptions
else {
this.renderOptions(options);
}
},
/**
* Adds the <option> html to the DOM
* @param {Mixed} Options as a simple array e.g. ['option1', 'option2']
* or as an array of objects e.g. [{val: 543, label: 'Title for object 543'}]
* or as a string of <option> HTML to insert into the <select>
* or any object
*/
renderOptions: function(options) {
var $select = this.$el,
html;
html = this._getOptionsHtml(options);
//Insert options
$select.html(html);
//Select correct option
this.setValue(this.value);
},
_getOptionsHtml: function(options) {
var html;
//Accept string of HTML
if (_.isString(options)) {
html = options;
}
//Or array
else if (_.isArray(options)) {
html = this._arrayToHtml(options);
}
//Or Backbone collection
else if (options instanceof Backbone.Collection) {
html = this._collectionToHtml(options);
}
else if (_.isFunction(options)) {
var newOptions;
options(function(opts) {
newOptions = opts;
}, this);
html = this._getOptionsHtml(newOptions);
//Or any object
}else{
html = this._objectToHtml(options);
}
return html;
},
determineChange: function(event) {
var currentValue = this.getValue();
var changed = (currentValue !== this.previousValue);
if (changed) {
this.previousValue = currentValue;
this.trigger('change', this);
}
},
getValue: function() {
return this.$el.val();
},
setValue: function(value) {
this.value = value;
this.$el.val(value);
},
focus: function() {
if (this.hasFocus) return;
this.$el.focus();
},
blur: function() {
if (!this.hasFocus) return;
this.$el.blur();
},
/**
* Transforms a collection into HTML ready to use in the renderOptions method
* @param {Backbone.Collection}
* @return {String}
*/
_collectionToHtml: function(collection) {
//Convert collection to array first
var array = [];
collection.each(function(model) {
array.push({ val: model.id, label: model.toString() });
});
//Now convert to HTML
var html = this._arrayToHtml(array);
return html;
},
/**
* Transforms an object into HTML ready to use in the renderOptions method
* @param {Object}
* @return {String}
*/
_objectToHtml: function(obj) {
//Convert object to array first
var array = [];
for(var key in obj){
if( obj.hasOwnProperty( key ) ) {
array.push({ val: key, label: obj[key] });
}
}
//Now convert to HTML
var html = this._arrayToHtml(array);
return html;
},
/**
* Create the <option> HTML
* @param {Array} Options as a simple array e.g. ['option1', 'option2']
* or as an array of objects e.g. [{val: 543, label: 'Title for object 543'}]
* @return {String} HTML
*/
_arrayToHtml: function(array) {
var html = [];
//Generate HTML
_.each(array, function(option) {
if (_.isObject(option)) {
if (option.group) {
var optgroup = $("<optgroup>")
.attr("label",option.group)
.html( this._getOptionsHtml(option.options) );
html.push(optgroup[0]);
} else {
var val = (option.val || option.val === 0) ? option.val : '';
html.push($('<option>').val(val).text(option.label)[0]);
}
}
else {
html.push($('<option>').text(option)[0]);
}
}, this);
return $().add(html);
}
});