All files / web/bundles/pimui/js/product field-manager.js

97.14% Statements 34/35
83.33% Branches 5/6
100% Functions 13/13
97.06% Lines 33/34

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                  901x     901x 901x 901x                 901x 3558x   3558x 766x   766x     2792x 2792x   2792x       2792x 2792x 2792x     2792x     901x                 6058x   6058x 2500x   2500x     3558x 3558x 3558x 3558x       3558x                 1679x                 98x                 6035x                 1x             458x 458x             1512x          
'use strict';
/**
 * Field manager
 *
 * @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', 'pim/fetcher-registry', 'pim/form-config-provider', 'require-context'],
    function ($, _, FetcherRegistry, ConfigProvider, requireContext) {
        var fields = {};
        var visibleFields = {};
        var loadedModules = {};
 
        /**
         * Create a field view for the given attribute
         *
         * @param {Object} attribute
         *
         * @return {View}
         */
        var getFieldForAttribute = function (attribute) {
            var deferred = $.Deferred();
 
            if (loadedModules[attribute.field_type]) {
                deferred.resolve(loadedModules[attribute.field_type]);
 
                return deferred.promise();
            }
 
            ConfigProvider.getAttributeFields().done(function (attributeFields) {
                var fieldModule = attributeFields[attribute.field_type];
 
                Iif (!fieldModule) {
                    throw new Error('No field defined for attribute type "' + attribute.field_type + '"');
                }
 
                var ResolvedModule = requireContext(fieldModule);
                loadedModules[attribute.field_type] = ResolvedModule;
                deferred.resolve(ResolvedModule)
            });
 
            return deferred.promise();
        };
 
        return {
            /**
             * Get the field view for the given attribute code
             *
             * @param {string} attributeCode
             *
             * @return {View}
             */
            getField: function (attributeCode) {
                var deferred = $.Deferred();
 
                if (fields[attributeCode]) {
                    deferred.resolve(fields[attributeCode]);
 
                    return deferred.promise();
                }
 
                FetcherRegistry.getFetcher('attribute').fetch(attributeCode).done(function (attribute) {
                    getFieldForAttribute(attribute).done(function (Field) {
                        fields[attributeCode] = new Field(attribute);
                        deferred.resolve(fields[attributeCode]);
                    });
                });
 
                return deferred.promise();
            },
 
            /**
             * Get all the fields that are not ready (for example media fields that are currently uploading)
             *
             * @return {array}
             */
            getNotReadyFields: function () {
                return Object.values(fields).filter(field => !field.isReady());
            },
 
            /**
             * Get all the cached fields
             *
             * @return {array}
             */
            getFields: function () {
                return fields;
            },
 
            /**
             * Add a field to the collection of currently displayed fields
             *
             * @param {string} attributeCode
             */
            addVisibleField: function (attributeCode) {
                visibleFields[attributeCode] = fields[attributeCode];
            },
 
            /**
             * Get all visible fields
             *
             * @return {[type]}
             */
            getVisibleFields: function () {
                return visibleFields;
            },
 
            /**
             * Clear the field collection
             */
            clearFields: function () {
                fields = {};
                this.clearVisibleFields();
            },
 
            /**
             * Clear the displayed field collection
             */
            clearVisibleFields: function () {
                visibleFields = {};
            }
        };
    }
);