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

define(
    [
	"acf2/dom",
	"acf2/statusbar",
	"acf2/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", checked: selected
		    });

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