summaryrefslogtreecommitdiffstats
path: root/web/widget/combobox.js
blob: e1759adef6250bd6ad8ffc400326f6fa7f6f2e30 (plain)
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
/*
 * Copyright (c) 2012-2014 Kaarle Ritvanen
 * See LICENSE file for license details
 */

define(["aconf/widget/field", "jquery", "underscore"], function(Base, $, _) {
    return Base.extend({
	createEl: function() { return $("<select>"); },

	staticRender: function(value, meta) {
	    return Base.staticRender.call(
		this,
		value ? _.findWhere(
		    meta.choice, {value: value}
		)["ui-value"] : "",
		meta
	    );
	},

	render: function(value, meta) {
	    var el = this.field.empty();

	    function opt(value, ui_value, selected) {
		var option = $("<option>").attr("value", value).text(ui_value);
		if (selected) option.attr("selected", "true");
		el.append(option);
	    }

	    if (!meta.required) opt("", "(none)", value == null);

	    _.each(
		meta.choice,
		function(choice) {
		    var selected = value == choice.value;
		    if (choice.enabled || selected)
			opt(choice.value, choice["ui-value"], selected);
		}
	    );
	}
    });
});