Wizard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.widget.Wizard"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.widget.Wizard"] = true;
  8. dojo.provide("dojox.widget.Wizard");
  9. dojo.require("dijit.layout.StackContainer");
  10. dojo.require("dijit.layout.ContentPane");
  11. dojo.require("dijit.form.Button");
  12. dojo.require("dojo.i18n");
  13. dojo.requireLocalization("dijit", "common", 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");
  14. dojo.requireLocalization("dojox.widget", "Wizard", 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");
  15. dojo.declare("dojox.widget.Wizard", [dijit.layout.StackContainer, dijit._Templated], {
  16. // summary:
  17. // A set of panels that display sequentially, typically notating a step-by-step
  18. // procedure like an install
  19. //
  20. widgetsInTemplate: true,
  21. templateString: dojo.cache("dojox.widget", "Wizard/Wizard.html", "<div class=\"dojoxWizard\" dojoAttachPoint=\"wizardNode\">\n <div class=\"dojoxWizardContainer\" dojoAttachPoint=\"containerNode\"></div>\n <div class=\"dojoxWizardButtons\" dojoAttachPoint=\"wizardNav\">\n <button dojoType=\"dijit.form.Button\" type=\"button\" dojoAttachPoint=\"previousButton\">${previousButtonLabel}</button>\n <button dojoType=\"dijit.form.Button\" type=\"button\" dojoAttachPoint=\"nextButton\">${nextButtonLabel}</button>\n <button dojoType=\"dijit.form.Button\" type=\"button\" dojoAttachPoint=\"doneButton\" style=\"display:none\">${doneButtonLabel}</button>\n <button dojoType=\"dijit.form.Button\" type=\"button\" dojoAttachPoint=\"cancelButton\">${cancelButtonLabel}</button>\n </div>\n</div>\n"),
  22. // nextButtonLabel: String
  23. // Label override for the "Next" button.
  24. nextButtonLabel: "",
  25. // previousButtonLabel: String
  26. // Label override for the "Previous" button.
  27. previousButtonLabel: "",
  28. // cancelButtonLabel: String
  29. // Label override for the "Cancel" button.
  30. cancelButtonLabel: "",
  31. // doneButtonLabel: String
  32. // Label override for the "Done" button.
  33. doneButtonLabel: "",
  34. // cancelFunction: Function|String
  35. // Name of function to call if user presses cancel button.
  36. // Cancel button is not displayed if function is not specified.
  37. cancelFunction: null,
  38. // hideDisabled: Boolean
  39. // If true, disabled buttons are hidden; otherwise, they are assigned the
  40. // "WizardButtonDisabled" CSS class
  41. hideDisabled: false,
  42. postMixInProperties: function(){
  43. this.inherited(arguments);
  44. var labels = dojo.mixin({cancel: dojo.i18n.getLocalization("dijit", "common", this.lang).buttonCancel},
  45. dojo.i18n.getLocalization("dojox.widget", "Wizard", this.lang));
  46. var prop;
  47. for(prop in labels){
  48. if(!this[prop + "ButtonLabel"]){
  49. this[prop + "ButtonLabel"] = labels[prop];
  50. }
  51. }
  52. },
  53. startup: function(){
  54. if(this._started){
  55. //console.log('started');
  56. return;
  57. }
  58. this.inherited(arguments);
  59. this.connect(this.nextButton, "onClick", "_forward");
  60. this.connect(this.previousButton, "onClick", "back");
  61. if(this.cancelFunction){
  62. if(dojo.isString(this.cancelFunction)){
  63. this.cancelFunction = dojo.getObject(this.cancelFunction);
  64. }
  65. this.connect(this.cancelButton, "onClick", this.cancelFunction);
  66. }else{
  67. this.cancelButton.domNode.style.display = "none";
  68. }
  69. this.connect(this.doneButton, "onClick", "done");
  70. this._subscription = dojo.subscribe(this.id + "-selectChild", dojo.hitch(this,"_checkButtons"));
  71. this._started = true;
  72. },
  73. resize: function(){
  74. this.inherited(arguments);
  75. this._checkButtons();
  76. },
  77. _checkButtons: function(){
  78. var sw = this.selectedChildWidget;
  79. var lastStep = sw.isLastChild;
  80. this.nextButton.set("disabled", lastStep);
  81. this._setButtonClass(this.nextButton);
  82. if(sw.doneFunction){
  83. //console.log(sw.doneFunction);
  84. this.doneButton.domNode.style.display = "";
  85. if(lastStep){
  86. this.nextButton.domNode.style.display = "none";
  87. }
  88. }else{
  89. // #1438 issue here.
  90. this.doneButton.domNode.style.display = "none";
  91. }
  92. this.previousButton.set("disabled", !this.selectedChildWidget.canGoBack);
  93. this._setButtonClass(this.previousButton);
  94. },
  95. _setButtonClass: function(button){
  96. button.domNode.style.display = (this.hideDisabled && button.disabled) ? "none" : "";
  97. },
  98. _forward: function(){
  99. // summary: callback when next button is clicked
  100. if(this.selectedChildWidget._checkPass()){
  101. this.forward();
  102. }
  103. },
  104. done: function(){
  105. // summary: Finish the wizard's operation
  106. this.selectedChildWidget.done();
  107. },
  108. destroy: function(){
  109. dojo.unsubscribe(this._subscription);
  110. this.inherited(arguments);
  111. }
  112. });
  113. dojo.declare("dojox.widget.WizardPane", dijit.layout.ContentPane, {
  114. // summary: A panel in a `dojox.widget.Wizard`
  115. //
  116. // description:
  117. // An extended ContentPane with additional hooks for passing named
  118. // functions to prevent the pane from going either forward or
  119. // backwards.
  120. //
  121. // canGoBack: Boolean
  122. // If true, then can move back to a previous panel (by clicking the "Previous" button)
  123. canGoBack: true,
  124. // passFunction: String
  125. // Name of function that checks if it's OK to advance to the next panel.
  126. // If it's not OK (for example, mandatory field hasn't been entered), then
  127. // returns an error message (String) explaining the reason. Can return null (pass)
  128. // or a Boolean (true == pass)
  129. passFunction: null,
  130. // doneFunction: String
  131. // Name of function that is run if you press the "Done" button from this panel
  132. doneFunction: null,
  133. startup: function(){
  134. this.inherited(arguments);
  135. if(this.isFirstChild){ this.canGoBack = false; }
  136. if(dojo.isString(this.passFunction)){
  137. this.passFunction = dojo.getObject(this.passFunction);
  138. }
  139. if(dojo.isString(this.doneFunction) && this.doneFunction){
  140. this.doneFunction = dojo.getObject(this.doneFunction);
  141. }
  142. },
  143. _onShow: function(){
  144. if(this.isFirstChild){ this.canGoBack = false; }
  145. this.inherited(arguments);
  146. },
  147. _checkPass: function(){
  148. // summary:
  149. // Called when the user presses the "next" button.
  150. // Calls passFunction to see if it's OK to advance to next panel, and
  151. // if it isn't, then display error.
  152. // Returns true to advance, false to not advance. If passFunction
  153. // returns a string, it is assumed to be a custom error message, and
  154. // is alert()'ed
  155. var r = true;
  156. if(this.passFunction && dojo.isFunction(this.passFunction)){
  157. var failMessage = this.passFunction();
  158. switch(typeof failMessage){
  159. case "boolean":
  160. r = failMessage;
  161. break;
  162. case "string":
  163. alert(failMessage);
  164. r = false;
  165. break;
  166. }
  167. }
  168. return r; // Boolean
  169. },
  170. done: function(){
  171. if(this.doneFunction && dojo.isFunction(this.doneFunction)){ this.doneFunction(); }
  172. }
  173. });
  174. }