map / forEach use native impl, if available. ace2_common extends the prototype of Array, if not

pull/556/head
Matthias Bartelmeß 2012-03-13 21:10:10 +01:00
parent cccd8a923c
commit b28bfe8e31
4 changed files with 44 additions and 60 deletions

View File

@ -29,8 +29,7 @@ function isNodeText(node)
function object(o)
{
var f = function()
{};
var f = function(){};
f.prototype = o;
return new f();
}
@ -44,37 +43,32 @@ function extend(obj, props)
return obj;
}
function forEach(array, func)
{
for (var i = 0; i < array.length; i++)
var forEachImpl = function(fn){
for (var i = 0; i < this.length; i++)
{
var result = func(array[i], i);
if (result) break;
var result = func(this[i], i, this);
}
}
function map(array, func)
function mapImpl(fn)
{
var result = [];
// must remain compatible with "arguments" pseudo-array
for (var i = 0; i < array.length; i++)
for (var i = 0; i < this.length; i++)
{
if (func) result.push(func(array[i], i));
else result.push(array[i]);
if (fn) result.push(fn(this[i], i, this));
else result.push(this[i]);
}
return result;
}
function filter(array, func)
{
var result = [];
// must remain compatible with "arguments" pseudo-array
for (var i = 0; i < array.length; i++)
{
if (func(array[i], i)) result.push(array[i]);
}
return result;
}
Array.prototype.forEach = Array.prototype.forEach || forEachImpl;
Array.prototype.each = Array.prototype.each || forEachImpl;
Array.prototype.map = Array.prototype.map || mapImpl;
function isArray(testObject)
{
@ -147,9 +141,6 @@ var identity = function(x){return x};
exports.isNodeText = isNodeText;
exports.object = object;
exports.extend = extend;
exports.forEach = forEach;
exports.map = map;
exports.filter = filter;
exports.isArray = isArray;
exports.browser = browser;
exports.getAssoc = getAssoc;
@ -157,6 +148,5 @@ exports.setAssoc = setAssoc;
exports.binarySearch = binarySearch;
exports.binarySearchInfinite = binarySearchInfinite;
exports.htmlPrettyEscape = htmlPrettyEscape;
exports.map = map;
exports.noop = noop;
exports.identity = identity;

View File

@ -26,16 +26,14 @@ var Ace2Common = require('./ace2_common');
var isNodeText = Ace2Common.isNodeText;
var object = Ace2Common.object;
var extend = Ace2Common.extend;
var forEach = Ace2Common.forEach;
var map = Ace2Common.map;
var filter = Ace2Common.filter;
var isArray = Ace2Common.isArray;
var browser = Ace2Common.browser;
var getAssoc = Ace2Common.getAssoc;
var setAssoc = Ace2Common.setAssoc;
var binarySearchInfinite = Ace2Common.binarySearchInfinite;
var htmlPrettyEscape = Ace2Common.htmlPrettyEscape;
var map = Ace2Common.map;
var noop = Ace2Common.noop;
var makeChangesetTracker = require('./changesettracker').makeChangesetTracker;
@ -684,7 +682,7 @@ function Ace2Inner(){
}
else
{
lines = map(text.split('\n'), textify);
lines = text.split('\n').map(textify);
}
var newText = "\n";
if (lines.length > 0)
@ -1622,8 +1620,7 @@ function Ace2Inner(){
}
//var fragment = magicdom.wrapDom(document.createDocumentFragment());
domInsertsNeeded.push([nodeToAddAfter, lineNodeInfos]);
forEach(dirtyNodes, function(n)
{
dirtyNodes.forEach(function(n){
toDeleteAtEnd.push(n);
});
var spliceHints = {};
@ -1655,14 +1652,14 @@ function Ace2Inner(){
//var isTimeUp = newTimeLimit(100);
// do DOM inserts
p.mark("insert");
forEach(domInsertsNeeded, function(ins)
domInsertsNeeded.forEach(function(ins)
{
insertDomLines(ins[0], ins[1], isTimeUp);
});
p.mark("del");
// delete old dom nodes
forEach(toDeleteAtEnd, function(n)
toDeleteAtEnd.forEach(function(n)
{
//var id = n.uniqueId();
// parent of n may not be "root" in IE due to non-tree-shaped DOM (wtf)
@ -1772,7 +1769,7 @@ function Ace2Inner(){
var charEnd = rep.lines.offsetOfEntry(endEntry) + endEntry.width;
//rep.lexer.lexCharRange([charStart, charEnd], isTimeUp);
forEach(infoStructs, function(info)
infoStructs.forEach(function(info)
{
var p2 = PROFILER("insertLine", false);
var node = info.node;
@ -2083,10 +2080,8 @@ function Ace2Inner(){
var linesMutatee = {
splice: function(start, numRemoved, newLinesVA)
{
domAndRepSplice(start, numRemoved, map(Array.prototype.slice.call(arguments, 2), function(s)
{
return s.slice(0, -1);
}), null);
var args = Array.prototype.slice.call(arguments, 2);
domAndRepSplice(start, numRemoved, args.map(function(s){ return s.slice(0, -1); }), null);
},
get: function(i)
{
@ -2098,7 +2093,7 @@ function Ace2Inner(){
},
slice_notused: function(start, end)
{
return map(rep.lines.slice(start, end), function(e)
return rep.lines.slice(start, end).map(function(e)
{
return e.text + '\n';
});
@ -2131,7 +2126,7 @@ function Ace2Inner(){
}
}
var lineEntries = map(newLineStrings, createDomLineEntry);
var lineEntries = newLineStrings.map(createDomLineEntry);
doRepLineSplice(startLine, deleteCount, lineEntries);
@ -2142,12 +2137,12 @@ function Ace2Inner(){
}
else nodeToAddAfter = null;
insertDomLines(nodeToAddAfter, map(lineEntries, function(entry)
insertDomLines(nodeToAddAfter, lineEntries.map(function(entry)
{
return entry.domInfo;
}), isTimeUp);
forEach(keysToDelete, function(k)
infoStructs.forEach(function(k)
{
var n = doc.getElementById(k);
n.parentNode.removeChild(n);
@ -2467,7 +2462,7 @@ function Ace2Inner(){
function doRepLineSplice(startLine, deleteCount, newLineEntries)
{
forEach(newLineEntries, function(entry)
newLineEntries.forEach(function(entry)
{
entry.width = entry.text.length + 1;
});
@ -2482,7 +2477,7 @@ function Ace2Inner(){
currentCallStack.repChanged = true;
var newRegionEnd = rep.lines.offsetOfIndex(startLine + newLineEntries.length);
var newText = map(newLineEntries, function(e)
var newText = newLineEntries.map(function(e)
{
return e.text + '\n';
}).join('');
@ -2512,7 +2507,7 @@ function Ace2Inner(){
selEndHintChar = rep.lines.offsetOfIndex(hints.selEnd[0]) + hints.selEnd[1] - oldRegionStart;
}
var newText = map(newLineEntries, function(e)
var newText = newLineEntries.map(function(e)
{
return e.text + '\n';
}).join('');
@ -3053,7 +3048,7 @@ function Ace2Inner(){
{
// returns index of cleanRange containing i, or -1 if none
var answer = -1;
forEach(cleanRanges, function(r, idx)
cleanRanges.forEach(function(r, idx)
{
if (i >= r[1]) return false; // keep looking
if (i < r[0]) return true; // not found, stop looking
@ -3846,7 +3841,7 @@ function Ace2Inner(){
function getRepHTML()
{
return map(rep.lines.slice(), function(entry)
return rep.lines.slice().map(function(entry)
{
var text = entry.text;
var content;
@ -4585,7 +4580,7 @@ function Ace2Inner(){
function teardown()
{
forEach(_teardownActions, function(a)
_teardownActions.forEach(function(a)
{
a();
});

View File

@ -28,9 +28,6 @@ var linestylefilter = require('./linestylefilter').linestylefilter;
var colorutils = require('./colorutils').colorutils;
var Ace2Common = require('./ace2_common');
var map = Ace2Common.map;
var forEach = Ace2Common.forEach;
// These parameters were global, now they are injected. A reference to the
// Timeslider controller would probably be more appropriate.
function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, BroadcastSlider)
@ -155,7 +152,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
// splice the lines
splice: function(start, numRemoved, newLinesVA)
{
var newLines = map(Array.prototype.slice.call(arguments, 2), function(s) {
var newLines = Array.prototype.slice.call(arguments, 2).map(function(s) {
return s;
});
@ -278,7 +275,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
debugLog('Time Delta: ', timeDelta)
updateTimer();
var authors = map(padContents.getActiveAuthors(), function(name)
var authors = padContents.getActiveAuthors().map(function(name)
{
return authorData[name];
});
@ -384,7 +381,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
changesetLoader.queueUp(start, 1, update);
}
var authors = map(padContents.getActiveAuthors(), function(name){
var authors = padContents.getActiveAuthors().map(function(name){
return authorData[name];
});
BroadcastSlider.setAuthors(authors);
@ -527,7 +524,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
authorMap[obj.author] = obj.data;
receiveAuthorData(authorMap);
var authors = map(padContents.getActiveAuthors(),function(name) {
var authors = padContents.getActiveAuthors().map(function(name) {
return authorData[name];
});
@ -607,10 +604,13 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
setChannelState("DISCONNECTED", reason);
}
/// Since its not used, import 'forEach' has been dropped
/*window['onloadFuncts'] = [];
window.onload = function ()
{
window['isloaded'] = true;
forEach(window['onloadFuncts'],function (funct)
{
funct();

View File

@ -29,7 +29,6 @@
var Security = require('./security');
var hooks = require('./pluginfw/hooks');
var Ace2Common = require('./ace2_common');
var map = Ace2Common.map;
var noop = Ace2Common.noop;
var identity = Ace2Common.identity;
@ -142,10 +141,10 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
var extraOpenTags = "";
var extraCloseTags = "";
map(hooks.callAll("aceCreateDomLine", {
hooks.callAll("aceCreateDomLine", {
domline: domline,
cls: cls
}), function(modifier)
}).map(function(modifier)
{
cls = modifier.cls;
extraOpenTags = extraOpenTags + modifier.extraOpenTags;