-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeviceProximityEvent.js
306 lines (284 loc) · 9.54 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* 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;
* };
**/
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;
}
console.warn("Proximity sensor is an unsupported component with an indefinite lifetime. This should be used for evaluation purposes only and should not be used for production level applications.");
var min, max, value, props, iProtoObj,
callback = null,
//interface object + constructor
iObj = function DeviceProximityEvent(type, eventInitDict) {
var props, key, event, value, idlValue, converter, converters = Object.create({}),
dict = {
value: sensor.value || +Infinity,
max: sensor.max || +Infinity,
min: sensor.min || -Infinity,
cancelable: false,
bubbles: true
};
if (arguments.length === 0) {
throw new TypeError('Not Enough Arguments');
}
//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 GetOwnProperty
function getOwnProperty(O, P) {
if (!O.hasOwnProperty(P)) {
return undefined;
}
var D = Object.create({}),
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;
}
//ECMAScript5 GetProperty
function getProperty(O, P) {
var prop = getOwnProperty(O, P),
proto = Object.getPrototypeOf(O);
if (prop !== undefined) {
return prop;
}
if (proto === null) {
return undefined;
}
return getProperty(proto, P);
}
//ECMAScript5 HasProperty
function hasProperty(O, P) {
var desc = getProperty(O, P);
if (desc === undefined) {
return false;
}
return true;
}
//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 = globalObject.Object(eventInitDict);
for (key in eventInitDict) {
if (dict.hasOwnProperty(key)) {
converter = converters[key];
if (hasProperty(eventInitDict, key)) {
value = eventInitDict[key];
idlValue = converter(value);
dict[key] = idlValue;
}
}
}
}
//initialize the underlying event
event = new globalObject.Event(String(type), dict);
//create the min attribute
props = {
value: dict.min,
writable: false,
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'min', props);
//create the max attribute
props = {
value: dict.max,
writable: false,
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'max', props);
//create the value attribute
props = {
value: dict.value,
writable: false,
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'value', props);
return event;
};
//Set up prototype for iterface
DeviceProximityEvent.prototype = new globalObject.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);
//[TreatNonCallableAsNull] attribute Function? ondeviceproximity;
function treatNonCallableAsNull(arg) {
if (arg !== callback) {
if (callback) {
sensor.removeListener(callback);
callback = null;
}
if (typeof arg === 'function' && arg.call !== undefined && typeof arg.call === 'function') {
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
new FakeDeviceProximitySensor().sense());