# Code coverage report for ./src/ocLazyLoad.loaders.core.js

## Statements: 81.9% (95 / 116) Branches: 72.92% (70 / 96) Functions: 100% (19 / 19) Lines: 81.9% (95 / 116) Ignored: none

All files » ./src/ » ocLazyLoad.loaders.core.js

    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

    1

    1
    29

    28
    21

    21

    21

    21

    21
    21

    21
    1
    1

    21
    21

    20

    1
    1

    20
    18

    18

    20
    3
    17
    4
    13
    13

    1
    1

    21
    2

    19
    19

    21
    3
    3
    3
    1
    1

    2

    3

    21
    4
    4
    4
    1
    1

    3

    4

    21
    13
    13
    13
    1
    1

    12

    13

    21

    21
    1
    1

    20
    20
    20

    28
    25

    25

    25
    25

    25

    5
    10

    5
    4

    1

    5

    20
    16
    16
    16

    4

    4
    1

    3

    20

    20

    20

    20

    20
    17
    16

    1

    3

    20

    28

     'use strict';

    (function (angular) {
        'use strict';

        angular.module('oc.lazyLoad').config(function ($provide) {
            $provide.decorator('$ocLazyLoad', function ($delegate, $q) {
                /**
                 * The function that loads new files
                 * @param config
                 * @param params
                 * @returns {*}
                 */
                $delegate.filesLoader = function filesLoader(config) {
                    var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

                    var cssFiles = [],
                        templatesFiles = [],
                        jsFiles = [],
                        promises = [],
                        cachePromise = null,
                        filesCache = $delegate._getFilesCache();

                    $delegate.toggleWatch(true); // start watching angular.module calls

                    angular.extend(params, config);

                    var pushFile = function pushFile(path) {
                        var file_type = null,
                            m;
                        if (angular.isObject(path)) {
                            file_type = path.type;
                            path = path.path;
                        }
                        cachePromise = filesCache.get(path);
                        if (angular.isUndefined(cachePromise) || params.cache === false) {

                            // always check for requirejs syntax just in case
                            if ((m = /^(css|less|html|htm|js)?(?=!)/.exec(path)) !== null) {
                                // Detect file type using preceding type declaration (ala requireJS)
                                file_type = m[1];
                                path = path.substr(m[1].length + 1, path.length); // Strip the type from the path
                            }

                            if (!file_type) {
                                Eif ((m = /[.](css|less|html|htm|js)?((\?|#).*)?$/.exec(path)) !== null) {
                                    // Detect file type via file extension
                                    file_type = m[1];
                                } else if (!$delegate.jsLoader.hasOwnProperty('ocLazyLoadLoader') && $delegate.jsLoader.hasOwnProperty('requirejs')) {
                                    // requirejs
                                    file_type = 'js';
                                } else {
                                    $delegate._$log.error('File type could not be determined. ' + path);
                                    return;
                                }
                            }

                            if ((file_type === 'css' || file_type === 'less') && cssFiles.indexOf(path) === -1) {
                                cssFiles.push(path);
                            } else if ((file_type === 'html' || file_type === 'htm') && templatesFiles.indexOf(path) === -1) {
                                templatesFiles.push(path);
                            } else Eif (file_type === 'js' || jsFiles.indexOf(path) === -1) {
                                jsFiles.push(path);
                            } else {
                                $delegate._$log.error('File type is not valid. ' + path);
                            }
                        } else Eif (cachePromise) {
                            promises.push(cachePromise);
                        }
                    };

                    if (params.serie) {
                        pushFile(params.files.shift());
                    } else {
                        angular.forEach(params.files, function (path) {
                            pushFile(path);
                        });
                    }

                    if (cssFiles.length > 0) {
                        var cssDeferred = $q.defer();
                        $delegate.cssLoader(cssFiles, function (err) {
                            if (angular.isDefined(err) && $delegate.cssLoader.hasOwnProperty('ocLazyLoadLoader')) {
                                $delegate._$log.error(err);
                                cssDeferred.reject(err);
                            } else {
                                cssDeferred.resolve();
                            }
                        }, params);
                        promises.push(cssDeferred.promise);
                    }

                    if (templatesFiles.length > 0) {
                        var templatesDeferred = $q.defer();
                        $delegate.templatesLoader(templatesFiles, function (err) {
                            if (angular.isDefined(err) && $delegate.templatesLoader.hasOwnProperty('ocLazyLoadLoader')) {
                                $delegate._$log.error(err);
                                templatesDeferred.reject(err);
                            } else {
                                templatesDeferred.resolve();
                            }
                        }, params);
                        promises.push(templatesDeferred.promise);
                    }

                    if (jsFiles.length > 0) {
                        var jsDeferred = $q.defer();
                        $delegate.jsLoader(jsFiles, function (err) {
                            if (angular.isDefined(err) && ($delegate.jsLoader.hasOwnProperty("ocLazyLoadLoader") || $delegate.jsLoader.hasOwnProperty("requirejs"))) {
                                $delegate._$log.error(err);
                                jsDeferred.reject(err);
                            } else {
                                jsDeferred.resolve();
                            }
                        }, params);
                        promises.push(jsDeferred.promise);
                    }

                    Iif (promises.length === 0) {
                        var deferred = $q.defer(),
                            err = "Error: no file to load has been found, if you're trying to load an existing module you should use the 'inject' method instead of 'load'.";
                        $delegate._$log.error(err);
                        deferred.reject(err);
                        return deferred.promise;
                    } else if (params.serie && params.files.length > 0) {
                        return $q.all(promises).then(function () {
                            return $delegate.filesLoader(config, params);
                        });
                    } else {
                        return $q.all(promises)['finally'](function (res) {
                            $delegate.toggleWatch(false); // stop watching angular.module calls
                            return res;
                        });
                    }
                };

                /**
                 * Load a module or a list of modules into Angular
                 * @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
                 * @param params Object optional parameters
                 * @returns promise
                 */
                $delegate.load = function (originalModule) {
                    var originalParams = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

                    var self = this,
                        config = null,
                        deferredList = [],
                        deferred = $q.defer(),
                        errText;

                    // clean copy
                    var module = angular.copy(originalModule);
                    var params = angular.copy(originalParams);

                    // If module is an array, break it down
                    if (angular.isArray(module)) {
                        // Resubmit each entry as a single module
                        angular.forEach(module, function (m) {
                            deferredList.push(self.load(m, params));
                        });

                        // Resolve the promise once everything has loaded
                        $q.all(deferredList).then(function (res) {
                            deferred.resolve(res);
                        }, function (err) {
                            deferred.reject(err);
                        });

                        return deferred.promise;
                    }

                    // Get or Set a configuration depending on what was passed in
                    if (angular.isString(module)) {
                        config = self.getModuleConfig(module);
                        Eif (!config) {
                            config = {
                                files: [module]
                            };
                        }
                    } else Eif (angular.isObject(module)) {
                        // case {type: 'js', path: lazyLoadUrl + 'testModule.fakejs'}
                        if (angular.isDefined(module.path) && angular.isDefined(module.type)) {
                            config = {
                                files: [module]
                            };
                        } else {
                            config = self.setModuleConfig(module);
                        }
                    }

                    Iif (config === null) {
                        var moduleName = self._getModuleName(module);
                        errText = 'Module "' + (moduleName || 'unknown') + '" is not configured, cannot load.';
                        $delegate._$log.error(errText);
                        deferred.reject(new Error(errText));
                        return deferred.promise;
                    } else {
                        // deprecated
                        Iif (angular.isDefined(config.template)) {
                            if (angular.isUndefined(config.files)) {
                                config.files = [];
                            }
                            if (angular.isString(config.template)) {
                                config.files.push(config.template);
                            } else if (angular.isArray(config.template)) {
                                config.files.concat(config.template);
                            }
                        }
                    }

                    var localParams = angular.extend({}, params, config);

                    // if someone used an external loader and called the load function with just the module name
                    Iif (angular.isUndefined(config.files) && angular.isDefined(config.name) && $delegate.moduleExists(config.name)) {
                        return $delegate.inject(config.name, localParams, true);
                    }

                    $delegate.filesLoader(config, localParams).then(function () {
                        $delegate.inject(null, localParams).then(function (res) {
                            deferred.resolve(res);
                        }, function (err) {
                            deferred.reject(err);
                        });
                    }, function (err) {
                        deferred.reject(err);
                    });

                    return deferred.promise;
                };

                // return the patched service
                return $delegate;
            });
        });
    })(angular);
    //# sourceMappingURL=data:application/json;base64,