All files / web/bundles/pimdatagrid/js/datagrid action-launcher.js

75% Statements 39/52
72.5% Branches 29/40
75% Functions 6/8
75% Lines 39/52

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239  781x                                       781x                                                                                                                     53718x 53718x       53718x       53718x       53718x       53718x 3297x     53718x       53718x 48242x     53718x 26440x     53718x 5476x     53718x 26440x     53718x 15360x     53718x 53718x                 48242x   48242x   48242x                       48242x   48242x                 5476x   5476x   5476x                   5476x   5476x                   180x     180x 180x 180x   180x     180x                                                                       24742x        
/* global define */
define([
    'jquery',
    'underscore',
    'oro/translator',
    'backbone',
    'pim/template/datagrid/action-launcher-button',
    'pim/template/datagrid/action-launcher-list-item'
], function($, _, __, Backbone, buttonTemplate, listItemTemplate) {
    'use strict';
 
    /**
     * Action launcher implemented as simple link. Click on link triggers action run
     *
     * Events:
     * click: Fired when launcher was clicked
     *
     * @export  oro/datagrid/action-launcher
     * @class   oro.datagrid.ActionLauncher
     * @extends Backbone.View
     */
    return Backbone.View.extend({
        /** @property */
        enabled: true,
 
        /** @property {String} */
        tagName: 'a',
 
        /** @property {Boolean} */
        onClickReturnValue: true,
 
        /** @property {oro.datagrid.AbstractAction} */
        action: undefined,
 
        /** @property {String} */
        label: undefined,
 
        /** @property {String} */
        icon: undefined,
 
        /** @property {String} */
        iconClassName: undefined,
 
        /** @property {String} */
        className: undefined,
 
        /** @property {String} */
        link: 'javascript:void(0);',
 
        /** @property {String} */
        runAction: true,
 
        /** @property {String} */
        group: undefined,
 
        /** @property {function(Object, ?Object=): String} */
        buttonTemplate: _.template(buttonTemplate),
 
        /** @property {function(Object, ?Object=): String} */
        listItemTemplate: _.template(listItemTemplate),
 
        /** @property */
        events: {
            'click': 'onClick'
        },
 
        /**
         * Initialize
         *
         * @param {Object} options
         * @param {oro.datagrid.AbstractAction} options.action
         * @param {function(Object, ?Object=): string} [options.template]
         * @param {String} [options.label]
         * @param {String} [options.icon]
         * @param {String} [options.link]
         * @param {Boolean} [options.runAction]
         * @param {Boolean} [options.onClickReturnValue]
         * @throws {TypeError} If mandatory option is undefined
         */
        initialize: function(options) {
            options = options || {};
            Iif (!options.action) {
                throw new TypeError("'action' is required");
            }
 
            Iif (options.template) {
                this.template = options.template;
            }
 
            Iif (options.label) {
                this.label = options.label;
            }
 
            Iif (options.icon) {
                this.icon = options.icon;
            }
 
            if (options.link) {
                this.link = options.link;
            }
 
            Iif (options.iconClassName) {
                this.iconClassName = options.iconClassName;
            }
 
            if (options.className) {
                this.className = options.className;
            }
 
            if (_.has(options, 'runAction')) {
                this.runAction = options.runAction;
            }
 
            if (_.has(options, 'group')) {
                this.group = options.group;
            }
 
            if (_.has(options, 'onClickReturnValue')) {
                this.onClickReturnValue = options.onClickReturnValue;
            }
 
            if (_.has(options, 'enabled')) {
                this.enabled = options.enabled;
            }
 
            this.action = options.action;
            Backbone.View.prototype.initialize.apply(this, arguments);
        },
 
        /**
         * Render the launcher as a simple button
         *
         * @return {*}
         */
        render: function () {
            this.$el.empty();
 
            const labelKey = this.label || this.action.label;
 
            const $el = $(this.buttonTemplate({
                label: labelKey ? __(labelKey) : '',
                icon: this.icon,
                className: this.className ? this.className : '',
                iconClassName: this.iconClassName,
                link: this.link,
                action: this.action,
                attributes: this.attributes,
                enabled: this.enabled,
                tagName: this.tagName
            }));
 
            this.setElement($el);
 
            return this;
        },
 
        /**
         * Render the launcher as a list item
         *
         * @return {*}
         */
        renderAsListItem: function () {
            this.$el.empty();
 
            const labelKey = this.label || this.action.label;
 
            const $el = $(this.listItemTemplate({
                label: labelKey ? __(labelKey) : '',
                className: 'AknDropdown-menuLink' + (this.className ? ' ' + this.className : ''),
                link: this.link,
                action: this.action,
                attributes: this.attributes,
                enabled: this.enabled,
                tagName: this.tagName
            }));
 
            this.setElement($el);
 
            return this;
        },
 
        /**
         * Handle launcher click
         *
         * @protected
         * @return {Boolean}
         */
        onClick: function() {
            Iif (!this.enabled) {
                return this.onClickReturnValue;
            }
            this.trigger('click', this);
            Eif (this.runAction) {
                this.action.run();
 
                this.$el.closest('.btn-group').removeClass('open');
 
                // skip launcher functionality, if action was executed
                return false;
            }
 
            return this.onClickReturnValue;
        },
 
        /**
         * Disable
         *
         * @return {*}
         */
        disable: function() {
            this.enabled = false;
            this.$el.addClass('disabled');
 
            return this;
        },
 
        /**
         * Enable
         *
         * @return {*}
         */
        enable: function() {
            this.enabled = true;
            this.$el.removeClass('disabled');
 
            return this;
        },
 
        /**
         * Return the action group
         *
         * @return {String}
         */
        getGroup: function () {
            return this.group;
        }
    });
});