Skip to content
Snippets Groups Projects
Commit 5a05da2a authored by Robin Appelman's avatar Robin Appelman
Browse files

Some common javascript functions for working with arrays and functions

the callBack class becomes obsolete with these but all places where it is used are updates
parent 7710dc73
No related branches found
No related tags found
No related merge requests found
......@@ -20,11 +20,10 @@
*/
//The callBack object provides an easy way to pass a member of an object as callback parameter and makes sure that the 'this' is always set correctly when called.
//bindScope provides a much cleaner sollution but we keep this one for compatibility and instead implement is with bindScope
callBack=function(func,obj){
this.id=callBack.callBacks.length;
callBack.callBacks[this.id]=this;
this.func=func;
this.obj=obj;
var newFunction=func.bindScope(obj);
callBack.callBacks[this.id]=newFunction;
}
callBack.callBacks=Array();
......@@ -36,18 +35,6 @@ callBack.call=function(id,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
}
}
callBack.prototype=function(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
return this.call(false,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.func=false;
callBack.prototype.obj=false;
callBack.prototype.call=function(dummy,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
return this.func.call(this.obj,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.apply=function(dummy,arguments){
return this.func.apply(this.obj,arguments);
}
//provide a simple way to add things to the onload
OC_onload=new Object();
......@@ -198,4 +185,49 @@ loadScript=function(url){//dynamicly load javascript files
script.setAttribute('src',url);
body=document.getElementsByTagName('body').item(0);
body.appendChild(script);
}
\ No newline at end of file
}
Function.prototype.bindScope=function(obj){
var o=obj;
var fn=this;
return function(){
return fn.apply(o,arguments);
}
}
Function.prototype.bind=function(){
var args = [];
var fn=this;
for (var n = 0; n < arguments.length; n++){
args.push(arguments[n]);
}
return function (){
var myargs = [];
for (var m = 0; m < arguments.length; m++){
myargs.push(arguments[m]);
}
return fn.apply(this, args.concat(myargs));
};
}
Array.prototype.foreach=function(func,that){
if (!func) return;
that=that||this;
var returns=[];
for(var i=0;i<this.length;i++){
returns.push(func.call(that,this[i]));
}
return returns;
}
Array.prototype.where = function(func,that) {
var found = [];
that=that||this;
for(var i = 0, l = this.length; i < l; ++i) {
var item = this[i];
if(func.call(that,item)){
found.push(item);
}
}
return found;
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment