FieldSet2.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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-FieldSet'>/**
  19. </span> * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
  20. *
  21. * A container for grouping sets of fields, rendered as a HTML `fieldset` element. The {@link #title}
  22. * config will be rendered as the fieldset's `legend`.
  23. *
  24. * While FieldSets commonly contain simple groups of fields, they are general {@link Ext.container.Container Containers}
  25. * and may therefore contain any type of components in their {@link #cfg-items}, including other nested containers.
  26. * The default {@link #layout} for the FieldSet's items is `'anchor'`, but it can be configured to use any other
  27. * layout type.
  28. *
  29. * FieldSets may also be collapsed if configured to do so; this can be done in two ways:
  30. *
  31. * 1. Set the {@link #collapsible} config to true; this will result in a collapse button being rendered next to
  32. * the {@link #title legend title}, or:
  33. * 2. Set the {@link #checkboxToggle} config to true; this is similar to using {@link #collapsible} but renders
  34. * a {@link Ext.form.field.Checkbox checkbox} in place of the toggle button. The fieldset will be expanded when the
  35. * checkbox is checked and collapsed when it is unchecked. The checkbox will also be included in the
  36. * {@link Ext.form.Basic#submit form submit parameters} using the {@link #checkboxName} as its parameter name.
  37. *
  38. * # Example usage
  39. *
  40. * @example
  41. * Ext.create('Ext.form.Panel', {
  42. * title: 'Simple Form with FieldSets',
  43. * labelWidth: 75, // label settings here cascade unless overridden
  44. * url: 'save-form.php',
  45. * frame: true,
  46. * bodyStyle: 'padding:5px 5px 0',
  47. * width: 550,
  48. * renderTo: Ext.getBody(),
  49. * layout: 'column', // arrange fieldsets side by side
  50. * defaults: {
  51. * bodyPadding: 4
  52. * },
  53. * items: [{
  54. * // Fieldset in Column 1 - collapsible via toggle button
  55. * xtype:'fieldset',
  56. * columnWidth: 0.5,
  57. * title: 'Fieldset 1',
  58. * collapsible: true,
  59. * defaultType: 'textfield',
  60. * defaults: {anchor: '100%'},
  61. * layout: 'anchor',
  62. * items :[{
  63. * fieldLabel: 'Field 1',
  64. * name: 'field1'
  65. * }, {
  66. * fieldLabel: 'Field 2',
  67. * name: 'field2'
  68. * }]
  69. * }, {
  70. * // Fieldset in Column 2 - collapsible via checkbox, collapsed by default, contains a panel
  71. * xtype:'fieldset',
  72. * title: 'Show Panel', // title or checkboxToggle creates fieldset header
  73. * columnWidth: 0.5,
  74. * checkboxToggle: true,
  75. * collapsed: true, // fieldset initially collapsed
  76. * layout:'anchor',
  77. * items :[{
  78. * xtype: 'panel',
  79. * anchor: '100%',
  80. * title: 'Panel inside a fieldset',
  81. * frame: true,
  82. * height: 52
  83. * }]
  84. * }]
  85. * });
  86. */
  87. Ext.define('Ext.form.FieldSet', {
  88. extend: 'Ext.container.Container',
  89. alias: 'widget.fieldset',
  90. uses: ['Ext.form.field.Checkbox', 'Ext.panel.Tool', 'Ext.layout.container.Anchor', 'Ext.layout.component.FieldSet'],
  91. <span id='Ext-form-FieldSet-cfg-title'> /**
  92. </span> * @cfg {String} title
  93. * A title to be displayed in the fieldset's legend. May contain HTML markup.
  94. */
  95. <span id='Ext-form-FieldSet-cfg-checkboxToggle'> /**
  96. </span> * @cfg {Boolean} [checkboxToggle=false]
  97. * Set to true to render a checkbox into the fieldset frame just in front of the legend to expand/collapse the
  98. * fieldset when the checkbox is toggled.. This checkbox will be included in form submits using
  99. * the {@link #checkboxName}.
  100. */
  101. <span id='Ext-form-FieldSet-cfg-checkboxName'> /**
  102. </span> * @cfg {String} checkboxName
  103. * The name to assign to the fieldset's checkbox if {@link #checkboxToggle} = true
  104. * (defaults to '[fieldset id]-checkbox').
  105. */
  106. <span id='Ext-form-FieldSet-cfg-collapsible'> /**
  107. </span> * @cfg {Boolean} [collapsible=false]
  108. * Set to true to make the fieldset collapsible and have the expand/collapse toggle button automatically rendered
  109. * into the legend element, false to keep the fieldset statically sized with no collapse button.
  110. * Another option is to configure {@link #checkboxToggle}. Use the {@link #collapsed} config to collapse the
  111. * fieldset by default.
  112. */
  113. <span id='Ext-form-FieldSet-cfg-collapsed'> /**
  114. </span> * @cfg {Boolean} collapsed
  115. * Set to true to render the fieldset as collapsed by default. If {@link #checkboxToggle} is specified, the checkbox
  116. * will also be unchecked by default.
  117. */
  118. collapsed: false,
  119. <span id='Ext-form-FieldSet-cfg-toggleOnTitleClick'> /**
  120. </span> * @cfg {Boolean} [toggleOnTitleClick=true]
  121. * Set to true will add a listener to the titleCmp property for the click event which will execute the
  122. * {@link #toggle} method. This option is only used when the {@link #collapsible} property is set to true.
  123. */
  124. toggleOnTitleClick : true,
  125. <span id='Ext-form-FieldSet-property-legend'> /**
  126. </span> * @property {Ext.Component} legend
  127. * The component for the fieldset's legend. Will only be defined if the configuration requires a legend to be
  128. * created, by setting the {@link #title} or {@link #checkboxToggle} options.
  129. */
  130. <span id='Ext-form-FieldSet-cfg-baseCls'> /**
  131. </span> * @cfg {String} [baseCls='x-fieldset']
  132. * The base CSS class applied to the fieldset.
  133. */
  134. baseCls: Ext.baseCSSPrefix + 'fieldset',
  135. <span id='Ext-form-FieldSet-cfg-layout'> /**
  136. </span> * @cfg {String} layout
  137. * The {@link Ext.container.Container#layout} for the fieldset's immediate child items.
  138. */
  139. layout: 'anchor',
  140. border: 1,
  141. componentLayout: 'fieldset',
  142. autoEl: 'fieldset',
  143. childEls: [
  144. 'body'
  145. ],
  146. renderTpl: [
  147. '{%this.renderLegend(out,values);%}',
  148. '&lt;div id=&quot;{id}-body&quot; class=&quot;{baseCls}-body&quot;&gt;',
  149. '{%this.renderContainer(out,values);%}',
  150. '&lt;/div&gt;'
  151. ],
  152. stateEvents : [ 'collapse', 'expand' ],
  153. maskOnDisable: false,
  154. beforeDestroy: function(){
  155. var me = this,
  156. legend = me.legend;
  157. if (legend) {
  158. // get rid of the ownerCt since it's not a proper item
  159. delete legend.ownerCt;
  160. legend.destroy();
  161. me.legend = null;
  162. }
  163. me.callParent();
  164. },
  165. initComponent: function() {
  166. var me = this,
  167. baseCls = me.baseCls;
  168. me.callParent();
  169. me.addEvents(
  170. <span id='Ext-form-FieldSet-event-beforeexpand'> /**
  171. </span> * @event beforeexpand
  172. * Fires before this FieldSet is expanded. Return false to prevent the expand.
  173. * @param {Ext.form.FieldSet} f The FieldSet being expanded.
  174. */
  175. &quot;beforeexpand&quot;,
  176. <span id='Ext-form-FieldSet-event-beforecollapse'> /**
  177. </span> * @event beforecollapse
  178. * Fires before this FieldSet is collapsed. Return false to prevent the collapse.
  179. * @param {Ext.form.FieldSet} f The FieldSet being collapsed.
  180. */
  181. &quot;beforecollapse&quot;,
  182. <span id='Ext-form-FieldSet-event-expand'> /**
  183. </span> * @event expand
  184. * Fires after this FieldSet has expanded.
  185. * @param {Ext.form.FieldSet} f The FieldSet that has been expanded.
  186. */
  187. &quot;expand&quot;,
  188. <span id='Ext-form-FieldSet-event-collapse'> /**
  189. </span> * @event collapse
  190. * Fires after this FieldSet has collapsed.
  191. * @param {Ext.form.FieldSet} f The FieldSet that has been collapsed.
  192. */
  193. &quot;collapse&quot;
  194. );
  195. if (me.collapsed) {
  196. me.addCls(baseCls + '-collapsed');
  197. me.collapse();
  198. }
  199. if (me.title) {
  200. me.addCls(baseCls + '-with-title');
  201. }
  202. if (me.title || me.checkboxToggle || me.collapsible) {
  203. me.addCls(baseCls + '-with-legend');
  204. me.legend = Ext.widget(me.createLegendCt());
  205. }
  206. },
  207. <span id='Ext-form-FieldSet-method-initRenderData'> /**
  208. </span> * Initialized the renderData to be used when rendering the renderTpl.
  209. * @return {Object} Object with keys and values that are going to be applied to the renderTpl
  210. * @private
  211. */
  212. initRenderData: function() {
  213. var data = this.callParent();
  214. data.baseCls = this.baseCls;
  215. return data;
  216. },
  217. getState: function () {
  218. var state = this.callParent();
  219. state = this.addPropertyToState(state, 'collapsed');
  220. return state;
  221. },
  222. afterCollapse: Ext.emptyFn,
  223. afterExpand: Ext.emptyFn,
  224. collapsedHorizontal: function () {
  225. return true;
  226. },
  227. collapsedVertical: function () {
  228. return true;
  229. },
  230. createLegendCt: function () {
  231. var me = this,
  232. items = [],
  233. legend = {
  234. xtype: 'container',
  235. baseCls: me.baseCls + '-header',
  236. id: me.id + '-legend',
  237. autoEl: 'legend',
  238. items: items,
  239. ownerCt: me,
  240. ownerLayout: me.componentLayout
  241. };
  242. // Checkbox
  243. if (me.checkboxToggle) {
  244. items.push(me.createCheckboxCmp());
  245. } else if (me.collapsible) {
  246. // Toggle button
  247. items.push(me.createToggleCmp());
  248. }
  249. // Title
  250. items.push(me.createTitleCmp());
  251. return legend;
  252. },
  253. <span id='Ext-form-FieldSet-method-createTitleCmp'> /**
  254. </span> * Creates the legend title component. This is only called internally, but could be overridden in subclasses to
  255. * customize the title component. If {@link #toggleOnTitleClick} is set to true, a listener for the click event
  256. * will toggle the collapsed state of the FieldSet.
  257. * @return Ext.Component
  258. * @protected
  259. */
  260. createTitleCmp: function() {
  261. var me = this,
  262. cfg = {
  263. xtype : 'component',
  264. html : me.title,
  265. cls : me.baseCls + '-header-text',
  266. id : me.id + '-legendTitle'
  267. };
  268. if (me.collapsible &amp;&amp; me.toggleOnTitleClick) {
  269. cfg.listeners = {
  270. el : {
  271. scope : me,
  272. click : me.toggle
  273. }
  274. };
  275. cfg.cls += ' ' + me.baseCls + '-header-text-collapsible';
  276. }
  277. return (me.titleCmp = Ext.widget(cfg));
  278. },
  279. <span id='Ext-form-FieldSet-property-checkboxCmp'> /**
  280. </span> * @property {Ext.form.field.Checkbox} checkboxCmp
  281. * Refers to the {@link Ext.form.field.Checkbox} component that is added next to the title in the legend. Only
  282. * populated if the fieldset is configured with {@link #checkboxToggle}:true.
  283. */
  284. <span id='Ext-form-FieldSet-method-createCheckboxCmp'> /**
  285. </span> * Creates the checkbox component. This is only called internally, but could be overridden in subclasses to
  286. * customize the checkbox's configuration or even return an entirely different component type.
  287. * @return Ext.Component
  288. * @protected
  289. */
  290. createCheckboxCmp: function() {
  291. var me = this,
  292. suffix = '-checkbox';
  293. me.checkboxCmp = Ext.widget({
  294. xtype: 'checkbox',
  295. hideEmptyLabel: true,
  296. name: me.checkboxName || me.id + suffix,
  297. cls: me.baseCls + '-header' + suffix,
  298. id: me.id + '-legendChk',
  299. checked: !me.collapsed,
  300. listeners: {
  301. change: me.onCheckChange,
  302. scope: me
  303. }
  304. });
  305. return me.checkboxCmp;
  306. },
  307. <span id='Ext-form-FieldSet-property-toggleCmp'> /**
  308. </span> * @property {Ext.panel.Tool} toggleCmp
  309. * Refers to the {@link Ext.panel.Tool} component that is added as the collapse/expand button next to the title in
  310. * the legend. Only populated if the fieldset is configured with {@link #collapsible}:true.
  311. */
  312. <span id='Ext-form-FieldSet-method-createToggleCmp'> /**
  313. </span> * Creates the toggle button component. This is only called internally, but could be overridden in subclasses to
  314. * customize the toggle component.
  315. * @return Ext.Component
  316. * @protected
  317. */
  318. createToggleCmp: function() {
  319. var me = this;
  320. me.toggleCmp = Ext.widget({
  321. xtype: 'tool',
  322. type: 'toggle',
  323. handler: me.toggle,
  324. id: me.id + '-legendToggle',
  325. scope: me
  326. });
  327. return me.toggleCmp;
  328. },
  329. doRenderLegend: function (out, renderData) {
  330. // Careful! This method is bolted on to the renderTpl so all we get for context is
  331. // the renderData! The &quot;this&quot; pointer is the renderTpl instance!
  332. var me = renderData.$comp,
  333. legend = me.legend,
  334. tree;
  335. // Create the Legend component if needed
  336. if (legend) {
  337. legend.ownerLayout.configureItem(legend);
  338. tree = legend.getRenderTree();
  339. Ext.DomHelper.generateMarkup(tree, out);
  340. }
  341. },
  342. finishRender: function () {
  343. var legend = this.legend;
  344. this.callParent();
  345. if (legend) {
  346. legend.finishRender();
  347. }
  348. },
  349. getCollapsed: function () {
  350. return this.collapsed ? 'top' : false;
  351. },
  352. getCollapsedDockedItems: function () {
  353. var legend = this.legend;
  354. return legend ? [ legend ] : [];
  355. },
  356. <span id='Ext-form-FieldSet-method-setTitle'> /**
  357. </span> * Sets the title of this fieldset
  358. * @param {String} title The new title
  359. * @return {Ext.form.FieldSet} this
  360. */
  361. setTitle: function(title) {
  362. var me = this,
  363. legend = me.legend;
  364. me.title = title;
  365. if (me.rendered) {
  366. if (!me.legend) {
  367. me.legend = legend = Ext.widget(me.createLegendCt());
  368. legend.ownerLayout.configureItem(legend);
  369. legend.render(me.el, 0);
  370. }
  371. me.titleCmp.update(title);
  372. }
  373. return me;
  374. },
  375. getTargetEl : function() {
  376. return this.body || this.frameBody || this.el;
  377. },
  378. getContentTarget: function() {
  379. return this.body;
  380. },
  381. <span id='Ext-form-FieldSet-method-expand'> /**
  382. </span> * Expands the fieldset.
  383. * @return {Ext.form.FieldSet} this
  384. */
  385. expand : function(){
  386. return this.setExpanded(true);
  387. },
  388. <span id='Ext-form-FieldSet-method-collapse'> /**
  389. </span> * Collapses the fieldset.
  390. * @return {Ext.form.FieldSet} this
  391. */
  392. collapse : function() {
  393. return this.setExpanded(false);
  394. },
  395. <span id='Ext-form-FieldSet-method-setExpanded'> /**
  396. </span> * @private Collapse or expand the fieldset
  397. */
  398. setExpanded: function(expanded) {
  399. var me = this,
  400. checkboxCmp = me.checkboxCmp,
  401. operation = expanded ? 'expand' : 'collapse';
  402. if (!me.rendered || me.fireEvent('before' + operation, me) !== false) {
  403. expanded = !!expanded;
  404. if (checkboxCmp) {
  405. checkboxCmp.setValue(expanded);
  406. }
  407. if (expanded) {
  408. me.removeCls(me.baseCls + '-collapsed');
  409. } else {
  410. me.addCls(me.baseCls + '-collapsed');
  411. }
  412. me.collapsed = !expanded;
  413. if (me.rendered) {
  414. // say explicitly we are not root because when we have a fixed/configured height
  415. // our ownerLayout would say we are root and so would not have it's height
  416. // updated since it's not included in the layout cycle
  417. me.updateLayout({ isRoot: false });
  418. me.fireEvent(operation, me);
  419. }
  420. }
  421. return me;
  422. },
  423. getRefItems: function(deep) {
  424. var refItems = this.callParent(arguments),
  425. legend = this.legend;
  426. // Prepend legend items to ensure correct order
  427. if (legend) {
  428. refItems.unshift(legend);
  429. if (deep) {
  430. refItems.unshift.apply(refItems, legend.getRefItems(true));
  431. }
  432. }
  433. return refItems;
  434. },
  435. <span id='Ext-form-FieldSet-method-toggle'> /**
  436. </span> * Toggle the fieldset's collapsed state to the opposite of what it is currently
  437. */
  438. toggle: function() {
  439. this.setExpanded(!!this.collapsed);
  440. },
  441. <span id='Ext-form-FieldSet-method-onCheckChange'> /**
  442. </span> * @private
  443. * Handle changes in the checkbox checked state
  444. */
  445. onCheckChange: function(cmp, checked) {
  446. this.setExpanded(checked);
  447. },
  448. setupRenderTpl: function (renderTpl) {
  449. this.callParent(arguments);
  450. renderTpl.renderLegend = this.doRenderLegend;
  451. }
  452. });
  453. </pre>
  454. </body>
  455. </html>