Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added widgets.js

  • Loading branch information...
commit bda80b1566765a08248e5765d42755b8a53a50e0 1 parent b10f076
@nrw nrw authored
Showing with 54 additions and 3 deletions.
  1. +51 −0 couchtypes/widgets.js
  2. +3 −3 kanso.json
View
51 couchtypes/widgets.js
@@ -0,0 +1,51 @@
+(function() {
+ var Widget, _;
+
+ Widget = require('couchtypes/widgets').Widget;
+
+ _ = require('underscore')._;
+
+ exports.multicheckbox = function(options) {
+ var w;
+ w = new Widget('multicheckbox', options);
+ w.rows = options.rows || [];
+ w.toHTML = function(name, value, raw, field, options) {
+ var html, r, r_id, _i, _len, _ref;
+ this.id = this._id(name, options.offset, options.path_extra);
+ html = "<div id=\"" + this.id + "\">";
+ html += "<input name=\"" + this._name(name, options.offset) + "\" " + "type=\"hidden\" value=\"[]\"/>";
+ html += '<ul>';
+ _ref = this.rows;
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ r = _ref[_i];
+ r_id = this.id + '_' + r._id;
+ html += '<li>';
+ html += '<input type="checkbox" id="' + r_id + '" value="' + r.name + '" />';
+ html += '<label for="' + r_id + '">' + r.name + '</label>';
+ html += '</li>';
+ }
+ html += "</ul>";
+ return html += "</div>";
+ };
+ w.clientInit = function(field, path, value, raw, errors, options) {
+ var el;
+ el = $('#' + this.id + ' input[type=hidden]').first();
+ return $('#' + this.id + ' input[type=checkbox]').change(function(event) {
+ var box, index, list;
+ list = JSON.parse(el.attr("value"));
+ box = event.target;
+ index = list.indexOf(box.value);
+ if (box.checked && index === -1) {
+ list.push(box.value);
+ } else if (index !== -1) {
+ list = _.reject(list, function(id) {
+ return id === box.value;
+ });
+ }
+ return el.val(JSON.stringify(list));
+ });
+ };
+ return w;
+ };
+
+}).call(this);
View
6 kanso.json
@@ -1,5 +1,5 @@
{
- "name": "couchtypes-widgets-multicheck",
- "version": "0.0.1",
- "description": "A widget for selecting more than one checkbox in a form"
+ "name": "couchtypes-widgets-multicheck",
+ "version": "0.0.1",
+ "description": "A widget for selecting more than one checkbox in a form"
}

0 comments on commit bda80b1

Please sign in to comment.
Something went wrong with that request. Please try again.