From 3fb62956b9c004ebf412382d933b2c78e87fd545 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sun, 5 Feb 2012 11:13:43 -0800 Subject: [PATCH 1/4] Plugins are injected and no longer exist in the global scope. --- static/js/ace.js | 9 +++++++++ static/js/contentcollector.js | 17 ++--------------- static/js/domline.js | 17 ++--------------- static/js/domline_client.js | 17 ++--------------- static/js/linestylefilter.js | 27 +++------------------------ static/js/linestylefilter_client.js | 27 +++------------------------ static/js/plugins.js | 2 +- 7 files changed, 22 insertions(+), 94 deletions(-) diff --git a/static/js/ace.js b/static/js/ace.js index 6854b1142..9efb5d74a 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -337,6 +337,15 @@ function Ace2Editor() $$INCLUDE_JS("../static/js/domline.js"); $$INCLUDE_JS("../static/js/ace2_inner.js"); pushRequireScriptTo(iframeHTML); + // Inject my plugins into my child. + iframeHTML.push('\ +\ +'); pushScriptTagsFor(iframeHTML, includedJS); iframeHTML.push(''); diff --git a/static/js/contentcollector.js b/static/js/contentcollector.js index fd90a07be..0437ccd7b 100644 --- a/static/js/contentcollector.js +++ b/static/js/contentcollector.js @@ -26,12 +26,7 @@ var _MAX_LIST_LEVEL = 8; var Changeset = require('/easysync2').Changeset -var plugins = undefined; -try { - plugins = require('/plugins').plugins; -} catch (e) { - // silence -} +var plugins = require('/plugins').plugins; function sanitizeUnicode(s) { @@ -42,15 +37,7 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class { browser = browser || {}; - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; var dom = domInterface || { isNodeText: function(n) diff --git a/static/js/domline.js b/static/js/domline.js index 5de33c5aa..d89eeb464 100644 --- a/static/js/domline.js +++ b/static/js/domline.js @@ -26,12 +26,7 @@ // requires: plugins // requires: undefined -var plugins = undefined; -try { - plugins = require('/plugins').plugins; -} catch (e) { - // silence -} +var plugins = require('/plugins').plugins; var domline = {}; domline.noop = function() @@ -148,15 +143,7 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument) var extraOpenTags = ""; var extraCloseTags = ""; - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; plugins_.callHook("aceCreateDomLine", { domline: domline, diff --git a/static/js/domline_client.js b/static/js/domline_client.js index b999e3589..095307141 100644 --- a/static/js/domline_client.js +++ b/static/js/domline_client.js @@ -25,12 +25,7 @@ // requires: plugins // requires: undefined -var plugins = undefined; -try { - plugins = require('/plugins').plugins; -} catch (e) { - // silence -} +var plugins = require('/plugins').plugins; var domline = {}; domline.noop = function() @@ -147,15 +142,7 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument) var extraOpenTags = ""; var extraCloseTags = ""; - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; plugins_.callHook("aceCreateDomLine", { domline: domline, diff --git a/static/js/linestylefilter.js b/static/js/linestylefilter.js index fa1b40dee..6bb09f07b 100644 --- a/static/js/linestylefilter.js +++ b/static/js/linestylefilter.js @@ -29,12 +29,7 @@ // requires: undefined var Changeset = require('/easysync2').Changeset -var plugins = undefined; -try { - plugins = require('/plugins').plugins; -} catch (e) { - // silence -} +var plugins = require('/plugins').plugins; var linestylefilter = {}; @@ -59,15 +54,7 @@ linestylefilter.getAuthorClassName = function(author) linestylefilter.getLineStyleFilter = function(lineLength, aline, textAndClassFunc, apool) { - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; if (lineLength == 0) return textAndClassFunc; @@ -312,15 +299,7 @@ linestylefilter.getFilterStack = function(lineText, textAndClassFunc, browser) { var func = linestylefilter.getURLFilter(lineText, textAndClassFunc); - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; var hookFilters = plugins_.callHook("aceGetFilterStack", { linestylefilter: linestylefilter, diff --git a/static/js/linestylefilter_client.js b/static/js/linestylefilter_client.js index 7ff5bef41..8f1a6bafd 100644 --- a/static/js/linestylefilter_client.js +++ b/static/js/linestylefilter_client.js @@ -27,12 +27,7 @@ // requires: undefined var Changeset = require('/easysync2_client').Changeset -var plugins = undefined; -try { - plugins = require('/plugins').plugins; -} catch (e) { - // silence -} +var plugins = require('/plugins').plugins; var linestylefilter = {}; @@ -57,15 +52,7 @@ linestylefilter.getAuthorClassName = function(author) linestylefilter.getLineStyleFilter = function(lineLength, aline, textAndClassFunc, apool) { - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; if (lineLength == 0) return textAndClassFunc; @@ -310,15 +297,7 @@ linestylefilter.getFilterStack = function(lineText, textAndClassFunc, browser) { var func = linestylefilter.getURLFilter(lineText, textAndClassFunc); - var plugins_; - if (typeof(plugins) != 'undefined') - { - plugins_ = plugins; - } - else - { - plugins_ = parent.parent.plugins; - } + var plugins_ = plugins; var hookFilters = plugins_.callHook("aceGetFilterStack", { linestylefilter: linestylefilter, diff --git a/static/js/plugins.js b/static/js/plugins.js index ce3ec9bd7..963f055cf 100644 --- a/static/js/plugins.js +++ b/static/js/plugins.js @@ -4,7 +4,7 @@ * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED */ -plugins = { +var plugins = { callHook: function(hookName, args) { var global = (function () {return this}()); From b1d871d0fddbd08e627ffdfc0d559eb6d370d90f Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sat, 28 Jan 2012 17:05:29 -0800 Subject: [PATCH 2/4] Defer socket setup until initialization and on document ready. Code at the top level of a module should be related to definition only. Things that execute code should be inside of initialization methods. All evidence suggests that the binding to `window.onload` was arbitrary, and that this can be done as soon as the document is ready with no ill-effects. --- static/js/collab_client.js | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/static/js/collab_client.js b/static/js/collab_client.js index b697d4853..ec575fe0a 100644 --- a/static/js/collab_client.js +++ b/static/js/collab_client.js @@ -20,11 +20,6 @@ * limitations under the License. */ -$(window).bind("load", function() -{ - getCollabClient.windowLoaded = true; -}); - var chat = require('/chat').chat; // Dependency fill on init. This exists for `pad.socket` only. @@ -268,19 +263,6 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) }*/ } - function setUpSocketWhenWindowLoaded() - { - if (getCollabClient.windowLoaded) - { - setUpSocket(); - } - else - { - setTimeout(setUpSocketWhenWindowLoaded, 200); - } - } - setTimeout(setUpSocketWhenWindowLoaded, 0); - var hiccupCount = 0; function handleCometHiccup(params) @@ -654,8 +636,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) }, 0); } - var self; - return (self = { + var self = { setOnUserJoin: function(cb) { callbacks.onUserJoin = cb; @@ -698,7 +679,10 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) callWhenNotCommitting: callWhenNotCommitting, addHistoricalAuthors: tellAceAboutHistoricalAuthors, setChannelState: setChannelState - }); + }; + + $(document).ready(setUpSocket); + return self; } function selectElementContents(elem) From f010f3ae9d2ff12be4ff666a1d44756dbe521268 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sun, 22 Jan 2012 16:42:07 -0800 Subject: [PATCH 3/4] Remove redundant line. --- node/server.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/node/server.js b/node/server.js index 68b7ab6ab..a6a57497a 100644 --- a/node/server.js +++ b/node/server.js @@ -141,9 +141,6 @@ async.waterfall([ gracefulShutdown(); }); - //serve minified files - app.get('/minified/:filename', minify.minifyJS); - //serve static files app.get('/static/js/require-kernel.js', function (req, res, next) { res.header("Content-Type","application/javascript; charset: utf-8"); From 3678625c7c5f944edccacb0f5b7f63eb164c649f Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Tue, 31 Jan 2012 21:20:33 -0800 Subject: [PATCH 4/4] Remove dependencies on native `map`. --- static/js/ace.js | 13 ------------- static/js/domline.js | 5 +++-- static/js/domline_client.js | 5 +++-- static/js/linestylefilter.js | 3 ++- static/js/linestylefilter_client.js | 3 ++- static/js/plugins.js | 10 ++++++---- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/static/js/ace.js b/static/js/ace.js index 9efb5d74a..4cad6b4f7 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -371,19 +371,6 @@ function Ace2Editor() // (throbs busy while typing) outerHTML.push('', '\x3cscript>\n', outerScript.replace(/<\//g, '<\\/'), '\n\x3c/script>', '
x
'); - if (!Array.prototype.map) Array.prototype.map = function(fun) - { //needed for IE - if (typeof fun != "function") throw new TypeError(); - var len = this.length; - var res = new Array(len); - var thisp = arguments[1]; - for (var i = 0; i < len; i++) - { - if (i in this) res[i] = fun.call(thisp, this[i], i, this); - } - return res; - }; - var outerFrame = document.createElement("IFRAME"); outerFrame.frameBorder = 0; // for IE info.frame = outerFrame; diff --git a/static/js/domline.js b/static/js/domline.js index d89eeb464..8d8c2ea9e 100644 --- a/static/js/domline.js +++ b/static/js/domline.js @@ -27,6 +27,7 @@ // requires: undefined var plugins = require('/plugins').plugins; +var map = require('/ace2_common').map; var domline = {}; domline.noop = function() @@ -145,10 +146,10 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument) var plugins_ = plugins; - plugins_.callHook("aceCreateDomLine", { + map(plugins_.callHook("aceCreateDomLine", { domline: domline, cls: cls - }).map(function(modifier) + }), function(modifier) { cls = modifier.cls; extraOpenTags = extraOpenTags + modifier.extraOpenTags; diff --git a/static/js/domline_client.js b/static/js/domline_client.js index 095307141..87b6ed558 100644 --- a/static/js/domline_client.js +++ b/static/js/domline_client.js @@ -26,6 +26,7 @@ // requires: undefined var plugins = require('/plugins').plugins; +var map = require('/ace2_common').map; var domline = {}; domline.noop = function() @@ -144,10 +145,10 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument) var plugins_ = plugins; - plugins_.callHook("aceCreateDomLine", { + map(plugins_.callHook("aceCreateDomLine", { domline: domline, cls: cls - }).map(function(modifier) + }), function(modifier) { cls = modifier.cls; extraOpenTags = extraOpenTags + modifier.extraOpenTags; diff --git a/static/js/linestylefilter.js b/static/js/linestylefilter.js index 6bb09f07b..d0b5bc6e8 100644 --- a/static/js/linestylefilter.js +++ b/static/js/linestylefilter.js @@ -30,6 +30,7 @@ var Changeset = require('/easysync2').Changeset var plugins = require('/plugins').plugins; +var map = require('/ace2_common').map; var linestylefilter = {}; @@ -305,7 +306,7 @@ linestylefilter.getFilterStack = function(lineText, textAndClassFunc, browser) linestylefilter: linestylefilter, browser: browser }); - hookFilters.map(function(hookFilter) + map(hookFilters, function(hookFilter) { func = hookFilter(lineText, func); }); diff --git a/static/js/linestylefilter_client.js b/static/js/linestylefilter_client.js index 8f1a6bafd..f057e21a2 100644 --- a/static/js/linestylefilter_client.js +++ b/static/js/linestylefilter_client.js @@ -28,6 +28,7 @@ var Changeset = require('/easysync2_client').Changeset var plugins = require('/plugins').plugins; +var map = require('/ace2_common').map; var linestylefilter = {}; @@ -303,7 +304,7 @@ linestylefilter.getFilterStack = function(lineText, textAndClassFunc, browser) linestylefilter: linestylefilter, browser: browser }); - hookFilters.map(function(hookFilter) + map(hookFilters, function(hookFilter) { func = hookFilter(lineText, func); }); diff --git a/static/js/plugins.js b/static/js/plugins.js index 963f055cf..3e226eba1 100644 --- a/static/js/plugins.js +++ b/static/js/plugins.js @@ -25,10 +25,12 @@ var plugins = { if (sep == undefined) sep = ''; if (pre == undefined) pre = ''; if (post == undefined) post = ''; - return plugins.callHook(hookName, args).map(function(x) - { - return pre + x + post - }).join(sep || ""); + var newCallhooks = []; + var callhooks = plugins.callHook(hookName, args); + for (var i = 0, ii = callhooks.length; i < ii; i++) { + newCallhooks[i] = pre + callhooks[i] + post; + } + return newCallhooks.join(sep || ""); } };