diff options
author | Egil Moeller <egil.moller@freecode.no> | 2012-04-05 20:14:48 +0200 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2012-04-05 20:14:48 +0200 |
commit | a3c9407ed6843fae334aebb29aadbac7a88a71ea (patch) | |
tree | 76eea2110cab5232ac32b7a36a45c32fcd529886 /src/static/js/pluginfw | |
parent | 65d32461e0efff4c92cacf89d554340b9e4b6764 (diff) | |
parent | 66fdacf9cf31343721dd8fb38a4584e11ea76e6e (diff) | |
download | etherpad-lite-a3c9407ed6843fae334aebb29aadbac7a88a71ea.zip |
Merge branch 'develop' of git://github.com/Pita/etherpad-lite
Diffstat (limited to 'src/static/js/pluginfw')
-rw-r--r-- | src/static/js/pluginfw/plugins.js | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.js index 7eb82d38..5f44bb6f 100644 --- a/src/static/js/pluginfw/plugins.js +++ b/src/static/js/pluginfw/plugins.js @@ -12,10 +12,9 @@ if (!exports.isClient) { var fs = require("fs"); var tsort = require("./tsort"); var util = require("util"); - var extend = require("node.extend"); _ = require("underscore"); }else{ - var $, jQuery + var $, jQuery; $ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$; _ = require("ep_etherpad-lite/static/js/underscore"); } @@ -31,15 +30,15 @@ exports.ensure = function (cb) { exports.update(cb); else cb(); -} +}; exports.formatPlugins = function () { return _.keys(exports.plugins).join(", "); -} +}; exports.formatParts = function () { return _.map(exports.parts, function (part) { return part.full_name; }).join("\n"); -} +}; exports.formatHooks = function () { var res = []; @@ -49,33 +48,39 @@ exports.formatHooks = function () { }); }); return res.join("\n"); -} +}; -exports.loadFn = function (path) { +exports.loadFn = function (path, hookName) { var x = path.split(":"); var fn = require(x[0]); - _.each(x[1].split("."), function (name) { + var functionName = x[1] ? x[1] : hookName; + + _.each(functionName.split("."), function (name) { fn = fn[name]; }); return fn; -} +}; exports.extractHooks = function (parts, hook_set_name) { var hooks = {}; _.each(parts,function (part) { - _.chain(part[hook_set_name] || {}).keys().each(function (hook_name) { + _.chain(part[hook_set_name] || {}) + .keys() + .each(function (hook_name) { if (hooks[hook_name] === undefined) hooks[hook_name] = []; + + var hook_fn_name = part[hook_set_name][hook_name]; - var hook_fn = exports.loadFn(part[hook_set_name][hook_name]); + var hook_fn = exports.loadFn(hook_fn_name, hook_name); if (hook_fn) { hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part}); } else { - console.error("Unable to load hook function for " + part.full_name + " for hook " + hook_name + ": " + part.hooks[hook_name]); + console.error("Unable to load hook function for " + part.full_name + " for hook " + hook_name + ": " + part.hooks[hook_name]); } }); }); return hooks; -} +}; if (exports.isClient) { @@ -90,7 +95,7 @@ if (exports.isClient) { console.error("Failed to load plugin-definitions: " + err); cb(); }); - } + }; } else { exports.callInit = function (cb) { @@ -135,7 +140,7 @@ exports.update = function (cb) { } ); }); -} + }; exports.getPackages = function (cb) { // Load list of installed NPM packages, flatten it to a list, and filter out only packages with names that @@ -145,49 +150,50 @@ exports.getPackages = function (cb) { var packages = {}; function flatten(deps) { _.chain(deps).keys().each(function (name) { - if (name.indexOf(exports.prefix) == 0) { - packages[name] = extend({}, deps[name]); + if (name.indexOf(exports.prefix) === 0) { + packages[name] = _.clone(deps[name]); // Delete anything that creates loops so that the plugin // list can be sent as JSON to the web client delete packages[name].dependencies; delete packages[name].parent; - } - if (deps[name].dependencies !== undefined) - flatten(deps[name].dependencies); + } + + if (deps[name].dependencies !== undefined) flatten(deps[name].dependencies); }); } + var tmp = {}; tmp[data.name] = data; flatten(tmp); cb(null, packages); }); -} + }; -exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) { + exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) { var plugin_path = path.resolve(packages[plugin_name].path, "ep.json"); fs.readFile( plugin_path, function (er, data) { if (er) { - console.error("Unable to load plugin definition file " + plugin_path); + console.error("Unable to load plugin definition file " + plugin_path); return cb(); } try { var plugin = JSON.parse(data); - plugin.package = packages[plugin_name]; - plugins[plugin_name] = plugin; - _.each(plugin.parts, function (part) { - part.plugin = plugin_name; - part.full_name = plugin_name + "/" + part.name; - parts[part.full_name] = part; - }); + plugin['package'] = packages[plugin_name]; + plugins[plugin_name] = plugin; + _.each(plugin.parts, function (part) { + part.plugin = plugin_name; + part.full_name = plugin_name + "/" + part.name; + parts[part.full_name] = part; + }); } catch (ex) { - console.error("Unable to parse plugin definition file " + plugin_path + ": " + ex.toString()); + console.error("Unable to parse plugin definition file " + plugin_path + ": " + ex.toString()); } cb(); } ); -} + }; exports.partsToParentChildList = function (parts) { var res = []; @@ -203,7 +209,7 @@ exports.partsToParentChildList = function (parts) { } }); return res; -} +}; // Used only in Node, so no need for _ |