BusyButton.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. define("dojox/form/BusyButton", [
  2. "dojo/_base/lang",
  3. "dojo/dom-attr",
  4. "dojo/dom-class",
  5. "dijit/form/Button",
  6. "dijit/form/DropDownButton",
  7. "dijit/form/ComboButton",
  8. "dojo/i18n",
  9. "dojo/i18n!dijit/nls/loading",
  10. "dojo/_base/declare"
  11. ], function(lang, domAttr, domClass, Button, DropDownButton, ComboButton, i18n, nlsLoading, declare){
  12. /*=====
  13. Button = dijit.form.Button;
  14. DropDownButton = dijit.form.DropDownButton;
  15. ComboButton = dijit.form.ComboButton;
  16. =====*/
  17. var _BusyButtonMixin = declare("dojox.form._BusyButtonMixin", null, {
  18. isBusy: false,
  19. busyLabel: "", // text while button is busy
  20. timeout: null, // timeout, should be controlled by xhr call
  21. useIcon: true, // use a busy icon
  22. postMixInProperties: function(){
  23. this.inherited(arguments);
  24. if(!this.busyLabel){
  25. this.busyLabel = i18n.getLocalization("dijit", "loading", this.lang).loadingState;
  26. }
  27. },
  28. postCreate: function(){
  29. // summary:
  30. // stores initial label and timeout for reference
  31. this.inherited(arguments);
  32. this._label = this.containerNode.innerHTML;
  33. this._initTimeout = this.timeout;
  34. // for initial busy buttons
  35. if(this.isBusy){
  36. this.makeBusy();
  37. }
  38. },
  39. makeBusy: function(){
  40. // summary:
  41. // sets state from idle to busy
  42. this.isBusy = true;
  43. this.set("disabled", true);
  44. this.setLabel(this.busyLabel, this.timeout);
  45. },
  46. cancel: function(){
  47. // summary:
  48. // if no timeout is set or for other reason the user can put the button back
  49. // to being idle
  50. this.set("disabled", false);
  51. this.isBusy = false;
  52. this.setLabel(this._label);
  53. if(this._timeout){ clearTimeout(this._timeout); }
  54. this.timeout = this._initTimeout;
  55. },
  56. resetTimeout: function(/*Int*/ timeout){
  57. // summary:
  58. // to reset existing timeout and setting a new timeout
  59. if(this._timeout){
  60. clearTimeout(this._timeout);
  61. }
  62. // new timeout
  63. if(timeout){
  64. this._timeout = setTimeout(lang.hitch(this, function(){
  65. this.cancel();
  66. }), timeout);
  67. }else if(timeout == undefined || timeout === 0){
  68. this.cancel();
  69. }
  70. },
  71. setLabel: function(/*String*/ content, /*Int*/ timeout){
  72. // summary:
  73. // setting a label and optional timeout of the labels state
  74. // this.inherited(arguments); FIXME: throws an Unknown runtime error
  75. // Begin IE hack
  76. // summary: reset the label (text) of the button; takes an HTML string
  77. this.label = content;
  78. // remove children
  79. while(this.containerNode.firstChild){
  80. this.containerNode.removeChild(this.containerNode.firstChild);
  81. }
  82. this.containerNode.appendChild(document.createTextNode(this.label));
  83. if(this.showLabel == false && !domAttr.get(this.domNode, "title")){
  84. this.titleNode.title=lang.trim(this.containerNode.innerText || this.containerNode.textContent || '');
  85. }
  86. // End IE hack
  87. // setting timeout
  88. if(timeout){
  89. this.resetTimeout(timeout);
  90. }else{
  91. this.timeout = null;
  92. }
  93. // create optional busy image
  94. if(this.useIcon && this.isBusy){
  95. var node = new Image();
  96. node.src = this._blankGif;
  97. domAttr.set(node, "id", this.id+"_icon");
  98. domClass.add(node, "dojoxBusyButtonIcon");
  99. this.containerNode.appendChild(node);
  100. }
  101. },
  102. _onClick: function(e){
  103. // summary:
  104. // on button click the button state gets changed
  105. // only do something if button is not busy
  106. if(!this.isBusy){
  107. this.inherited(arguments); // calls onClick()
  108. this.makeBusy();
  109. }
  110. }
  111. });
  112. var BusyButton = declare("dojox.form.BusyButton", [Button, _BusyButtonMixin], {});
  113. declare("dojox.form.BusyComboButton", [ComboButton, _BusyButtonMixin], {});
  114. declare("dojox.form.BusyDropDownButton", [DropDownButton, _BusyButtonMixin], {});
  115. return BusyButton;
  116. });