jam-cloud/web/app/assets/javascripts/wizard/wizard.js

256 lines
6.0 KiB
JavaScript
Raw Permalink Normal View History

2014-05-22 16:26:56 +00:00
(function (context, $) {
"use strict";
context.JK = context.JK || {};
context.JK.Wizard = function (app) {
2014-05-23 03:05:05 +00:00
var STEPS = null;
var step = null;
var previousStep = null;
2014-05-22 16:26:56 +00:00
var $dialog = null;
var $templateButtons = null;
var $wizardSteps = null;
2014-05-23 03:05:05 +00:00
var $currentWizardStep = null;
var $wizardButtons = null;
2014-06-03 21:19:39 +00:00
var options = {};
2014-05-27 15:23:16 +00:00
var $btnHelp = null;
2014-05-22 16:26:56 +00:00
var $btnNext = null;
var $btnBack = null;
var $btnClose = null;
var $btnCancel = null;
2014-06-03 21:19:39 +00:00
var dialogName = null;
2014-05-23 03:05:05 +00:00
var self = this;
var $self = $(this);
2014-05-22 16:26:56 +00:00
2014-05-23 03:05:05 +00:00
function totalSteps() {
2014-05-27 15:23:16 +00:00
return context.JK.dkeys(STEPS).length;
2014-05-23 03:05:05 +00:00
}
2014-05-22 16:26:56 +00:00
function beforeHideStep() {
2014-05-29 19:14:46 +00:00
var currentStep = getCurrentStep();
if(currentStep === null) return;
2014-05-22 16:26:56 +00:00
2014-05-29 19:14:46 +00:00
var stepInfo = STEPS[currentStep];
2014-05-22 16:26:56 +00:00
if (!stepInfo) {
2014-05-29 19:14:46 +00:00
throw "unknown step: " + currentStep;
2014-05-22 16:26:56 +00:00
}
if(stepInfo.beforeHide) {
stepInfo.beforeHide.call(stepInfo);
}
}
function beforeShowStep($step) {
var stepInfo = STEPS[step];
if (!stepInfo) {
throw "unknown step: " + step;
}
stepInfo.beforeShow.call(stepInfo);
}
2014-05-23 03:05:05 +00:00
function back() {
if ($(this).is('.disabled')) return false;
2014-05-23 03:05:05 +00:00
previousStep = step;
step = step - 1;
moveToStep();
return false;
}
function next() {
if ($(this).is('.disabled')) return false;
2014-05-23 03:05:05 +00:00
var stepInfo = STEPS[step];
if(stepInfo.handleNext) {
var result = stepInfo.handleNext.call(stepInfo);
2014-05-27 15:23:16 +00:00
if(result === false) {return false;}
2014-05-23 03:05:05 +00:00
}
moveToNext();
return false;
}
function moveToNext() {
2014-05-23 03:05:05 +00:00
previousStep = step;
step = step + 1;
moveToStep();
}
function help() {
if ($(this).is('.disabled')) return false;
var stepInfo = STEPS[step];
if(stepInfo.handleHelp) {
var result = stepInfo.handleHelp.call(stepInfo);
if(!result) {return false;}
context.JK.popExternalLink(result);
}
return false;
}
2014-05-22 16:26:56 +00:00
function moveToStep() {
var $nextWizardStep = $wizardSteps.filter($('[layout-wizard-step=' + step + ']'));
beforeHideStep();
$wizardSteps.hide();
$currentWizardStep = $nextWizardStep;
2014-06-03 21:19:39 +00:00
context.JK.GA.virtualPageView('/client/' + dialogName, dialogName + ": " + $currentWizardStep.attr('dialog-title'));
2014-05-27 15:23:16 +00:00
2014-05-23 03:05:05 +00:00
$self.triggerHandler('step_changed', {step:step});
2014-05-22 16:26:56 +00:00
// update buttons
var $wizardButtonsContent = $(context._.template($templateButtons.html(), {}, {variable: 'data'}));
2014-05-27 15:23:16 +00:00
$btnHelp = $wizardButtonsContent.find('.btn-help');
2014-05-22 16:26:56 +00:00
$btnBack = $wizardButtonsContent.find('.btn-back');
$btnNext = $wizardButtonsContent.find('.btn-next');
$btnClose = $wizardButtonsContent.find('.btn-close');
$btnCancel = $wizardButtonsContent.find('.btn-cancel');
2014-05-27 15:23:16 +00:00
// hide help button if on last step
if (step == totalSteps() - 1) {
$btnHelp.hide();
}
2014-05-22 16:26:56 +00:00
// hide back button if 1st step or last step
2014-05-27 15:23:16 +00:00
if (step == 0 || step == totalSteps() - 1) {
2014-05-22 16:26:56 +00:00
$btnBack.hide();
}
2014-05-27 15:23:16 +00:00
// hide next button if on last step
2014-05-23 03:05:05 +00:00
if (step == totalSteps() - 1) {
2014-05-22 16:26:56 +00:00
$btnNext.hide();
}
2014-05-27 15:23:16 +00:00
// hide close if not on last step
2014-05-23 03:05:05 +00:00
if (step != totalSteps() - 1) {
2014-05-22 16:26:56 +00:00
$btnClose.hide();
}
// hide cancel if not on last step
2014-05-23 03:05:05 +00:00
if (step == totalSteps() - 1) {
2014-05-22 16:26:56 +00:00
$btnCancel.hide();
}
$btnHelp.on('click', help);
2014-05-22 16:26:56 +00:00
$btnNext.on('click', next);
$btnBack.on('click', back);
2014-05-23 03:05:05 +00:00
$btnClose.on('click', function() {$self.triggerHandler('wizard_close'); return false;});
$btnCancel.on('click', function() {$self.triggerHandler('wizard_cancel'); return false;});
2014-05-22 16:26:56 +00:00
2014-05-23 03:05:05 +00:00
$wizardButtons.empty();
$wizardButtons.append($wizardButtonsContent);
2014-05-22 16:26:56 +00:00
beforeShowStep($currentWizardStep);
$currentWizardStep.show();
}
2014-05-23 03:05:05 +00:00
// called by owner whenever
function onCloseDialog() {
2014-05-29 19:14:46 +00:00
beforeHideStep();
2014-05-23 03:05:05 +00:00
}
2014-05-22 16:26:56 +00:00
2014-05-23 03:05:05 +00:00
function onBeforeShow(args) {
context._.each(STEPS, function(step) {
// let every step know the wizard is being shown
if(step.beforeWizardShow) {
step.beforeWizardShow(args);
}
});
2014-05-29 19:14:46 +00:00
$currentWizardStep = null;
2014-05-23 03:05:05 +00:00
previousStep = null;
2014-05-22 16:26:56 +00:00
2014-05-27 15:23:16 +00:00
step = args != null ? args.d1 : 0;
2014-05-22 16:26:56 +00:00
if (!step) step = 0;
step = parseInt(step);
2014-05-23 03:05:05 +00:00
moveToStep();
}
function onAfterHide() {
step = null;
}
2014-06-03 21:19:39 +00:00
function getNextButton() {
return $btnNext;
}
2014-05-23 03:05:05 +00:00
function setNextState(enabled) {
if(!$btnNext) return;
if(enabled) {
$btnNext.removeClass('disabled');
2014-05-23 03:05:05 +00:00
}
else {
$btnNext.addClass('disabled');
2014-05-23 03:05:05 +00:00
}
2014-05-23 03:05:05 +00:00
}
function setBackState(enabled) {
if(!$btnBack) return;
if(enabled) {
$btnBack.removeClass('disabled');
2014-05-23 03:05:05 +00:00
}
else {
$btnBack.addClass('disabled');
2014-05-23 03:05:05 +00:00
}
}
function getCurrentStep() {
2014-05-29 19:14:46 +00:00
if($currentWizardStep) {
return parseInt($currentWizardStep.attr('layout-wizard-step'));
}
else {
return null;
}
2014-05-23 03:05:05 +00:00
}
function getCurrentWizardStep() {
return $currentWizardStep;
2014-05-22 16:26:56 +00:00
}
function getDialog() {
return $dialog;
}
2014-06-03 21:19:39 +00:00
function initialize(_$dialog, _$wizardSteps, _STEPS, _options) {
2014-05-22 16:26:56 +00:00
$dialog = _$dialog;
2014-06-03 21:19:39 +00:00
dialogName = $dialog.attr('layout-id');
if (!dialogName) throw "no dialog name (layout-id) in wizard";
2014-05-22 16:26:56 +00:00
$wizardSteps = _$wizardSteps;
STEPS = _STEPS;
2014-05-23 03:05:05 +00:00
$wizardButtons = $dialog.find('.wizard-buttons');
2014-05-22 16:26:56 +00:00
$templateButtons = $('#template-wizard-buttons');
2014-06-03 21:19:39 +00:00
if(_options) {_options = {}};
options = _options;
2014-05-22 16:26:56 +00:00
}
2014-06-03 21:19:39 +00:00
this.getNextButton = getNextButton;
2014-05-23 03:05:05 +00:00
this.setNextState = setNextState;
this.setBackState = setBackState;
this.moveToNext = moveToNext;
2014-05-23 03:05:05 +00:00
this.getCurrentStep = getCurrentStep;
this.getCurrentWizardStep = getCurrentWizardStep;
this.onCloseDialog = onCloseDialog;
this.onBeforeShow = onBeforeShow;
this.onAfterHide = onAfterHide;
this.getDialog = getDialog;
2014-05-22 16:26:56 +00:00
this.initialize = initialize;
}
})(window, jQuery);