You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

510 lines
19 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-CheckboxGroup-method-constructor'><span id='Ext-form-CheckboxGroup'>/**
  19. </span></span> * @class Ext.form.CheckboxGroup
  20. * @extends Ext.form.Field
  21. * &lt;p&gt;A grouping container for {@link Ext.form.Checkbox} controls.&lt;/p&gt;
  22. * &lt;p&gt;Sample usage:&lt;/p&gt;
  23. * &lt;pre&gt;&lt;code&gt;
  24. var myCheckboxGroup = new Ext.form.CheckboxGroup({
  25. id:'myGroup',
  26. xtype: 'checkboxgroup',
  27. fieldLabel: 'Single Column',
  28. itemCls: 'x-check-group-alt',
  29. // Put all controls in a single column with width 100%
  30. columns: 1,
  31. items: [
  32. {boxLabel: 'Item 1', name: 'cb-col-1'},
  33. {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
  34. {boxLabel: 'Item 3', name: 'cb-col-3'}
  35. ]
  36. });
  37. * &lt;/code&gt;&lt;/pre&gt;
  38. * @constructor
  39. * Creates a new CheckboxGroup
  40. * @param {Object} config Configuration options
  41. * @xtype checkboxgroup
  42. */
  43. Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
  44. <span id='Ext-form-CheckboxGroup-cfg-items'> /**
  45. </span> * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
  46. * to arrange in the group.
  47. */
  48. <span id='Ext-form-CheckboxGroup-cfg-columns'> /**
  49. </span> * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
  50. * checkbox/radio controls using automatic layout. This config can take several types of values:
  51. * &lt;ul&gt;&lt;li&gt;&lt;b&gt;'auto'&lt;/b&gt; : &lt;p class=&quot;sub-desc&quot;&gt;The controls will be rendered one per column on one row and the width
  52. * of each column will be evenly distributed based on the width of the overall field container. This is the default.&lt;/p&gt;&lt;/li&gt;
  53. * &lt;li&gt;&lt;b&gt;Number&lt;/b&gt; : &lt;p class=&quot;sub-desc&quot;&gt;If you specific a number (e.g., 3) that number of columns will be
  54. * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.&lt;/p&gt;&lt;/li&gt;
  55. * &lt;li&gt;&lt;b&gt;Array&lt;/b&gt; : Object&lt;p class=&quot;sub-desc&quot;&gt;You can also specify an array of column widths, mixing integer
  56. * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
  57. * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
  58. * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
  59. * container you should do so.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;
  60. */
  61. columns : 'auto',
  62. <span id='Ext-form-CheckboxGroup-cfg-vertical'> /**
  63. </span> * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
  64. * top to bottom before starting on the next column. The number of controls in each column will be automatically
  65. * calculated to keep columns as even as possible. The default value is false, so that controls will be added
  66. * to columns one at a time, completely filling each row left to right before starting on the next row.
  67. */
  68. vertical : false,
  69. <span id='Ext-form-CheckboxGroup-cfg-allowBlank'> /**
  70. </span> * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
  71. * If no items are selected at validation time, {@link @blankText} will be used as the error text.
  72. */
  73. allowBlank : true,
  74. <span id='Ext-form-CheckboxGroup-cfg-blankText'> /**
  75. </span> * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to &quot;You must
  76. * select at least one item in this group&quot;)
  77. */
  78. blankText : &quot;You must select at least one item in this group&quot;,
  79. <span id='Ext-form-CheckboxGroup-property-defaultType'> // private
  80. </span> defaultType : 'checkbox',
  81. <span id='Ext-form-CheckboxGroup-property-groupCls'> // private
  82. </span> groupCls : 'x-form-check-group',
  83. <span id='Ext-form-CheckboxGroup-method-initComponent'> // private
  84. </span> initComponent: function(){
  85. this.addEvents(
  86. <span id='Ext-form-CheckboxGroup-event-change'> /**
  87. </span> * @event change
  88. * Fires when the state of a child checkbox changes.
  89. * @param {Ext.form.CheckboxGroup} this
  90. * @param {Array} checked An array containing the checked boxes.
  91. */
  92. 'change'
  93. );
  94. this.on('change', this.validate, this);
  95. Ext.form.CheckboxGroup.superclass.initComponent.call(this);
  96. },
  97. <span id='Ext-form-CheckboxGroup-method-onRender'> // private
  98. </span> onRender : function(ct, position){
  99. if(!this.el){
  100. var panelCfg = {
  101. autoEl: {
  102. id: this.id
  103. },
  104. cls: this.groupCls,
  105. layout: 'column',
  106. renderTo: ct,
  107. bufferResize: false // Default this to false, since it doesn't really have a proper ownerCt.
  108. };
  109. var colCfg = {
  110. xtype: 'container',
  111. defaultType: this.defaultType,
  112. layout: 'form',
  113. defaults: {
  114. hideLabel: true,
  115. anchor: '100%'
  116. }
  117. };
  118. if(this.items[0].items){
  119. // The container has standard ColumnLayout configs, so pass them in directly
  120. Ext.apply(panelCfg, {
  121. layoutConfig: {columns: this.items.length},
  122. defaults: this.defaults,
  123. items: this.items
  124. });
  125. for(var i=0, len=this.items.length; i&lt;len; i++){
  126. Ext.applyIf(this.items[i], colCfg);
  127. }
  128. }else{
  129. // The container has field item configs, so we have to generate the column
  130. // panels first then move the items into the columns as needed.
  131. var numCols, cols = [];
  132. if(typeof this.columns == 'string'){ // 'auto' so create a col per item
  133. this.columns = this.items.length;
  134. }
  135. if(!Ext.isArray(this.columns)){
  136. var cs = [];
  137. for(var i=0; i&lt;this.columns; i++){
  138. cs.push((100/this.columns)*.01); // distribute by even %
  139. }
  140. this.columns = cs;
  141. }
  142. numCols = this.columns.length;
  143. // Generate the column configs with the correct width setting
  144. for(var i=0; i&lt;numCols; i++){
  145. var cc = Ext.apply({items:[]}, colCfg);
  146. cc[this.columns[i] &lt;= 1 ? 'columnWidth' : 'width'] = this.columns[i];
  147. if(this.defaults){
  148. cc.defaults = Ext.apply(cc.defaults || {}, this.defaults);
  149. }
  150. cols.push(cc);
  151. };
  152. // Distribute the original items into the columns
  153. if(this.vertical){
  154. var rows = Math.ceil(this.items.length / numCols), ri = 0;
  155. for(var i=0, len=this.items.length; i&lt;len; i++){
  156. if(i&gt;0 &amp;&amp; i%rows==0){
  157. ri++;
  158. }
  159. if(this.items[i].fieldLabel){
  160. this.items[i].hideLabel = false;
  161. }
  162. cols[ri].items.push(this.items[i]);
  163. };
  164. }else{
  165. for(var i=0, len=this.items.length; i&lt;len; i++){
  166. var ci = i % numCols;
  167. if(this.items[i].fieldLabel){
  168. this.items[i].hideLabel = false;
  169. }
  170. cols[ci].items.push(this.items[i]);
  171. };
  172. }
  173. Ext.apply(panelCfg, {
  174. layoutConfig: {columns: numCols},
  175. items: cols
  176. });
  177. }
  178. this.panel = new Ext.Container(panelCfg);
  179. this.panel.ownerCt = this;
  180. this.el = this.panel.getEl();
  181. if(this.forId &amp;&amp; this.itemCls){
  182. var l = this.el.up(this.itemCls).child('label', true);
  183. if(l){
  184. l.setAttribute('htmlFor', this.forId);
  185. }
  186. }
  187. var fields = this.panel.findBy(function(c){
  188. return c.isFormField;
  189. }, this);
  190. this.items = new Ext.util.MixedCollection();
  191. this.items.addAll(fields);
  192. }
  193. Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
  194. },
  195. <span id='Ext-form-CheckboxGroup-method-initValue'> initValue : function(){
  196. </span> if(this.value){
  197. this.setValue.apply(this, this.buffered ? this.value : [this.value]);
  198. delete this.buffered;
  199. delete this.value;
  200. }
  201. },
  202. <span id='Ext-form-CheckboxGroup-method-afterRender'> afterRender : function(){
  203. </span> Ext.form.CheckboxGroup.superclass.afterRender.call(this);
  204. this.eachItem(function(item){
  205. item.on('check', this.fireChecked, this);
  206. item.inGroup = true;
  207. });
  208. },
  209. <span id='Ext-form-CheckboxGroup-method-doLayout'> // private
  210. </span> doLayout: function(){
  211. //ugly method required to layout hidden items
  212. if(this.rendered){
  213. this.panel.forceLayout = this.ownerCt.forceLayout;
  214. this.panel.doLayout();
  215. }
  216. },
  217. <span id='Ext-form-CheckboxGroup-method-fireChecked'> // private
  218. </span> fireChecked: function(){
  219. var arr = [];
  220. this.eachItem(function(item){
  221. if(item.checked){
  222. arr.push(item);
  223. }
  224. });
  225. this.fireEvent('change', this, arr);
  226. },
  227. <span id='Ext-form-CheckboxGroup-method-getErrors'> /**
  228. </span> * Runs CheckboxGroup's validations and returns an array of any errors. The only error by default
  229. * is if allowBlank is set to true and no items are checked.
  230. * @return {Array} Array of all validation errors
  231. */
  232. getErrors: function() {
  233. var errors = Ext.form.CheckboxGroup.superclass.getErrors.apply(this, arguments);
  234. if (!this.allowBlank) {
  235. var blank = true;
  236. this.eachItem(function(f){
  237. if (f.checked) {
  238. return (blank = false);
  239. }
  240. });
  241. if (blank) errors.push(this.blankText);
  242. }
  243. return errors;
  244. },
  245. <span id='Ext-form-CheckboxGroup-method-isDirty'> // private
  246. </span> isDirty: function(){
  247. //override the behaviour to check sub items.
  248. if (this.disabled || !this.rendered) {
  249. return false;
  250. }
  251. var dirty = false;
  252. this.eachItem(function(item){
  253. if(item.isDirty()){
  254. dirty = true;
  255. return false;
  256. }
  257. });
  258. return dirty;
  259. },
  260. <span id='Ext-form-CheckboxGroup-method-setReadOnly'> // private
  261. </span> setReadOnly : function(readOnly){
  262. if(this.rendered){
  263. this.eachItem(function(item){
  264. item.setReadOnly(readOnly);
  265. });
  266. }
  267. this.readOnly = readOnly;
  268. },
  269. <span id='Ext-form-CheckboxGroup-method-onDisable'> // private
  270. </span> onDisable : function(){
  271. this.eachItem(function(item){
  272. item.disable();
  273. });
  274. },
  275. <span id='Ext-form-CheckboxGroup-method-onEnable'> // private
  276. </span> onEnable : function(){
  277. this.eachItem(function(item){
  278. item.enable();
  279. });
  280. },
  281. <span id='Ext-form-CheckboxGroup-method-onResize'> // private
  282. </span> onResize : function(w, h){
  283. this.panel.setSize(w, h);
  284. this.panel.doLayout();
  285. },
  286. <span id='Ext-form-CheckboxGroup-method-reset'> // inherit docs from Field
  287. </span> reset : function(){
  288. if (this.originalValue) {
  289. // Clear all items
  290. this.eachItem(function(c){
  291. if(c.setValue){
  292. c.setValue(false);
  293. c.originalValue = c.getValue();
  294. }
  295. });
  296. // Set items stored in originalValue, ugly - set a flag to reset the originalValue
  297. // during the horrible onSetValue. This will allow trackResetOnLoad to function.
  298. this.resetOriginal = true;
  299. this.setValue(this.originalValue);
  300. delete this.resetOriginal;
  301. } else {
  302. this.eachItem(function(c){
  303. if(c.reset){
  304. c.reset();
  305. }
  306. });
  307. }
  308. // Defer the clearInvalid so if BaseForm's collection is being iterated it will be called AFTER it is complete.
  309. // Important because reset is being called on both the group and the individual items.
  310. (function() {
  311. this.clearInvalid();
  312. }).defer(50, this);
  313. },
  314. <span id='Ext-form-CheckboxGroup-method-setValue'> /**
  315. </span> * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
  316. * in the group. Examples illustrating how this method may be called:
  317. * &lt;pre&gt;&lt;code&gt;
  318. // call with name and value
  319. myCheckboxGroup.setValue('cb-col-1', true);
  320. // call with an array of boolean values
  321. myCheckboxGroup.setValue([true, false, false]);
  322. // call with an object literal specifying item:value pairs
  323. myCheckboxGroup.setValue({
  324. 'cb-col-2': false,
  325. 'cb-col-3': true
  326. });
  327. // use comma separated string to set items with name to true (checked)
  328. myCheckboxGroup.setValue('cb-col-1,cb-col-3');
  329. * &lt;/code&gt;&lt;/pre&gt;
  330. * See {@link Ext.form.Checkbox#setValue} for additional information.
  331. * @param {Mixed} id The checkbox to check, or as described by example shown.
  332. * @param {Boolean} value (optional) The value to set the item.
  333. * @return {Ext.form.CheckboxGroup} this
  334. */
  335. setValue: function(){
  336. if(this.rendered){
  337. this.onSetValue.apply(this, arguments);
  338. }else{
  339. this.buffered = true;
  340. this.value = arguments;
  341. }
  342. return this;
  343. },
  344. <span id='Ext-form-CheckboxGroup-method-onSetValue'> /**
  345. </span> * @private
  346. * Sets the values of one or more of the items within the CheckboxGroup
  347. * @param {String|Array|Object} id Can take multiple forms. Can be optionally:
  348. * &lt;ul&gt;
  349. * &lt;li&gt;An ID string to be used with a second argument&lt;/li&gt;
  350. * &lt;li&gt;An array of the form ['some', 'list', 'of', 'ids', 'to', 'mark', 'checked']&lt;/li&gt;
  351. * &lt;li&gt;An array in the form [true, true, false, true, false] etc, where each item relates to the check status of
  352. * the checkbox at the same index&lt;/li&gt;
  353. * &lt;li&gt;An object containing ids of the checkboxes as keys and check values as properties&lt;/li&gt;
  354. * &lt;/ul&gt;
  355. * @param {String} value The value to set the field to if the first argument was a string
  356. */
  357. onSetValue: function(id, value){
  358. if(arguments.length == 1){
  359. if(Ext.isArray(id)){
  360. Ext.each(id, function(val, idx){
  361. if (Ext.isObject(val) &amp;&amp; val.setValue){ // array of checkbox components to be checked
  362. val.setValue(true);
  363. if (this.resetOriginal === true) {
  364. val.originalValue = val.getValue();
  365. }
  366. } else { // an array of boolean values
  367. var item = this.items.itemAt(idx);
  368. if(item){
  369. item.setValue(val);
  370. }
  371. }
  372. }, this);
  373. }else if(Ext.isObject(id)){
  374. // set of name/value pairs
  375. for(var i in id){
  376. var f = this.getBox(i);
  377. if(f){
  378. f.setValue(id[i]);
  379. }
  380. }
  381. }else{
  382. this.setValueForItem(id);
  383. }
  384. }else{
  385. var f = this.getBox(id);
  386. if(f){
  387. f.setValue(value);
  388. }
  389. }
  390. },
  391. <span id='Ext-form-CheckboxGroup-method-beforeDestroy'> // private
  392. </span> beforeDestroy: function(){
  393. Ext.destroy(this.panel);
  394. if (!this.rendered) {
  395. Ext.destroy(this.items);
  396. }
  397. Ext.form.CheckboxGroup.superclass.beforeDestroy.call(this);
  398. },
  399. <span id='Ext-form-CheckboxGroup-method-setValueForItem'> setValueForItem : function(val){
  400. </span> val = String(val).split(',');
  401. this.eachItem(function(item){
  402. if(val.indexOf(item.inputValue)&gt; -1){
  403. item.setValue(true);
  404. }
  405. });
  406. },
  407. <span id='Ext-form-CheckboxGroup-method-getBox'> // private
  408. </span> getBox : function(id){
  409. var box = null;
  410. this.eachItem(function(f){
  411. if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
  412. box = f;
  413. return false;
  414. }
  415. });
  416. return box;
  417. },
  418. <span id='Ext-form-CheckboxGroup-method-getValue'> /**
  419. </span> * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
  420. * @return {Array} An array of the selected checkboxes.
  421. */
  422. getValue : function(){
  423. var out = [];
  424. this.eachItem(function(item){
  425. if(item.checked){
  426. out.push(item);
  427. }
  428. });
  429. return out;
  430. },
  431. <span id='Ext-form-CheckboxGroup-method-eachItem'> /**
  432. </span> * @private
  433. * Convenience function which passes the given function to every item in the composite
  434. * @param {Function} fn The function to call
  435. * @param {Object} scope Optional scope object
  436. */
  437. eachItem: function(fn, scope) {
  438. if(this.items &amp;&amp; this.items.each){
  439. this.items.each(fn, scope || this);
  440. }
  441. },
  442. <span id='Ext-form-CheckboxGroup-cfg-name'> /**
  443. </span> * @cfg {String} name
  444. * @hide
  445. */
  446. <span id='Ext-form-CheckboxGroup-method-getRawValue'> /**
  447. </span> * @method getRawValue
  448. * @hide
  449. */
  450. getRawValue : Ext.emptyFn,
  451. <span id='Ext-form-CheckboxGroup-method-setRawValue'> /**
  452. </span> * @method setRawValue
  453. * @hide
  454. */
  455. setRawValue : Ext.emptyFn
  456. });
  457. Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);
  458. </pre>
  459. </body>
  460. </html>