<commit_before>"use strict";

angular.module('arethusa.core').service('plugins', [
  'configurator',
  '$injector',
  '$rootScope',
  function(configurator, $injector, $rootScope) {
    var self = this;
    var readyPlugins;
    var initCallbacks;

    function retrievePlugin(name) {
      var pluginConf = configurator.configurationFor(name);
      if (pluginConf.external) {
        // DEPRECATED: This API will change again.

        // We copy because this object will be extended once the plugin
        // is really initialized through the inclusion of its template
        // by the plugin directive.
        return angular.copy(pluginConf);
      } else {
        return $injector.get(name);
      }
    }

    function partitionPlugins() {
      self.main = [];
      self.sub  = [];
      self.withMenu = [];

      angular.forEach(self.all, self.registerPlugin);
    }

    function hasView(plugin) {
      return !(!plugin.template || plugin.noView);
    }

    this.registerPlugin = function(plugin, name) {
      if (hasView(plugin)) {
        if (plugin.main) {
          self.main.push(plugin);
        } else {
          self.sub.push(plugin);
        }
      }
      if (plugin.contextMenu) {
        self.withMenu.push(plugin);
      }
    };


    this.start = function(pluginNames) {
      self.all = arethusaUtil.inject({}, pluginNames, function(memo, name) {
        memo[name] = retrievePlugin(name);
      });

      partitionPlugins();
      declareFirstActive();
      notifyListeners();
      self.init();
    };

    function notifyListeners() {
      angular.forEach(self.all, function(plugin, name) {
        $rootScope.$broadcast('pluginAdded', name, plugin);
      });<commit_msg>LAZY LOAD MODULES THROUGH PLUGIN CONFIGURATION<commit_after>"use strict";

angular.module('arethusa.core').service('plugins', [
  'configurator',
  '$injector',
  '$rootScope',
  '$ocLazyLoad',
  '$q',
  function(configurator, $injector, $rootScope, $ocLazyLoad, $q) {
    var self = this;
    var readyPlugins;
    var initCallbacks;

    function partitionPlugins() {
      self.main = [];
      self.sub  = [];
      self.withMenu = [];

      angular.forEach(self.all, self.registerPlugin);
    }

    function hasView(plugin) {
      return !(!plugin.template || plugin.noView);
    }

    this.registerPlugin = function(plugin, name) {
      if (hasView(plugin)) {
        if (plugin.main) {
          self.main.push(plugin);
        } else {
          self.sub.push(plugin);
        }
      }
      if (plugin.contextMenu) {
        self.withMenu.push(plugin);
      }
    };

    function toSnakeCase(str) {
      return str.replace(/([A-Z])/g, '_$1').toLowerCase();
    }

    function LoadRequest(name, location) {
      var self = this;
      function getName(name, location) {
        return location ? name : 'arethusa.' + name;
      }

      function getLocation(location) {
        return (location || '../dist/' + toSnakeCase(self.name) + '.min.js');
      }

      this.name = getName(name, location);
      this.files = [getLocation(location)];
    }

    function loadPlugin(name) {
      var pluginConf = configurator.configurationFor(name);
      var request = new LoadRequest(name, pluginConf.location);
      return $ocLazyLoad.load(request);
    }

    function loadComplete(pluginNames) {
      return Object.keys(self.loader).length === pluginNames.length;
    }

    function loadPlugins(pluginNames) {
      var loader = $q.defer();

      angular.forEach(pluginNames, function(name, i) {
        var load = loadPlugin(name);
        load.then(
          function() { self.loader[name] = $injector.get(name); },
          function() { self.loader[name] = false; }
        );

        load['finally'](function() {
          if (loadComplete(pluginNames)) loader.resolve();
        });
      });

      return loader.promise;
    }

    function sortPlugins(names) {
      angular.forEach(names, function(name, i) {
        var plugin = self.loader[name];
        if (plugin) self.all[name] = plugin;
      });
    }

    this.start = function(pluginNames) {
      self.loader = {};
      self.all = {};
      var result = $q.defer();

      loadPlugins(pluginNames).then(function() {
        sortPlugins(pluginNames);
        partitionPlugins();
        declareFirstActive();
        self.init();
        notifyListeners();
        self.loader = {};
        result.resolve();
      });

      return result.promise;
    };

    function notify(plugin, name) {
      $rootScope.$broadcast('pluginAdded', name, plugin);
    }

    function notifyListeners() {
      angular.forEach(self.all, notify);
{'fraction_non_alphanumeric': 0.1394396551724138, 'fraction_numerical': 0.00021551724137931034, 'mean_word_length': 2.3703703703703702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4901970', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1362, 'n_words': 265}