歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用JavaScript實現的簡單Wizard

用JavaScript實現的簡單Wizard

日期:2017/3/1 10:25:47   编辑:Linux編程

今天分享一個自己幾年前用JavaScript寫的Wizard,代碼不多,另外我也沒有添加任何樣式,因為畢竟如果有人要用一般還都是會設計自己的樣式的。

下面是Wizard的JavaScript代碼,代碼比較簡單,就不多做解釋了,wizard.js代碼內容如下:

  1. xx = {};
  2. xx.wizard = {};
  3. /**
  4. * The Wizard Object constructor.
  5. * @param (Object) oConfig the object to initialize the Wizard Object. It
  6. * must include the following attributes:
  7. * (String)wizardNavigation The id of html element that used to contain navitaion links.
  8. * (String)wizardContent The id of html element that used to contain all wizard step elements.
  9. * (String)wizardController The id of html element that used to contain all controller button.
  10. * @return (Object) an instance of Wizard.
  11. */
  12. xx.wizard.Wizard = function(oConfig) {
  13. // Private attributes
  14. /** The wizard navigation element. */
  15. var eleWizardNavigation = document.getElementById(oConfig.wizardNavigation);
  16. /** The wizard steps content element. */
  17. var eleWizardContent = document.getElementById(oConfig.wizardContent);
  18. /** The wizard controller element. */
  19. var eleWizardController = document.getElementById(oConfig.wizardController);
  20. /** All WizardSteps list. */
  21. var steps = new Array();
  22. /** Current selected WizardStep. */
  23. var curStep;
  24. /** An instance of WizardNavigation class is used to switch between steps. */
  25. var wizardNavigation;
  26. /** An instance of WizardController class is used to contains controller buttons. */
  27. var wizardController;
  28. /** The reference to current Wizard object. */
  29. var thiz = this;
  30. // Public methods
  31. /**
  32. * Add a WizardStep to steps list.
  33. * @param (Object)step An instance of WizardStep.
  34. * @return (void)
  35. */
  36. this.addStep = function(step) {
  37. steps.push(step)
  38. }
  39. /**
  40. * Search an instance of WizardStep with given id of WizardStep. If cannot
  41. * find return null.
  42. * @param (String)stepId The id of WizardStep instance.
  43. * @return (Object) An instance of WizardStep.
  44. */
  45. this.findStep = function(stepId) {
  46. for (var idx = 0; idx < steps.length; idx++) {
  47. if (stepId == steps[idx].getId()) {
  48. return steps[idx];
  49. }
  50. }
  51. return null;
  52. }
  53. /**
  54. * Get the index location of the WizardStep in step list with given id of
  55. * WizardStep instance. If cannot find it, return -1.
  56. * @param (String)stepId The id of WizardStep instance.
  57. * @return (int) The index location.
  58. */
  59. this.getStepIndex = function(stepId) {
  60. for (var idx = 0; idx < steps.length; idx++){
  61. var step = steps[idx];
  62. if( step.getId() == stepId ) {
  63. return idx;
  64. }
  65. }
  66. return -1;
  67. }
  68. /**
  69. * Remove all WizardStep instancs from step list.
  70. * @return (void)
  71. */
  72. this.removeAllSteps = function() {
  73. for (var idx = 0; idx < steps.length; idx++){
  74. var step = steps[idx];
  75. step = null;
  76. }
  77. steps = new Array();
  78. }
  79. /**
  80. * Move to previous WizardStep.
  81. * @return (void)
  82. */
  83. this.moveToPrevious = function() {
  84. var idx = thiz.getStepIndex(curStep.getId());
  85. if( idx > 0 ) {
  86. var preStep = steps[idx - 1];
  87. thiz.moveTo(preStep.getId());
  88. }
  89. }
  90. /**
  91. * Move to next WizardStep.
  92. * @return (void)
  93. */
  94. this.moveToNext = function() {
  95. var idx = thiz.getStepIndex(curStep.getId());
  96. if( idx < steps.length - 1 ) {
  97. var nextStep = steps[idx + 1];
  98. thiz.moveTo(nextStep.getId());
  99. }
  100. }
  101. /**
  102. * Move to the WizardStep that has given id of WizardStep.
  103. * @param (String)stepId The id of WizardStep instance.
  104. * @return (void)
  105. */
  106. this.moveTo = function(stepId) {
  107. var step = thiz.findStep(stepId);
  108. wizardNavigation.setSelected(step);
  109. wizardNavigation.refresh();
  110. wizardController.setSelected(step);
  111. wizardController.refresh();
  112. for (var i = 0; i < steps.length; i++){
  113. if( steps[i].getId() == stepId ) {
  114. steps[i].setVisible(true);
  115. curStep = steps[i];
  116. } else {
  117. steps[i].setVisible(false);
  118. }
  119. }
  120. }
  121. /**
  122. * Render the wizard object to page.
  123. */
  124. this.render = function() {
  125. curStep = steps[0];
  126. steps[0].setVisible(true);
  127. wizardNavigation = new xx.wizard.WizardNavigation({wizard: thiz, steps: steps});
  128. wizardNavigation.render(eleWizardNavigation);
  129. wizardController = new xx.wizard.WizardController({wizard: thiz, steps: steps});
  130. wizardController.render(eleWizardController);
  131. }
  132. /**
  133. * A util method to generate a controller button.
  134. * @param (String)id The id of button.
  135. * @param (String)name The name of button.
  136. * @param (String)value The value of button.
  137. * @param (Function)clickHandler The callback function for click this buttion.
  138. * @return (Element) An html element.
  139. */
  140. this.generateButton = function(id, name, value, clickHandler) {
  141. var eleBtn = document.createElement("input");
  142. eleBtn.type = 'button';
  143. eleBtn.id = id;
  144. eleBtn.name = name;
  145. eleBtn.value = value;
  146. eleBtn.onclick = clickHandler;
  147. return eleBtn;
  148. }
  149. }
  150. xx.wizard.WizardNavigation = function(oConfig) {
  151. var wizard = oConfig.wizard;
  152. var steps = oConfig.steps;
  153. var selectedStep;
  154. var eleParent;
  155. this.render = function(ele) {
  156. if (eleParent == null) {
  157. eleParent = ele;
  158. }
  159. var eleUL = document.createElement("ul");
  160. if (selectedStep == null) {
  161. selectedStep = steps[0];
  162. }
  163. var selectedStepIdx = 0;
  164. for (var idx = 0; idx < steps.length; idx++){
  165. if (steps[idx].getId() == selectedStep.getId()) {
  166. selectedStepIdx = idx;
  167. break;
  168. }
  169. }
  170. for (var idx = 0; idx < steps.length; idx++){
  171. var eleLI = document.createElement("li");
  172. var className = ''
  173. if (steps[idx].getId() == selectedStep.getId()) {
  174. className += ' selected';
  175. }
  176. eleLI.className = className;
  177. var eleSpan = document.createElement("span");
  178. if (idx < selectedStepIdx) {
  179. var eleLink = document.createElement("a");
  180. eleLink.href = '#';
  181. var fnCallback = function(wizard, step) {
  182. return function() {
  183. var navigationCallback = step.getNavigationCallback();
  184. if (navigationCallback != null) {
  185. navigationCallback();
  186. } else {
  187. wizard.moveTo(step.getId());
  188. }
  189. };
  190. }(wizard, steps[idx]);
  191. eleLink.onclick = fnCallback;
  192. eleLink.innerHTML = steps[idx].getTitle();
  193. eleSpan.appendChild(eleLink);
  194. } else {
  195. eleSpan.innerHTML = steps[idx].getTitle();
  196. }
  197. eleLI.appendChild(eleSpan);
  198. eleUL.appendChild(eleLI);
  199. }
  200. ele.appendChild(eleUL);
  201. }
  202. this.refresh = function() {
  203. var childNodes = eleParent.childNodes;
  204. for(var idx = childNodes.length - 1 ; idx >= 0 ; idx-- ) {
  205. eleParent.removeChild(childNodes[idx]);
  206. }
  207. this.render(eleParent);
  208. }
  209. this.setSelected = function(oWizardStep) {
  210. selectedStep = oWizardStep;
  211. }
  212. }
  213. xx.wizard.WizardController = function(oConfig) {
  214. var wizard = oConfig.wizard;
  215. var steps = oConfig.steps;
  216. var selectedStep;
  217. var eleParent;
  218. this.render = function(parent) {
  219. eleParent = parent;
  220. var controlButtons = steps[0].getControlButtons();
  221. if (controlButtons != null) {
  222. for(var idx = 0; idx < controlButtons.length; idx ++) {
  223. eleParent.appendChild(controlButtons[idx]);
  224. }
  225. }
  226. }
  227. this.refresh = function() {
  228. var childNodes = eleParent.childNodes;
  229. for(var idx = childNodes.length - 1 ; idx >= 0 ; idx-- ) {
  230. eleParent.removeChild(childNodes[idx]);
  231. }
  232. var controlButtons = selectedStep.getControlButtons();
  233. if (controlButtons != null) {
  234. for(var idx = 0; idx < controlButtons.length; idx ++) {
  235. eleParent.appendChild(controlButtons[idx]);
  236. }
  237. }
  238. }
  239. this.setSelected = function(oWizardStep) {
  240. selectedStep = oWizardStep;
  241. }
  242. }
  243. /**
  244. * The constructor of WizardStep class.
  245. * @param (Object)oConfig the object to initialize the WizardStep Object. It
  246. * must include the following attributes:
  247. * (String)id The identity id of WizardStep object
  248. * (String)name The id of WizardStep object.
  249. * (String)title The title of WizardStep object that is used to display in navigation area.
  250. * (Array)controlButtons The control buttons of WizardStep that are used to display in controller area.
  251. * (Function)navigationCallback The navigation callback function that will be used on click the step title in navigation area.
  252. * @return (Object) an instance of WizardStep.
  253. */
  254. xx.wizard.WizardStep = function(oConfig) {
  255. var id = oConfig.id;
  256. var name = oConfig.name;
  257. var title = oConfig.title;
  258. var controlButtons = oConfig.controlButtons;
  259. var navigationCallback = oConfig.navigationCallback;
  260. this.getId = function() {
  261. return id;
  262. }
  263. this.getName = function() {
  264. return name;
  265. }
  266. this.getTitle = function() {
  267. return title;
  268. }
  269. this.isVisible = function() {
  270. return document.getElementById(id).style.display;
  271. }
  272. this.setVisible = function(visible) {
  273. document.getElementById(id).style.display = (visible)? 'block':'none';
  274. }
  275. this.getControlButtons = function() {
  276. return controlButtons;
  277. }
  278. this.getNavigationCallback = function() {
  279. return navigationCallback;
  280. }
  281. }
Copyright © Linux教程網 All Rights Reserved