Added support for events, made window.location Async (and based upon XHR), added trigger for window.onload.

This commit is contained in:
John Resig 2007-07-09 16:14:19 +00:00
parent d776dc9d5c
commit 0bb035a1ab
2 changed files with 115 additions and 30 deletions

View file

@ -20,18 +20,29 @@ var window = this;
var curLocation = (new java.io.File("./")).toURL(); var curLocation = (new java.io.File("./")).toURL();
window.__defineSetter__("location", function(url){ window.__defineSetter__("location", function(url){
curLocation = new java.net.URL( curLocation, url ); var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = function(){
curLocation = new java.net.URL( curLocation, url );
window.document = xhr.responseXML;
window.document = new DOMDocument( var event = document.createEvent();
new Packages.org.xml.sax.InputSource( event.initEvent("load");
new java.io.InputStreamReader( window.dispatchEvent( event );
new java.io.FileInputStream( url )))); };
xhr.send();
}); });
window.__defineGetter__("location", function(url){ window.__defineGetter__("location", function(url){
return { return {
get protocol(){ get protocol(){
return curLocation.getProtocol() + ":"; return curLocation.getProtocol() + ":";
},
get href(){
return curLocation.toString();
},
toString: function(){
return this.href;
} }
}; };
}); });
@ -74,8 +85,50 @@ var window = this;
// Window Events // Window Events
window.addEventListener = function(){}; var events = [{}];
window.removeEventListener = function(){};
window.addEventListener = function(type, fn){
if ( !this.uuid || this == window ) {
this.uuid = events.length;
events[this.uuid] = {};
}
if ( !events[this.uuid][type] )
events[this.uuid][type] = [];
if ( events[this.uuid][type].indexOf( fn ) < 0 )
events[this.uuid][type].push( fn );
};
window.removeEventListener = function(type, fn){
if ( !this.uuid || this == window ) {
this.uuid = events.length;
events[this.uuid] = {};
}
if ( !events[this.uuid][type] )
events[this.uuid][type] = [];
events[this.uuid][type] =
events[this.uuid][type].filter(function(f){
return f != fn;
});
};
window.dispatchEvent = function(event){
if ( event.type ) {
if ( this.uuid && events[this.uuid][event.type] ) {
var self = this;
events[this.uuid][event.type].forEach(function(fn){
fn.call( self, event );
});
}
if ( this["on" + event.type] )
this["on" + event.type].call( self, event );
}
};
// DOM Document // DOM Document
@ -121,8 +174,9 @@ var window = this;
get ownerDocument(){ get ownerDocument(){
return null; return null;
}, },
addEventListener: function(){}, addEventListener: window.addEventListener,
removeEventListener: function(){}, removeEventListener: window.removeEventListener,
dispatchEvent: window.dispatchEvent,
get nodeName() { get nodeName() {
return "#document"; return "#document";
}, },
@ -152,6 +206,15 @@ var window = this;
}; };
} }
}; };
},
createEvent: function(){
return {
type: "",
initEvent: function(type){
this.type = type;
}
};
} }
}; };
@ -411,12 +474,31 @@ var window = this;
}, },
getElementsByTagName: DOMDocument.prototype.getElementsByTagName, getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
addEventListener: function(){},
removeEventListener: function(){}, addEventListener: window.addEventListener,
click: function(){}, removeEventListener: window.removeEventListener,
submit: function(){}, dispatchEvent: window.dispatchEvent,
focus: function(){},
blur: function(){}, click: function(){
var event = document.createEvent();
event.initEvent("click");
this.dispatchEvent(event);
},
submit: function(){
var event = document.createEvent();
event.initEvent("submit");
this.dispatchEvent(event);
},
focus: function(){
var event = document.createEvent();
event.initEvent("focus");
this.dispatchEvent(event);
},
blur: function(){
var event = document.createEvent();
event.initEvent("blur");
this.dispatchEvent(event);
},
get elements(){ get elements(){
return this.getElementsByTagName("*"); return this.getElementsByTagName("*");
}, },

View file

@ -1,18 +1,21 @@
// Init // Init
load("build/runtest/env.js"); load("build/runtest/env.js");
window.location = "test/index.html"; window.location = "test/index.html";
// Load the test runner window.onload = function(){
load("dist/jquery.js","build/runtest/testrunner.js"); // Load the test runner
load("dist/jquery.js","build/runtest/testrunner.js");
// Load the tests // Load the tests
load( load(
"src/jquery/coreTest.js", "src/jquery/coreTest.js",
"src/selector/selectorTest.js", "src/selector/selectorTest.js",
"src/event/eventTest.js", "src/event/eventTest.js",
"src/fx/fxTest.js" "src/fx/fxTest.js"
//"src/ajax/ajaxTest.js" //"src/ajax/ajaxTest.js"
); );
// Display the results // Display the results
results(); results();
};