Web Interface For Lite Argus Control modules
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.

454 lines
15 KiB

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