All files / web/bundles/pimui/js/form/common edit-form.js

81.82% Statements 27/33
68.75% Branches 11/16
90% Functions 9/10
81.82% Lines 27/33

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                  901x                                                     901x             3182x   3182x 9x               2856x 2856x   2856x 2856x     2856x   2856x 713x 713x       713x     2856x             1946x     1946x   1946x   1946x   1946x   1946x   1946x             1829x                                   162x 164x                       2076x   2076x          
'use strict';
/**
 * Edit form
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alps <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(
    [
        'underscore',
        'oro/translator',
        'backbone',
        'pim/template/common/default-template',
        'pim/form',
        'oro/mediator',
        'pim/fetcher-registry',
        'pim/field-manager',
        'pim/form-builder',
        'require-context',
        'oro/messenger'
    ],
    function (
        _,
        __,
        Backbone,
        template,
        BaseForm,
        mediator,
        FetcherRegistry,
        FieldManager,
        formBuilder,
        RequireContext,
        messenger
    ) {
        return BaseForm.extend({
            template: _.template(template),
 
            /**
             * {@inheritdoc}
             */
            initialize: function (options) {
                options = options || {};
 
                if (options.config && options.config.template) {
                    this.template = _.template(RequireContext(options.config.template));
                }
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                mediator.clear('pim_enrich:form');
                Backbone.Router.prototype.once('route', this.unbindEvents);
 
                Eif (_.has(__moduleConfig, 'forwarded-events')) {
                    this.forwardMediatorEvents(__moduleConfig['forwarded-events']);
                }
 
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:bad_request', this.displayError.bind(this));
 
                this.onExtensions('save-buttons:register-button', function (button) {
                    const saveButtonsExtension = this.getExtension('save-buttons');
                    Iif (undefined === saveButtonsExtension) {
                        throw Error('edit-form extension should declare save-buttons extension to be able to use ' +
                            'save extension');
                    }
                    saveButtonsExtension.trigger('save-buttons:add-button', button);
                }.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
                const scrollPosition = this.$el.find('.edit-form').scrollTop();
 
                this.getRoot().trigger('pim_enrich:form:render:before');
 
                this.$el.html(this.template());
 
                this.renderExtensions();
 
                this.getRoot().trigger('pim_enrich:form:render:after');
 
                this.$el.find('.edit-form').scrollTop(scrollPosition);
            },
 
            /**
             * Clear the mediator
             */
            unbindEvents: function () {
                mediator.clear('pim_enrich:form');
            },
 
            /**
             * Clear the cached information
             */
            clearCache: function () {
                FetcherRegistry.clearAll();
                FieldManager.clearFields();
                this.render();
            },
 
            /**
             * Display validation error as flash message
             *
             * @param {Event} event
             */
            displayError: function (event) {
                _.each(event.response, function (error) {
                    Iif (error.global) {
                        messenger.notify('error', error.message);
                    }
                })
            },
 
            /**
             * Get header size of the form
             *
             * @return {number}
             */
            headerSize: function () {
                const header = this.el.querySelector('header.navigation');
 
                return null !== header ? header.offsetHeight : 0;
            }
        });
    }
);