stable checkin
[xtuple] / lib / enyo-x / source / widgets / checkbox.js
1 /*jshint node:true, indent:2, curly:true, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, trailing:true, white:true */
3 /*global XT:true, Globalize:true, enyo:true, _:true */
4
5 (function () {
6
7   /**
8     @name XV.CheckboxWidget
9     @class An input control consisting of fittable columns:
10       label, decorator, and checkbox.<br />
11     Use to implement a styled checkbox
12       which is made up of a checkbox input control and its label.
13     @extends XV.Input
14    */
15   enyo.kind({
16     name: "XV.CheckboxWidget",
17     kind: "XV.InputWidget",
18     classes: "xv-checkboxwidget",
19     components: [
20       {controlClasses: 'enyo-inline', components: [
21         {name: "label", classes: "xv-label"},
22         {kind: "onyx.InputDecorator", components: [
23           {name: "input", kind: "onyx.Checkbox", onchange: "inputChanged"}
24         ]}
25       ]}
26     ],
27     /**
28     @todo Document the clear method.
29     */
30     clear: function (options) {
31       this.setValue(false, options);
32     },
33
34     inputChanged: function (inSender, inEvent) {
35       var input = this.$.input.getValue();
36       this.setValue(input);
37     },
38     /**
39     @todo Document the valueChanged method.
40     */
41     valueChanged: function (value) {
42       this.$.input.setValue(value);
43       return value;
44     }
45   });
46
47   /**
48     @name XV.StickyCheckboxWidget
49     @class An input control consisting of fittable columns:
50       label, decorator, and checkbox.<br />
51     Use to implement a styled checkbox
52       which is made up of a checkbox input control and its label.
53       Remembers last checked setting using cookies. Not to be
54       used with models.
55     The object should be given a unique name as the name of the
56     object will be used for the cookie name.
57     @extends XV.Input
58    */
59   enyo.kind(/** @lends XV.StickyCheckboxWidget# */{
60     name: "XV.StickyCheckboxWidget",
61     classes: "xv-input xv-checkboxwidget",
62     published: {
63       label: "",
64       disabled: false,
65       checked: false
66     },
67     events: {
68       onValueChange: ""
69     },
70     components: [
71       {controlClasses: 'enyo-inline', components: [
72         {name: "label", classes: "xv-label"},
73         {kind: "onyx.InputDecorator", components: [
74           {name: "input", kind: "onyx.Checkbox", onchange: "inputChanged"}
75         ]}
76       ]}
77     ],
78     /**
79     @todo Document the clear method.
80     */
81     clear: function () {
82       this.$.input.setValue(false);
83     },
84     /**
85     @todo Document the create method.
86     */
87     create: function () {
88       var value,
89         toBoolean = function (val) {
90           if (_.isString(val)) {
91             return val === "true" ? true : false;
92           }
93           return val;
94         };
95
96       this.inherited(arguments);
97       this.labelChanged();
98       if (this.name === "stickyCheckboxWidget") {
99         this._err = true;
100         throw "Sticky Checkbox Widget should have a unique name.";
101       } else {
102         value = toBoolean(enyo.getCookie(this.name));
103         value = _.isBoolean(value) ? value : this.checked;
104         this.checked = value;
105         this.$.input.setValue(value);
106       }
107     },
108     disabledChanged: function () {
109       var disabled = this.getDisabled();
110       this.$.input.setDisabled(disabled);
111       this.$.label.addRemoveClass("disabled", disabled);
112     },
113     /**
114     @todo Document the focus method.
115     */
116     focus: function () {
117       this.$.input.focus();
118     },
119     inputChanged: function (inSender, inEvent) {
120       this.setChecked(this.$.input.checked);
121     },
122     isChecked: function () {
123       return this.$.input.checked;
124     },
125     /**
126     @todo Document the labelChanged method.
127     */
128     labelChanged: function () {
129       var label = (this.getLabel() || ("_" + this.attr || "").loc()) + ":";
130       this.$.label.setContent(label);
131     },
132     checkedChanged: function () {
133       this.$.input.setChecked(this.checked);
134       if (!this._err) {
135         enyo.setCookie(this.name, this.checked);
136       }
137       this.doValueChange({value: this.checked});
138     }
139   });
140
141 }());