(function (context, $) { "use strict"; context.JK = context.JK || {}; context.JK.Wizard = function (app) { var STEPS = null; var step = null; var previousStep = null; var $dialog = null; var $templateButtons = null; var $wizardSteps = null; var $currentWizardStep = null; var $wizardButtons = null; var $btnHelp = null; var $btnNext = null; var $btnBack = null; var $btnClose = null; var $btnCancel = null; var self = this; var $self = $(this); function totalSteps() { return context.JK.dkeys(STEPS).length; } function beforeHideStep() { var currentStep = getCurrentStep(); if(currentStep === null) return; var stepInfo = STEPS[currentStep]; if (!stepInfo) { throw "unknown step: " + currentStep; } if(stepInfo.beforeHide) { stepInfo.beforeHide.call(stepInfo); } } function beforeShowStep($step) { var stepInfo = STEPS[step]; if (!stepInfo) { throw "unknown step: " + step; } stepInfo.beforeShow.call(stepInfo); } function back() { if ($(this).is('.button-grey')) return false; previousStep = step; step = step - 1; moveToStep(); return false; } function next() { if ($(this).is('.button-grey')) return false; var stepInfo = STEPS[step]; if(stepInfo.handleNext) { var result = stepInfo.handleNext.call(stepInfo); if(result === false) {return false;} } previousStep = step; step = step + 1; moveToStep(); return false; } function moveToStep() { var $nextWizardStep = $wizardSteps.filter($('[layout-wizard-step=' + step + ']')); beforeHideStep(); $wizardSteps.hide(); $currentWizardStep = $nextWizardStep; context.JK.GA.virtualPageView(location.pathname + location.search + location.hash + '/d1=' + step, $currentWizardStep.attr('dialog-title')); $self.triggerHandler('step_changed', {step:step}); // update buttons var $wizardButtonsContent = $(context._.template($templateButtons.html(), {}, {variable: 'data'})); $btnHelp = $wizardButtonsContent.find('.btn-help'); $btnBack = $wizardButtonsContent.find('.btn-back'); $btnNext = $wizardButtonsContent.find('.btn-next'); $btnClose = $wizardButtonsContent.find('.btn-close'); $btnCancel = $wizardButtonsContent.find('.btn-cancel'); // hide help button if on last step if (step == totalSteps() - 1) { $btnHelp.hide(); } // hide back button if 1st step or last step if (step == 0 || step == totalSteps() - 1) { $btnBack.hide(); } // hide next button if on last step if (step == totalSteps() - 1) { $btnNext.hide(); } // hide close if not on last step if (step != totalSteps() - 1) { $btnClose.hide(); } // hide cancel if not on last step if (step == totalSteps() - 1) { $btnCancel.hide(); } $btnNext.on('click', next); $btnBack.on('click', back); $btnClose.on('click', function() {$self.triggerHandler('wizard_close'); return false;}); $btnCancel.on('click', function() {$self.triggerHandler('wizard_cancel'); return false;}); $wizardButtons.empty(); $wizardButtons.append($wizardButtonsContent); beforeShowStep($currentWizardStep); $currentWizardStep.show(); } // called by owner whenever function onCloseDialog() { beforeHideStep(); } function onBeforeShow(args) { $currentWizardStep = null; previousStep = null; step = args != null ? args.d1 : 0; if (!step) step = 0; step = parseInt(step); moveToStep(); } function onAfterHide() { step = null; } function setNextState(enabled) { if(!$btnNext) return; $btnNext.removeClass('button-orange button-grey'); if (enabled) { $btnNext.addClass('button-orange'); } else { $btnNext.addClass('button-grey'); } } function setBackState(enabled) { if(!$btnBack) return; $btnBack.removeClass('button-orange button-grey'); if (enabled) { $btnBack.addClass('button-orange'); } else { $btnBack.addClass('button-grey'); } } function getCurrentStep() { if($currentWizardStep) { return parseInt($currentWizardStep.attr('layout-wizard-step')); } else { return null; } } function getCurrentWizardStep() { return $currentWizardStep; } function initialize(_$dialog, _$wizardSteps, _STEPS) { $dialog = _$dialog; $wizardSteps = _$wizardSteps; STEPS = _STEPS; $wizardButtons = $dialog.find('.wizard-buttons'); $templateButtons = $('#template-wizard-buttons'); } this.setNextState = setNextState; this.setBackState = setBackState; this.getCurrentStep = getCurrentStep; this.getCurrentWizardStep = getCurrentWizardStep; this.onCloseDialog = onCloseDialog; this.onBeforeShow = onBeforeShow; this.onAfterHide = onAfterHide; this.initialize = initialize; } })(window, jQuery);