Split client and server plugin functionality.

There is virtually no shared code for the client, extract it into its own
module and do away with the switches.
This commit is contained in:
Chad Weider 2012-05-28 18:39:32 -07:00
parent 9394495364
commit 1258ed3a0d
9 changed files with 117 additions and 108 deletions

View File

@ -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 hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm/lib/npm.js"); var npm = require("npm/lib/npm.js");
hooks.plugins = plugins;
//set loglevel //set loglevel
log4js.setGlobalLogLevel(settings.loglevel); log4js.setGlobalLogLevel(settings.loglevel);

View File

@ -68,7 +68,8 @@
, "security.js" , "security.js"
, "$security.js" , "$security.js"
, "json2.js" , "json2.js"
, "pluginfw/plugins.js" , "pluginfw/client_plugins.js"
, "pluginfw/shared.js"
, "pluginfw/hooks.js" , "pluginfw/hooks.js"
, "pluginfw/parent_require.js" , "pluginfw/parent_require.js"
] ]

View File

@ -244,7 +244,7 @@ require.setGlobalKeyPath("require");\n\
<script type="text/javascript">\ <script type="text/javascript">\
parent_req = require("ep_etherpad-lite/static/js/pluginfw/parent_require");\ parent_req = require("ep_etherpad-lite/static/js/pluginfw/parent_require");\
parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/hooks");\ parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/hooks");\
parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/plugins");\ parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/client_plugins");\
</script>\ </script>\
'); ');

View File

@ -23,7 +23,7 @@ var editor, _, $, jQuery, plugins, Ace2Common;
Ace2Common = require('./ace2_common'); 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').$; $ = jQuery = require('./rjquery').$;
_ = require("./underscore"); _ = require("./underscore");

View File

@ -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();
});
};

View File

@ -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 _; var pluginUtils = require('./shared');
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");
}
exports.prefix = 'ep_'; exports.prefix = 'ep_';
exports.loaded = false; exports.loaded = false;
exports.plugins = {}; exports.plugins = {};
exports.parts = []; exports.parts = [];
exports.hooks = {}; exports.hooks = {};
exports.baseURL = '';
exports.ensure = function (cb) { exports.ensure = function (cb) {
if (!exports.loaded) if (!exports.loaded)
@ -43,7 +34,7 @@ exports.formatParts = function () {
exports.formatHooks = function (hook_set_name) { exports.formatHooks = function (hook_set_name) {
var res = []; 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) { _.chain(hooks).keys().forEach(function (hook_name) {
_.forEach(hooks[hook_name], function (hook) { _.forEach(hooks[hook_name], function (hook) {
@ -53,84 +44,6 @@ exports.formatHooks = function (hook_set_name) {
return "<dl>" + res.join("\n") + "</dl>"; return "<dl>" + res.join("\n") + "</dl>";
}; };
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;
};
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 = 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) { exports.callInit = function (cb) {
var hooks = require("./hooks"); var hooks = require("./hooks");
async.map( async.map(
@ -171,7 +84,7 @@ exports.update = function (cb) {
if (err) cb(err); if (err) cb(err);
exports.plugins = plugins; exports.plugins = plugins;
exports.parts = sortParts(parts); exports.parts = sortParts(parts);
exports.hooks = exports.extractHooks(exports.parts, "hooks", exports.pathNormalization); exports.hooks = pluginUtils.extractHooks(exports.parts, "hooks", exports.pathNormalization);
exports.loaded = true; exports.loaded = true;
exports.callInit(cb); exports.callInit(cb);
} }
@ -206,9 +119,6 @@ exports.getPackages = function (cb) {
}); });
}; };
}
function loadPlugin(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"); var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
fs.readFile( fs.readFile(

View File

@ -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;

View File

@ -369,7 +369,7 @@
document.domain = document.domain; // for comet 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'); var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
plugins.baseURL = baseURL; plugins.baseURL = baseURL;

View File

@ -175,7 +175,7 @@
document.domain = document.domain; // for comet 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.baseURL = baseURL;
plugins.update(function () { plugins.update(function () {