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

define(
    [
	"aconf/dom",
	"aconf/statusbar",
	"aconf/widget/abstract/node",
	"jquery",
	"underscore"
    ],
    function(dom, statusBar, Base, $, _) {
	return Base.extend({
	    showStatus: true,

	    setStatus: function(status) {
		Base.setStatus.call(
		    this, status == "invalid" ? "invalid" : null
		);
	    },

	    render: function(data, meta) {
		this.dynamic = meta.members.dynamic;

		Base.render.call(this, data, meta);

		var table = $("<tbody>");
		this.el.append($("<table>").html(table));

		var self = this;

		_.each(meta.members.choice, function(choice) {
		    var selected = _.contains(data.data, choice.value);
		    if (!(choice.enabled || selected)) return;

		    var cbox = $("<input>").attr("type", "checkbox");
		    if (selected) cbox.attr("checked", "true");

		    var row = $("<tr>");
		    row.append($("<td>").html(cbox));

		    var item = $("<td>");
		    if (choice.ref)
			item.html(dom.objectRef(choice.ref)
				  .text(choice["ui-value"]));
		    else item.text(choice["ui-value"]);
		    row.append(item);

		    function setRowStatus() {
			dom.setStatus(row, data.status(choice.value));
		    }
		    setRowStatus();

		    cbox.change(function() {
			(
			    cbox.is(":checked") ?
				data.add(choice.value) :
				data.delete(choice.value)
			).done(function(txnValid) {
			    self.setStatus(data.status());
			    setRowStatus();
			    statusBar.validationReady(txnValid);
			});
		    });

		    table.append(row);
		});
	    }
	});
    }
);