All files / web/bundles/pimui/js/job/import switcher.js

90% Statements 18/20
70% Branches 7/10
100% Functions 6/6
90% Lines 18/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                  207x                         207x                         209x   209x   209x             643x       643x 209x       643x 434x           643x                     418x       418x 418x                 16x 16x                 225x 225x          
'use strict';
/**
 * Import switcher extension.
 * This will display all the main actions related to import (upload, import now)
 *
 * @author    Pierre Allard <pierre.allard@akeneo.com>
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'underscore',
        'pim/form',
        'pim/edition',
        'pim/template/import/switcher'
    ],
    function (
        _,
        BaseForm,
        pimEdition,
        template
    ) {
        return BaseForm.extend({
            className: 'AknButtonList',
            template: _.template(template),
            actions: [],
            events: {
                'click .switcher-action': 'switch'
            },
            currentActionCode: null,
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.actions = [];
 
                this.listenTo(this.getRoot(), 'switcher:register', this.registerAction);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (_.isEmpty(this.actions)) {
                    return;
                }
 
                if (null === this.currentActionCode) {
                    this.setCurrentActionCode(_.first(this.actions).code);
                }
 
 
                if (this.actions.length > 1) {
                    this.$el.empty().append(this.template({
                        actions: this.actions,
                        current: this.currentActionCode
                    }));
                }
 
                return BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * Registers a new main action
             *
             * @param {Object} action
             * @param {String} action.label The label to display in this switcher
             * @param {String} action.code  The extension code to display on click
             */
            registerAction: function (action) {
                Iif (pimEdition.isCloudEdition() && action.hideForCloudEdition) {
                    return;
                }
 
                this.actions.push(action);
                this.render();
            },
 
            /**
             * Switches a new action to display
             *
             * @param {Event} event
             */
            switch: function (event) {
                this.setCurrentActionCode(event.target.dataset.code);
                this.render();
            },
 
            /**
             * Sets the new displayed action
             *
             * @param {String} code The code of the current extension
             */
            setCurrentActionCode: function (code) {
                this.currentActionCode = code;
                this.getRoot().trigger('switcher:switch', { code: code });
            }
        });
    }
);