Replace ParentRequire hack.

Instead of hacking with the internals of require, make client_plugins aware
and capable of sharing behavior.
pull/993/head
Chad Weider 2012-09-11 20:45:14 -07:00
parent 1258ed3a0d
commit b691606c4e
4 changed files with 39 additions and 44 deletions

View File

@ -71,6 +71,5 @@
, "pluginfw/client_plugins.js"
, "pluginfw/shared.js"
, "pluginfw/hooks.js"
, "pluginfw/parent_require.js"
]
}

View File

@ -241,10 +241,11 @@ require.setGlobalKeyPath("require");\n\
// Inject my plugins into my child.
iframeHTML.push('\
<script type="text/javascript">\
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/client_plugins");\
<script type="text/javascript">\n\
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");\n\
var plugins = require("ep_etherpad-lite/static/js/pluginfw/client_plugins");\n\
hooks.plugins = plugins;\n\
plugins.adoptPluginsFromAncestorsOf(window);\n\
</script>\
');

View File

@ -34,3 +34,37 @@ exports.update = function (cb) {
callback();
});
};
function adoptPlugins(plugins) {
var keys = [
'loaded', 'plugins', 'parts', 'hooks', 'baseURL', 'ensure', 'update'];
for (var i = 0, ii = keys.length; i < ii; i++) {
var key = keys[i];
exports[key] = plugins[key];
}
}
function adoptPluginsFromAncestorsOf(frame) {
// Bind plugins with parent;
var parentRequire = null;
try {
while (frame = frame.parent) {
if (typeof (frame.require) !== "undefined") {
parentRequire = frame.require;
break;
}
}
} catch (error) {
// Silence (this can only be a XDomain issue).
}
if (parentRequire) {
var ancestorPlugins = parentRequire("ep_etherpad-lite/static/js/pluginfw/client_plugins");
exports.adoptPlugins(ancestorPlugins);
} else {
throw new Error("Parent plugins could not be found.")
}
}
exports.adoptPlugins = adoptPlugins;
exports.adoptPluginsFromAncestorsOf = adoptPluginsFromAncestorsOf;

View File

@ -1,39 +0,0 @@
/**
* This module allows passing require modules instances to
* embedded iframes in a page.
* For example, if a page has the "plugins" module initialized,
* it is important to use exactly the same "plugins" instance
* inside iframes as well. Otherwise, plugins cannot save any
* state.
*/
/**
* Instructs the require object that when a reqModuleName module
* needs to be loaded, that it iterates through the parents of the
* current window until it finds one who can execute "require"
* statements and asks it to perform require on reqModuleName.
*
* @params requireDefObj Require object which supports define
* statements. This object is accessible after loading require-kernel.
* @params reqModuleName Module name e.g. (ep_etherpad-lite/static/js/plugins)
*/
exports.getRequirementFromParent = function(requireDefObj, reqModuleName) {
// Force the 'undefinition' of the modules (if they already have been loaded).
delete (requireDefObj._definitions)[reqModuleName];
delete (requireDefObj._modules)[reqModuleName];
requireDefObj.define(reqModuleName, function(require, exports, module) {
var t = parent;
var max = 0; // make sure I don't go up more than 10 times
while (typeof(t) != "undefined") {
max++;
if (max==10)
break;
if (typeof(t.require) != "undefined") {
module.exports = t.require(reqModuleName);
return;
}
t = t.parent;
}
});
}