From b92782f698366cdec9d6aa9b66a62d46db7f351c Mon Sep 17 00:00:00 2001 From: Marcos Caceres Date: Fri, 25 May 2012 11:31:22 +0100 Subject: [PATCH] Implemented onuserproximity and ondeviceproximity attributes --- DeviceProximityEvent.js | 137 ++++++++++++++++++++++++++-------------- UserProximityEvent.js | 126 ++++++++++++++++++++++++------------ 2 files changed, 172 insertions(+), 91 deletions(-) diff --git a/DeviceProximityEvent.js b/DeviceProximityEvent.js index a914f1d..312396a 100644 --- a/DeviceProximityEvent.js +++ b/DeviceProximityEvent.js @@ -19,13 +19,75 @@ * double min; * double max; * }; - **/ (function implementDeviceProximityEvent(globalObject, sensor) { + **/ +FakeDeviceProximitySensor.prototype = { + fireEvent: function fireEvent() { + 'use strict'; + var e = new window.DeviceProximityEvent('deviceproximity', this.dict); + window.dispatchEvent(e); + }, + get dict() { + 'use strict'; + return { + min: this.min, + max: this.max, + value: this.value, + near: this.near + }; + }, + get min() { + 'use strict'; + return 0.2; + }, + get max() { + 'use strict'; + return 5.0; + }, + value: null, + refreshValue: function updateValue() { + 'use strict'; + this.value = Math.abs(Math.sin(Date.now() / 1000) * 10); + return this.value; + }, + sense: function sense() { + 'use strict'; + var obj = this; + //first run + if (this.value === null) { + this.refreshValue(); + //queue a task to fire event + setTimeout(this.fireEvent, 4); + } + + setInterval(function() { + //new random value + obj.refreshValue(); + obj.fireEvent(); + }, 16); + return this; + }, + + registerListener: function registerListener(callback) { + 'use strict'; + window.addEventListener('deviceproximity', callback); + }, + + removeListener: function removeListener(callback) { + 'use strict'; + window.removeEventListener('deviceproximity', callback); + } +}; + +function FakeDeviceProximitySensor() {} + + +(function implementDeviceProximityEvent(globalObject, sensor) { 'use strict'; //only polyfill if needed if (globalObject.DeviceProximityEvent) { return; } - var min, max, value, props, iProtoObj, + var min, max, value, props, iProtoObj, callback, //interface object + constructor iObj = function DeviceProximityEvent(type, eventInitDict) { var props, key, event, value, idlValue, converter, converters = Object.create({}), @@ -208,56 +270,33 @@ }; Object.defineProperty(iObj, 'toString', props); + //[TreatNonCallableAsNull] attribute Function? ondeviceproximity; + function treatNonCallableAsNull(arg) { + if (callback) { + sensor.removeListener(callback); + } + if (typeof arg !== 'function' && !(arg.call) && typeof arg.call !== 'function') { + callback = null; + } else { + callback = arg; + sensor.registerListener(callback); + } + return arg; + } + props = { + get: function() { + return callback; + }, + set: treatNonCallableAsNull, + enumerable: false, + configurable: true + }; + Object.defineProperty(globalObject, 'ondeviceproximity', props); + //Interface Prototype Object function DeviceProximityEvent() { } })(this, //fake device proximity sensor -{ - fireEvent: function fireEvent() { - 'use strict'; - var e = new window.DeviceProximityEvent('deviceproximity', this.dict); - window.dispatchEvent(e); - }, - get dict() { - 'use strict'; - return { - min: this.min, - max: this.max, - value: this.value, - near: this.near - }; - }, - get min() { - 'use strict'; - return 0.2; - }, - get max() { - 'use strict'; - return 5.0; - }, - value: null, - refreshValue: function updateValue() { - 'use strict'; - this.value = Math.abs(Math.sin(Date.now() / 1000) * 10); - return this.value; - }, - sense: function sense() { - 'use strict'; - var obj = this; - //first run - if (this.value === null) { - this.refreshValue(); - //queue a task to fire event - setTimeout(this.fireEvent, 4); - } - - setInterval(function() { - //new random value - obj.refreshValue(); - obj.fireEvent(); - }, 16); - return this; - } -}.sense()); +new FakeDeviceProximitySensor().sense()); diff --git a/UserProximityEvent.js b/UserProximityEvent.js index 52cc811..638cbdb 100644 --- a/UserProximityEvent.js +++ b/UserProximityEvent.js @@ -15,7 +15,67 @@ * dictionary UserProximityEventInit : EventInit { * boolean near; * }; - **/ (function implementUserProximityEvent(globalObject, sensor) { + + * partial interface Window { + * [TreatNonCallableAsNull] + * attribute Function? onuserproximity; + * }; + **/ +FakeUserProximitySensor.prototype = { + fireEvent: function fireEvent() { + 'use strict'; + var userEvent = new window.UserProximityEvent('userproximity', this.dict); + window.dispatchEvent(userEvent); + return this; + }, + get dict() { + 'use strict'; + return { + near: this.near + }; + }, + value: null, + get near() { + 'use strict'; + return Boolean(this.value); + }, + refreshValue: function refreshValue() { + 'use strict'; + this.value = Math.round(Math.random()); + return this.value; + }, + sense: function sense() { + 'use strict'; + var obj = this; + //first run + if (this.value === null) { + this.refreshValue(); + //queue a task to fire event + setTimeout(this.fireEvent, 4); + } + setInterval(function() { + var oldNear = obj.near; + obj.refreshValue(); + if (oldNear !== obj.near) { + obj.fireEvent(); + } + }, 500); + return this; + }, + registerListener: function registerListener(callback) { + 'use strict'; + window.addEventListener('deviceproximity', callback); + }, + removeListener: function removeListener(callback) { + 'use strict'; + window.removeEventListener('deviceproximity', callback); + } +}; + +function FakeUserProximitySensor() {} + + +(function implementUserProximityEvent(globalObject, sensor) { 'use strict'; //only polyfill if needed if (globalObject.UserProximityEvent) { @@ -23,6 +83,7 @@ } var stringify, props, selfRef = this, + callback = null, //Interface Object, as per WebIDL iObj = function UserProximityEvent(type, eventInitDict) { var props, converters = Object.create(null), @@ -181,46 +242,27 @@ } }; Object.defineProperty(iObj, 'toString', props); -}(this, -//fake user proximity sensor -{ - fireEvent: function fireEvent() { - 'use strict'; - var userEvent = new window.UserProximityEvent('userproximity', this.dict); - window.dispatchEvent(userEvent); - return this; - }, - get dict() { - 'use strict'; - return { - near: this.near - }; - }, value: null, - get near() { - 'use strict'; - return Boolean(this.value); - }, - refreshValue: function updateValue() { - 'use strict'; - this.value = Math.round(Math.random()); - return this.value; - }, - sense: function sense() { - 'use strict'; - var obj = this; - //first run - if (this.value === null) { - this.refreshValue(); - //queue a task to fire event - setTimeout(this.fireEvent, 4); + + //[TreatNonCallableAsNull] attribute Function? onuserproximity; + function treatNonCallableAsNull(arg) { + if (callback) { + sensor.removeListener(callback); } - setInterval(function() { - var oldNear = obj.near; - obj.refreshValue(); - if (oldNear !== this.near) { - obj.fireEvent(); - } - }, 500); - return this; + if (typeof arg !== 'function' && !(arg.call) && typeof arg.call !== 'function') { + callback = null; + } else { + callback = arg; + sensor.registerListener(callback); + } + return arg; } -}.sense())); + props = { + get: function() { + return callback; + }, + set: treatNonCallableAsNull, + enumerable: false, + configurable: true + }; + Object.defineProperty(globalObject, 'onuserproximity', props); +}(this, new FakeUserProximitySensor().sense()));