util.js
function(settings, pluginInfo, options) {
  // resolve plugin and plugin options
  var plugin;
  if (_.has(pluginInfo, 'plugin')) {
    plugin = pluginInfo.plugin;
  } else if (_.has(pluginInfo, 'module')) {
    // make sure we don't load the same module twice
    if (_.contains(settings.loadedModules, pluginInfo.module)) {
      return settings;
    }
    // try to load module
    try {
      plugin = require(pluginInfo.module);
    } catch (err) {
      throw new GrawlixPluginError({
        msg: "cannot find module '" + pluginInfo.module + "'",
        plugin: pluginInfo,
        trace: new Error() 
      });
    }
    settings.loadedModules.push(pluginInfo.module);
  } else {
    plugin = pluginInfo;
  }
  var pluginOpts = _.has(pluginInfo, 'options') ? pluginInfo.options : {};
  // instantiate plugin if necessary
  if (_.isFunction(plugin)) {
    plugin = plugin(pluginOpts, options);
  }
  // validate plugin
  if (!(plugin instanceof GrawlixPlugin)) {
    throw new GrawlixPluginError({
      msg: 'invalid plugin',
      plugin: pluginInfo
    });
  } else if (plugin.name === null || _.isEmpty(plugin.name)) {
    throw new GrawlixPluginError({
      msg: 'invalid plugin - name property not provided',
      plugin: pluginInfo
    });
  } else if (_.contains(settings.loadedPlugins, plugin.name)) {
    // don't load the same plugin twice
    return settings;
  }
  // initialize plugin
  plugin.init(pluginOpts);
  // load filters
  if (!_.isUndefined(plugin.filters) && _.isArray(plugin.filters)) {
    try {
      loadFilters(settings, plugin.filters, options.allowed);
    } catch (err) {
      err.plugin = pluginInfo;
      throw err;
    }
  }
  // load styles
  if (!_.isUndefined(plugin.styles) && _.isArray(plugin.styles)) {
    try {
      loadStyles(settings, plugin.styles);
    } catch (err) {
      err.plugin = pluginInfo;
      throw err;
    }
  }
  // add name to loaded plugins
  settings.loadedPlugins.push(plugin.name);
  // return
  return settings;
}
function(settings, pluginInfo, options) {
  // resolve plugin and plugin options
  var plugin;
  if (_.has(pluginInfo, 'plugin')) {
    plugin = pluginInfo.plugin;
  } else if (_.has(pluginInfo, 'module')) {
    // make sure we don't load the same module twice
    if (_.contains(settings.loadedModules, pluginInfo.module)) {
      return settings;
    }
    // try to load module
    try {
      plugin = require(pluginInfo.module);
    } catch (err) {
      throw new GrawlixPluginError({
        msg: "cannot find module '" + pluginInfo.module + "'",
        plugin: pluginInfo,
        trace: new Error() 
      });
    }
    settings.loadedModules.push(pluginInfo.module);
  } else {
    plugin = pluginInfo;
  }
  var pluginOpts = _.has(pluginInfo, 'options') ? pluginInfo.options : {};
  // instantiate plugin if necessary
  if (_.isFunction(plugin)) {
    plugin = plugin(pluginOpts, options);
  }
  // validate plugin
  if (!(plugin instanceof GrawlixPlugin)) {
    throw new GrawlixPluginError({
      msg: 'invalid plugin',
      plugin: pluginInfo
    });
  } else if (plugin.name === null || _.isEmpty(plugin.name)) {
    throw new GrawlixPluginError({
      msg: 'invalid plugin - name property not provided',
      plugin: pluginInfo
    });
  } else if (_.contains(settings.loadedPlugins, plugin.name)) {
    // don't load the same plugin twice
    return settings;
  }
  // initialize plugin
  plugin.init(pluginOpts);
  // load filters
  if (!_.isUndefined(plugin.filters) && _.isArray(plugin.filters)) {
    try {
      loadFilters(settings, plugin.filters, options.allowed);
    } catch (err) {
      err.plugin = pluginInfo;
      throw err;
    }
  }
  // load styles
  if (!_.isUndefined(plugin.styles) && _.isArray(plugin.styles)) {
    try {
      loadStyles(settings, plugin.styles);
    } catch (err) {
      err.plugin = pluginInfo;
      throw err;
    }
  }
  // add name to loaded plugins
  settings.loadedPlugins.push(plugin.name);
  // return
  return settings;
}
function ( settings , pluginInfo , options ) { // resolve plugin and plugin options var plugin ; if ( _ . has ( pluginInfo , 'plugin' ) ) { plugin = pluginInfo . plugin ; } else if ( _ . has ( pluginInfo , 'module' ) ) { // make sure we don't load the same module twice if ( _ . contains ( settings . loadedModules , pluginInfo . module ) ) { return settings ; } // try to load module try { plugin = require ( pluginInfo . module ) ; } catch ( err ) { throw new GrawlixPluginError ( { msg : "cannot find module '" + pluginInfo . module + "'" , plugin : pluginInfo , trace : new Error ( ) } ) ; } settings . loadedModules . push ( pluginInfo . module ) ; } else { plugin = pluginInfo ; } var pluginOpts = _ . has ( pluginInfo , 'options' ) ? pluginInfo . options : { } ; // instantiate plugin if necessary if ( _ . isFunction ( plugin ) ) { plugin = plugin ( pluginOpts , options ) ; } // validate plugin if ( ! ( plugin instanceof GrawlixPlugin ) ) { throw new GrawlixPluginError ( { msg : 'invalid plugin' , plugin : pluginInfo } ) ; } else if ( plugin . name === null || _ . isEmpty ( plugin . name ) ) { throw new GrawlixPluginError ( { msg : 'invalid plugin - name property not provided' , plugin : pluginInfo } ) ; } else if ( _ . contains ( settings . loadedPlugins , plugin . name ) ) { // don't load the same plugin twice return settings ; } // initialize plugin plugin . init ( pluginOpts ) ; // load filters if ( ! _ . isUndefined ( plugin . filters ) && _ . isArray ( plugin . filters ) ) { try { loadFilters ( settings , plugin . filters , options . allowed ) ; } catch ( err ) { err . plugin = pluginInfo ; throw err ; } } // load styles if ( ! _ . isUndefined ( plugin . styles ) && _ . isArray ( plugin . styles ) ) { try { loadStyles ( settings , plugin . styles ) ; } catch ( err ) { err . plugin = pluginInfo ; throw err ; } } // add name to loaded plugins settings . loadedPlugins . push ( plugin . name ) ; // return return settings ; }
Loads the given plugin into the given GrawlixSettings object
@param  {GrawlixSettings} settings   GrawlixSettings object
@param  {Object|Function} pluginInfo Either a factory function, a
GrawlixPlugin object, or an object
wrapping a factory function or
GrawlixPlugin with additional
plugin-specific config options
@param  {Object}          options    Main grawlix options object
@return {GrawlixSettings}            Settings object with plugin loaded
Loads the given plugin into the given GrawlixSettings object