BusyButton.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.form.BusyButton"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.form.BusyButton"] = true;
  8. dojo.provide("dojox.form.BusyButton");
  9. dojo.require("dijit.form.Button");
  10. dojo.requireLocalization("dijit", "loading", null, "ROOT,ar,az,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  11. dojo.declare("dojox.form._BusyButtonMixin",
  12. null,
  13. {
  14. isBusy: false,
  15. busyLabel: "", // text while button is busy
  16. timeout: null, // timeout, should be controlled by xhr call
  17. useIcon: true, // use a busy icon
  18. postMixInProperties: function(){
  19. this.inherited(arguments);
  20. if(!this.busyLabel){
  21. this.busyLabel = dojo.i18n.getLocalization("dijit", "loading", this.lang).loadingState;
  22. }
  23. },
  24. postCreate: function(){
  25. // summary:
  26. // stores initial label and timeout for reference
  27. this.inherited(arguments);
  28. this._label = this.containerNode.innerHTML;
  29. this._initTimeout = this.timeout;
  30. // for initial busy buttons
  31. if(this.isBusy){
  32. this.makeBusy();
  33. }
  34. },
  35. makeBusy: function(){
  36. // summary:
  37. // sets state from idle to busy
  38. this.isBusy = true;
  39. this.set("disabled", true);
  40. this.setLabel(this.busyLabel, this.timeout);
  41. },
  42. cancel: function(){
  43. // summary:
  44. // if no timeout is set or for other reason the user can put the button back
  45. // to being idle
  46. this.set("disabled", false);
  47. this.isBusy = false;
  48. this.setLabel(this._label);
  49. if(this._timeout){ clearTimeout(this._timeout); }
  50. this.timeout = this._initTimeout;
  51. },
  52. resetTimeout: function(/*Int*/ timeout){
  53. // summary:
  54. // to reset existing timeout and setting a new timeout
  55. if(this._timeout){
  56. clearTimeout(this._timeout);
  57. }
  58. // new timeout
  59. if(timeout){
  60. this._timeout = setTimeout(dojo.hitch(this, function(){
  61. this.cancel();
  62. }), timeout);
  63. }else if(timeout == undefined || timeout === 0){
  64. this.cancel();
  65. }
  66. },
  67. setLabel: function(/*String*/ content, /*Int*/ timeout){
  68. // summary:
  69. // setting a label and optional timeout of the labels state
  70. // this.inherited(arguments); FIXME: throws an Unknown runtime error
  71. // Begin IE hack
  72. // summary: reset the label (text) of the button; takes an HTML string
  73. this.label = content;
  74. // remove children
  75. while (this.containerNode.firstChild){
  76. this.containerNode.removeChild(this.containerNode.firstChild);
  77. }
  78. this.containerNode.innerHTML = this.label;
  79. if(this.showLabel == false && !(dojo.attr(this.domNode, "title"))){
  80. this.titleNode.title=dojo.trim(this.containerNode.innerText || this.containerNode.textContent || '');
  81. }
  82. // End IE hack
  83. // setting timeout
  84. if(timeout){
  85. this.resetTimeout(timeout);
  86. }else{
  87. this.timeout = null;
  88. }
  89. // create optional busy image
  90. if(this.useIcon && this.isBusy){
  91. var node = new Image();
  92. node.src = this._blankGif;
  93. dojo.attr(node, "id", this.id+"_icon");
  94. dojo.addClass(node, "dojoxBusyButtonIcon");
  95. this.containerNode.appendChild(node);
  96. }
  97. },
  98. _clicked: function(e){
  99. // summary:
  100. // on button click the button state gets changed
  101. // only do something if button is not busy
  102. if(!this.isBusy){
  103. this.makeBusy();
  104. }
  105. }
  106. });
  107. dojo.declare("dojox.form.BusyButton", [dijit.form.Button, dojox.form._BusyButtonMixin], {});
  108. dojo.declare("dojox.form.BusyComboButton", [dijit.form.ComboButton, dojox.form._BusyButtonMixin], {});
  109. dojo.declare("dojox.form.BusyDropDownButton", [dijit.form.DropDownButton, dojox.form._BusyButtonMixin], {});
  110. }