1
/* Copyright (c) 2010, Canonical Ltd. All rights reserved.
3
* Display a functioning form in a lazr.formoverlay.
7
YUI.add('lazr.wizard', function(Y) {
9
var namespace = Y.namespace("lazr.wizard");
12
* The Step class isn't so much a Widget as it is just an Object with
13
* references to perform certain functions. They are used to tell the
14
* Wizard what to do next.
17
* @namespace lazr.wizard
19
function Step(config) {
20
Step.superclass.constructor.apply(this, arguments);
38
Y.extend(Step, Y.Widget, {
40
this.get("funcLoad").apply(this, arguments);
43
namespace.Step = Step;
47
* The Wizard class builds on the lazr.FormOverlay class
48
* to display form content and extract form data for the callsite.
51
* @namespace lazr.wizard
53
function Wizard(config) {
54
Wizard.superclass.constructor.apply(this, arguments);
56
if (this.get("steps").length == 0) {
57
throw "Cannot create a Wizard with no steps.";
59
Y.after(this._renderUIWizard, this, "renderUI");
60
Y.after(this._bindUIWizard, this, "bindUI");
63
Wizard.NAME = "lazr-wizard";
68
previous_step_index: {
79
Y.extend(Wizard, Y.lazr.FormOverlay, {
81
initializer: function() {
85
_renderUIWizard: function() {
89
_bindUIWizard: function() {
91
Y.bind(function(e){ this.hide();}, this),
92
this.get("form_cancel_button"));
96
* Add a step to the end of the steps array.
100
addStep: function(step) {
101
this.get("steps").push(step);
102
// If the widget is currently on its final step, update the
103
// next_step_index to reflect the fact that we've just added
105
if (!this.hasNextStep()) {
106
var current_step_index = this.get("current_step_index");
107
this.set("next_step_index", current_step_index + 1);
112
* Transition to the step at a given index.
114
* @method _transitionToStep
116
* @param step_index The index of the step to transition to.
118
_transitionToStep: function(step_index) {
119
var step = this.get("steps")[step_index];
120
this.set("steptitle", step.get("title"));
122
var step_form_content = step.get("form_content");
123
if (Y.Lang.isValue(step_form_content)) {
124
this.set("form_content", step_form_content);
125
this._setFormContent()
129
this.fire("wizard:stepChange");
130
this.set("current_step", step);
131
this._updateStepIndices(step_index);
135
* Transition to the next step in the steps array.
140
var step_index = this.get("next_step_index");
141
if (step_index < 0) {
142
throw "Wizard is already on its last step.";
144
this._transitionToStep(step_index);
148
* Transition to the previous step in the steps array.
152
previous: function() {
153
var step_index = this.get("previous_step_index");
154
if (step_index < 0) {
155
throw "Wizard is already on its first step.";
157
this._transitionToStep(step_index);
161
* Update the step indices based on the current step index.
163
* @method _updateStepIndices
165
* @param current_step_index The index of the current step.
167
_updateStepIndices: function(current_step_index) {
168
if (current_step_index > 0) {
169
this.set("previous_step_index", current_step_index - 1);
171
this.set("previous_step_index", -1);
174
if (this.get("steps").length > current_step_index + 1) {
175
this.set("next_step_index", current_step_index + 1);
177
this.set("next_step_index", -1);
180
this.set("current_step_index", current_step_index + 1);
184
* Return true if there's another step after the current one.
186
* @method hasNextStep
188
hasNextStep: function() {
189
var next_step_index = this.get("next_step_index");
190
return (next_step_index > 0)
194
* Return true if there's a step before the current one.
196
* @method hasPreviousStep
198
hasPreviousStep: function() {
199
var previous_step_index = this.get("previous_step_index");
200
return (previous_step_index > -1)
204
* Destroy all the Steps of the widget.
208
destructor: function() {
209
// Loop over all the steps and delete them.
210
while(this.get("steps").length > 0) {
211
var step = this.get("steps").pop();
218
namespace.Wizard = Wizard;
220
}, "0.1", {"skinnable": true, "requires": ["lazr.formoverlay"]});