diff --git a/src/node/server.js b/src/node/server.js
index d2ef93343..cca76c1f9 100755
--- a/src/node/server.js
+++ b/src/node/server.js
@@ -29,6 +29,7 @@ var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm/lib/npm.js");
+hooks.plugins = plugins;
//set loglevel
log4js.setGlobalLogLevel(settings.loglevel);
diff --git a/src/node/utils/tar.json b/src/node/utils/tar.json
index 36276767e..9bb2027e1 100644
--- a/src/node/utils/tar.json
+++ b/src/node/utils/tar.json
@@ -68,7 +68,8 @@
, "security.js"
, "$security.js"
, "json2.js"
- , "pluginfw/plugins.js"
+ , "pluginfw/client_plugins.js"
+ , "pluginfw/shared.js"
, "pluginfw/hooks.js"
, "pluginfw/parent_require.js"
]
diff --git a/src/static/js/ace.js b/src/static/js/ace.js
index dabe5595f..6bea8d22d 100644
--- a/src/static/js/ace.js
+++ b/src/static/js/ace.js
@@ -244,7 +244,7 @@ require.setGlobalKeyPath("require");\n\
\
');
diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js
index 07580faa5..6bf6720fd 100644
--- a/src/static/js/ace2_inner.js
+++ b/src/static/js/ace2_inner.js
@@ -23,7 +23,7 @@ var editor, _, $, jQuery, plugins, Ace2Common;
Ace2Common = require('./ace2_common');
-plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
+plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
$ = jQuery = require('./rjquery').$;
_ = require("./underscore");
diff --git a/src/static/js/pluginfw/client_plugins.js b/src/static/js/pluginfw/client_plugins.js
new file mode 100644
index 000000000..273500280
--- /dev/null
+++ b/src/static/js/pluginfw/client_plugins.js
@@ -0,0 +1,36 @@
+var $, jQuery;
+$ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
+var _ = require("underscore");
+
+var pluginUtils = require('./shared');
+
+exports.loaded = false;
+exports.plugins = {};
+exports.parts = [];
+exports.hooks = {};
+exports.baseURL = '';
+
+exports.ensure = function (cb) {
+ if (!exports.loaded)
+ exports.update(cb);
+ else
+ cb();
+};
+
+exports.update = function (cb) {
+ // It appears that this response (see #620) may interrupt the current thread
+ // of execution on Firefox. This schedules the response in the run-loop,
+ // which appears to fix the issue.
+ var callback = function () {setTimeout(cb, 0);};
+
+ jQuery.getJSON(exports.baseURL + 'pluginfw/plugin-definitions.json', function(data) {
+ exports.plugins = data.plugins;
+ exports.parts = data.parts;
+ exports.hooks = pluginUtils.extractHooks(exports.parts, "client_hooks");
+ exports.loaded = true;
+ callback();
+ }).error(function(xhr, s, err){
+ console.error("Failed to load plugin-definitions: " + err);
+ callback();
+ });
+};
diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.js
index 44f5fc3f0..d9a14d857 100644
--- a/src/static/js/pluginfw/hooks.js
+++ b/src/static/js/pluginfw/hooks.js
@@ -1,4 +1,3 @@
-var plugins = require("./plugins");
var async = require("async");
var _ = require("underscore");
@@ -71,8 +70,8 @@ exports.flatten = function (lst) {
exports.callAll = function (hook_name, args) {
if (!args) args = {};
- if (plugins.hooks[hook_name] === undefined) return [];
- return _.flatten(_.map(plugins.hooks[hook_name], function (hook) {
+ if (exports.plugins.hooks[hook_name] === undefined) return [];
+ return _.flatten(_.map(exports.plugins.hooks[hook_name], function (hook) {
return hookCallWrapper(hook, hook_name, args);
}), true);
}
@@ -80,9 +79,9 @@ exports.callAll = function (hook_name, args) {
exports.aCallAll = function (hook_name, args, cb) {
if (!args) args = {};
if (!cb) cb = function () {};
- if (plugins.hooks[hook_name] === undefined) return cb(null, []);
+ if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
async.map(
- plugins.hooks[hook_name],
+ exports.plugins.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},
@@ -94,8 +93,8 @@ exports.aCallAll = function (hook_name, args, cb) {
exports.callFirst = function (hook_name, args) {
if (!args) args = {};
- if (plugins.hooks[hook_name] === undefined) return [];
- return exports.syncMapFirst(plugins.hooks[hook_name], function (hook) {
+ if (exports.plugins.hooks[hook_name] === undefined) return [];
+ return exports.syncMapFirst(exports.plugins.hooks[hook_name], function (hook) {
return hookCallWrapper(hook, hook_name, args);
});
}
@@ -103,9 +102,9 @@ exports.callFirst = function (hook_name, args) {
exports.aCallFirst = function (hook_name, args, cb) {
if (!args) args = {};
if (!cb) cb = function () {};
- if (plugins.hooks[hook_name] === undefined) return cb(null, []);
+ if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
exports.mapFirst(
- plugins.hooks[hook_name],
+ exports.plugins.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},
diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.js
index 7efcb49c2..12ba94a27 100644
--- a/src/static/js/pluginfw/plugins.js
+++ b/src/static/js/pluginfw/plugins.js
@@ -1,30 +1,21 @@
-exports.isClient = typeof global != "object";
+var npm = require("npm/lib/npm.js");
+var readInstalled = require("./read-installed.js");
+var relativize = require("npm/lib/utils/relativize.js");
+var readJson = require("npm/lib/utils/read-json.js");
+var path = require("path");
+var async = require("async");
+var fs = require("fs");
+var tsort = require("./tsort");
+var util = require("util");
+var _ = require("underscore");
-var _;
-
-if (!exports.isClient) {
- var npm = require("npm/lib/npm.js");
- var readInstalled = require("./read-installed.js");
- var relativize = require("npm/lib/utils/relativize.js");
- var readJson = require("npm/lib/utils/read-json.js");
- var path = require("path");
- var async = require("async");
- var fs = require("fs");
- var tsort = require("./tsort");
- var util = require("util");
- _ = require("underscore");
-}else{
- var $, jQuery;
- $ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
- _ = require("ep_etherpad-lite/static/js/underscore");
-}
+var pluginUtils = require('./shared');
exports.prefix = 'ep_';
exports.loaded = false;
exports.plugins = {};
exports.parts = [];
exports.hooks = {};
-exports.baseURL = '';
exports.ensure = function (cb) {
if (!exports.loaded)
@@ -43,7 +34,7 @@ exports.formatParts = function () {
exports.formatHooks = function (hook_set_name) {
var res = [];
- var hooks = exports.extractHooks(exports.parts, hook_set_name || "hooks");
+ var hooks = pluginUtils.extractHooks(exports.parts, hook_set_name || "hooks");
_.chain(hooks).keys().forEach(function (hook_name) {
_.forEach(hooks[hook_name], function (hook) {
@@ -53,84 +44,6 @@ exports.formatHooks = function (hook_set_name) {
return "
" + res.join("\n") + "
";
};
-exports.loadFn = function (path, hookName) {
- var functionName
- , parts = path.split(":");
-
- // on windows: C:\foo\bar:xyz
- if(parts[0].length == 1) {
- if(parts.length == 3)
- functionName = parts.pop();
- path = parts.join(":");
- }else{
- path = parts[0];
- functionName = parts[1];
- }
-
- var fn = require(path);
- functionName = functionName ? functionName : 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) {
- if (hooks[hook_name] === undefined) hooks[hook_name] = [];
-
- var hook_fn_name = part[hook_set_name][hook_name];
-
- /* On the server side, you can't just
- * require("pluginname/whatever") if the plugin is installed as
- * a dependency of another plugin! Bah, pesky little details of
- * npm... */
- if (!exports.isClient) {
- hook_fn_name = path.normalize(path.join(path.dirname(exports.plugins[part.plugin].package.path), hook_fn_name));
- }
-
- try {
- var hook_fn = exports.loadFn(hook_fn_name, hook_name);
- if (!hook_fn) {
- throw "Not a function";
- }
- } catch (exc) {
- console.error("Failed to load '" + hook_fn_name + "' for '" + part.full_name + "/" + hook_set_name + "/" + hook_name + "': " + exc.toString())
- }
- if (hook_fn) {
- hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
- }
- });
- });
- return hooks;
-};
-
-
-if (exports.isClient) {
- exports.update = function (cb) {
- // It appears that this response (see #620) may interrupt the current thread
- // of execution on Firefox. This schedules the response in the run-loop,
- // which appears to fix the issue.
- var callback = function () {setTimeout(cb, 0);};
-
- jQuery.getJSON(exports.baseURL + 'pluginfw/plugin-definitions.json', function(data) {
- exports.plugins = data.plugins;
- exports.parts = data.parts;
- exports.hooks = exports.extractHooks(exports.parts, "client_hooks");
- exports.loaded = true;
- callback();
- }).error(function(xhr, s, err){
- console.error("Failed to load plugin-definitions: " + err);
- callback();
- });
- };
-} else {
-
exports.callInit = function (cb) {
var hooks = require("./hooks");
async.map(
@@ -153,6 +66,10 @@ exports.callInit = function (cb) {
);
}
+exports.pathNormalization = function (part, hook_fn_name) {
+ return path.normalize(path.join(path.dirname(exports.plugins[part.plugin].package.path), hook_fn_name));
+}
+
exports.update = function (cb) {
exports.getPackages(function (er, packages) {
var parts = [];
@@ -161,14 +78,14 @@ exports.update = function (cb) {
async.forEach(
Object.keys(packages),
function (plugin_name, cb) {
- exports.loadPlugin(packages, plugin_name, plugins, parts, cb);
+ loadPlugin(packages, plugin_name, plugins, parts, cb);
},
function (err) {
if (err) cb(err);
- exports.plugins = plugins;
- exports.parts = exports.sortParts(parts);
- exports.hooks = exports.extractHooks(exports.parts, "hooks");
- exports.loaded = true;
+ exports.plugins = plugins;
+ exports.parts = sortParts(parts);
+ exports.hooks = pluginUtils.extractHooks(exports.parts, "hooks", exports.pathNormalization);
+ exports.loaded = true;
exports.callInit(cb);
}
);
@@ -200,9 +117,9 @@ exports.getPackages = function (cb) {
flatten(tmp);
cb(null, packages);
});
- };
+};
- exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) {
+function loadPlugin(packages, plugin_name, plugins, parts, cb) {
var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
fs.readFile(
plugin_path,
@@ -226,9 +143,9 @@ exports.getPackages = function (cb) {
cb();
}
);
- };
+}
-exports.partsToParentChildList = function (parts) {
+function partsToParentChildList(parts) {
var res = [];
_.chain(parts).keys().forEach(function (name) {
_.each(parts[name].post || [], function (child_name) {
@@ -242,18 +159,15 @@ exports.partsToParentChildList = function (parts) {
}
});
return res;
-};
-
+}
// Used only in Node, so no need for _
-exports.sortParts = function(parts) {
+function sortParts(parts) {
return tsort(
- exports.partsToParentChildList(parts)
+ partsToParentChildList(parts)
).filter(
function (name) { return parts[name] !== undefined; }
).map(
function (name) { return parts[name]; }
);
-};
-
-}
\ No newline at end of file
+}
diff --git a/src/static/js/pluginfw/shared.js b/src/static/js/pluginfw/shared.js
new file mode 100644
index 000000000..4df71aee8
--- /dev/null
+++ b/src/static/js/pluginfw/shared.js
@@ -0,0 +1,61 @@
+var _ = require("underscore");
+
+function loadFn(path, hookName) {
+ var functionName
+ , parts = path.split(":");
+
+ // on windows: C:\foo\bar:xyz
+ if (parts[0].length == 1) {
+ if (parts.length == 3) {
+ functionName = parts.pop();
+ }
+ path = parts.join(":");
+ } else {
+ path = parts[0];
+ functionName = parts[1];
+ }
+
+ var fn = require(path);
+ functionName = functionName ? functionName : hookName;
+
+ _.each(functionName.split("."), function (name) {
+ fn = fn[name];
+ });
+ return fn;
+};
+
+function extractHooks(parts, hook_set_name, normalizer) {
+ var hooks = {};
+ _.each(parts,function (part) {
+ _.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];
+
+ /* On the server side, you can't just
+ * require("pluginname/whatever") if the plugin is installed as
+ * a dependency of another plugin! Bah, pesky little details of
+ * npm... */
+ if (normalizer) {
+ hook_fn_name = normalizer(part, hook_fn_name);
+ }
+
+ try {
+ var hook_fn = loadFn(hook_fn_name, hook_name);
+ if (!hook_fn) {
+ throw "Not a function";
+ }
+ } catch (exc) {
+ console.error("Failed to load '" + hook_fn_name + "' for '" + part.full_name + "/" + hook_set_name + "/" + hook_name + "': " + exc.toString())
+ }
+ if (hook_fn) {
+ hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
+ }
+ });
+ });
+ return hooks;
+};
+
+exports.extractHooks = extractHooks;
diff --git a/src/templates/pad.html b/src/templates/pad.html
index 1f179785a..425e476d8 100644
--- a/src/templates/pad.html
+++ b/src/templates/pad.html
@@ -369,11 +369,13 @@
document.domain = document.domain; // for comet
}
- var plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
+ var plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
plugins.baseURL = baseURL;
plugins.update(function () {
+ hooks.plugins = plugins;
+
// Call documentReady hook
$(function() {
hooks.aCallAll('documentReady');
diff --git a/src/templates/timeslider.html b/src/templates/timeslider.html
index 02af9f6cc..469ddd94e 100644
--- a/src/templates/timeslider.html
+++ b/src/templates/timeslider.html
@@ -175,10 +175,13 @@
document.domain = document.domain; // for comet
}
- var plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
+ var plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
plugins.baseURL = baseURL;
plugins.update(function () {
+ var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
+ hooks.plugins = plugins;
+
var timeslider = require('ep_etherpad-lite/static/js/timeslider')
timeslider.baseURL = baseURL;
timeslider.init();