[js] Old JS libraries don't play nice with other libraries on the page
[commit: http://hg.dwscoalition.org/js/rev/cdb7bebfacb1]
http://bugs.dwscoalition.org/show_bug.cgi?id=3531
Play nice: stop extending Object.prototype, so we don't break "for ( var i
in...)" loops.
Patch by
fu.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3531
Play nice: stop extending Object.prototype, so we don't break "for ( var i
in...)" loops.
Patch by
Files modified:
- checkallbutton.js
- core.js
- datasource.js
- dom.js
- json.js
- selectable_table.js
- view.js
--------------------------------------------------------------------------------
diff -r 94d9f670a934 -r cdb7bebfacb1 checkallbutton.js
--- a/checkallbutton.js Mon Feb 28 14:10:05 2011 +0800
+++ b/checkallbutton.js Mon Feb 28 20:30:17 2011 +0800
@@ -14,7 +14,9 @@ CheckallButton = new Class(Object, {
// button => the "check all" button element
// parent => [optional] only check boxes that are children of this element
init: function (opts) {
- CheckallButton.superClass.init.apply(arguments);
+ if ( CheckallButton.superClass.init ) {
+ CheckallButton.superClass.init.apply(arguments);
+ }
this.button = opts["button"];
this.className = opts["class"];
diff -r 94d9f670a934 -r cdb7bebfacb1 core.js
--- a/core.js Mon Feb 28 14:10:05 2011 +0800
+++ b/core.js Mon Feb 28 20:30:17 2011 +0800
@@ -185,47 +185,43 @@ if( !Object.prototype.hasOwnProperty ) {
}
-Object.prototype.extend = function() {
+if ( ! defined ( window.OBJ ) )
+ OBJ = {};
+
+OBJ.extend = function(obj_this) {
var a = arguments;
for( var i = 0; i < a.length; i++ ) {
var o = a[ i ];
for( var p in o ) {
try {
- if( !this[ p ] &&
+ if( !obj_this[ p ] &&
(!o.hasOwnProperty || o.hasOwnProperty( p )) )
- this[ p ] = o[ p ];
+ obj_this[ p ] = o[ p ];
} catch( e ) {}
}
}
- return this;
+ return obj_this;
}
-Object.prototype.override = function() {
+OBJ.override = function(obj_this) {
var a = arguments;
for( var i = 0; i < a.length; i++ ) {
var o = a[ i ];
for( var p in o ) {
try {
if( !o.hasOwnProperty || o.hasOwnProperty( p ) )
- this[ p ] = o[ p ];
+ obj_this[ p ] = o[ p ];
} catch( e ) {}
}
}
- return this;
+ return obj_this;
}
-
-
-Object.prototype.extend( {
- init: Function.stub,
- destroy: Function.stub
-} );
-
/* function extensions */
-Function.prototype.extend( {
+OBJ.extend( Function.prototype, {
bind: function( object ) {
var method = this;
return function() {
@@ -255,36 +251,37 @@ Class = function( superClass ) {
// Set the constructor:
var constructor = function() {
- if( arguments.length )
+ if( arguments.length && this.init )
this.init.apply( this, arguments );
};
// -- Accomplish static-inheritance:
- constructor.override( Class ); // inherit static methods from Class
+ OBJ.override( constructor,Class ); // inherit static methods from Class
superClass = superClass || Object;
- constructor.override( superClass ); // inherit static methods from the superClass
+ OBJ.override(constructor, superClass ); // inherit static methods from the superClass
constructor.superClass = superClass.prototype;
// Set the constructor's prototype (accomplish object-inheritance):
constructor.prototype = new superClass();
constructor.prototype.constructor = constructor; // rev. 0.7
// -- extend prototype with Class instance methods
- constructor.prototype.extend( Class.prototype );
+ OBJ.extend(constructor.prototype, Class.prototype );
// -- override prototype with interface methods
for( var i = 1; i < arguments.length; i++ )
- constructor.prototype.override( arguments[ i ] );
+ OBJ.override(constructor.prototype, arguments[ i ] );
return constructor;
}
-Class.extend( {
+OBJ.extend( Class, {
initSingleton: function() {
if( this.singleton )
return this.singleton;
this.singleton = this.singletonConstructor
? new this.singletonConstructor()
: new this();
- this.singleton.init.apply( this.singleton, arguments );
+ if ( this.singleton.init )
+ this.singleton.init.apply( this.singleton, arguments );
return this.singleton;
}
} );
@@ -364,7 +361,7 @@ Class.prototype = {
/* string extensions */
-String.extend( {
+OBJ.extend( String, {
escapeJSChar: function( c ) {
// try simple escaping
switch( c ) {
@@ -426,7 +423,7 @@ String.extend( {
} );
-String.prototype.extend( {
+OBJ.extend( String.prototype, {
escapeJS: function() {
return this.replace( /([^ -!#-\[\]-~])/g, function( m, c ) { return String.escapeJSChar( c ); } )
},
@@ -506,7 +503,7 @@ String.prototype.extend( {
/* extend array object */
-Array.extend( {
+OBJ.extend( Array, {
fromPseudo: function ( args ) {
var out = [];
for ( var i = 0; i < args.length; i++ )
@@ -518,7 +515,7 @@ Array.extend( {
/* extend array object */
-Array.prototype.extend( {
+OBJ.extend(Array.prototype, {
copy: function() {
var out = [];
for( var i = 0; i < this.length; i++ )
@@ -713,7 +710,7 @@ Array.prototype.extend( {
/* date extensions */
-Date.extend( {
+OBJ.extend(Date, {
/* iso 8601 date format parser
this was fun to write...
thanks to: http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
@@ -763,7 +760,7 @@ Date.extend( {
} );
-Date.prototype.extend( {
+OBJ.extend(Date.prototype, {
getISOTimezoneOffset: function() {
var offset = -this.getTimezoneOffset();
var negative = false;
diff -r 94d9f670a934 -r cdb7bebfacb1 datasource.js
--- a/datasource.js Mon Feb 28 14:10:05 2011 +0800
+++ b/datasource.js Mon Feb 28 20:30:17 2011 +0800
@@ -4,7 +4,8 @@ DataSource = new Class(Object, {
DataSource = new Class(Object, {
init: function (initialData) {
- DataSource.superClass.init.apply(this, arguments);
+ if ( DataSource.superClass.init )
+ DataSource.superClass.init.apply(this, arguments);
this.watchers = [];
this.theData = defined(initialData) ? initialData : [];
this.sortField = "";
diff -r 94d9f670a934 -r cdb7bebfacb1 dom.js
--- a/dom.js Mon Feb 28 14:10:05 2011 +0800
+++ b/dom.js Mon Feb 28 20:30:17 2011 +0800
@@ -42,7 +42,7 @@ if( !defined( window.Node ) )
Node = {};
try {
- Node.extend( {
+ OBJ.extend( Node, {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
@@ -60,7 +60,7 @@ if( !defined( window.DOM ) )
DOM = {};
-DOM.extend( {
+OBJ.extend( DOM, {
getElement: function( e ) {
return (typeof e == "string" || typeof e == "number") ? document.getElementById( e ) : e;
},
diff -r 94d9f670a934 -r cdb7bebfacb1 json.js
--- a/json.js Mon Feb 28 14:10:05 2011 +0800
+++ b/json.js Mon Feb 28 20:30:17 2011 +0800
@@ -103,29 +103,29 @@ Array.prototype.toJSON = function( root
}
-Object.prototype.toJSON = function( root ) {
+OBJ.toJSON = function( obj_this, root ) {
// crude recursion detection
if( !root )
- root = this;
- else if( root == this )
+ root = obj_this;
+ else if( root == obj_this )
return "{}";
var out = [ "{" ];
- for( var i in this ) {
- if( typeof this[ i ] == "undefined" ||
- (this.hasOwnProperty && !this.hasOwnProperty( i )) )
+ for( var i in obj_this ) {
+ if( typeof obj_this[ i ] == "undefined" ||
+ (obj_this.hasOwnProperty && !obj_this.hasOwnProperty( i )) )
continue;
if( out.length > 1 )
out.push( "," );
- out.push( i.toJSON() );
+ out.push( OBJ.toJSON(i) );
if( this[ i ] == null )
out.push( ":null" );
- else if( typeof this[ i ] == "function" )
+ else if( typeof obj_this[ i ] == "function" )
continue;
- else if( !this[ i ].toJSON )
+ else if( !OBJ.toJSON(obj_this[ i ]) )
out.push( ":{}" );
else
- out.push( ":", this[ i ].toJSON( root ) );
+ out.push( ":", OBJ.toJSON(this[ i ], root) );
}
out.push( "}" );
return out.join( "" );
diff -r 94d9f670a934 -r cdb7bebfacb1 selectable_table.js
--- a/selectable_table.js Mon Feb 28 14:10:05 2011 +0800
+++ b/selectable_table.js Mon Feb 28 20:30:17 2011 +0800
@@ -19,7 +19,8 @@ SelectableTable = new Class(DataSource,
// you can specify the class of your checkboxes to make them stay in sync
// selectableItem: What type of elements can be selected. Values are "cell" or "row"
init: function (opts) {
- SelectableTable.superClass.init.apply(this, []);
+ if ( SelectableTable.superClass.init )
+ SelectableTable.superClass.init.apply(this, []);
var table = opts.table;
var selectableClass = opts.selectableClass;
diff -r 94d9f670a934 -r cdb7bebfacb1 view.js
--- a/view.js Mon Feb 28 14:10:05 2011 +0800
+++ b/view.js Mon Feb 28 20:30:17 2011 +0800
@@ -4,7 +4,8 @@ View = new Class(Object, {
View = new Class(Object, {
init: function (opts) {
- View.superClass.init.apply(this, arguments);
+ if ( View.superClass.init )
+ View.superClass.init.apply(this, arguments);
this.watchers = [];
this.datasource = opts.datasource;
this.view = opts.view;
--------------------------------------------------------------------------------
