2013-02-22 15:41:12 -05:00
|
|
|
/**
|
|
|
|
This view handles rendering of a combobox
|
|
|
|
|
|
|
|
@class ComboboxView
|
|
|
|
@extends Discourse.View
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
|
|
|
**/
|
2014-06-11 10:57:30 -04:00
|
|
|
export default Discourse.View.extend({
|
2014-12-04 15:21:47 -05:00
|
|
|
tagName: 'select',
|
2014-09-04 13:50:32 -04:00
|
|
|
attributeBindings: ['tabindex'],
|
2013-02-22 15:41:12 -05:00
|
|
|
classNames: ['combobox'],
|
|
|
|
valueAttribute: 'id',
|
|
|
|
|
2014-02-10 11:15:42 -05:00
|
|
|
buildData: function(o) {
|
|
|
|
var data = "";
|
|
|
|
if (this.dataAttributes) {
|
|
|
|
this.dataAttributes.forEach(function(a) {
|
|
|
|
data += "data-" + a + "=\"" + o.get(a) + "\" ";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2014-02-10 11:15:42 -05:00
|
|
|
render: function(buffer) {
|
|
|
|
var nameProperty = this.get('nameProperty') || 'name',
|
|
|
|
none = this.get('none');
|
2013-07-12 16:24:15 -04:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
// Add none option if required
|
2014-02-10 11:15:42 -05:00
|
|
|
if (typeof none === "string") {
|
|
|
|
buffer.push('<option value="">' + I18n.t(none) + "</option>");
|
|
|
|
} else if (typeof none === "object") {
|
|
|
|
buffer.push("<option value=\"\" " + this.buildData(none) + ">" + Em.get(none, nameProperty) + "</option>");
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
|
|
|
|
2013-06-07 12:13:46 -04:00
|
|
|
var selected = this.get('value');
|
2014-07-04 13:11:43 -04:00
|
|
|
if (!Em.isNone(selected)) { selected = selected.toString(); }
|
2013-05-20 13:42:26 -04:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
if (this.get('content')) {
|
2014-02-10 11:15:42 -05:00
|
|
|
var self = this;
|
|
|
|
this.get('content').forEach(function(o) {
|
|
|
|
var val = o[self.get('valueAttribute')];
|
2014-07-14 15:15:30 -04:00
|
|
|
if (!Em.isNone(val)) { val = val.toString(); }
|
2013-06-07 12:13:46 -04:00
|
|
|
|
|
|
|
var selectedText = (val === selected) ? "selected" : "";
|
2014-03-19 06:04:43 -04:00
|
|
|
buffer.push("<option " + selectedText + " value=\"" + val + "\" " + self.buildData(o) + ">" + Handlebars.Utils.escapeExpression(Em.get(o, nameProperty)) + "</option>");
|
2013-02-20 13:15:50 -05:00
|
|
|
});
|
|
|
|
}
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
2013-05-20 13:42:26 -04:00
|
|
|
valueChanged: function() {
|
2014-12-04 15:21:47 -05:00
|
|
|
var $combo = this.$(),
|
2014-02-10 11:15:42 -05:00
|
|
|
val = this.get('value');
|
2013-06-07 14:45:31 -04:00
|
|
|
if (val !== undefined && val !== null) {
|
2013-06-07 12:13:46 -04:00
|
|
|
$combo.val(val.toString());
|
2013-05-20 13:42:26 -04:00
|
|
|
} else {
|
|
|
|
$combo.val(null);
|
|
|
|
}
|
2013-06-07 14:45:31 -04:00
|
|
|
$combo.trigger("liszt:updated");
|
2013-05-20 13:42:26 -04:00
|
|
|
}.observes('value'),
|
|
|
|
|
2014-03-25 05:59:13 -04:00
|
|
|
contentChanged: function() {
|
|
|
|
this.rerender();
|
|
|
|
}.observes('content.@each'),
|
|
|
|
|
2015-01-02 13:47:07 -05:00
|
|
|
_initializeCombo: function() {
|
2014-12-04 15:21:47 -05:00
|
|
|
var $elem = this.$(),
|
2014-02-10 11:15:42 -05:00
|
|
|
self = this;
|
2013-06-07 12:13:46 -04:00
|
|
|
|
2014-12-04 15:21:47 -05:00
|
|
|
// Workaround for https://github.com/emberjs/ember.js/issues/9813
|
|
|
|
// Can be removed when fixed. Without it, the wrong option is selected
|
|
|
|
this.$('option').each(function(i, o) {
|
|
|
|
o.selected = !!$(o).attr('selected');
|
|
|
|
});
|
|
|
|
|
2014-05-11 00:50:03 -04:00
|
|
|
$elem.select2({formatResult: this.template, minimumResultsForSearch: 5, width: 'resolve'});
|
2013-05-20 13:42:26 -04:00
|
|
|
|
2015-01-02 13:47:07 -05:00
|
|
|
var castInteger = this.get('castInteger');
|
2014-05-11 00:50:03 -04:00
|
|
|
$elem.on("change", function (e) {
|
2015-01-02 13:47:07 -05:00
|
|
|
var val = $(e.target).val();
|
|
|
|
if (val.length && castInteger) {
|
|
|
|
val = parseInt(val, 10);
|
|
|
|
}
|
|
|
|
self.set('value', val);
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
2015-01-02 13:47:07 -05:00
|
|
|
}.on('didInsertElement'),
|
2014-03-25 05:59:13 -04:00
|
|
|
|
|
|
|
willClearRender: function() {
|
2014-09-04 13:50:32 -04:00
|
|
|
var elementId = "s2id_" + this.$().attr('id');
|
|
|
|
Ember.$("#" + elementId).remove();
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|