All files / web/bundles/pimui/js/product/form history.js

92.75% Statements 64/69
80% Branches 24/30
86.36% Functions 19/22
94.03% Lines 63/67

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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258                  302x                                                       302x                           384x   384x 384x     384x             383x         383x 383x   383x             628x       628x 628x 537x     91x 91x   91x                 91x   91x 91x 188x           91x       91x             550x   550x 550x     550x                 91x   91x                       641x 73x     568x                 91x   91x   91x 188x 517x 517x 517x       91x                       91x 91x 188x 517x       91x                       312x 312x   312x 312x   312x 312x 43x   312x 23x   312x 99x   312x 122x     312x                                   32x   32x     32x     32x          
'use strict';
/**
 * History extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'pim/form',
        'pim/template/product/history',
        'routing',
        'oro/mediator',
        'pim/fetcher-registry',
        'pim/user-context',
        'pim/i18n',
        'bootstrap-modal'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        template,
        Routing,
        mediator,
        FetcherRegistry,
        UserContext,
        i18n
    ) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'panel-pane history-panel',
            loading: false,
            expandedVersions: [],
            actions: {},
            events: {
                'click .expanded .AknGrid-bodyCell': 'toggleVersion'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.actions = {};
 
                Eif (undefined !== config) {
                    this.config = config.config;
                }
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('tab:register', {
                    code: (undefined === this.config.tabCode) ? this.code : this.config.tabCode,
                    label: __('pim_common.history')
                });
 
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_update', this.update);
                this.onExtensions('action:register',  this.addAction.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                const tabCode = (undefined === this.config.tabCode) ? this.code : this.config.tabCode;
                if (tabCode !== this.getParent().getCurrentTab()) {
                    return this;
                }
 
                Eif (this.getFormData().meta) {
                    this.getVersions()
                        .then(function (versions) {
                            this.$el.html(
                                this.template({
                                    versions: versions,
                                    expandedVersions: this.expandedVersions,
                                    expanded: true,
                                    hasAction: this.actions
                                })
                            );
 
                            this.renderExtensions();
 
                            Eif (this.actions) {
                                _.each(this.$el.find('td.actions'), function (element) {
                                    _.each(this.actions, function (action) {
                                        $(element).append(action.clone(true));
                                    }.bind(this));
                                }.bind(this));
                            }
 
                            this.delegateEvents();
                        }.bind(this));
                }
 
                return this;
            },
 
            /**
             * Update the history by fetching it from the backend
             */
            update: function () {
                const entity = this.getFormData();
 
                Eif (entity.meta) {
                    this.getHistoryFetcher(entity).clear(entity.meta.id);
                }
 
                this.render();
            },
 
            /**
             * Get history versions from the backend
             *
             * @return {Promise}
             */
            getVersions: function () {
                const entity = this.getFormData();
 
                return this.getHistoryFetcher(entity).fetch(
                    entity.meta.id,
                    { entityId: entity.meta.id }
                ).then(this.addAttributesLabelToVersions.bind(this));
            },
 
            /**
             * @param {Object} entity
             *
             * @returns Fetcher
             */
            getHistoryFetcher: function (entity) {
                if ('product_model' === entity.meta.model_type) {
                    return FetcherRegistry.getFetcher('product-model-history');
                }
 
                return FetcherRegistry.getFetcher('product-history');
            },
 
            /**
             * Add attributes label to all versions
             *
             * @param {Array} versions
             */
            addAttributesLabelToVersions: function (versions) {
                var codes = this.getAttributeCodesInVersions(versions);
 
                return FetcherRegistry.getFetcher('attribute').fetchByIdentifiers(codes)
                    .then(function (attributes) {
                        _.each(versions, function (version) {
                            _.each(version.changeset, function (data, index) {
                                var code      = index.split('-')[0];
                                var attribute = _.findWhere(attributes, { code: code });
                                data.label    = attribute ? this.getAttributeLabel(attribute, index) : index;
                            }.bind(this));
                        }.bind(this));
 
                        return versions;
                    }.bind(this));
            },
 
            /**
             * Return the list of unique attribute codes found in all versions
             *
             * @param {Array} versions
             *
             * @returns {Array}
             */
            getAttributeCodesInVersions: function (versions) {
                var codes = [];
                _.each(versions, function (version) {
                    _.each(version.changeset, function (data, index) {
                        codes.push(index.split('-')[0]);
                    });
                });
 
                return _.uniq(codes);
            },
 
            /**
             * Get attribute label
             *
             * @param {object} attribute
             * @param {string} key
             *
             * @return {string}
             */
            getAttributeLabel: function (attribute, key) {
                var uiLocale = UserContext.get('catalogLocale');
                var label    = i18n.getLabel(attribute.labels, uiLocale, attribute.code);
 
                key = key.split('-');
                key.shift();
 
                var info = '';
                if (attribute.localizable) {
                    info += i18n.getFlag(key.shift());
                }
                if (attribute.scopable) {
                    info = ' <span>' + key.shift() + '</span>' + info;
                }
                if (0 < key.length) {
                    info = key.join(' ') + info;
                }
                if (info) {
                    info = ' <span class="attribute-info">' + info + '</span>';
                }
 
                return label + info;
            },
 
            /**
             * Add action to the history
             *
             * @param {Event} event
             */
            addAction: function (event) {
                this.actions[event.code] = event.element;
            },
 
            /**
             * Toggle history version line
             *
             * @param {Event} event
             */
            toggleVersion: function (event) {
                const versionToToggle = event.currentTarget.parentNode.dataset.version;
 
                Iif (this.expandedVersions.find(version => version === versionToToggle)) {
                    this.expandedVersions = this.expandedVersions.filter(version => version !== versionToToggle);
                } else {
                    this.expandedVersions.push(versionToToggle);
                }
 
                this.render();
            }
        });
    }
);