-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeviceProximityEvent.js
248 lines (229 loc) · 7.84 KB
/
DeviceProximityEvent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* This is an implementation of "Proximity Events":
* http://dvcs.w3.org/hg/dap/raw-file/tip/proximity/Overview.html
*
* Public Domain Software
* To the extent possible under law, Marcos Caceres has waived all copyright and
* related or neighboring rights to DeviceProximityEvent Implementation.
*
* This program implements the following intefaces:
*
* [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, sensor) {
'use strict';
var min, max, value, props, iProtoObj,
//interface object + constructor
iObj = function DeviceProximityEvent(type, eventInitDict) {
if (arguments.length === 0) {
throw new TypeError('Not Enough Arguments');
}
var props,
converters = Object.create({}),
dict = {
value: sensor.value || +Infinity,
max: sensor.max || +Infinity,
min: sensor.min || -Infinity,
cancelable: false,
bubbles: true
},
//We can't use Event.call to inherit properly,
//so we have to have a private event instead :(
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);
for (var key in eventInitDict) {
if (dict.hasOwnProperty(key)) {
var value,
idlValue,
converter = converters[key];
if (HasProperty(eventInitDict, key)) {
value = eventInitDict[key];
idlValue = converter(value);
dict[key] = idlValue;
}
}
}
}
//initialize the underlying event
event = new Event(String(type), dict);
//create the min attribute
props = {
get: function() {
return dict.min;
},
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'min', props);
//create the max attribute
props = {
get: function() {
return dict.max;
},
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'max', props);
//create the value attribute
props = {
get: function() {
return dict.value;
},
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'value', props);
return event;
//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('');
props = {
writable: true,
enumerable: false,
configurable: false
};
Object.defineProperty(DeviceProximityEvent, 'prototype', props);
//Define toString method, as per WebIDL
props = {
writable: true,
enumerable: false,
configurable: true,
value: function toString() {
return '[object DeviceProximityEvent]';
}
};
Object.defineProperty(DeviceProximityEvent.prototype, 'toString', props);
iProtoObj = new DeviceProximityEvent();
//set up the prototype object's constructor
iProtoObj.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);
//Add DeviceProximityEvent to global object (i.e., to Window)
props = {
writable: true,
enumerable: false,
configurable: true,
value: iObj
};
Object.defineProperty(globalObject, 'DeviceProximityEvent', props);
//redefine toString() for interface object
props = {
writable: true,
enumerable: false,
configurable: true,
value: function toString() {
return 'function DeviceProximityEvent() { [native code] }';
}
};
Object.defineProperty(iObj, 'toString', props);
//Interface Prototype Object
function DeviceProximityEvent() {
}
})(this,
//fake user proximity sensor
{
get min() {
return 0.2;
}, get max() {
return 5.0;
}, get value() {
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());