diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-02-06 23:09:13 +0200 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-02-14 22:35:56 +0200 |
commit | 38ac196b2f00c1e13a0928cd892384bc296c8a98 (patch) | |
tree | 473663650b5b4fd898bd9bab4c666dca6b900dd4 /web/widget/checkboxes.js | |
parent | 7d434464a843a8ca8acc903367492186a0ae13e8 (diff) | |
download | aconf-38ac196b2f00c1e13a0928cd892384bc296c8a98.tar.bz2 aconf-38ac196b2f00c1e13a0928cd892384bc296c8a98.tar.xz |
web client: extract each widget to a dedicated file
Diffstat (limited to 'web/widget/checkboxes.js')
-rw-r--r-- | web/widget/checkboxes.js | 78 |
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; + } +); |