Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit of DeviceProximityEvent interface… not quite working
yet, but it's a start
  • Loading branch information
marcoscaceres-remote committed May 20, 2012
0 parents commit 2490140
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
210 changes: 210 additions & 0 deletions DeviceProximityEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/**
[Constructor (DOMString type, optional DeviceProximityEventInit eventInitDict)]
interface DeviceProximityEvent : Event {
readonly attribute double value;
readonly attribute double min;
readonly attribute double max;
};
dictionary DeviceProximityEventInit : EventInit {
double value;
double min;
double max;
};
**/
(function implementDeviceProximityEvent(globalObject, proximitySensor) {
'use strict';
var min, max, value, props, iProtoObj,
//interface object + constructor
iObj = function DeviceProximityEvent(type, eventInitDict) {
var typeString = String.valueOf()(type),
converters = Object.create({}),
dict = {
value: +Infinity,
max: +Infinity,
min: -Infinity,
cancelable: false,
bubbles: true
};

//ECMAScript to WebIDL converters
converters.min = toDouble;
converters.max = toDouble;
converters.value = toDouble;
converters.bubbles = toBool;
converters.cancelable = toBool;

if (type === undefined || type === null) {
throw new TypeError('Not Enough Arguments');
}

if (typeof type !== 'string') {
throw new TypeError('type is was not a string');
}

//process eventInitDict if it was passed
if (eventInitDict) {
if (Type(eventInitDict) !== 'object') {
throw new TypeError('wrong argument');
}

for (var key in eventInitDict) {
if (dict.hasOwnProperty(key)) {
var converter = converters[key],
value, idlValue;

if (HasProperty(eventInitDict, key)) {
value = eventInitDict[key];
idlValue = converter(value);
dict[key] = idlValue;
}
}
}
}

min = dict.min;
max = dict.max;
value = dict.value;

this.prototype = new Event(type, {bubbles: dict.bubbles, cancelable: dict.cancelable});

//WebIDL ECMAScript to double
function toDouble(value) {
var x = Number(value);
if (isNaN(x) || x === Infinity || x === -Infinity) {
throw new TypeError('Cannot convert to double.');
}
return x;
}
//WebIDL ECMAScript to WebIDL boolean
function toBool(value) {
return Boolean(value);
}
//ECMAScript5 Type(x)
function Type(x) {
var type = (x === null) ? 'null' : typeof x;
if (type === 'function') {
type = 'object';
}
return type;
}
//ECMAScript5 HasProperty
function HasProperty(O, P) {
var desc = GetProperty(O, P);
if (desc === undefined) {
return false;
}
return true;
}
//ECMAScript5 GetProperty
function GetProperty(O, P) {
var prop = GetOwnProperty(O, P);
if (prop !== undefined) {
return prop;
}
var proto = Object.getPrototypeOf(O);
if (proto === null) {
return undefined;
}
return GetProperty(proto, P);
}
//ECMAScript5 GetOwnProperty
function GetOwnProperty(O, P) {
if (!O.hasOwnProperty(P)) {
return undefined;
}
var D = Object.create({});
var X = Object.getOwnPropertyDescriptor(O, P);
if (X.hasOwnProperty('value') || X.hasOwnProperty('writable')) {
D.value = X.value;
D.writable = X.writable;
} else {
D.get = X.get;
D.set = X.set;
}
D.enumerable = X.enumerable;
D.configurable = X.configurable;
return D;
}
};

//Set up prototype for iterface
DeviceProximityEvent.prototype = new Event('deviceproximity');
props = {
writable: false,
enumerable: false,
configurable: false
};
Object.defineProperty(DeviceProximityEvent, 'prototype', props);
iProtoObj = new DeviceProximityEvent();

//set up the prototype object's constructor
DeviceProximityEvent.constructor = iObj;
props = {
writable: true,
enumerable: false,
configurable: true,
};
Object.defineProperty(DeviceProximityEvent, 'constructor', props);

//Set up prototype for interface
iObj.prototype = iProtoObj;
props = {
writable: false,
enumerable: false,
configurable: false
};
Object.defineProperty(iObj, 'prototype', props);

//create the min attribute
props = {
get: getMinAttr,
enumerable: true,
configurable: true
};
Object.defineProperty(iProtoObj, 'min', props);

//create the max attribute
props = {
get: getMaxAttr,
enumerable: true,
configurable: true
};
Object.defineProperty(iProtoObj, 'max', props);

//create the value attribute
props = {
get: getValueAttr,
enumerable: true,
configurable: true
};
Object.defineProperty(iProtoObj, 'value', props);

//Add DeviceProximityEvent to global object (i.e., to Window)
props = {
writable: true,
enumerable: false,
configurable: true,
value: iObj
};
Object.defineProperty(globalObject, 'DeviceProximityEvent', props);


//Private resolver for minimum
function getMinAttr() {
return min;
}

//private resolver for maximum
function getMaxAttr() {
return max;
}

//private resolver for value
function getValueAttr() {
return value;
}
//Interface Prototype Object
function DeviceProximityEvent() {

}
})(this);
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<script src="DeviceProximityEvent.js"></script>

0 comments on commit 2490140

Please sign in to comment.