All files / web/bundles/pimui/js/product/form sequential-edit.js

93.33% Statements 56/60
94.74% Branches 18/19
86.36% Functions 19/22
93.22% Lines 55/59

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                  302x                                                           302x 55x     302x 10x   10x               302x               384x   384x     383x   383x     9x                       383x 374x   374x   9x     9x   9x         9x 8x 8x 8x 8x     9x     9x 9x 9x 9x 9x   9x 9x   9x 9x 3x 3x     9x 8x 7x       9x 8x                   8x 8x         8x 8x 4x 4x         3x     3x 3x 3x                         3x                 15x     21x 21x           21x          
'use strict';
/**
 * Sequential edit 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',
        'oro/mediator',
        'pim/form',
        'pim/template/product/sequential-edit',
        'routing',
        'pim/router',
        'pim/fetcher-registry',
        'pim/user-context',
        'pim/provider/sequential-edit-provider',
        'bootstrap'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        mediator,
        BaseForm,
        template,
        Routing,
        router,
        FetcherRegistry,
        UserContext,
        sequentialEditProvider
    ) {
        const findObjectIndex = (objects, id, type) => {
            return objects.findIndex(item => item.id === id && item.type === type);
        }
 
        const getObjectViewParams = (object) => {
            const label = object.meta.label[UserContext.get('catalogLocale')];
 
            return {
                id:         object.meta.id,
                type:       object.meta.model_type,
                label:      label,
                shortLabel: label.length > 25 ? label.slice(0, 22) + '...' : label
            }
        }
 
        return BaseForm.extend({
            id: 'sequentialEdit',
            className: 'AknSequentialEdit AknDefault-bottomPanel',
            template: _.template(template),
            events: {
                'click .next, .previous': 'followLink'
            },
            initialize: function () {
                this.model = new Backbone.Model();
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
            configure: function () {
                this.model.set({objectSet: sequentialEditProvider.get()});
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
            addSaveButton: function () {
                this.trigger('save-buttons:register-button', {
                    className: 'save-and-continue',
                    priority: 250,
                    label: undefined !== this.getNextObject() ?
                        __('pim_enrich.entity.product.module.sequential_edit.save_and_next') :
                        __('pim_enrich.entity.product.module.sequential_edit.save_and_finish'),
                    events: {
                        'click .save-and-continue': this.saveAndContinue.bind(this)
                    }
                });
            },
            render: function () {
                if (!this.configured || !this.model.get('objectSet') || 0 === this.model.get('objectSet').length) {
                    this.$el.addClass('AknDefault-bottomPanel--hidden');
 
                    return this;
                } else {
                    this.$el.removeClass('AknDefault-bottomPanel--hidden');
                }
 
                this.addSaveButton();
 
                this.getRoot().trigger(
                    'pim_enrich:form:sequential-edit',
                    {isLast: undefined === this.getNextObject()}
                )
 
                this.getTemplateParameters().done(function (templateParameters) {
                    this.$el.html(this.template(templateParameters));
                    this.$('[data-toggle="tooltip"]').tooltip();
                    this.delegateEvents();
                    this.preloadNext();
                }.bind(this));
 
                return this;
            },
            getTemplateParameters: function () {
                const objectSet     = this.model.get('objectSet');
                const currentMeta = this.getFormData().meta;
                const index         = findObjectIndex(objectSet, currentMeta.id, currentMeta.model_type);
                const previous      = objectSet[index - 1];
                const next          = objectSet[index + 1];
 
                var previousObject = null;
                var nextObject = null;
 
                var promises = [];
                if (previous) {
                    promises.push(this.getEntity(previous.type, previous.id).then(function (product) {
                            previousObject = getObjectViewParams(product);
                        }));
                }
                if (next) {
                    promises.push(this.getEntity(next.type, next.id).then(function (product) {
                            nextObject = getObjectViewParams(product);
                        }));
                }
 
                return $.when.apply($, promises).then(function () {
                    return {
                        objectCount:    objectSet.length,
                        currentIndex:   index + 1,
                        previousObject: previousObject,
                        nextObject:     nextObject,
                        ratio:          (index + 1) / objectSet.length * 100
                    };
                });
            },
            preloadNext: function () {
                var objectSet = this.model.get('objectSet');
                var currentIndex = findObjectIndex(
                    objectSet,
                    this.getFormData().meta.id,
                    this.getFormData().meta.model_type
                );
                var pending = objectSet[currentIndex + 2];
                if (pending) {
                    setTimeout(() => {
                        this.getEntity(pending.type, pending.id)
                    }, 2000);
                }
            },
            saveAndContinue: function () {
                this.parent.getExtension('save').save({ silent: true }).done(this.continue.bind(this));
            },
            continue: function () {
                var nextObject = this.getNextObject();
                Eif (nextObject) {
                    this.goToProduct(nextObject.type, nextObject.id);
                } else {
                    this.finish();
                }
            },
            followLink: function (event) {
                this.getRoot().trigger('pim_enrich:form:state:confirm', {
                    action: function () {
                        this.goToProduct(event.currentTarget.dataset.type, event.currentTarget.dataset.id);
                    }.bind(this)
                });
            },
            goToProduct: function (type, id) {
                router.redirectToRoute(
                    'pim_enrich_' + type + '_edit',
                    { id: id }
                );
            },
            finish: function () {
                router.redirectToRoute('pim_enrich_product_index');
            },
            getEntity(type, id) {
                return FetcherRegistry.getFetcher(type.replace('_', '-')).fetch(id);
            },
            getNextObject: function () {
                var objectSet    = this.model.get('objectSet');
                var currentIndex = findObjectIndex(
                    objectSet,
                    this.getFormData().meta.id,
                    this.getFormData().meta.model_type
                );
 
                return objectSet[currentIndex + 1];
            }
        });
    }
);