Skip to content

Commit

Permalink
Implemented onuserproximity and ondeviceproximity attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres-remote committed May 25, 2012
1 parent 203017b commit b92782f
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 91 deletions.
137 changes: 88 additions & 49 deletions DeviceProximityEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}),
Expand Down Expand Up @@ -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());
126 changes: 84 additions & 42 deletions UserProximityEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,75 @@
* 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) {
return;
}

var stringify, props, selfRef = this,
callback = null,
//Interface Object, as per WebIDL
iObj = function UserProximityEvent(type, eventInitDict) {
var props, converters = Object.create(null),
Expand Down Expand Up @@ -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()));

0 comments on commit b92782f

Please sign in to comment.