All files / web/bundles/pimui/js/form builder.js

87.5% Statements 28/32
60% Branches 6/10
100% Functions 13/13
87.5% Lines 28/32

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    1184x     1184x 103571x 103182x             103182x       1184x 103093x 102747x   102747x                 102747x             102747x 102747x   102747x 96750x     102747x 96750x     95534x   95534x       102747x 102385x 95152x               102385x         1184x       5171x     4995x   4995x                 4995x 4993x              
'use strict';
 
define(
    ['jquery', 'underscore', 'pim/form-registry', 'require-context'],
    function ($, _, FormRegistry, requireContext) {
        const getFormMeta = function (formName) {
            return FormRegistry.getFormMeta(formName).then((formMeta) => {
                Iif (undefined === formMeta) {
                    throw new Error(`
The extension "${formName}" was not found. Are you sure you registered it properly?
Check your form_extension files and be sure to clear your prod cache before proceeding
                    `);
                }
 
                return formMeta;
            });
        };
 
        const buildForm = function (formMeta) {
            return FormRegistry.getFormExtensions(formMeta).then((extensionsMeta) => {
                const FormClass = requireContext(formMeta.module);
 
                Iif (undefined === FormClass) {
                    throw new Error(`
The module "${formMeta.module}" is undefined.
Most of the time it's because it's not well registered in your requirejs.yml file.
Here is the documentation to fix this problem
https://docs.akeneo.com/latest/design_pim/overview.html#register-it`
                    );
                }
 
                Iif (typeof FormClass !== 'function') {
                    throw new Error(`
                        The module "${formMeta.module}" must return a function.
                        It returns: ${typeof FormClass}`
                    );
                }
 
                const form = new FormClass(formMeta);
                form.code = formMeta.code;
 
                const filteredExtensionsMeta = extensionsMeta.filter(
                    (extensionsMeta) => null !== extensionsMeta.module
                );
 
                const extensionPromises = filteredExtensionsMeta.map((extension) => {
                    return getFormMeta(extension.code)
                        .then(buildForm)
                        .then(function (loadedModule) {
                            extension.loadedModule = loadedModule;
 
                            return extension;
                        });
                });
 
                return $.when.apply($, extensionPromises).then(function () {
                    filteredExtensionsMeta.forEach((extension) => {
                        form.addExtension(
                            extension.code,
                            extension.loadedModule,
                            extension.targetZone,
                            extension.position
                        );
                    });
 
                    return form;
                });
            });
        };
 
        return {
            getFormMeta: getFormMeta,
            buildForm: buildForm,
            build: function (formName) {
                return getFormMeta(formName)
                    .then(buildForm)
                    .then(function (form) {
                        const promise = form.configure();
 
                        Iif (undefined === promise || typeof promise.then !== 'function') {
                            throw new Error(`
The method configure for the module "${form.code}" must return a promise.
If you get this error, it often means that you forgot to return:
BaseForm.prototype.configure.apply(this, arguments)
(See details here: https://docs.akeneo.com/latest/design_pim/overview.html#useful-methods)
                            `);
                        }
 
                        return promise.then(function () {
                            return form;
                        });
                    });
            }
        };
    }
);