PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/web-app/qooxdoo/framework/source/class/qx/ui/form/RepeatButton.js

https://github.com/flomotlik/grails-qooxdoo
JavaScript | 460 lines | 187 code | 68 blank | 205 comment | 18 complexity | a10200409c952d82a76b19d68760c2b2 MD5 | raw file
Possible License(s): Unlicense, CC-BY-SA-3.0
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Sebastian Werner (wpbasti)
  12. * Andreas Ecker (ecker)
  13. * Martin Wittemann (martinwittemann)
  14. * Fabian Jakobs (fjakobs)
  15. ************************************************************************ */
  16. /**
  17. * The RepeatButton is a special button, which fires repeatedly {@link #execute}
  18. * events, while the mouse button is pressed on the button. The initial delay
  19. * and the interval time can be set using the properties {@link #firstInterval}
  20. * and {@link #interval}. The {@link #execute} events will be fired in a shorter
  21. * amount of time if the mouse button is hold, until the min {@link #minTimer}
  22. * is reached. The {@link #timerDecrease} property sets the amount of milliseconds
  23. * which will decreased after every firing.
  24. *
  25. * <pre class='javascript'>
  26. * var button = new qx.ui.form.RepeatButton("Hello World");
  27. *
  28. * button.addListener("execute", function(e) {
  29. * alert("Button is executed");
  30. * }, this);
  31. *
  32. * this.getRoot.add(button);
  33. * </pre>
  34. *
  35. * This example creates a button with the label "Hello World" and attaches an
  36. * event listener to the {@link #execute} event.
  37. *
  38. * *External Documentation*
  39. *
  40. * <a href='http://qooxdoo.org/documentation/1.0/widget/repeatbutton' target='_blank'>
  41. * Documentation of this widget in the qooxdoo wiki.</a>
  42. */
  43. qx.Class.define("qx.ui.form.RepeatButton",
  44. {
  45. extend : qx.ui.form.Button,
  46. /**
  47. * @param label {String} Label to use
  48. * @param icon {String?null} Icon to use
  49. */
  50. construct : function(label, icon)
  51. {
  52. this.base(arguments, label, icon);
  53. // create the timer and add the listener
  54. this.__timer = new qx.event.AcceleratingTimer();
  55. this.__timer.addListener("interval", this._onInterval, this);
  56. },
  57. events :
  58. {
  59. /**
  60. * This event gets dispatched with every interval. The timer gets executed
  61. * as long as the user holds down the mouse button.
  62. */
  63. "execute" : "qx.event.type.Event",
  64. /**
  65. * This event gets dispatched when the button is pressed.
  66. */
  67. "press" : "qx.event.type.Event",
  68. /**
  69. * This event gets dispatched when the button is released.
  70. */
  71. "release" : "qx.event.type.Event"
  72. },
  73. properties :
  74. {
  75. /**
  76. * Interval used after the first run of the timer. Usually a smaller value
  77. * than the "firstInterval" property value to get a faster reaction.
  78. */
  79. interval :
  80. {
  81. check : "Integer",
  82. init : 100
  83. },
  84. /**
  85. * Interval used for the first run of the timer. Usually a greater value
  86. * than the "interval" property value to a little delayed reaction at the first
  87. * time.
  88. */
  89. firstInterval :
  90. {
  91. check : "Integer",
  92. init : 500
  93. },
  94. /** This configures the minimum value for the timer interval. */
  95. minTimer :
  96. {
  97. check : "Integer",
  98. init : 20
  99. },
  100. /** Decrease of the timer on each interval (for the next interval) until minTimer reached. */
  101. timerDecrease :
  102. {
  103. check : "Integer",
  104. init : 2
  105. }
  106. },
  107. members :
  108. {
  109. __executed : null,
  110. __timer : null,
  111. /**
  112. * Calling this function is like a click from the user on the
  113. * button with all consequences.
  114. * <span style='color: red'>Be sure to call the {@link #release} function.</span>
  115. *
  116. * @return {void}
  117. */
  118. press : function()
  119. {
  120. // only if the button is enabled
  121. if (this.isEnabled())
  122. {
  123. // if the state pressed must be applied (first call)
  124. if (!this.hasState("pressed"))
  125. {
  126. // start the timer
  127. this.__startInternalTimer();
  128. }
  129. // set the states
  130. this.removeState("abandoned");
  131. this.addState("pressed");
  132. }
  133. },
  134. /**
  135. * Calling this function is like a release from the user on the
  136. * button with all consequences.
  137. * Usually the {@link #release} function will be called before the call of
  138. * this function.
  139. *
  140. * @param fireExecuteEvent {Boolean?true} flag which signals, if a event should be fired
  141. * @return {void}
  142. */
  143. release : function(fireExecuteEvent)
  144. {
  145. // only if the button is enabled
  146. if (!this.isEnabled()) {
  147. return;
  148. }
  149. // only if the button is pressed
  150. if (this.hasState("pressed"))
  151. {
  152. // if the button has not been executed
  153. if (!this.__executed) {
  154. this.execute();
  155. }
  156. }
  157. // remove button states
  158. this.removeState("pressed");
  159. this.removeState("abandoned");
  160. // stop the repeat timer and therefore the execution
  161. this.__stopInternalTimer();
  162. },
  163. /*
  164. ---------------------------------------------------------------------------
  165. PROPERTY APPLY ROUTINES
  166. ---------------------------------------------------------------------------
  167. */
  168. // overridden
  169. _applyEnabled : function(value, old)
  170. {
  171. this.base(arguments, value, old);
  172. if (!value)
  173. {
  174. // remove button states
  175. this.removeState("pressed");
  176. this.removeState("abandoned");
  177. // stop the repeat timer and therefore the execution
  178. this.__stopInternalTimer();
  179. }
  180. },
  181. /*
  182. ---------------------------------------------------------------------------
  183. EVENT HANDLER
  184. ---------------------------------------------------------------------------
  185. */
  186. /**
  187. * Listener method for "mouseover" event
  188. * <ul>
  189. * <li>Adds state "hovered"</li>
  190. * <li>Removes "abandoned" and adds "pressed" state (if "abandoned" state is set)</li>
  191. * </ul>
  192. *
  193. * @param e {Event} Mouse event
  194. * @return {void}
  195. */
  196. _onMouseOver : function(e)
  197. {
  198. if (!this.isEnabled() || e.getTarget() !== this) {
  199. return;
  200. }
  201. if (this.hasState("abandoned"))
  202. {
  203. this.removeState("abandoned");
  204. this.addState("pressed");
  205. this.__timer.start();
  206. }
  207. this.addState("hovered");
  208. },
  209. /**
  210. * Listener method for "mouseout" event
  211. * <ul>
  212. * <li>Removes "hovered" state</li>
  213. * <li>Adds "abandoned" and removes "pressed" state (if "pressed" state is set)</li>
  214. * </ul>
  215. *
  216. * @param e {Event} Mouse event
  217. * @return {void}
  218. */
  219. _onMouseOut : function(e)
  220. {
  221. if (!this.isEnabled() || e.getTarget() !== this) {
  222. return;
  223. }
  224. this.removeState("hovered");
  225. if (this.hasState("pressed"))
  226. {
  227. this.removeState("pressed");
  228. this.addState("abandoned");
  229. this.__timer.stop();
  230. }
  231. },
  232. /**
  233. * Callback method for the "mouseDown" method.
  234. *
  235. * Sets the interval of the timer (value of firstInterval property) and
  236. * starts the timer. Additionally removes the state "abandoned" and adds the
  237. * state "pressed".
  238. *
  239. * @param e {qx.event.type.Mouse} mouseDown event
  240. * @return {void}
  241. */
  242. _onMouseDown : function(e)
  243. {
  244. if (!e.isLeftPressed()) {
  245. return;
  246. }
  247. // Activate capturing if the button get a mouseout while
  248. // the button is pressed.
  249. this.capture();
  250. this.__startInternalTimer();
  251. e.stopPropagation();
  252. },
  253. /**
  254. * Callback method for the "mouseUp" event.
  255. *
  256. * Handles the case that the user is releasing the mouse button
  257. * before the timer interval method got executed. This way the
  258. * "execute" method get executed at least one time.
  259. *
  260. * @param e {qx.event.type.Mouse} mouseUp event
  261. * @return {void}
  262. */
  263. _onMouseUp : function(e)
  264. {
  265. this.releaseCapture();
  266. if (!this.hasState("abandoned"))
  267. {
  268. this.addState("hovered");
  269. if (this.hasState("pressed") && !this.__executed) {
  270. this.execute();
  271. }
  272. }
  273. this.__stopInternalTimer();
  274. e.stopPropagation();
  275. },
  276. /**
  277. * Listener method for "keyup" event.
  278. *
  279. * Removes "abandoned" and "pressed" state (if "pressed" state is set)
  280. * for the keys "Enter" or "Space" and stopps the internal timer
  281. * (same like mouse up).
  282. *
  283. * @param e {Event} Key event
  284. * @return {void}
  285. */
  286. _onKeyUp : function(e)
  287. {
  288. switch(e.getKeyIdentifier())
  289. {
  290. case "Enter":
  291. case "Space":
  292. if (this.hasState("pressed"))
  293. {
  294. if (!this.__executed) {
  295. this.execute();
  296. }
  297. this.removeState("pressed");
  298. this.removeState("abandoned");
  299. e.stopPropagation();
  300. this.__stopInternalTimer();
  301. }
  302. }
  303. },
  304. /**
  305. * Listener method for "keydown" event.
  306. *
  307. * Removes "abandoned" and adds "pressed" state
  308. * for the keys "Enter" or "Space". It also starts
  309. * the internal timer (same like mousedown).
  310. *
  311. * @param e {Event} Key event
  312. * @return {void}
  313. */
  314. _onKeyDown : function(e)
  315. {
  316. switch(e.getKeyIdentifier())
  317. {
  318. case "Enter":
  319. case "Space":
  320. this.removeState("abandoned");
  321. this.addState("pressed");
  322. e.stopPropagation();
  323. this.__startInternalTimer();
  324. }
  325. },
  326. /**
  327. * Callback for the interval event.
  328. *
  329. * Stops the timer and starts it with a new interval
  330. * (value of the "interval" property - value of the "timerDecrease" property).
  331. * Dispatches the "execute" event.
  332. *
  333. * @param e {qx.event.type.Event} interval event
  334. * @return {void}
  335. */
  336. _onInterval : function(e)
  337. {
  338. this.__executed = true;
  339. this.fireEvent("execute");
  340. },
  341. /*
  342. ---------------------------------------------------------------------------
  343. INTERNAL TIMER
  344. ---------------------------------------------------------------------------
  345. */
  346. /**
  347. * Starts the internal timer which causes firing of execution
  348. * events in an interval. It also presses the button.
  349. *
  350. * @return {void}
  351. */
  352. __startInternalTimer : function()
  353. {
  354. this.fireEvent("press");
  355. this.__executed = false;
  356. this.__timer.set({
  357. interval: this.getInterval(),
  358. firstInterval: this.getFirstInterval(),
  359. minimum: this.getMinTimer(),
  360. decrease: this.getTimerDecrease()
  361. }).start();
  362. this.removeState("abandoned");
  363. this.addState("pressed");
  364. },
  365. /**
  366. * Stops the internal timer and releases the button.
  367. *
  368. * @return {void}
  369. */
  370. __stopInternalTimer : function()
  371. {
  372. this.fireEvent("release");
  373. this.__timer.stop();
  374. this.removeState("abandoned");
  375. this.removeState("pressed");
  376. }
  377. },
  378. /*
  379. *****************************************************************************
  380. DESTRUCTOR
  381. *****************************************************************************
  382. */
  383. destruct : function() {
  384. this._disposeObjects("__timer");
  385. }
  386. });