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.

463 lines
16 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-CompositeField'>/**
  19. </span> * @class Ext.form.CompositeField
  20. * @extends Ext.form.Field
  21. * Composite field allowing a number of form Fields to be rendered on the same row. The fields are rendered
  22. * using an hbox layout internally, so all of the normal HBox layout config items are available. Example usage:
  23. * &lt;pre&gt;
  24. {
  25. xtype: 'compositefield',
  26. labelWidth: 120
  27. items: [
  28. {
  29. xtype : 'textfield',
  30. fieldLabel: 'Title',
  31. width : 20
  32. },
  33. {
  34. xtype : 'textfield',
  35. fieldLabel: 'First',
  36. flex : 1
  37. },
  38. {
  39. xtype : 'textfield',
  40. fieldLabel: 'Last',
  41. flex : 1
  42. }
  43. ]
  44. }
  45. * &lt;/pre&gt;
  46. * In the example above the composite's fieldLabel will be set to 'Title, First, Last' as it groups the fieldLabels
  47. * of each of its children. This can be overridden by setting a fieldLabel on the compositefield itself:
  48. * &lt;pre&gt;
  49. {
  50. xtype: 'compositefield',
  51. fieldLabel: 'Custom label',
  52. items: [...]
  53. }
  54. * &lt;/pre&gt;
  55. * Any Ext.form.* component can be placed inside a composite field.
  56. */
  57. Ext.form.CompositeField = Ext.extend(Ext.form.Field, {
  58. <span id='Ext-form-CompositeField-property-defaultMargins'> /**
  59. </span> * @property defaultMargins
  60. * @type String
  61. * The margins to apply by default to each field in the composite
  62. */
  63. defaultMargins: '0 5 0 0',
  64. <span id='Ext-form-CompositeField-property-skipLastItemMargin'> /**
  65. </span> * @property skipLastItemMargin
  66. * @type Boolean
  67. * If true, the defaultMargins are not applied to the last item in the composite field set (defaults to true)
  68. */
  69. skipLastItemMargin: true,
  70. <span id='Ext-form-CompositeField-property-isComposite'> /**
  71. </span> * @property isComposite
  72. * @type Boolean
  73. * Signifies that this is a Composite field
  74. */
  75. isComposite: true,
  76. <span id='Ext-form-CompositeField-property-combineErrors'> /**
  77. </span> * @property combineErrors
  78. * @type Boolean
  79. * True to combine errors from the individual fields into a single error message at the CompositeField level (defaults to true)
  80. */
  81. combineErrors: true,
  82. <span id='Ext-form-CompositeField-cfg-labelConnector'> /**
  83. </span> * @cfg {String} labelConnector The string to use when joining segments of the built label together (defaults to ', ')
  84. */
  85. labelConnector: ', ',
  86. <span id='Ext-form-CompositeField-cfg-defaults'> /**
  87. </span> * @cfg {Object} defaults Any default properties to assign to the child fields.
  88. */
  89. <span id='Ext-form-CompositeField-method-initComponent'> //inherit docs
  90. </span> //Builds the composite field label
  91. initComponent: function() {
  92. var labels = [],
  93. items = this.items,
  94. item;
  95. for (var i=0, j = items.length; i &lt; j; i++) {
  96. item = items[i];
  97. if (!Ext.isEmpty(item.ref)){
  98. item.ref = '../' + item.ref;
  99. }
  100. labels.push(item.fieldLabel);
  101. //apply any defaults
  102. Ext.applyIf(item, this.defaults);
  103. //apply default margins to each item except the last
  104. if (!(i == j - 1 &amp;&amp; this.skipLastItemMargin)) {
  105. Ext.applyIf(item, {margins: this.defaultMargins});
  106. }
  107. }
  108. this.fieldLabel = this.fieldLabel || this.buildLabel(labels);
  109. <span id='Ext-form-CompositeField-property-fieldErrors'> /**
  110. </span> * @property fieldErrors
  111. * @type Ext.util.MixedCollection
  112. * MixedCollection of current errors on the Composite's subfields. This is used internally to track when
  113. * to show and hide error messages at the Composite level. Listeners are attached to the MixedCollection's
  114. * add, remove and replace events to update the error icon in the UI as errors are added or removed.
  115. */
  116. this.fieldErrors = new Ext.util.MixedCollection(true, function(item) {
  117. return item.field;
  118. });
  119. this.fieldErrors.on({
  120. scope : this,
  121. add : this.updateInvalidMark,
  122. remove : this.updateInvalidMark,
  123. replace: this.updateInvalidMark
  124. });
  125. Ext.form.CompositeField.superclass.initComponent.apply(this, arguments);
  126. this.innerCt = new Ext.Container({
  127. layout : 'hbox',
  128. items : this.items,
  129. cls : 'x-form-composite',
  130. defaultMargins: '0 3 0 0',
  131. ownerCt: this
  132. });
  133. delete this.innerCt.ownerCt;
  134. var fields = this.innerCt.findBy(function(c) {
  135. return c.isFormField;
  136. }, this);
  137. <span id='Ext-form-CompositeField-property-items'> /**
  138. </span> * @property items
  139. * @type Ext.util.MixedCollection
  140. * Internal collection of all of the subfields in this Composite
  141. */
  142. this.items = new Ext.util.MixedCollection();
  143. this.items.addAll(fields);
  144. },
  145. <span id='Ext-form-CompositeField-method-onRender'> /**
  146. </span> * @private
  147. * Creates an internal container using hbox and renders the fields to it
  148. */
  149. onRender: function(ct, position) {
  150. if (!this.el) {
  151. <span id='Ext-form-CompositeField-property-innerCt'> /**
  152. </span> * @property innerCt
  153. * @type Ext.Container
  154. * A container configured with hbox layout which is responsible for laying out the subfields
  155. */
  156. var innerCt = this.innerCt;
  157. innerCt.render(ct);
  158. this.innerCt.ownerCt = this;
  159. this.el = innerCt.getEl();
  160. //if we're combining subfield errors into a single message, override the markInvalid and clearInvalid
  161. //methods of each subfield and show them at the Composite level instead
  162. if (this.combineErrors) {
  163. this.eachItem(function(field) {
  164. Ext.apply(field, {
  165. markInvalid : this.onFieldMarkInvalid.createDelegate(this, [field], 0),
  166. clearInvalid: this.onFieldClearInvalid.createDelegate(this, [field], 0)
  167. });
  168. });
  169. }
  170. //set the label 'for' to the first item
  171. var l = this.el.parent().parent().child('label', true);
  172. if (l) {
  173. l.setAttribute('for', this.items.items[0].id);
  174. }
  175. }
  176. Ext.form.CompositeField.superclass.onRender.apply(this, arguments);
  177. },
  178. <span id='Ext-form-CompositeField-method-onFieldMarkInvalid'> /**
  179. </span> * Called if combineErrors is true and a subfield's markInvalid method is called.
  180. * By default this just adds the subfield's error to the internal fieldErrors MixedCollection
  181. * @param {Ext.form.Field} field The field that was marked invalid
  182. * @param {String} message The error message
  183. */
  184. onFieldMarkInvalid: function(field, message) {
  185. var name = field.getName(),
  186. error = {
  187. field: name,
  188. errorName: field.fieldLabel || name,
  189. error: message
  190. };
  191. this.fieldErrors.replace(name, error);
  192. if (!field.preventMark) {
  193. field.el.addClass(field.invalidClass);
  194. }
  195. },
  196. <span id='Ext-form-CompositeField-method-onFieldClearInvalid'> /**
  197. </span> * Called if combineErrors is true and a subfield's clearInvalid method is called.
  198. * By default this just updates the internal fieldErrors MixedCollection.
  199. * @param {Ext.form.Field} field The field that was marked invalid
  200. */
  201. onFieldClearInvalid: function(field) {
  202. this.fieldErrors.removeKey(field.getName());
  203. field.el.removeClass(field.invalidClass);
  204. },
  205. <span id='Ext-form-CompositeField-method-updateInvalidMark'> /**
  206. </span> * @private
  207. * Called after a subfield is marked valid or invalid, this checks to see if any of the subfields are
  208. * currently invalid. If any subfields are invalid it builds a combined error message marks the composite
  209. * invalid, otherwise clearInvalid is called
  210. */
  211. updateInvalidMark: function() {
  212. var ieStrict = Ext.isIE6 &amp;&amp; Ext.isStrict;
  213. if (this.fieldErrors.length == 0) {
  214. this.clearInvalid();
  215. //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
  216. if (ieStrict) {
  217. this.clearInvalid.defer(50, this);
  218. }
  219. } else {
  220. var message = this.buildCombinedErrorMessage(this.fieldErrors.items);
  221. this.sortErrors();
  222. this.markInvalid(message);
  223. //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
  224. if (ieStrict) {
  225. this.markInvalid(message);
  226. }
  227. }
  228. },
  229. <span id='Ext-form-CompositeField-method-validateValue'> /**
  230. </span> * Performs validation checks on each subfield and returns false if any of them fail validation.
  231. * @return {Boolean} False if any subfield failed validation
  232. */
  233. validateValue: function(value, preventMark) {
  234. var valid = true;
  235. this.eachItem(function(field) {
  236. if (!field.isValid(preventMark)) {
  237. valid = false;
  238. }
  239. });
  240. return valid;
  241. },
  242. <span id='Ext-form-CompositeField-method-buildCombinedErrorMessage'> /**
  243. </span> * Takes an object containing error messages for contained fields, returning a combined error
  244. * string (defaults to just placing each item on a new line). This can be overridden to provide
  245. * custom combined error message handling.
  246. * @param {Array} errors Array of errors in format: [{field: 'title', error: 'some error'}]
  247. * @return {String} The combined error message
  248. */
  249. buildCombinedErrorMessage: function(errors) {
  250. var combined = [],
  251. error;
  252. for (var i = 0, j = errors.length; i &lt; j; i++) {
  253. error = errors[i];
  254. combined.push(String.format(&quot;{0}: {1}&quot;, error.errorName, error.error));
  255. }
  256. return combined.join(&quot;&lt;br /&gt;&quot;);
  257. },
  258. <span id='Ext-form-CompositeField-method-sortErrors'> /**
  259. </span> * Sorts the internal fieldErrors MixedCollection by the order in which the fields are defined.
  260. * This is called before displaying errors to ensure that the errors are presented in the expected order.
  261. * This function can be overridden to provide a custom sorting order if needed.
  262. */
  263. sortErrors: function() {
  264. var fields = this.items;
  265. this.fieldErrors.sort(&quot;ASC&quot;, function(a, b) {
  266. var findByName = function(key) {
  267. return function(field) {
  268. return field.getName() == key;
  269. };
  270. };
  271. var aIndex = fields.findIndexBy(findByName(a.field)),
  272. bIndex = fields.findIndexBy(findByName(b.field));
  273. return aIndex &lt; bIndex ? -1 : 1;
  274. });
  275. },
  276. <span id='Ext-form-CompositeField-method-reset'> /**
  277. </span> * Resets each field in the composite to their previous value
  278. */
  279. reset: function() {
  280. this.eachItem(function(item) {
  281. item.reset();
  282. });
  283. // Defer the clearInvalid so if BaseForm's collection is being iterated it will be called AFTER it is complete.
  284. // Important because reset is being called on both the group and the individual items.
  285. (function() {
  286. this.clearInvalid();
  287. }).defer(50, this);
  288. },
  289. <span id='Ext-form-CompositeField-method-clearInvalidChildren'> /**
  290. </span> * Calls clearInvalid on all child fields. This is a convenience function and should not often need to be called
  291. * as fields usually take care of clearing themselves
  292. */
  293. clearInvalidChildren: function() {
  294. this.eachItem(function(item) {
  295. item.clearInvalid();
  296. });
  297. },
  298. <span id='Ext-form-CompositeField-method-buildLabel'> /**
  299. </span> * Builds a label string from an array of subfield labels.
  300. * By default this just joins the labels together with a comma
  301. * @param {Array} segments Array of each of the labels in the composite field's subfields
  302. * @return {String} The built label
  303. */
  304. buildLabel: function(segments) {
  305. return Ext.clean(segments).join(this.labelConnector);
  306. },
  307. <span id='Ext-form-CompositeField-method-isDirty'> /**
  308. </span> * Checks each field in the composite and returns true if any is dirty
  309. * @return {Boolean} True if any field is dirty
  310. */
  311. isDirty: function(){
  312. //override the behaviour to check sub items.
  313. if (this.disabled || !this.rendered) {
  314. return false;
  315. }
  316. var dirty = false;
  317. this.eachItem(function(item){
  318. if(item.isDirty()){
  319. dirty = true;
  320. return false;
  321. }
  322. });
  323. return dirty;
  324. },
  325. <span id='Ext-form-CompositeField-method-eachItem'> /**
  326. </span> * @private
  327. * Convenience function which passes the given function to every item in the composite
  328. * @param {Function} fn The function to call
  329. * @param {Object} scope Optional scope object
  330. */
  331. eachItem: function(fn, scope) {
  332. if(this.items &amp;&amp; this.items.each){
  333. this.items.each(fn, scope || this);
  334. }
  335. },
  336. <span id='Ext-form-CompositeField-method-onResize'> /**
  337. </span> * @private
  338. * Passes the resize call through to the inner panel
  339. */
  340. onResize: function(adjWidth, adjHeight, rawWidth, rawHeight) {
  341. var innerCt = this.innerCt;
  342. if (this.rendered &amp;&amp; innerCt.rendered) {
  343. innerCt.setSize(adjWidth, adjHeight);
  344. }
  345. Ext.form.CompositeField.superclass.onResize.apply(this, arguments);
  346. },
  347. <span id='Ext-form-CompositeField-method-doLayout'> /**
  348. </span> * @private
  349. * Forces the internal container to be laid out again
  350. */
  351. doLayout: function(shallow, force) {
  352. if (this.rendered) {
  353. var innerCt = this.innerCt;
  354. innerCt.forceLayout = this.ownerCt.forceLayout;
  355. innerCt.doLayout(shallow, force);
  356. }
  357. },
  358. <span id='Ext-form-CompositeField-method-beforeDestroy'> /**
  359. </span> * @private
  360. */
  361. beforeDestroy: function(){
  362. Ext.destroy(this.innerCt);
  363. Ext.form.CompositeField.superclass.beforeDestroy.call(this);
  364. },
  365. <span id='Ext-form-CompositeField-method-setReadOnly'> //override the behaviour to check sub items.
  366. </span> setReadOnly : function(readOnly) {
  367. if (readOnly == undefined) {
  368. readOnly = true;
  369. }
  370. readOnly = !!readOnly;
  371. if(this.rendered){
  372. this.eachItem(function(item){
  373. item.setReadOnly(readOnly);
  374. });
  375. }
  376. this.readOnly = readOnly;
  377. },
  378. <span id='Ext-form-CompositeField-method-onShow'> onShow : function() {
  379. </span> Ext.form.CompositeField.superclass.onShow.call(this);
  380. this.doLayout();
  381. },
  382. <span id='Ext-form-CompositeField-method-onDisable'> //override the behaviour to check sub items.
  383. </span> onDisable : function(){
  384. this.eachItem(function(item){
  385. item.disable();
  386. });
  387. },
  388. <span id='Ext-form-CompositeField-method-onEnable'> //override the behaviour to check sub items.
  389. </span> onEnable : function(){
  390. this.eachItem(function(item){
  391. item.enable();
  392. });
  393. }
  394. });
  395. Ext.reg('compositefield', Ext.form.CompositeField);</pre>
  396. </body>
  397. </html>