summaryrefslogtreecommitdiffstats
path: root/web/widget/combobox.js
blob: 6d9c16e0a305bb94b4daa63a2a13cee8650284cf (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(["acf2/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) {
		el.append($("<option>").attr(
		    {value: value, selected: selected}
		).text(ui_value));
	    }

	    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);
		}
	    );
	}
    });
});