From 0bb035a1abc37fbf033a18ac53dd62ec85599a49 Mon Sep 17 00:00:00 2001 From: John Resig Date: Mon, 9 Jul 2007 16:14:19 +0000 Subject: [PATCH] Added support for events, made window.location Async (and based upon XHR), added trigger for window.onload. --- build/runtest/env.js | 114 ++++++++++++++++++++++++++++++++++++++++++------- build/runtest/test.js | 31 ++++++++------ 2 files changed, 115 insertions(+), 30 deletions(-) diff --git a/build/runtest/env.js b/build/runtest/env.js index b2a94ff..febc08a 100644 --- a/build/runtest/env.js +++ b/build/runtest/env.js @@ -20,18 +20,29 @@ var window = this; var curLocation = (new java.io.File("./")).toURL(); window.__defineSetter__("location", function(url){ - curLocation = new java.net.URL( curLocation, url ); - - window.document = new DOMDocument( - new Packages.org.xml.sax.InputSource( - new java.io.InputStreamReader( - new java.io.FileInputStream( url )))); + var xhr = new XMLHttpRequest(); + xhr.open("GET", url); + xhr.onreadystatechange = function(){ + curLocation = new java.net.URL( curLocation, url ); + window.document = xhr.responseXML; + + var event = document.createEvent(); + event.initEvent("load"); + window.dispatchEvent( event ); + }; + xhr.send(); }); window.__defineGetter__("location", function(url){ return { get protocol(){ return curLocation.getProtocol() + ":"; + }, + get href(){ + return curLocation.toString(); + }, + toString: function(){ + return this.href; } }; }); @@ -73,9 +84,51 @@ var window = this; }; // Window Events + + var events = [{}]; - window.addEventListener = function(){}; - 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 @@ -121,8 +174,9 @@ var window = this; get ownerDocument(){ return null; }, - addEventListener: function(){}, - removeEventListener: function(){}, + addEventListener: window.addEventListener, + removeEventListener: window.removeEventListener, + dispatchEvent: window.dispatchEvent, get nodeName() { 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, - addEventListener: function(){}, - removeEventListener: function(){}, - click: function(){}, - submit: function(){}, - focus: function(){}, - blur: function(){}, + + addEventListener: window.addEventListener, + removeEventListener: window.removeEventListener, + dispatchEvent: window.dispatchEvent, + + 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(){ return this.getElementsByTagName("*"); }, diff --git a/build/runtest/test.js b/build/runtest/test.js index 1bc9ac1..00b85e4 100644 --- a/build/runtest/test.js +++ b/build/runtest/test.js @@ -1,18 +1,21 @@ // Init load("build/runtest/env.js"); -window.location = "test/index.html"; - -// Load the test runner -load("dist/jquery.js","build/runtest/testrunner.js"); -// Load the tests -load( - "src/jquery/coreTest.js", - "src/selector/selectorTest.js", - "src/event/eventTest.js", - "src/fx/fxTest.js" - //"src/ajax/ajaxTest.js" -); +window.location = "test/index.html"; -// Display the results -results(); +window.onload = function(){ + // Load the test runner + load("dist/jquery.js","build/runtest/testrunner.js"); + + // Load the tests + load( + "src/jquery/coreTest.js", + "src/selector/selectorTest.js", + "src/event/eventTest.js", + "src/fx/fxTest.js" + //"src/ajax/ajaxTest.js" + ); + + // Display the results + results(); +}; \ No newline at end of file -- 1.7.10.4