Permalink
Browse files

Fixed up event dispatching… using custom event :(

  • Loading branch information...
1 parent 289ce24 commit 736f48de6956f2823bb1e69b3282ccf5dccc67c8 Marcos Caceres committed May 22, 2012
Showing with 66 additions and 81 deletions.
  1. +30 −35 DeviceProximityEvent.js
  2. +35 −45 UserProximityEvent.js
  3. +1 −1 index.html
View
@@ -38,18 +38,18 @@
},
//We can't use Event.call to inherit properly,
//so we have to have a private event instead :(
- event = new Event(String(type));
+ event;
//ECMAScript to WebIDL converters
converters.value = converters.min = converters.max = toDouble;
converters.cancelable = converters.bubbles = toBool;
//process eventInitDict if it was passed, overriding 'dict'
if (arguments.length === 2) {
- eventInitDict = Object(eventInitDict);
+ eventInitDict = Object(eventInitDict);
for (var key in eventInitDict) {
if (dict.hasOwnProperty(key)) {
- var value,
+ var value,
idlValue,
converter = converters[key];
@@ -63,56 +63,40 @@
}
//initialize the underlying event
- event.initEvent(String(type), dict.bubbles, dict.cancelable);
- copyEventProps(this);
-
- //copy underlying properties to this object
- function copyEventProps(obj){
- var propName,
- propDesc,
- propNames = Object.getOwnPropertyNames(event);
- for(var i = 0; i < propNames.length; i++ ){
- propName = propNames[i];
- propDesc = Object.getOwnPropertyDescriptor(event , propName);
- delete propDesc.value;
- delete propDesc.writable;
- propDesc.get = (function(p,e){
- return function(){
- return e[p];
- }
- })(propName,event)
- Object.defineProperty(obj, propName, propDesc);
- }
- }
+ event = new Event(String(type), dict);
+
+
//create the min attribute
props = {
- get: function () {
+ get: function() {
return dict.min;
},
enumerable: true,
configurable: true
};
- Object.defineProperty(this, 'min', props);
+ Object.defineProperty(event, 'min', props);
//create the max attribute
props = {
- get: function () {
+ get: function() {
return dict.max;
},
enumerable: true,
configurable: true
};
- Object.defineProperty(this, 'max', props);
+ Object.defineProperty(event, 'max', props);
//create the value attribute
props = {
- get: function () {
+ get: function() {
return dict.value;
},
enumerable: true,
configurable: true
};
- Object.defineProperty(this, 'value', props);
+ Object.defineProperty(event, 'value', props);
+
+ return event;
//WebIDL ECMAScript to double
function toDouble(value) {
@@ -189,7 +173,7 @@
enumerable: false,
configurable: true,
value: function toString() {
- return "[object DeviceProximityEvent]"
+ return '[object DeviceProximityEvent]';
}
};
Object.defineProperty(DeviceProximityEvent.prototype, 'toString', props);
@@ -229,17 +213,17 @@
enumerable: false,
configurable: true,
value: function toString() {
- return "function DeviceProximityEvent() { [native code] }"
+ return 'function DeviceProximityEvent() { [native code] }';
}
};
- Object.defineProperty(iObj, "toString", props);
+ Object.defineProperty(iObj, 'toString', props);
//Interface Prototype Object
function DeviceProximityEvent() {
}
})(this,
-//fake proximity sensor
+//fake user proximity sensor
{
get min() {
return 0.2;
@@ -249,5 +233,16 @@
return Math.round(this.max * Math.random());
}, get near() {
return Boolean(this.value);
+ },
+ sense: function sense() {
+ var event,
+ dict;
+ obj = this;
+ setInterval(function() {
+ dict = {min: obj.min, max: obj.max, value: obj.value, near: obj.near};
+ event = new DeviceProximityEvent('deviceproximity', dict);
+ window.dispatchEvent(event);
+ },Math.round(2000 * Math.random() + 500));
+ return this;
}
-});
+}.sense());
View
@@ -1,13 +1,13 @@
/**
* Implementation of "User Proximity"
* http://dvcs.w3.org/hg/dap/raw-file/tip/proximity/userproximity.html
-*
+*
* Public Domain Software
* To the extent possible under law, Marcos Caceres has waived all copyright and
* related or neighboring rights to UserProximityEvent Implementation.
-*
+*
* This program implements the following intefaces:
-*
+*
* [Constructor (DOMString type, optional UserProximityEventInit eventInitDict)]
* interface UserProximityEvent : Event {
* readonly attribute boolean near;
@@ -17,32 +17,31 @@
* };
**/
(function implementUserProximityEvent(globalObject, sensor) {
- var props,
+ var stringify,
+ props,
selfRef = this;
//Interface Object, as per WebIDL
iObj = function UserProximityEvent(type, eventInitDict) {
- if (arguments.length === 0) {
- throw new TypeError('Not Enough Arguments');
- }
-
var props,
converters = Object.create(null),
dict = {
near: sensor.near,
cancelable: false,
bubbles: true
},
- event = new Event(String(type));
+ event;
+ if (arguments.length === 0) {
+ throw new TypeError('Not Enough Arguments');
+ }
//ECMAScript to WebIDL converters
+ converters.cancelable =
+ converters.bubbles =
converters.near = toBool;
- converters.bubbles = toBool;
- converters.cancelable = toBool;
-
//process eventInitDict if it was passed, overriding 'dict'
if (arguments.length === 2) {
- eventInitDict = Object(eventInitDict);
+ eventInitDict = Object(eventInitDict);
for (var key in eventInitDict) {
if (dict.hasOwnProperty(key)) {
var converter = converters[key],
@@ -57,39 +56,19 @@
}
}
- //create the near attribute
+ //initialize the internal Event
+ event = new Event(String(type), dict);
+
+ //add the near attribute
props = {
get: function() {
return dict.near;
},
enumerable: true,
configurable: true
};
- Object.defineProperty(this, 'near', props);
-
- //initialize the internal Event
- event.initEvent(String(type), dict.bubbles, dict.cancelable);
- copyEventProps(this);
-
-
- //copy underlying properties to this object
- function copyEventProps(obj){
- var name,
- propDesc,
- propNames = Object.getOwnPropertyNames(event);
- for(var i = 0; i < propNames.length; i++ ){
- name = propNames[i];
- propDesc = Object.getOwnPropertyDescriptor(event , name);
- delete propDesc.value;
- delete propDesc.writable;
- propDesc.get = (function(prop,event){
- return function(){
- return event[prop];
- }
- })(name,event)
- Object.defineProperty(obj, name, propDesc);
- }
- }
+ Object.defineProperty(event, 'near', props);
+ return event;
//WebIDL ECMAScript to WebIDL boolean
function toBool(value) {
@@ -141,11 +120,10 @@
D.configurable = X.configurable;
return D;
}
-
};
//Set up prototype for interface
- UserProximityEvent.prototype = new Event('');
+ UserProximityEvent.prototype = new Event('userproximity', {});
props = {
writable: false,
enumerable: false,
@@ -159,7 +137,7 @@
enumerable: false,
configurable: true,
value: function toString() {
- return "[object UserProximityEvent]"
+ return '[object UserProximityEvent]';
}
};
Object.defineProperty(UserProximityEvent.prototype, 'toString', props);
@@ -192,13 +170,14 @@
Object.defineProperty(iObj, 'prototype', props);
//redefine toString() for interface object
+ stringify = 'function UserProximityEvent() { [native code] }';
props = {
writable: true,
enumerable: false,
configurable: true,
- value: function toString(){return "function UserProximityEvent() { [native code] }"}
+ value: function toString() {return stringify}
};
- Object.defineProperty(iObj, "toString", props);
+ Object.defineProperty(iObj, 'toString', props);
//Inteface Prototype Object
function UserProximityEvent() {
@@ -215,5 +194,16 @@
return Math.round(this.max * Math.random());
}, get near() {
return Boolean(this.value);
+ },
+ sense: function sense() {
+ var event,
+ dict;
+ obj = this;
+ setInterval(function() {
+ dict = {min: obj.min, max: obj.max, value: obj.value, near: obj.near};
+ event = new UserProximityEvent('userproximity', dict);
+ window.dispatchEvent(event);
+ },Math.round(2000 * Math.random() + 500));
+ return this;
}
-});
+}.sense());
View
@@ -1,6 +1,6 @@
<!doctype html>
<title>DeviceProximityEvent and UserProximity</title>
-<script src="//www.w3c-test.org/resources/testharness.js"></script>
+<script src="http://www.w3c-test.org/resources/testharness.js"></script>
<script src="DeviceProximityEvent.js"></script>
<script src="UserProximityEvent.js"></script>
<script src="DeviceProximityEvent_tests.js"></script>

0 comments on commit 736f48d

Please sign in to comment.