Checkbox.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-field-Checkbox'>/**
  19. </span> * @docauthor Robert Dougan &lt;rob@sencha.com&gt;
  20. *
  21. * Single checkbox field. Can be used as a direct replacement for traditional checkbox fields. Also serves as a
  22. * parent class for {@link Ext.form.field.Radio radio buttons}.
  23. *
  24. * # Labeling
  25. *
  26. * In addition to the {@link Ext.form.Labelable standard field labeling options}, checkboxes
  27. * may be given an optional {@link #boxLabel} which will be displayed immediately after checkbox. Also see
  28. * {@link Ext.form.CheckboxGroup} for a convenient method of grouping related checkboxes.
  29. *
  30. * # Values
  31. *
  32. * The main value of a checkbox is a boolean, indicating whether or not the checkbox is checked.
  33. * The following values will check the checkbox:
  34. *
  35. * - `true`
  36. * - `'true'`
  37. * - `'1'`
  38. * - `'on'`
  39. *
  40. * Any other value will uncheck the checkbox.
  41. *
  42. * In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be
  43. * sent as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set
  44. * this value if you have multiple checkboxes with the same {@link #name}. If not specified, the value `on`
  45. * will be used.
  46. *
  47. * # Example usage
  48. *
  49. * @example
  50. * Ext.create('Ext.form.Panel', {
  51. * bodyPadding: 10,
  52. * width: 300,
  53. * title: 'Pizza Order',
  54. * items: [
  55. * {
  56. * xtype: 'fieldcontainer',
  57. * fieldLabel: 'Toppings',
  58. * defaultType: 'checkboxfield',
  59. * items: [
  60. * {
  61. * boxLabel : 'Anchovies',
  62. * name : 'topping',
  63. * inputValue: '1',
  64. * id : 'checkbox1'
  65. * }, {
  66. * boxLabel : 'Artichoke Hearts',
  67. * name : 'topping',
  68. * inputValue: '2',
  69. * checked : true,
  70. * id : 'checkbox2'
  71. * }, {
  72. * boxLabel : 'Bacon',
  73. * name : 'topping',
  74. * inputValue: '3',
  75. * id : 'checkbox3'
  76. * }
  77. * ]
  78. * }
  79. * ],
  80. * bbar: [
  81. * {
  82. * text: 'Select Bacon',
  83. * handler: function() {
  84. * Ext.getCmp('checkbox3').setValue(true);
  85. * }
  86. * },
  87. * '-',
  88. * {
  89. * text: 'Select All',
  90. * handler: function() {
  91. * Ext.getCmp('checkbox1').setValue(true);
  92. * Ext.getCmp('checkbox2').setValue(true);
  93. * Ext.getCmp('checkbox3').setValue(true);
  94. * }
  95. * },
  96. * {
  97. * text: 'Deselect All',
  98. * handler: function() {
  99. * Ext.getCmp('checkbox1').setValue(false);
  100. * Ext.getCmp('checkbox2').setValue(false);
  101. * Ext.getCmp('checkbox3').setValue(false);
  102. * }
  103. * }
  104. * ],
  105. * renderTo: Ext.getBody()
  106. * });
  107. */
  108. Ext.define('Ext.form.field.Checkbox', {
  109. extend: 'Ext.form.field.Base',
  110. alias: ['widget.checkboxfield', 'widget.checkbox'],
  111. alternateClassName: 'Ext.form.Checkbox',
  112. requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager' ],
  113. componentLayout: 'field',
  114. childEls: [
  115. <span id='Ext-form-field-Checkbox-property-boxLabelEl'> /**
  116. </span> * @property {Ext.Element} boxLabelEl
  117. * A reference to the label element created for the {@link #boxLabel}. Only present if the component has been
  118. * rendered and has a boxLabel configured.
  119. */
  120. 'boxLabelEl'
  121. ],
  122. // note: {id} here is really {inputId}, but {cmpId} is available
  123. fieldSubTpl: [
  124. '&lt;tpl if=&quot;boxLabel &amp;&amp; boxLabelAlign == \'before\'&quot;&gt;',
  125. '{beforeBoxLabelTpl}',
  126. '&lt;label id=&quot;{cmpId}-boxLabelEl&quot; {boxLabelAttrTpl} class=&quot;{boxLabelCls} {boxLabelCls}-{boxLabelAlign}&quot; for=&quot;{id}&quot;&gt;',
  127. '{beforeBoxLabelTextTpl}',
  128. '{boxLabel}',
  129. '{afterBoxLabelTextTpl}',
  130. '&lt;/label&gt;',
  131. '{afterBoxLabelTpl}',
  132. '&lt;/tpl&gt;',
  133. // Creates not an actual checkbox, but a button which is given aria role=&quot;checkbox&quot; (If ARIA is required) and
  134. // styled with a custom checkbox image. This allows greater control and consistency in
  135. // styling, and using a button allows it to gain focus and handle keyboard nav properly.
  136. '&lt;input type=&quot;button&quot; id=&quot;{id}&quot; {inputAttrTpl}',
  137. '&lt;tpl if=&quot;tabIdx&quot;&gt; tabIndex=&quot;{tabIdx}&quot;&lt;/tpl&gt;',
  138. '&lt;tpl if=&quot;disabled&quot;&gt; disabled=&quot;disabled&quot;&lt;/tpl&gt;',
  139. '&lt;tpl if=&quot;fieldStyle&quot;&gt; style=&quot;{fieldStyle}&quot;&lt;/tpl&gt;',
  140. ' class=&quot;{fieldCls} {typeCls}&quot; autocomplete=&quot;off&quot; hidefocus=&quot;true&quot; /&gt;',
  141. '&lt;tpl if=&quot;boxLabel &amp;&amp; boxLabelAlign == \'after\'&quot;&gt;',
  142. '{beforeBoxLabelTpl}',
  143. '&lt;label id=&quot;{cmpId}-boxLabelEl&quot; {boxLabelAttrTpl} class=&quot;{boxLabelCls} {boxLabelCls}-{boxLabelAlign}&quot; for=&quot;{id}&quot;&gt;',
  144. '{beforeBoxLabelTextTpl}',
  145. '{boxLabel}',
  146. '{afterBoxLabelTextTpl}',
  147. '&lt;/label&gt;',
  148. '{afterBoxLabelTpl}',
  149. '&lt;/tpl&gt;',
  150. {
  151. disableFormats: true,
  152. compiled: true
  153. }
  154. ],
  155. subTplInsertions: [
  156. <span id='Ext-form-field-Checkbox-cfg-beforeBoxLabelTpl'> /**
  157. </span> * @cfg {String/Array/Ext.XTemplate} beforeBoxLabelTpl
  158. * An optional string or `XTemplate` configuration to insert in the field markup
  159. * before the box label element. If an `XTemplate` is used, the component's
  160. * {@link Ext.form.field.Base#getSubTplData subTpl data} serves as the context.
  161. */
  162. 'beforeBoxLabelTpl',
  163. <span id='Ext-form-field-Checkbox-cfg-afterBoxLabelTpl'> /**
  164. </span> * @cfg {String/Array/Ext.XTemplate} afterBoxLabelTpl
  165. * An optional string or `XTemplate` configuration to insert in the field markup
  166. * after the box label element. If an `XTemplate` is used, the component's
  167. * {@link Ext.form.field.Base#getSubTplData subTpl data} serves as the context.
  168. */
  169. 'afterBoxLabelTpl',
  170. <span id='Ext-form-field-Checkbox-cfg-beforeBoxLabelTextTpl'> /**
  171. </span> * @cfg {String/Array/Ext.XTemplate} beforeBoxLabelTextTpl
  172. * An optional string or `XTemplate` configuration to insert in the field markup
  173. * before the box label text. If an `XTemplate` is used, the component's
  174. * {@link Ext.form.field.Base#getSubTplData subTpl data} serves as the context.
  175. */
  176. 'beforeBoxLabelTextTpl',
  177. <span id='Ext-form-field-Checkbox-cfg-afterBoxLabelTextTpl'> /**
  178. </span> * @cfg {String/Array/Ext.XTemplate} afterBoxLabelTextTpl
  179. * An optional string or `XTemplate` configuration to insert in the field markup
  180. * after the box label text. If an `XTemplate` is used, the component's
  181. * {@link Ext.form.field.Base#getSubTplData subTpl data} serves as the context.
  182. */
  183. 'afterBoxLabelTextTpl',
  184. <span id='Ext-form-field-Checkbox-cfg-boxLabelAttrTpl'> /**
  185. </span> * @cfg {String/Array/Ext.XTemplate} boxLabelAttrTpl
  186. * An optional string or `XTemplate` configuration to insert in the field markup
  187. * inside the box label element (as attributes). If an `XTemplate` is used, the component's
  188. * {@link Ext.form.field.Base#getSubTplData subTpl data} serves as the context.
  189. */
  190. 'boxLabelAttrTpl',
  191. // inherited
  192. 'inputAttrTpl'
  193. ],
  194. /*
  195. * @property {Boolean} isCheckbox
  196. * `true` in this class to identify an object as an instantiated Checkbox, or subclass thereof.
  197. */
  198. isCheckbox: true,
  199. <span id='Ext-form-field-Checkbox-cfg-focusCls'> /**
  200. </span> * @cfg {String} [focusCls='x-form-cb-focus']
  201. * The CSS class to use when the checkbox receives focus
  202. */
  203. focusCls: 'form-cb-focus',
  204. <span id='Ext-form-field-Checkbox-cfg-fieldCls'> /**
  205. </span> * @cfg {String} [fieldCls='x-form-field']
  206. * The default CSS class for the checkbox
  207. */
  208. <span id='Ext-form-field-Checkbox-cfg-fieldBodyCls'> /**
  209. </span> * @cfg {String} [fieldBodyCls='x-form-cb-wrap']
  210. * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
  211. * .
  212. */
  213. fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
  214. <span id='Ext-form-field-Checkbox-cfg-checked'> /**
  215. </span> * @cfg {Boolean} checked
  216. * true if the checkbox should render initially checked
  217. */
  218. checked: false,
  219. <span id='Ext-form-field-Checkbox-cfg-checkedCls'> /**
  220. </span> * @cfg {String} [checkedCls='x-form-cb-checked']
  221. * The CSS class added to the component's main element when it is in the checked state.
  222. */
  223. checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
  224. <span id='Ext-form-field-Checkbox-cfg-boxLabel'> /**
  225. </span> * @cfg {String} boxLabel
  226. * An optional text label that will appear next to the checkbox. Whether it appears before or after the checkbox is
  227. * determined by the {@link #boxLabelAlign} config.
  228. */
  229. <span id='Ext-form-field-Checkbox-cfg-boxLabelCls'> /**
  230. </span> * @cfg {String} [boxLabelCls='x-form-cb-label']
  231. * The CSS class to be applied to the {@link #boxLabel} element
  232. */
  233. boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
  234. <span id='Ext-form-field-Checkbox-cfg-boxLabelAlign'> /**
  235. </span> * @cfg {String} boxLabelAlign
  236. * The position relative to the checkbox where the {@link #boxLabel} should appear. Recognized values are 'before'
  237. * and 'after'.
  238. */
  239. boxLabelAlign: 'after',
  240. <span id='Ext-form-field-Checkbox-cfg-inputValue'> /**
  241. </span> * @cfg {String} inputValue
  242. * The value that should go into the generated input element's value attribute and should be used as the parameter
  243. * value when submitting as part of a form.
  244. */
  245. inputValue: 'on',
  246. <span id='Ext-form-field-Checkbox-cfg-uncheckedValue'> /**
  247. </span> * @cfg {String} uncheckedValue
  248. * If configured, this will be submitted as the checkbox's value during form submit if the checkbox is unchecked. By
  249. * default this is undefined, which results in nothing being submitted for the checkbox field when the form is
  250. * submitted (the default behavior of HTML checkboxes).
  251. */
  252. <span id='Ext-form-field-Checkbox-cfg-handler'> /**
  253. </span> * @cfg {Function} handler
  254. * A function called when the {@link #checked} value changes (can be used instead of handling the {@link #change
  255. * change event}).
  256. * @cfg {Ext.form.field.Checkbox} handler.checkbox The Checkbox being toggled.
  257. * @cfg {Boolean} handler.checked The new checked state of the checkbox.
  258. */
  259. <span id='Ext-form-field-Checkbox-cfg-scope'> /**
  260. </span> * @cfg {Object} scope
  261. * An object to use as the scope ('this' reference) of the {@link #handler} function.
  262. *
  263. * Defaults to this Checkbox.
  264. */
  265. // private overrides
  266. checkChangeEvents: [],
  267. inputType: 'checkbox',
  268. // private
  269. onRe: /^on$/i,
  270. initComponent: function() {
  271. this.callParent(arguments);
  272. this.getManager().add(this);
  273. },
  274. initValue: function() {
  275. var me = this,
  276. checked = !!me.checked;
  277. <span id='Ext-form-field-Checkbox-property-originalValue'> /**
  278. </span> * @property {Object} originalValue
  279. * The original value of the field as configured in the {@link #checked} configuration, or as loaded by the last
  280. * form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} setting is `true`.
  281. */
  282. me.originalValue = me.lastValue = checked;
  283. // Set the initial checked state
  284. me.setValue(checked);
  285. },
  286. getElConfig: function() {
  287. var me = this;
  288. // Add the checked class if this begins checked
  289. if (me.isChecked(me.rawValue, me.inputValue)) {
  290. me.addCls(me.checkedCls);
  291. }
  292. return me.callParent();
  293. },
  294. getFieldStyle: function() {
  295. return Ext.isObject(this.fieldStyle) ? Ext.DomHelper.generateStyles(this.fieldStyle) : this.fieldStyle ||'';
  296. },
  297. getSubTplData: function() {
  298. var me = this;
  299. return Ext.apply(me.callParent(), {
  300. disabled : me.readOnly || me.disabled,
  301. boxLabel : me.boxLabel,
  302. boxLabelCls : me.boxLabelCls,
  303. boxLabelAlign : me.boxLabelAlign
  304. });
  305. },
  306. initEvents: function() {
  307. var me = this;
  308. me.callParent();
  309. me.mon(me.inputEl, 'click', me.onBoxClick, me);
  310. },
  311. <span id='Ext-form-field-Checkbox-method-onBoxClick'> /**
  312. </span> * @private Handle click on the checkbox button
  313. */
  314. onBoxClick: function(e) {
  315. var me = this;
  316. if (!me.disabled &amp;&amp; !me.readOnly) {
  317. this.setValue(!this.checked);
  318. }
  319. },
  320. <span id='Ext-form-field-Checkbox-method-getRawValue'> /**
  321. </span> * Returns the checked state of the checkbox.
  322. * @return {Boolean} True if checked, else false
  323. */
  324. getRawValue: function() {
  325. return this.checked;
  326. },
  327. <span id='Ext-form-field-Checkbox-method-getValue'> /**
  328. </span> * Returns the checked state of the checkbox.
  329. * @return {Boolean} True if checked, else false
  330. */
  331. getValue: function() {
  332. return this.checked;
  333. },
  334. <span id='Ext-form-field-Checkbox-method-getSubmitValue'> /**
  335. </span> * Returns the submit value for the checkbox which can be used when submitting forms.
  336. * @return {String} If checked the {@link #inputValue} is returned; otherwise the {@link #uncheckedValue}
  337. * (or null if the latter is not configured).
  338. */
  339. getSubmitValue: function() {
  340. var unchecked = this.uncheckedValue,
  341. uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
  342. return this.checked ? this.inputValue : uncheckedVal;
  343. },
  344. isChecked: function(rawValue, inputValue) {
  345. return (rawValue === true || rawValue === 'true' || rawValue === '1' || rawValue === 1 ||
  346. (((Ext.isString(rawValue) || Ext.isNumber(rawValue)) &amp;&amp; inputValue) ? rawValue == inputValue : this.onRe.test(rawValue)));
  347. },
  348. <span id='Ext-form-field-Checkbox-method-setRawValue'> /**
  349. </span> * Sets the checked state of the checkbox.
  350. *
  351. * @param {Boolean/String/Number} value The following values will check the checkbox:
  352. * `true, 'true', '1', 1, or 'on'`, as well as a String that matches the {@link #inputValue}.
  353. * Any other value will uncheck the checkbox.
  354. * @return {Boolean} the new checked state of the checkbox
  355. */
  356. setRawValue: function(value) {
  357. var me = this,
  358. inputEl = me.inputEl,
  359. checked = me.isChecked(value, me.inputValue);
  360. if (inputEl) {
  361. me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
  362. }
  363. me.checked = me.rawValue = checked;
  364. return checked;
  365. },
  366. <span id='Ext-form-field-Checkbox-method-setValue'> /**
  367. </span> * Sets the checked state of the checkbox, and invokes change detection.
  368. * @param {Boolean/String} checked The following values will check the checkbox: `true, 'true', '1', or 'on'`, as
  369. * well as a String that matches the {@link #inputValue}. Any other value will uncheck the checkbox.
  370. * @return {Ext.form.field.Checkbox} this
  371. */
  372. setValue: function(checked) {
  373. var me = this,
  374. boxes, i, len, box;
  375. // If an array of strings is passed, find all checkboxes in the group with the same name as this
  376. // one and check all those whose inputValue is in the array, unchecking all the others. This is to
  377. // facilitate setting values from Ext.form.Basic#setValues, but is not publicly documented as we
  378. // don't want users depending on this behavior.
  379. if (Ext.isArray(checked)) {
  380. boxes = me.getManager().getByName(me.name, me.getFormId()).items;
  381. len = boxes.length;
  382. for (i = 0; i &lt; len; ++i) {
  383. box = boxes[i];
  384. box.setValue(Ext.Array.contains(checked, box.inputValue));
  385. }
  386. } else {
  387. me.callParent(arguments);
  388. }
  389. return me;
  390. },
  391. // private
  392. valueToRaw: function(value) {
  393. // No extra conversion for checkboxes
  394. return value;
  395. },
  396. <span id='Ext-form-field-Checkbox-method-onChange'> /**
  397. </span> * @private
  398. * Called when the checkbox's checked state changes. Invokes the {@link #handler} callback
  399. * function if specified.
  400. */
  401. onChange: function(newVal, oldVal) {
  402. var me = this,
  403. handler = me.handler;
  404. if (handler) {
  405. handler.call(me.scope || me, me, newVal);
  406. }
  407. me.callParent(arguments);
  408. },
  409. resetOriginalValue: function(/* private */ fromBoxInGroup){
  410. var me = this,
  411. boxes,
  412. box,
  413. len,
  414. i;
  415. // If we're resetting the value of a field in a group, also reset the others.
  416. if (!fromBoxInGroup) {
  417. boxes = me.getManager().getByName(me.name, me.getFormId()).items;
  418. len = boxes.length;
  419. for (i = 0; i &lt; len; ++i) {
  420. box = boxes[i];
  421. if (box !== me) {
  422. boxes[i].resetOriginalValue(true);
  423. }
  424. }
  425. }
  426. me.callParent();
  427. },
  428. // inherit docs
  429. beforeDestroy: function(){
  430. this.callParent();
  431. this.getManager().removeAtKey(this.id);
  432. },
  433. // inherit docs
  434. getManager: function() {
  435. return Ext.form.CheckboxManager;
  436. },
  437. onEnable: function() {
  438. var me = this,
  439. inputEl = me.inputEl;
  440. me.callParent();
  441. if (inputEl) {
  442. // Can still be disabled if the field is readOnly
  443. inputEl.dom.disabled = me.readOnly;
  444. }
  445. },
  446. setReadOnly: function(readOnly) {
  447. var me = this,
  448. inputEl = me.inputEl;
  449. if (inputEl) {
  450. // Set the button to disabled when readonly
  451. inputEl.dom.disabled = !!readOnly || me.disabled;
  452. }
  453. me.callParent(arguments);
  454. },
  455. getFormId: function(){
  456. var me = this,
  457. form;
  458. if (!me.formId) {
  459. form = me.up('form');
  460. if (form) {
  461. me.formId = form.id;
  462. }
  463. }
  464. return me.formId;
  465. }
  466. });
  467. </pre>
  468. </body>
  469. </html>