Base3.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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-Base'>/**
  19. </span> * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
  20. *
  21. * Base class for form fields that provides default event handling, rendering, and other common functionality
  22. * needed by all form field types. Utilizes the {@link Ext.form.field.Field} mixin for value handling and validation,
  23. * and the {@link Ext.form.Labelable} mixin to provide label and error message display.
  24. *
  25. * In most cases you will want to use a subclass, such as {@link Ext.form.field.Text} or {@link Ext.form.field.Checkbox},
  26. * rather than creating instances of this class directly. However if you are implementing a custom form field,
  27. * using this as the parent class is recommended.
  28. *
  29. * # Values and Conversions
  30. *
  31. * Because Base implements the Field mixin, it has a main value that can be initialized with the
  32. * {@link #value} config and manipulated via the {@link #getValue} and {@link #setValue} methods. This main
  33. * value can be one of many data types appropriate to the current field, for instance a {@link Ext.form.field.Date Date}
  34. * field would use a JavaScript Date object as its value type. However, because the field is rendered as a HTML
  35. * input, this value data type can not always be directly used in the rendered field.
  36. *
  37. * Therefore Base introduces the concept of a &quot;raw value&quot;. This is the value of the rendered HTML input field,
  38. * and is normally a String. The {@link #getRawValue} and {@link #setRawValue} methods can be used to directly
  39. * work with the raw value, though it is recommended to use getValue and setValue in most cases.
  40. *
  41. * Conversion back and forth between the main value and the raw value is handled by the {@link #valueToRaw} and
  42. * {@link #rawToValue} methods. If you are implementing a subclass that uses a non-String value data type, you
  43. * should override these methods to handle the conversion.
  44. *
  45. * # Rendering
  46. *
  47. * The content of the field body is defined by the {@link #fieldSubTpl} XTemplate, with its argument data
  48. * created by the {@link #getSubTplData} method. Override this template and/or method to create custom
  49. * field renderings.
  50. *
  51. * # Example usage:
  52. *
  53. * @example
  54. * // A simple subclass of Base that creates a HTML5 search field. Redirects to the
  55. * // searchUrl when the Enter key is pressed.222
  56. * Ext.define('Ext.form.SearchField', {
  57. * extend: 'Ext.form.field.Base',
  58. * alias: 'widget.searchfield',
  59. *
  60. * inputType: 'search',
  61. *
  62. * // Config defining the search URL
  63. * searchUrl: 'http://www.google.com/search?q={0}',
  64. *
  65. * // Add specialkey listener
  66. * initComponent: function() {
  67. * this.callParent();
  68. * this.on('specialkey', this.checkEnterKey, this);
  69. * },
  70. *
  71. * // Handle enter key presses, execute the search if the field has a value
  72. * checkEnterKey: function(field, e) {
  73. * var value = this.getValue();
  74. * if (e.getKey() === e.ENTER &amp;&amp; !Ext.isEmpty(value)) {
  75. * location.href = Ext.String.format(this.searchUrl, value);
  76. * }
  77. * }
  78. * });
  79. *
  80. * Ext.create('Ext.form.Panel', {
  81. * title: 'Base Example',
  82. * bodyPadding: 5,
  83. * width: 250,
  84. *
  85. * // Fields will be arranged vertically, stretched to full width
  86. * layout: 'anchor',
  87. * defaults: {
  88. * anchor: '100%'
  89. * },
  90. * items: [{
  91. * xtype: 'searchfield',
  92. * fieldLabel: 'Search',
  93. * name: 'query'
  94. * }],
  95. * renderTo: Ext.getBody()
  96. * });
  97. */
  98. Ext.define('Ext.form.field.Base', {
  99. extend: 'Ext.Component',
  100. mixins: {
  101. labelable: 'Ext.form.Labelable',
  102. field: 'Ext.form.field.Field'
  103. },
  104. alias: 'widget.field',
  105. alternateClassName: ['Ext.form.Field', 'Ext.form.BaseField'],
  106. requires: ['Ext.util.DelayedTask', 'Ext.XTemplate', 'Ext.layout.component.field.Field'],
  107. <span id='Ext-form-field-Base-cfg-fieldSubTpl'> /**
  108. </span> * @cfg {Ext.XTemplate} fieldSubTpl
  109. * The content of the field body is defined by this config option.
  110. * @private
  111. */
  112. fieldSubTpl: [ // note: {id} here is really {inputId}, but {cmpId} is available
  113. '&lt;input id=&quot;{id}&quot; type=&quot;{type}&quot; {inputAttrTpl}',
  114. ' size=&quot;1&quot;', // allows inputs to fully respect CSS widths across all browsers
  115. '&lt;tpl if=&quot;name&quot;&gt; name=&quot;{name}&quot;&lt;/tpl&gt;',
  116. '&lt;tpl if=&quot;value&quot;&gt; value=&quot;{[Ext.util.Format.htmlEncode(values.value)]}&quot;&lt;/tpl&gt;',
  117. '&lt;tpl if=&quot;placeholder&quot;&gt; placeholder=&quot;{placeholder}&quot;&lt;/tpl&gt;',
  118. '{%if (values.maxLength !== undefined){%} maxlength=&quot;{maxLength}&quot;{%}%}',
  119. '&lt;tpl if=&quot;readOnly&quot;&gt; readonly=&quot;readonly&quot;&lt;/tpl&gt;',
  120. '&lt;tpl if=&quot;disabled&quot;&gt; disabled=&quot;disabled&quot;&lt;/tpl&gt;',
  121. '&lt;tpl if=&quot;tabIdx&quot;&gt; tabIndex=&quot;{tabIdx}&quot;&lt;/tpl&gt;',
  122. '&lt;tpl if=&quot;fieldStyle&quot;&gt; style=&quot;{fieldStyle}&quot;&lt;/tpl&gt;',
  123. ' class=&quot;{fieldCls} {typeCls} {editableCls}&quot; autocomplete=&quot;off&quot;/&gt;',
  124. {
  125. disableFormats: true
  126. }
  127. ],
  128. subTplInsertions: [
  129. <span id='Ext-form-field-Base-cfg-inputAttrTpl'> /**
  130. </span> * @cfg {String/Array/Ext.XTemplate} inputAttrTpl
  131. * An optional string or `XTemplate` configuration to insert in the field markup
  132. * inside the input element (as attributes). If an `XTemplate` is used, the component's
  133. * {@link #getSubTplData subTpl data} serves as the context.
  134. */
  135. 'inputAttrTpl'
  136. ],
  137. <span id='Ext-form-field-Base-cfg-name'> /**
  138. </span> * @cfg {String} name
  139. * The name of the field. This is used as the parameter name when including the field value
  140. * in a {@link Ext.form.Basic#submit form submit()}. If no name is configured, it falls back to the {@link #inputId}.
  141. * To prevent the field from being included in the form submit, set {@link #submitValue} to false.
  142. */
  143. <span id='Ext-form-field-Base-cfg-inputType'> /**
  144. </span> * @cfg {String} inputType
  145. * The type attribute for input fields -- e.g. radio, text, password, file. The extended types
  146. * supported by HTML5 inputs (url, email, etc.) may also be used, though using them will cause older browsers to
  147. * fall back to 'text'.
  148. *
  149. * The type 'password' must be used to render that field type currently -- there is no separate Ext component for
  150. * that. You can use {@link Ext.form.field.File} which creates a custom-rendered file upload field, but if you want
  151. * a plain unstyled file input you can use a Base with inputType:'file'.
  152. */
  153. inputType: 'text',
  154. <span id='Ext-form-field-Base-cfg-tabIndex'> /**
  155. </span> * @cfg {Number} tabIndex
  156. * The tabIndex for this field. Note this only applies to fields that are rendered, not those which are built via
  157. * applyTo
  158. */
  159. //&lt;locale&gt;
  160. <span id='Ext-form-field-Base-cfg-invalidText'> /**
  161. </span> * @cfg {String} invalidText
  162. * The error text to use when marking a field invalid and no message is provided
  163. */
  164. invalidText : 'The value in this field is invalid',
  165. //&lt;/locale&gt;
  166. <span id='Ext-form-field-Base-cfg-fieldCls'> /**
  167. </span> * @cfg {String} [fieldCls='x-form-field']
  168. * The default CSS class for the field input
  169. */
  170. fieldCls : Ext.baseCSSPrefix + 'form-field',
  171. <span id='Ext-form-field-Base-cfg-fieldStyle'> /**
  172. </span> * @cfg {String} fieldStyle
  173. * Optional CSS style(s) to be applied to the {@link #inputEl field input element}. Should be a valid argument to
  174. * {@link Ext.Element#applyStyles}. Defaults to undefined. See also the {@link #setFieldStyle} method for changing
  175. * the style after initialization.
  176. */
  177. <span id='Ext-form-field-Base-cfg-focusCls'> /**
  178. </span> * @cfg {String} [focusCls='x-form-focus']
  179. * The CSS class to use when the field receives focus
  180. */
  181. focusCls : 'form-focus',
  182. <span id='Ext-form-field-Base-cfg-dirtyCls'> /**
  183. </span> * @cfg {String} dirtyCls
  184. * The CSS class to use when the field value {@link #isDirty is dirty}.
  185. */
  186. dirtyCls : Ext.baseCSSPrefix + 'form-dirty',
  187. <span id='Ext-form-field-Base-cfg-checkChangeEvents'> /**
  188. </span> * @cfg {String[]} checkChangeEvents
  189. * A list of event names that will be listened for on the field's {@link #inputEl input element}, which will cause
  190. * the field's value to be checked for changes. If a change is detected, the {@link #change change event} will be
  191. * fired, followed by validation if the {@link #validateOnChange} option is enabled.
  192. *
  193. * Defaults to ['change', 'propertychange'] in Internet Explorer, and ['change', 'input', 'textInput', 'keyup',
  194. * 'dragdrop'] in other browsers. This catches all the ways that field values can be changed in most supported
  195. * browsers; the only known exceptions at the time of writing are:
  196. *
  197. * - Safari 3.2 and older: cut/paste in textareas via the context menu, and dragging text into textareas
  198. * - Opera 10 and 11: dragging text into text fields and textareas, and cut via the context menu in text fields
  199. * and textareas
  200. * - Opera 9: Same as Opera 10 and 11, plus paste from context menu in text fields and textareas
  201. *
  202. * If you need to guarantee on-the-fly change notifications including these edge cases, you can call the
  203. * {@link #checkChange} method on a repeating interval, e.g. using {@link Ext.TaskManager}, or if the field is within
  204. * a {@link Ext.form.Panel}, you can use the FormPanel's {@link Ext.form.Panel#pollForChanges} configuration to set up
  205. * such a task automatically.
  206. */
  207. checkChangeEvents: Ext.isIE &amp;&amp; (!document.documentMode || document.documentMode &lt; 9) ?
  208. ['change', 'propertychange'] :
  209. ['change', 'input', 'textInput', 'keyup', 'dragdrop'],
  210. <span id='Ext-form-field-Base-cfg-checkChangeBuffer'> /**
  211. </span> * @cfg {Number} checkChangeBuffer
  212. * Defines a timeout in milliseconds for buffering {@link #checkChangeEvents} that fire in rapid succession.
  213. * Defaults to 50 milliseconds.
  214. */
  215. checkChangeBuffer: 50,
  216. componentLayout: 'field',
  217. <span id='Ext-form-field-Base-cfg-readOnly'> /**
  218. </span> * @cfg {Boolean} readOnly
  219. * true to mark the field as readOnly in HTML.
  220. *
  221. * **Note**: this only sets the element's readOnly DOM attribute. Setting `readOnly=true`, for example, will not
  222. * disable triggering a ComboBox or Date; it gives you the option of forcing the user to choose via the trigger
  223. * without typing in the text box. To hide the trigger use `{@link Ext.form.field.Trigger#hideTrigger hideTrigger}`.
  224. */
  225. readOnly : false,
  226. <span id='Ext-form-field-Base-cfg-readOnlyCls'> /**
  227. </span> * @cfg {String} readOnlyCls
  228. * The CSS class applied to the component's main element when it is {@link #readOnly}.
  229. */
  230. readOnlyCls: Ext.baseCSSPrefix + 'form-readonly',
  231. <span id='Ext-form-field-Base-cfg-inputId'> /**
  232. </span> * @cfg {String} inputId
  233. * The id that will be given to the generated input DOM element. Defaults to an automatically generated id. If you
  234. * configure this manually, you must make sure it is unique in the document.
  235. */
  236. <span id='Ext-form-field-Base-cfg-validateOnBlur'> /**
  237. </span> * @cfg {Boolean} validateOnBlur
  238. * Whether the field should validate when it loses focus. This will cause fields to be validated
  239. * as the user steps through the fields in the form regardless of whether they are making changes to those fields
  240. * along the way. See also {@link #validateOnChange}.
  241. */
  242. validateOnBlur: true,
  243. // private
  244. hasFocus : false,
  245. baseCls: Ext.baseCSSPrefix + 'field',
  246. maskOnDisable: false,
  247. // private
  248. initComponent : function() {
  249. var me = this;
  250. me.callParent();
  251. me.subTplData = me.subTplData || {};
  252. me.addEvents(
  253. <span id='Ext-form-field-Base-event-specialkey'> /**
  254. </span> * @event specialkey
  255. * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. To handle other keys
  256. * see {@link Ext.util.KeyMap}. You can check {@link Ext.EventObject#getKey} to determine which key was
  257. * pressed. For example:
  258. *
  259. * var form = new Ext.form.Panel({
  260. * ...
  261. * items: [{
  262. * fieldLabel: 'Field 1',
  263. * name: 'field1',
  264. * allowBlank: false
  265. * },{
  266. * fieldLabel: 'Field 2',
  267. * name: 'field2',
  268. * listeners: {
  269. * specialkey: function(field, e){
  270. * // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
  271. * // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
  272. * if (e.{@link Ext.EventObject#getKey getKey()} == e.ENTER) {
  273. * var form = field.up('form').getForm();
  274. * form.submit();
  275. * }
  276. * }
  277. * }
  278. * }
  279. * ],
  280. * ...
  281. * });
  282. *
  283. * @param {Ext.form.field.Base} this
  284. * @param {Ext.EventObject} e The event object
  285. */
  286. 'specialkey',
  287. <span id='Ext-form-field-Base-event-writeablechange'> /**
  288. </span> * @event writeablechange
  289. * Fires when this field changes its read-only status.
  290. * @param {Ext.form.field.Base} this
  291. * @param {Boolean} Read only flag
  292. */
  293. 'writeablechange'
  294. );
  295. // Init mixins
  296. me.initLabelable();
  297. me.initField();
  298. // Default name to inputId
  299. if (!me.name) {
  300. me.name = me.getInputId();
  301. }
  302. },
  303. beforeRender: function(){
  304. var me = this;
  305. me.callParent(arguments);
  306. me.beforeLabelableRender(arguments);
  307. if (me.readOnly) {
  308. me.addCls(me.readOnlyCls);
  309. }
  310. },
  311. <span id='Ext-form-field-Base-method-getInputId'> /**
  312. </span> * Returns the input id for this field. If none was specified via the {@link #inputId} config, then an id will be
  313. * automatically generated.
  314. */
  315. getInputId: function() {
  316. return this.inputId || (this.inputId = this.id + '-inputEl');
  317. },
  318. <span id='Ext-form-field-Base-method-getSubTplData'> /**
  319. </span> * Creates and returns the data object to be used when rendering the {@link #fieldSubTpl}.
  320. * @return {Object} The template data
  321. * @template
  322. */
  323. getSubTplData: function() {
  324. var me = this,
  325. type = me.inputType,
  326. inputId = me.getInputId(),
  327. data;
  328. data = Ext.apply({
  329. id : inputId,
  330. cmpId : me.id,
  331. name : me.name || inputId,
  332. disabled : me.disabled,
  333. readOnly : me.readOnly,
  334. value : me.getRawValue(),
  335. type : type,
  336. fieldCls : me.fieldCls,
  337. fieldStyle : me.getFieldStyle(),
  338. tabIdx : me.tabIndex,
  339. typeCls : Ext.baseCSSPrefix + 'form-' + (type === 'password' ? 'text' : type)
  340. }, me.subTplData);
  341. me.getInsertionRenderData(data, me.subTplInsertions);
  342. return data;
  343. },
  344. afterFirstLayout: function() {
  345. this.callParent();
  346. var el = this.inputEl;
  347. if (el) {
  348. el.selectable();
  349. }
  350. },
  351. applyRenderSelectors: function() {
  352. var me = this;
  353. me.callParent();
  354. <span id='Ext-form-field-Base-property-inputEl'> /**
  355. </span> * @property {Ext.Element} inputEl
  356. * The input Element for this Field. Only available after the field has been rendered.
  357. */
  358. me.inputEl = me.el.getById(me.getInputId());
  359. },
  360. <span id='Ext-form-field-Base-method-getSubTplMarkup'> /**
  361. </span> * Gets the markup to be inserted into the outer template's bodyEl. For fields this is the actual input element.
  362. */
  363. getSubTplMarkup: function() {
  364. return this.getTpl('fieldSubTpl').apply(this.getSubTplData());
  365. },
  366. initRenderTpl: function() {
  367. var me = this;
  368. if (!me.hasOwnProperty('renderTpl')) {
  369. me.renderTpl = me.getTpl('labelableRenderTpl');
  370. }
  371. return me.callParent();
  372. },
  373. initRenderData: function() {
  374. return Ext.applyIf(this.callParent(), this.getLabelableRenderData());
  375. },
  376. <span id='Ext-form-field-Base-method-setFieldStyle'> /**
  377. </span> * Set the {@link #fieldStyle CSS style} of the {@link #inputEl field input element}.
  378. * @param {String/Object/Function} style The style(s) to apply. Should be a valid argument to {@link
  379. * Ext.Element#applyStyles}.
  380. */
  381. setFieldStyle: function(style) {
  382. var me = this,
  383. inputEl = me.inputEl;
  384. if (inputEl) {
  385. inputEl.applyStyles(style);
  386. }
  387. me.fieldStyle = style;
  388. },
  389. getFieldStyle: function() {
  390. return 'width:100%;' + (Ext.isObject(this.fieldStyle) ? Ext.DomHelper.generateStyles(this.fieldStyle) : this.fieldStyle ||'');
  391. },
  392. // private
  393. onRender : function() {
  394. var me = this;
  395. me.callParent(arguments);
  396. me.onLabelableRender();
  397. me.renderActiveError();
  398. },
  399. getFocusEl: function() {
  400. return this.inputEl;
  401. },
  402. isFileUpload: function() {
  403. return this.inputType === 'file';
  404. },
  405. extractFileInput: function() {
  406. var me = this,
  407. fileInput = me.isFileUpload() ? me.inputEl.dom : null,
  408. clone;
  409. if (fileInput) {
  410. clone = fileInput.cloneNode(true);
  411. fileInput.parentNode.replaceChild(clone, fileInput);
  412. me.inputEl = Ext.get(clone);
  413. }
  414. return fileInput;
  415. },
  416. // private override to use getSubmitValue() as a convenience
  417. getSubmitData: function() {
  418. var me = this,
  419. data = null,
  420. val;
  421. if (!me.disabled &amp;&amp; me.submitValue &amp;&amp; !me.isFileUpload()) {
  422. val = me.getSubmitValue();
  423. if (val !== null) {
  424. data = {};
  425. data[me.getName()] = val;
  426. }
  427. }
  428. return data;
  429. },
  430. <span id='Ext-form-field-Base-method-getSubmitValue'> /**
  431. </span> * Returns the value that would be included in a standard form submit for this field. This will be combined with the
  432. * field's name to form a name=value pair in the {@link #getSubmitData submitted parameters}. If an empty string is
  433. * returned then just the name= will be submitted; if null is returned then nothing will be submitted.
  434. *
  435. * Note that the value returned will have been {@link #processRawValue processed} but may or may not have been
  436. * successfully {@link #validate validated}.
  437. *
  438. * @return {String} The value to be submitted, or null.
  439. */
  440. getSubmitValue: function() {
  441. return this.processRawValue(this.getRawValue());
  442. },
  443. <span id='Ext-form-field-Base-method-getRawValue'> /**
  444. </span> * Returns the raw value of the field, without performing any normalization, conversion, or validation. To get a
  445. * normalized and converted value see {@link #getValue}.
  446. * @return {String} value The raw String value of the field
  447. */
  448. getRawValue: function() {
  449. var me = this,
  450. v = (me.inputEl ? me.inputEl.getValue() : Ext.value(me.rawValue, ''));
  451. me.rawValue = v;
  452. return v;
  453. },
  454. <span id='Ext-form-field-Base-method-setRawValue'> /**
  455. </span> * Sets the field's raw value directly, bypassing {@link #valueToRaw value conversion}, change detection, and
  456. * validation. To set the value with these additional inspections see {@link #setValue}.
  457. * @param {Object} value The value to set
  458. * @return {Object} value The field value that is set
  459. */
  460. setRawValue: function(value) {
  461. var me = this;
  462. value = Ext.value(me.transformRawValue(value), '');
  463. me.rawValue = value;
  464. // Some Field subclasses may not render an inputEl
  465. if (me.inputEl) {
  466. me.inputEl.dom.value = value;
  467. }
  468. return value;
  469. },
  470. <span id='Ext-form-field-Base-method-transformRawValue'> /**
  471. </span> * Transform the raw value before it is set
  472. * @protected
  473. * @param {Object} value The value
  474. * @return {Object} The value to set
  475. */
  476. transformRawValue: function(value) {
  477. return value;
  478. },
  479. <span id='Ext-form-field-Base-method-valueToRaw'> /**
  480. </span> * Converts a mixed-type value to a raw representation suitable for displaying in the field. This allows controlling
  481. * how value objects passed to {@link #setValue} are shown to the user, including localization. For instance, for a
  482. * {@link Ext.form.field.Date}, this would control how a Date object passed to {@link #setValue} would be converted
  483. * to a String for display in the field.
  484. *
  485. * See {@link #rawToValue} for the opposite conversion.
  486. *
  487. * The base implementation simply does a standard toString conversion, and converts {@link Ext#isEmpty empty values}
  488. * to an empty string.
  489. *
  490. * @param {Object} value The mixed-type value to convert to the raw representation.
  491. * @return {Object} The converted raw value.
  492. */
  493. valueToRaw: function(value) {
  494. return '' + Ext.value(value, '');
  495. },
  496. <span id='Ext-form-field-Base-method-rawToValue'> /**
  497. </span> * Converts a raw input field value into a mixed-type value that is suitable for this particular field type. This
  498. * allows controlling the normalization and conversion of user-entered values into field-type-appropriate values,
  499. * e.g. a Date object for {@link Ext.form.field.Date}, and is invoked by {@link #getValue}.
  500. *
  501. * It is up to individual implementations to decide how to handle raw values that cannot be successfully converted
  502. * to the desired object type.
  503. *
  504. * See {@link #valueToRaw} for the opposite conversion.
  505. *
  506. * The base implementation does no conversion, returning the raw value untouched.
  507. *
  508. * @param {Object} rawValue
  509. * @return {Object} The converted value.
  510. */
  511. rawToValue: function(rawValue) {
  512. return rawValue;
  513. },
  514. <span id='Ext-form-field-Base-method-processRawValue'> /**
  515. </span> * Performs any necessary manipulation of a raw field value to prepare it for {@link #rawToValue conversion} and/or
  516. * {@link #validate validation}, for instance stripping out ignored characters. In the base implementation it does
  517. * nothing; individual subclasses may override this as needed.
  518. *
  519. * @param {Object} value The unprocessed string value
  520. * @return {Object} The processed string value
  521. */
  522. processRawValue: function(value) {
  523. return value;
  524. },
  525. <span id='Ext-form-field-Base-method-getValue'> /**
  526. </span> * Returns the current data value of the field. The type of value returned is particular to the type of the
  527. * particular field (e.g. a Date object for {@link Ext.form.field.Date}), as the result of calling {@link #rawToValue} on
  528. * the field's {@link #processRawValue processed} String value. To return the raw String value, see {@link #getRawValue}.
  529. * @return {Object} value The field value
  530. */
  531. getValue: function() {
  532. var me = this,
  533. val = me.rawToValue(me.processRawValue(me.getRawValue()));
  534. me.value = val;
  535. return val;
  536. },
  537. <span id='Ext-form-field-Base-method-setValue'> /**
  538. </span> * Sets a data value into the field and runs the change detection and validation. To set the value directly
  539. * without these inspections see {@link #setRawValue}.
  540. * @param {Object} value The value to set
  541. * @return {Ext.form.field.Field} this
  542. */
  543. setValue: function(value) {
  544. var me = this;
  545. me.setRawValue(me.valueToRaw(value));
  546. return me.mixins.field.setValue.call(me, value);
  547. },
  548. onBoxReady: function() {
  549. var me = this;
  550. me.callParent();
  551. if (me.setReadOnlyOnBoxReady) {
  552. me.setReadOnly(me.readOnly);
  553. }
  554. },
  555. //private
  556. onDisable: function() {
  557. var me = this,
  558. inputEl = me.inputEl;
  559. me.callParent();
  560. if (inputEl) {
  561. inputEl.dom.disabled = true;
  562. if (me.hasActiveError()) {
  563. // clear invalid state since the field is now disabled
  564. me.clearInvalid();
  565. me.needsValidateOnEnable = true;
  566. }
  567. }
  568. },
  569. //private
  570. onEnable: function() {
  571. var me = this,
  572. inputEl = me.inputEl;
  573. me.callParent();
  574. if (inputEl) {
  575. inputEl.dom.disabled = false;
  576. if (me.needsValidateOnEnable) {
  577. delete me.needsValidateOnEnable;
  578. // will trigger errors to be shown
  579. me.forceValidation = true;
  580. me.isValid();
  581. delete me.forceValidation;
  582. }
  583. }
  584. },
  585. <span id='Ext-form-field-Base-method-setReadOnly'> /**
  586. </span> * Sets the read only state of this field.
  587. * @param {Boolean} readOnly Whether the field should be read only.
  588. */
  589. setReadOnly: function(readOnly) {
  590. var me = this,
  591. inputEl = me.inputEl;
  592. readOnly = !!readOnly;
  593. me[readOnly ? 'addCls' : 'removeCls'](me.readOnlyCls);
  594. me.readOnly = readOnly;
  595. if (inputEl) {
  596. inputEl.dom.readOnly = readOnly;
  597. } else if (me.rendering) {
  598. me.setReadOnlyOnBoxReady = true;
  599. }
  600. me.fireEvent('writeablechange', me, readOnly);
  601. },
  602. // private
  603. fireKey: function(e){
  604. if(e.isSpecialKey()){
  605. this.fireEvent('specialkey', this, new Ext.EventObjectImpl(e));
  606. }
  607. },
  608. // private
  609. initEvents : function(){
  610. var me = this,
  611. inputEl = me.inputEl,
  612. onChangeTask,
  613. onChangeEvent,
  614. events = me.checkChangeEvents,
  615. e,
  616. eLen = events.length,
  617. event;
  618. // standardise buffer across all browsers + OS-es for consistent event order.
  619. // (the 10ms buffer for Editors fixes a weird FF/Win editor issue when changing OS window focus)
  620. if (me.inEditor) {
  621. me.onBlur = Ext.Function.createBuffered(me.onBlur, 10);
  622. }
  623. if (inputEl) {
  624. me.mon(inputEl, Ext.EventManager.getKeyEvent(), me.fireKey, me);
  625. // listen for immediate value changes
  626. onChangeTask = new Ext.util.DelayedTask(me.checkChange, me);
  627. me.onChangeEvent = onChangeEvent = function() {
  628. onChangeTask.delay(me.checkChangeBuffer);
  629. };
  630. for (e = 0; e &lt; eLen; e++) {
  631. event = events[e];
  632. if (event === 'propertychange') {
  633. me.usesPropertychange = true;
  634. }
  635. me.mon(inputEl, event, onChangeEvent);
  636. }
  637. }
  638. me.callParent();
  639. },
  640. doComponentLayout: function() {
  641. var me = this,
  642. inputEl = me.inputEl,
  643. usesPropertychange = me.usesPropertychange,
  644. ename = 'propertychange',
  645. onChangeEvent = me.onChangeEvent;
  646. // In IE if propertychange is one of the checkChangeEvents, we need to remove
  647. // the listener prior to layout and re-add it after, to prevent it from firing
  648. // needlessly for attribute and style changes applied to the inputEl.
  649. if (usesPropertychange) {
  650. me.mun(inputEl, ename, onChangeEvent);
  651. }
  652. me.callParent(arguments);
  653. if (usesPropertychange) {
  654. me.mon(inputEl, ename, onChangeEvent);
  655. }
  656. },
  657. <span id='Ext-form-field-Base-method-onDirtyChange'> /**
  658. </span> * @private Called when the field's dirty state changes. Adds/removes the {@link #dirtyCls} on the main element.
  659. * @param {Boolean} isDirty
  660. */
  661. onDirtyChange: function(isDirty) {
  662. this[isDirty ? 'addCls' : 'removeCls'](this.dirtyCls);
  663. },
  664. <span id='Ext-form-field-Base-method-isValid'> /**
  665. </span> * Returns whether or not the field value is currently valid by {@link #getErrors validating} the
  666. * {@link #processRawValue processed raw value} of the field. **Note**: {@link #disabled} fields are
  667. * always treated as valid.
  668. *
  669. * @return {Boolean} True if the value is valid, else false
  670. */
  671. isValid : function() {
  672. var me = this,
  673. disabled = me.disabled,
  674. validate = me.forceValidation || !disabled;
  675. return validate ? me.validateValue(me.processRawValue(me.getRawValue())) : disabled;
  676. },
  677. <span id='Ext-form-field-Base-method-validateValue'> /**
  678. </span> * Uses {@link #getErrors} to build an array of validation errors. If any errors are found, they are passed to
  679. * {@link #markInvalid} and false is returned, otherwise true is returned.
  680. *
  681. * Previously, subclasses were invited to provide an implementation of this to process validations - from 3.2
  682. * onwards {@link #getErrors} should be overridden instead.
  683. *
  684. * @param {Object} value The value to validate
  685. * @return {Boolean} True if all validations passed, false if one or more failed
  686. */
  687. validateValue: function(value) {
  688. var me = this,
  689. errors = me.getErrors(value),
  690. isValid = Ext.isEmpty(errors);
  691. if (!me.preventMark) {
  692. if (isValid) {
  693. me.clearInvalid();
  694. } else {
  695. me.markInvalid(errors);
  696. }
  697. }
  698. return isValid;
  699. },
  700. <span id='Ext-form-field-Base-method-markInvalid'> /**
  701. </span> * Display one or more error messages associated with this field, using {@link #msgTarget} to determine how to
  702. * display the messages and applying {@link #invalidCls} to the field's UI element.
  703. *
  704. * **Note**: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to return `false`
  705. * if the value does _pass_ validation. So simply marking a Field as invalid will not prevent submission of forms
  706. * submitted with the {@link Ext.form.action.Submit#clientValidation} option set.
  707. *
  708. * @param {String/String[]} errors The validation message(s) to display.
  709. */
  710. markInvalid : function(errors) {
  711. // Save the message and fire the 'invalid' event
  712. var me = this,
  713. oldMsg = me.getActiveError();
  714. me.setActiveErrors(Ext.Array.from(errors));
  715. if (oldMsg !== me.getActiveError()) {
  716. me.updateLayout();
  717. }
  718. },
  719. <span id='Ext-form-field-Base-method-clearInvalid'> /**
  720. </span> * Clear any invalid styles/messages for this field.
  721. *
  722. * **Note**: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to return `true`
  723. * if the value does not _pass_ validation. So simply clearing a field's errors will not necessarily allow
  724. * submission of forms submitted with the {@link Ext.form.action.Submit#clientValidation} option set.
  725. */
  726. clearInvalid : function() {
  727. // Clear the message and fire the 'valid' event
  728. var me = this,
  729. hadError = me.hasActiveError();
  730. me.unsetActiveError();
  731. if (hadError) {
  732. me.updateLayout();
  733. }
  734. },
  735. <span id='Ext-form-field-Base-method-renderActiveError'> /**
  736. </span> * @private Overrides the method from the Ext.form.Labelable mixin to also add the invalidCls to the inputEl,
  737. * as that is required for proper styling in IE with nested fields (due to lack of child selector)
  738. */
  739. renderActiveError: function() {
  740. var me = this,
  741. hasError = me.hasActiveError();
  742. if (me.inputEl) {
  743. // Add/remove invalid class
  744. me.inputEl[hasError ? 'addCls' : 'removeCls'](me.invalidCls + '-field');
  745. }
  746. me.mixins.labelable.renderActiveError.call(me);
  747. },
  748. getActionEl: function() {
  749. return this.inputEl || this.el;
  750. }
  751. });
  752. </pre>
  753. </body>
  754. </html>