From 8031c31bc3dca9bba844b2757b69b8b6e0dd2a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bartelme=C3=9F?= Date: Sun, 19 Feb 2012 03:16:42 +0100 Subject: [PATCH] more compact code for exposed functions in ace2.js --- static/js/ace.js | 78 ++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 55 deletions(-) diff --git a/static/js/ace.js b/static/js/ace.js index 04930910..63367e3f 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -49,8 +49,7 @@ function Ace2Editor() { var that = this; var args = arguments; - - function action() + var action = function() { func.apply(that, args); } @@ -71,78 +70,47 @@ function Ace2Editor() function doActionsPendingInit() { - for (var i = 0; i < actionsPendingInit.length; i++) - { - actionsPendingInit[i](); - } + $.each(actionsPendingInit, function(i,fn){ + fn() + }); actionsPendingInit = []; } - + ace2.registry[info.id] = info; - editor.importText = pendingInit(function(newCode, undoable) - { - info.ace_importText(newCode, undoable); - }); - editor.importAText = pendingInit(function(newCode, apoolJsonObj, undoable) - { - info.ace_importAText(newCode, apoolJsonObj, undoable); + // The following functions (prefixed by 'ace_') are exposed by editor, but + // execution is delayed until init is complete + var aceFunctionsPendingInit = ['importText', 'importAText', 'focus', + 'setEditable', 'getFormattedCode', 'setOnKeyPress', 'setOnKeyDown', + 'setNotifyDirty', 'setProperty', 'setBaseText', 'setBaseAttributedText', + 'applyChangesToBase', 'applyPreparedChangesetToBase', + 'setUserChangeNotificationCallback', 'setAuthorInfo', + 'setAuthorSelectionRange', 'callWithAce', 'execCommand', 'replaceRange']; + + $.each(aceFunctionsPendingInit, function(i,fnName){ + var prefix = 'ace_'; + var name = prefix + fnName; + editor[fnName] = pendingInit(function(){ + info[prefix + fnName].apply(this, arguments); + }); }); + editor.exportText = function() { if (!loaded) return "(awaiting init)\n"; return info.ace_exportText(); }; + editor.getFrame = function() { return info.frame || null; }; - editor.focus = pendingInit(function() - { - info.ace_focus(); - }); - editor.setEditable = pendingInit(function(newVal) - { - info.ace_setEditable(newVal); - }); - editor.getFormattedCode = function() - { - return info.ace_getFormattedCode(); - }; - editor.setOnKeyPress = pendingInit(function(handler) - { - info.ace_setOnKeyPress(handler); - }); - editor.setOnKeyDown = pendingInit(function(handler) - { - info.ace_setOnKeyDown(handler); - }); - editor.setNotifyDirty = pendingInit(function(handler) - { - info.ace_setNotifyDirty(handler); - }); - - editor.setProperty = pendingInit(function(key, value) - { - info.ace_setProperty(key, value); - }); + editor.getDebugProperty = function(prop) { return info.ace_getDebugProperty(prop); }; - editor.setBaseText = pendingInit(function(txt) - { - info.ace_setBaseText(txt); - }); - editor.setBaseAttributedText = pendingInit(function(atxt, apoolJsonObj) - { - info.ace_setBaseAttributedText(atxt, apoolJsonObj); - }); - editor.applyChangesToBase = pendingInit(function(changes, optAuthor, apoolJsonObj) - { - info.ace_applyChangesToBase(changes, optAuthor, apoolJsonObj); - }); // prepareUserChangeset: // Returns null if no new changes or ACE not ready. Otherwise, bundles up all user changes // to the latest base text into a Changeset, which is returned (as a string if encodeAsString).