123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- define([
- 'uiRegistry',
- 'uiComponent',
- 'jquery',
- 'underscore',
- 'ko',
- 'mage/backend/notification',
- 'mage/translate'
- ], function (uiRegistry, Component, $, _, ko) {
- 'use strict';
- var Wizard;
- ko.utils.domNodeDisposal.cleanExternalData = _.wrap(
- ko.utils.domNodeDisposal.cleanExternalData,
- function (func, node) {
- if (!$(node).closest('[data-type=skipKO]').length) {
- func(node);
- }
- }
- );
- /**
- * Wizard constructor.
- *
- * @param {Array} steps
- * @param {String} modalClass
- * @constructor
- */
- Wizard = function (steps, modalClass) {
- this.steps = steps;
- this.index = 0;
- this.data = {};
- this.nextLabelText = $.mage.__('Next');
- this.prevLabelText = $.mage.__('Back');
- this.elementSelector = '[data-role=steps-wizard-main]';
- this.element = modalClass ? $('.' + modalClass + this.elementSelector) : $(this.elementSelector);
- this.nextLabel = '[data-role="step-wizard-next"]';
- this.prevLabel = '[data-role="step-wizard-prev"]';
- this.element.notification();
- /**
- * Move to newIndex.
- *
- * @param {Number} newIndex
- * @return {String}
- */
- this.move = function (newIndex) {
- if (!this.preventSwitch(newIndex)) {
- if (newIndex > this.index) {
- this._next(newIndex);
- } else if (newIndex < this.index) {
- this._prev(newIndex);
- }
- }
- this.updateLabels(this.getStep());
- this.showNotificationMessage();
- return this.getStep().name;
- };
- /**
- * Move wizard to next step.
- *
- * @return {String}
- */
- this.next = function () {
- this.move(this.index + 1);
- return this.getStep().name;
- };
- /**
- * Move wizard to previous step.
- *
- * @return {String}
- */
- this.prev = function () {
- this.move(this.index - 1);
- return this.getStep().name;
- };
- /**
- * @return {*}
- */
- this.preventSwitch = function (newIndex) {
- return newIndex < 0 || (newIndex - this.index) > 1;//eslint-disable-line no-extra-parens
- };
- /**
- * @param {Number} newIndex
- * @return {Boolean}
- * @private
- */
- this._next = function (newIndex) {
- newIndex = _.isNumber(newIndex) ? newIndex : this.index + 1;
- try {
- this.getStep().force(this);
- if (newIndex >= steps.length) {
- return false;
- }
- } catch (e) {
- this.setNotificationMessage(e.message, true);
- return false;
- }
- this.cleanErrorNotificationMessage();
- this.index = newIndex;
- this.cleanNotificationMessage();
- this.render();
- };
- /**
- * @param {Number} newIndex
- * @private
- */
- this._prev = function (newIndex) {
- newIndex = _.isNumber(newIndex) ? newIndex : this.index - 1;
- this.getStep().back(this);
- this.index = newIndex;
- };
- /**
- * @param {Number} stepIndex
- * @return {Object}
- */
- this.getStep = function (stepIndex) {
- return this.steps[stepIndex || this.index] || {};
- };
- /**
- * @param {String} message
- * @param {String} error
- */
- this.notifyMessage = function (message, error) {
- $(this.element).notification('clear').notification('add', {
- error: error,
- message: message
- });
- };
- /**
- * @param {Object} step
- */
- this.updateLabels = function (step) {
- this.element.find(this.nextLabel).find('button').text(step.nextLabelText || this.nextLabelText);
- this.element.find(this.prevLabel).find('button').text(step.prevLabelText || this.prevLabelText);
- };
- /**
- * Show notification message.
- */
- this.showNotificationMessage = function () {
- if (!_.isEmpty(this.getStep())) {
- this.hideNotificationMessage();
- if (this.getStep().notificationMessage.text !== null) {
- this.notifyMessage(
- this.getStep().notificationMessage.text,
- this.getStep().notificationMessage.error
- );
- }
- }
- };
- /**
- * Remove notification message.
- */
- this.cleanNotificationMessage = function () {
- this.getStep().notificationMessage.text = null;
- this.hideNotificationMessage();
- };
- /**
- * Remove error message.
- */
- this.cleanErrorNotificationMessage = function () {
- if (this.getStep().notificationMessage.error === true) {
- this.cleanNotificationMessage();
- }
- };
- /**
- * @param {String} text
- * @param {String} error
- */
- this.setNotificationMessage = function (text, error) {
- error = error !== undefined;
- if (!_.isEmpty(this.getStep())) {
- this.getStep().notificationMessage.text = text;
- this.getStep().notificationMessage.error = error;
- this.showNotificationMessage();
- }
- };
- /**
- * Hide notification message.
- */
- this.hideNotificationMessage = function () {
- $(this.element).notification('clear');
- };
- /**
- * Render step.
- */
- this.render = function () {
- this.hideNotificationMessage();
- this.getStep().render(this);
- };
- /**
- * Initialize step.
- */
- this.init = function () {
- this.updateLabels(this.getStep());
- this.render();
- };
- this.init();
- };
- return Component.extend({
- defaults: {
- modalClass: '',
- initData: [],
- stepsNames: [],
- selectedStep: '',
- steps: [],
- disabled: true
- },
- /** @inheritdoc */
- initialize: function () {
- this._super();
- this.selectedStep.subscribe(this.wrapDisabledBackButton.bind(this));
- },
- /** @inheritdoc */
- initElement: function (step) {
- step.initData = this.initData;
- step.mode = _.all(this.initData, _.isEmpty) ? 'create' : 'edit';
- this.steps[this.getStepIndexByName(step.name)] = step;
- },
- /** @inheritdoc */
- initObservable: function () {
- this._super().observe([
- 'selectedStep',
- 'disabled'
- ]);
- return this;
- },
- /** @inheritdoc */
- destroy: function () {
- _.each(this.steps, function (step) {
- step.destroy();
- });
- this._super();
- },
- /**
- * Toggle disable property.
- *
- * @param {String} stepName
- */
- wrapDisabledBackButton: function (stepName) {
- if (_.first(this.stepsNames) === stepName) {
- this.disabled(true);
- } else {
- this.disabled(false);
- }
- },
- /**
- * Get step by index name.
- *
- * @param {String} stepName
- */
- getStepIndexByName: function (stepName) {
- return _.indexOf(this.stepsNames, stepName);
- },
- //controls, todo to another object
- /**
- * Select next step.
- */
- next: function () {
- this.selectedStep(this.wizard.next());
- },
- /**
- * Select previous step.
- */
- back: function () {
- this.selectedStep(this.wizard.prev());
- },
- /**
- * Open wizard.
- */
- open: function () {
- this.selectedStep(this.stepsNames.first());
- this.wizard = new Wizard(this.steps, this.modalClass);
- },
- /**
- * Close wizard.
- */
- close: function () {
- var modal = uiRegistry.get(this.initData.configurableModal);
- if (!_.isUndefined(modal)) {
- modal.closeModal();
- }
- },
- /**
- * @param {Object} data
- * @param {Object} event
- */
- showSpecificStep: function (data, event) {
- var index = _.indexOf(this.stepsNames, event.target.hash.substr(1)),
- stepName = this.wizard.move(index);
- this.selectedStep(stepName);
- }
- });
- });
|