Spinner.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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-Spinner'>/**
  19. </span> * A field with a pair of up/down spinner buttons. This class is not normally instantiated directly,
  20. * instead it is subclassed and the {@link #onSpinUp} and {@link #onSpinDown} methods are implemented
  21. * to handle when the buttons are clicked. A good example of this is the {@link Ext.form.field.Number}
  22. * field which uses the spinner to increment and decrement the field's value by its
  23. * {@link Ext.form.field.Number#step step} config value.
  24. *
  25. * For example:
  26. *
  27. * @example
  28. * Ext.define('Ext.ux.CustomSpinner', {
  29. * extend: 'Ext.form.field.Spinner',
  30. * alias: 'widget.customspinner',
  31. *
  32. * // override onSpinUp (using step isn't neccessary)
  33. * onSpinUp: function() {
  34. * var me = this;
  35. * if (!me.readOnly) {
  36. * var val = parseInt(me.getValue().split(' '), 10)||0; // gets rid of &quot; Pack&quot;, defaults to zero on parse failure
  37. * me.setValue((val + me.step) + ' Pack');
  38. * }
  39. * },
  40. *
  41. * // override onSpinDown
  42. * onSpinDown: function() {
  43. * var val, me = this;
  44. * if (!me.readOnly) {
  45. * var val = parseInt(me.getValue().split(' '), 10)||0; // gets rid of &quot; Pack&quot;, defaults to zero on parse failure
  46. * if (val &lt;= me.step) {
  47. * me.setValue('Dry!');
  48. * } else {
  49. * me.setValue((val - me.step) + ' Pack');
  50. * }
  51. * }
  52. * }
  53. * });
  54. *
  55. * Ext.create('Ext.form.FormPanel', {
  56. * title: 'Form with SpinnerField',
  57. * bodyPadding: 5,
  58. * width: 350,
  59. * renderTo: Ext.getBody(),
  60. * items:[{
  61. * xtype: 'customspinner',
  62. * fieldLabel: 'How Much Beer?',
  63. * step: 6
  64. * }]
  65. * });
  66. *
  67. * By default, pressing the up and down arrow keys will also trigger the onSpinUp and onSpinDown methods;
  68. * to prevent this, set `{@link #keyNavEnabled} = false`.
  69. */
  70. Ext.define('Ext.form.field.Spinner', {
  71. extend: 'Ext.form.field.Trigger',
  72. alias: 'widget.spinnerfield',
  73. alternateClassName: 'Ext.form.Spinner',
  74. requires: ['Ext.util.KeyNav'],
  75. trigger1Cls: Ext.baseCSSPrefix + 'form-spinner-up',
  76. trigger2Cls: Ext.baseCSSPrefix + 'form-spinner-down',
  77. <span id='Ext-form-field-Spinner-cfg-spinUpEnabled'> /**
  78. </span> * @cfg {Boolean} spinUpEnabled
  79. * Specifies whether the up spinner button is enabled. Defaults to true. To change this after the component is
  80. * created, use the {@link #setSpinUpEnabled} method.
  81. */
  82. spinUpEnabled: true,
  83. <span id='Ext-form-field-Spinner-cfg-spinDownEnabled'> /**
  84. </span> * @cfg {Boolean} spinDownEnabled
  85. * Specifies whether the down spinner button is enabled. Defaults to true. To change this after the component is
  86. * created, use the {@link #setSpinDownEnabled} method.
  87. */
  88. spinDownEnabled: true,
  89. <span id='Ext-form-field-Spinner-cfg-keyNavEnabled'> /**
  90. </span> * @cfg {Boolean} keyNavEnabled
  91. * Specifies whether the up and down arrow keys should trigger spinning up and down. Defaults to true.
  92. */
  93. keyNavEnabled: true,
  94. <span id='Ext-form-field-Spinner-cfg-mouseWheelEnabled'> /**
  95. </span> * @cfg {Boolean} mouseWheelEnabled
  96. * Specifies whether the mouse wheel should trigger spinning up and down while the field has focus.
  97. * Defaults to true.
  98. */
  99. mouseWheelEnabled: true,
  100. <span id='Ext-form-field-Spinner-cfg-repeatTriggerClick'> /**
  101. </span> * @cfg {Boolean} repeatTriggerClick
  102. * Whether a {@link Ext.util.ClickRepeater click repeater} should be attached to the spinner buttons.
  103. * Defaults to true.
  104. */
  105. repeatTriggerClick: true,
  106. <span id='Ext-form-field-Spinner-method-onSpinUp'> /**
  107. </span> * @method
  108. * @protected
  109. * This method is called when the spinner up button is clicked, or when the up arrow key is pressed if
  110. * {@link #keyNavEnabled} is true. Must be implemented by subclasses.
  111. */
  112. onSpinUp: Ext.emptyFn,
  113. <span id='Ext-form-field-Spinner-method-onSpinDown'> /**
  114. </span> * @method
  115. * @protected
  116. * This method is called when the spinner down button is clicked, or when the down arrow key is pressed if
  117. * {@link #keyNavEnabled} is true. Must be implemented by subclasses.
  118. */
  119. onSpinDown: Ext.emptyFn,
  120. triggerTpl: '&lt;td style=&quot;{triggerStyle}&quot;&gt;' +
  121. '&lt;div class=&quot;' + Ext.baseCSSPrefix + 'trigger-index-0 ' + Ext.baseCSSPrefix + 'form-trigger ' + Ext.baseCSSPrefix + 'form-spinner-up&quot; role=&quot;button&quot;&gt;&lt;/div&gt;' +
  122. '&lt;div class=&quot;' + Ext.baseCSSPrefix + 'trigger-index-1 ' + Ext.baseCSSPrefix + 'form-trigger ' + Ext.baseCSSPrefix + 'form-spinner-down&quot; role=&quot;button&quot;&gt;&lt;/div&gt;' +
  123. '&lt;/td&gt;' +
  124. '&lt;/tr&gt;',
  125. initComponent: function() {
  126. this.callParent();
  127. this.addEvents(
  128. <span id='Ext-form-field-Spinner-event-spin'> /**
  129. </span> * @event spin
  130. * Fires when the spinner is made to spin up or down.
  131. * @param {Ext.form.field.Spinner} this
  132. * @param {String} direction Either 'up' if spinning up, or 'down' if spinning down.
  133. */
  134. 'spin',
  135. <span id='Ext-form-field-Spinner-event-spinup'> /**
  136. </span> * @event spinup
  137. * Fires when the spinner is made to spin up.
  138. * @param {Ext.form.field.Spinner} this
  139. */
  140. 'spinup',
  141. <span id='Ext-form-field-Spinner-event-spindown'> /**
  142. </span> * @event spindown
  143. * Fires when the spinner is made to spin down.
  144. * @param {Ext.form.field.Spinner} this
  145. */
  146. 'spindown'
  147. );
  148. },
  149. <span id='Ext-form-field-Spinner-method-onRender'> /**
  150. </span> * @private
  151. * Override.
  152. */
  153. onRender: function() {
  154. var me = this,
  155. triggers;
  156. me.callParent(arguments);
  157. triggers = me.triggerEl;
  158. <span id='Ext-form-field-Spinner-property-spinUpEl'> /**
  159. </span> * @property {Ext.Element} spinUpEl
  160. * The spinner up button element
  161. */
  162. me.spinUpEl = triggers.item(0);
  163. <span id='Ext-form-field-Spinner-property-spinDownEl'> /**
  164. </span> * @property {Ext.Element} spinDownEl
  165. * The spinner down button element
  166. */
  167. me.spinDownEl = triggers.item(1);
  168. me.triggerCell = me.spinUpEl.parent();
  169. // Set initial enabled/disabled states
  170. me.setSpinUpEnabled(me.spinUpEnabled);
  171. me.setSpinDownEnabled(me.spinDownEnabled);
  172. // Init up/down arrow keys
  173. if (me.keyNavEnabled) {
  174. me.spinnerKeyNav = new Ext.util.KeyNav(me.inputEl, {
  175. scope: me,
  176. up: me.spinUp,
  177. down: me.spinDown
  178. });
  179. }
  180. // Init mouse wheel
  181. if (me.mouseWheelEnabled) {
  182. me.mon(me.bodyEl, 'mousewheel', me.onMouseWheel, me);
  183. }
  184. },
  185. getSubTplMarkup: function() {
  186. var me = this,
  187. field = Ext.form.field.Base.prototype.getSubTplMarkup.apply(me, arguments);
  188. return '&lt;table id=&quot;' + me.id + '-triggerWrap&quot; class=&quot;' + Ext.baseCSSPrefix + 'form-trigger-wrap&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;' +
  189. '&lt;tbody&gt;' +
  190. '&lt;tr&gt;&lt;td id=&quot;' + me.id + '-inputCell&quot; class=&quot;' + Ext.baseCSSPrefix + 'form-trigger-input-cell&quot;&gt;' + field + '&lt;/td&gt;' +
  191. me.getTriggerMarkup() +
  192. '&lt;/tbody&gt;&lt;/table&gt;';
  193. },
  194. getTriggerMarkup: function() {
  195. var me = this,
  196. hideTrigger = (me.readOnly || me.hideTrigger);
  197. return me.getTpl('triggerTpl').apply({
  198. triggerStyle: 'width:' + me.triggerWidth + (hideTrigger ? 'px;display:none' : 'px')
  199. });
  200. },
  201. <span id='Ext-form-field-Spinner-method-getTriggerWidth'> /**
  202. </span> * Get the total width of the spinner button area.
  203. * @return {Number} The total spinner button width
  204. */
  205. getTriggerWidth: function() {
  206. var me = this,
  207. totalTriggerWidth = 0;
  208. if (me.triggerWrap &amp;&amp; !me.hideTrigger &amp;&amp; !me.readOnly) {
  209. totalTriggerWidth = me.triggerWidth;
  210. }
  211. return totalTriggerWidth;
  212. },
  213. <span id='Ext-form-field-Spinner-method-onTrigger1Click'> /**
  214. </span> * @private
  215. * Handles the spinner up button clicks.
  216. */
  217. onTrigger1Click: function() {
  218. this.spinUp();
  219. },
  220. <span id='Ext-form-field-Spinner-method-onTrigger2Click'> /**
  221. </span> * @private
  222. * Handles the spinner down button clicks.
  223. */
  224. onTrigger2Click: function() {
  225. this.spinDown();
  226. },
  227. // private
  228. // Handle trigger mouse up gesture; refocuses the input element upon end of spin.
  229. onTriggerWrapMouseup: function() {
  230. this.inputEl.focus();
  231. },
  232. <span id='Ext-form-field-Spinner-method-spinUp'> /**
  233. </span> * Triggers the spinner to step up; fires the {@link #spin} and {@link #spinup} events and calls the
  234. * {@link #onSpinUp} method. Does nothing if the field is {@link #disabled} or if {@link #spinUpEnabled}
  235. * is false.
  236. */
  237. spinUp: function() {
  238. var me = this;
  239. if (me.spinUpEnabled &amp;&amp; !me.disabled) {
  240. me.fireEvent('spin', me, 'up');
  241. me.fireEvent('spinup', me);
  242. me.onSpinUp();
  243. }
  244. },
  245. <span id='Ext-form-field-Spinner-method-spinDown'> /**
  246. </span> * Triggers the spinner to step down; fires the {@link #spin} and {@link #spindown} events and calls the
  247. * {@link #onSpinDown} method. Does nothing if the field is {@link #disabled} or if {@link #spinDownEnabled}
  248. * is false.
  249. */
  250. spinDown: function() {
  251. var me = this;
  252. if (me.spinDownEnabled &amp;&amp; !me.disabled) {
  253. me.fireEvent('spin', me, 'down');
  254. me.fireEvent('spindown', me);
  255. me.onSpinDown();
  256. }
  257. },
  258. <span id='Ext-form-field-Spinner-method-setSpinUpEnabled'> /**
  259. </span> * Sets whether the spinner up button is enabled.
  260. * @param {Boolean} enabled true to enable the button, false to disable it.
  261. */
  262. setSpinUpEnabled: function(enabled) {
  263. var me = this,
  264. wasEnabled = me.spinUpEnabled;
  265. me.spinUpEnabled = enabled;
  266. if (wasEnabled !== enabled &amp;&amp; me.rendered) {
  267. me.spinUpEl[enabled ? 'removeCls' : 'addCls'](me.trigger1Cls + '-disabled');
  268. }
  269. },
  270. <span id='Ext-form-field-Spinner-method-setSpinDownEnabled'> /**
  271. </span> * Sets whether the spinner down button is enabled.
  272. * @param {Boolean} enabled true to enable the button, false to disable it.
  273. */
  274. setSpinDownEnabled: function(enabled) {
  275. var me = this,
  276. wasEnabled = me.spinDownEnabled;
  277. me.spinDownEnabled = enabled;
  278. if (wasEnabled !== enabled &amp;&amp; me.rendered) {
  279. me.spinDownEl[enabled ? 'removeCls' : 'addCls'](me.trigger2Cls + '-disabled');
  280. }
  281. },
  282. <span id='Ext-form-field-Spinner-method-onMouseWheel'> /**
  283. </span> * @private
  284. * Handles mousewheel events on the field
  285. */
  286. onMouseWheel: function(e) {
  287. var me = this,
  288. delta;
  289. if (me.hasFocus) {
  290. delta = e.getWheelDelta();
  291. if (delta &gt; 0) {
  292. me.spinUp();
  293. }
  294. else if (delta &lt; 0) {
  295. me.spinDown();
  296. }
  297. e.stopEvent();
  298. }
  299. },
  300. onDestroy: function() {
  301. Ext.destroyMembers(this, 'spinnerKeyNav', 'spinUpEl', 'spinDownEl');
  302. this.callParent();
  303. }
  304. });</pre>
  305. </body>
  306. </html>