TextArea2.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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-TextArea'>/**
  19. </span> * @docauthor Robert Dougan &lt;rob@sencha.com&gt;
  20. *
  21. * This class creates a multiline text field, which can be used as a direct replacement for traditional
  22. * textarea fields. In addition, it supports automatically {@link #grow growing} the height of the textarea to
  23. * fit its content.
  24. *
  25. * All of the configuration options from {@link Ext.form.field.Text} can be used on TextArea.
  26. *
  27. * Example usage:
  28. *
  29. * @example
  30. * Ext.create('Ext.form.FormPanel', {
  31. * title : 'Sample TextArea',
  32. * width : 400,
  33. * bodyPadding: 10,
  34. * renderTo : Ext.getBody(),
  35. * items: [{
  36. * xtype : 'textareafield',
  37. * grow : true,
  38. * name : 'message',
  39. * fieldLabel: 'Message',
  40. * anchor : '100%'
  41. * }]
  42. * });
  43. *
  44. * Some other useful configuration options when using {@link #grow} are {@link #growMin} and {@link #growMax}.
  45. * These allow you to set the minimum and maximum grow heights for the textarea.
  46. *
  47. * **NOTE:** In some browsers, carriage returns ('\r', not to be confused with new lines)
  48. * will be automatically stripped out the value is set to the textarea. Since we cannot
  49. * use any reasonable method to attempt to re-insert these, they will automatically be
  50. * stripped out to ensure the behaviour is consistent across browser.
  51. */
  52. Ext.define('Ext.form.field.TextArea', {
  53. extend:'Ext.form.field.Text',
  54. alias: ['widget.textareafield', 'widget.textarea'],
  55. alternateClassName: 'Ext.form.TextArea',
  56. requires: [
  57. 'Ext.XTemplate',
  58. 'Ext.layout.component.field.TextArea',
  59. 'Ext.util.DelayedTask'
  60. ],
  61. // This template includes a \n after &lt;textarea&gt; opening tag so that an initial value starting
  62. // with \n does not lose its first character when the markup is parsed.
  63. // Both textareas below have the same value:
  64. // &lt;textarea&gt;initial value&lt;/textarea&gt;
  65. // &lt;textarea&gt;
  66. // initial value
  67. // &lt;/textarea&gt;
  68. fieldSubTpl: [
  69. '&lt;textarea id=&quot;{id}&quot; {inputAttrTpl}',
  70. '&lt;tpl if=&quot;name&quot;&gt; name=&quot;{name}&quot;&lt;/tpl&gt;',
  71. '&lt;tpl if=&quot;rows&quot;&gt; rows=&quot;{rows}&quot; &lt;/tpl&gt;',
  72. '&lt;tpl if=&quot;cols&quot;&gt; cols=&quot;{cols}&quot; &lt;/tpl&gt;',
  73. '&lt;tpl if=&quot;placeholder&quot;&gt; placeholder=&quot;{placeholder}&quot;&lt;/tpl&gt;',
  74. '&lt;tpl if=&quot;size&quot;&gt; size=&quot;{size}&quot;&lt;/tpl&gt;',
  75. '&lt;tpl if=&quot;maxLength !== undefined&quot;&gt; maxlength=&quot;{maxLength}&quot;&lt;/tpl&gt;',
  76. '&lt;tpl if=&quot;readOnly&quot;&gt; readonly=&quot;readonly&quot;&lt;/tpl&gt;',
  77. '&lt;tpl if=&quot;disabled&quot;&gt; disabled=&quot;disabled&quot;&lt;/tpl&gt;',
  78. '&lt;tpl if=&quot;tabIdx&quot;&gt; tabIndex=&quot;{tabIdx}&quot;&lt;/tpl&gt;',
  79. ' class=&quot;{fieldCls} {typeCls}&quot; ',
  80. '&lt;tpl if=&quot;fieldStyle&quot;&gt; style=&quot;{fieldStyle}&quot;&lt;/tpl&gt;',
  81. ' autocomplete=&quot;off&quot;&gt;\n',
  82. '&lt;tpl if=&quot;value&quot;&gt;{[Ext.util.Format.htmlEncode(values.value)]}&lt;/tpl&gt;',
  83. '&lt;/textarea&gt;',
  84. {
  85. disableFormats: true
  86. }
  87. ],
  88. <span id='Ext-form-field-TextArea-cfg-growMin'> /**
  89. </span> * @cfg {Number} growMin
  90. * The minimum height to allow when {@link #grow}=true
  91. */
  92. growMin: 60,
  93. <span id='Ext-form-field-TextArea-cfg-growMax'> /**
  94. </span> * @cfg {Number} growMax
  95. * The maximum height to allow when {@link #grow}=true
  96. */
  97. growMax: 1000,
  98. <span id='Ext-form-field-TextArea-cfg-growAppend'> /**
  99. </span> * @cfg {String} growAppend
  100. * A string that will be appended to the field's current value for the purposes of calculating the target field
  101. * size. Only used when the {@link #grow} config is true. Defaults to a newline for TextArea to ensure there is
  102. * always a space below the current line.
  103. */
  104. growAppend: '\n-',
  105. <span id='Ext-form-field-TextArea-cfg-cols'> /**
  106. </span> * @cfg {Number} cols
  107. * An initial value for the 'cols' attribute on the textarea element. This is only used if the component has no
  108. * configured {@link #width} and is not given a width by its container's layout.
  109. */
  110. cols: 20,
  111. <span id='Ext-form-field-TextArea-cfg-rows'> /**
  112. </span> * @cfg {Number} rows
  113. * An initial value for the 'rows' attribute on the textarea element. This is only used if the component has no
  114. * configured {@link #height} and is not given a height by its container's layout. Defaults to 4.
  115. */
  116. rows: 4,
  117. <span id='Ext-form-field-TextArea-cfg-enterIsSpecial'> /**
  118. </span> * @cfg {Boolean} enterIsSpecial
  119. * True if you want the ENTER key to be classed as a special key and the {@link #specialkey} event to be fired
  120. * when ENTER is pressed.
  121. */
  122. enterIsSpecial: false,
  123. <span id='Ext-form-field-TextArea-cfg-preventScrollbars'> /**
  124. </span> * @cfg {Boolean} preventScrollbars
  125. * true to prevent scrollbars from appearing regardless of how much text is in the field. This option is only
  126. * relevant when {@link #grow} is true. Equivalent to setting overflow: hidden.
  127. */
  128. preventScrollbars: false,
  129. // private
  130. componentLayout: 'textareafield',
  131. setGrowSizePolicy: Ext.emptyFn,
  132. returnRe: /\r/g,
  133. // private
  134. getSubTplData: function() {
  135. var me = this,
  136. fieldStyle = me.getFieldStyle(),
  137. ret = me.callParent();
  138. if (me.grow) {
  139. if (me.preventScrollbars) {
  140. ret.fieldStyle = (fieldStyle||'') + ';overflow:hidden;height:' + me.growMin + 'px';
  141. }
  142. }
  143. Ext.applyIf(ret, {
  144. cols: me.cols,
  145. rows: me.rows
  146. });
  147. return ret;
  148. },
  149. afterRender: function () {
  150. var me = this;
  151. me.callParent(arguments);
  152. me.needsMaxCheck = me.enforceMaxLength &amp;&amp; me.maxLength !== Number.MAX_VALUE &amp;&amp; !Ext.supports.TextAreaMaxLength;
  153. if (me.needsMaxCheck) {
  154. me.inputEl.on('paste', me.onPaste, me);
  155. }
  156. },
  157. // The following overrides deal with an issue whereby some browsers
  158. // will strip carriage returns from the textarea input, while others
  159. // will not. Since there's no way to be sure where to insert returns,
  160. // the best solution is to strip them out in all cases to ensure that
  161. // the behaviour is consistent in a cross browser fashion. As such,
  162. // we override in all cases when setting the value to control this.
  163. transformRawValue: function(value){
  164. return this.stripReturns(value);
  165. },
  166. transformOriginalValue: function(value){
  167. return this.stripReturns(value);
  168. },
  169. valueToRaw: function(value){
  170. value = this.stripReturns(value);
  171. return this.callParent([value]);
  172. },
  173. stripReturns: function(value){
  174. if (value) {
  175. value = value.replace(this.returnRe, '');
  176. }
  177. return value;
  178. },
  179. onPaste: function(e){
  180. var me = this;
  181. if (!me.pasteTask) {
  182. me.pasteTask = new Ext.util.DelayedTask(me.pasteCheck, me);
  183. }
  184. // since we can't get the paste data, we'll give the area a chance to populate
  185. me.pasteTask.delay(1);
  186. },
  187. pasteCheck: function(){
  188. var me = this,
  189. value = me.getValue(),
  190. max = me.maxLength;
  191. if (value.length &gt; max) {
  192. value = value.substr(0, max);
  193. me.setValue(value);
  194. }
  195. },
  196. // private
  197. fireKey: function(e) {
  198. var me = this,
  199. key = e.getKey(),
  200. value;
  201. if (e.isSpecialKey() &amp;&amp; (me.enterIsSpecial || (key !== e.ENTER || e.hasModifier()))) {
  202. me.fireEvent('specialkey', me, e);
  203. }
  204. if (me.needsMaxCheck &amp;&amp; key !== e.BACKSPACE &amp;&amp; key !== e.DELETE &amp;&amp; !e.isNavKeyPress() &amp;&amp; !me.isCutCopyPasteSelectAll(e, key)) {
  205. value = me.getValue();
  206. if (value.length &gt;= me.maxLength) {
  207. e.stopEvent();
  208. }
  209. }
  210. },
  211. isCutCopyPasteSelectAll: function(e, key) {
  212. if (e.CTRL) {
  213. return key === e.A || key === e.C || key === e.V || key === e.X;
  214. }
  215. return false;
  216. },
  217. <span id='Ext-form-field-TextArea-method-autoSize'> /**
  218. </span> * Automatically grows the field to accomodate the height of the text up to the maximum field height allowed. This
  219. * only takes effect if {@link #grow} = true, and fires the {@link #autosize} event if the height changes.
  220. */
  221. autoSize: function() {
  222. var me = this,
  223. height;
  224. if (me.grow &amp;&amp; me.rendered) {
  225. me.updateLayout();
  226. height = me.inputEl.getHeight();
  227. if (height !== me.lastInputHeight) {
  228. <span id='Ext-form-field-TextArea-event-autosize'> /**
  229. </span> * @event autosize
  230. * Fires when the {@link #autoSize} function is triggered and the field is resized according to
  231. * the grow/growMin/growMax configs as a result. This event provides a hook for the developer
  232. * to apply additional logic at runtime to resize the field if needed.
  233. * @param {Ext.form.field.Text} this
  234. * @param {Number} height
  235. */
  236. me.fireEvent('autosize', me, height);
  237. me.lastInputHeight = height;
  238. }
  239. }
  240. },
  241. // private
  242. initAria: function() {
  243. this.callParent(arguments);
  244. this.getActionEl().dom.setAttribute('aria-multiline', true);
  245. },
  246. beforeDestroy: function(){
  247. var task = this.pasteTask;
  248. if (task) {
  249. task.delay();
  250. }
  251. this.callParent();
  252. }
  253. });</pre>
  254. </body>
  255. </html>