summaryrefslogtreecommitdiffstats
path: root/web/widget/checkboxes.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/widget/checkboxes.js')
-rw-r--r--web/widget/checkboxes.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/web/widget/checkboxes.js b/web/widget/checkboxes.js
new file mode 100644
index 0000000..a34aced
--- /dev/null
+++ b/web/widget/checkboxes.js
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2012-2014 Kaarle Ritvanen
+ * See LICENSE file for license details
+ */
+
+define(
+ [
+ "acf2/dom",
+ "acf2/statusbar",
+ "acf2/widget/abstract/inline",
+ "jquery",
+ "underscore"
+ ],
+ function(dom, statusBar, Base, $, _) {
+ var Class = Base.extend({
+ showStatus: true,
+
+ setStatus: function(status) {
+ this.super(
+ Class,
+ "setStatus",
+ status == "invalid" ? "invalid" : null
+ );
+ },
+
+ render: function(data, meta) {
+ this.dynamic = meta.members.dynamic;
+
+ this.super(Class, "render", 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);
+ });
+ }
+ });
+
+ return Class;
+ }
+);