-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserProximityEvent.js
270 lines (252 loc) · 8.45 KB
/
UserProximityEvent.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
/**
* Implementation of "User Proximity"
* http://dvcs.w3.org/hg/dap/raw-file/tip/proximity/userproximity.html
*
* Public Domain Software
* To the extent possible under law, Marcos Caceres has waived all copyright and
* related or neighboring rights to UserProximityEvent Implementation.
*
* This program implements the following intefaces:
*
* [Constructor (DOMString type, optional UserProximityEventInit eventInitDict)]
* interface UserProximityEvent : Event {
* readonly attribute boolean near;
* };
* dictionary UserProximityEventInit : EventInit {
* boolean near;
* };
* 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;
}
console.warn("User Proximity Events 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 stringify, props, selfRef = this,
callback = null,
//Interface Object, as per WebIDL
iObj = function UserProximityEvent(type, eventInitDict) {
var props, converters = Object.create(null),
converter, value, idlValue, dict = {
near: sensor.near,
cancelable: false,
bubbles: true
},
event, key;
//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;
}
if (arguments.length === 0) {
throw new TypeError('Not Enough Arguments');
}
//ECMAScript to WebIDL converters
converters.cancelable = converters.bubbles = converters.near = 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 internal Event
event = new globalObject.Event(String(type), dict);
//add the near attribute
props = {
value: dict.near,
writable: false,
enumerable: true,
configurable: true
};
Object.defineProperty(event, 'near', props);
return event;
};
//Inteface Prototype Object
function UserProximityEvent() {
}
//Set up prototype for interface
UserProximityEvent.prototype = new globalObject.Event('userproximity', {});
props = {
writable: false,
enumerable: false,
configurable: false
};
Object.defineProperty(UserProximityEvent, 'prototype', props);
//Define toString method, as per WebIDL
props = {
writable: true,
enumerable: false,
configurable: true,
value: function toString() {
return '[object UserProximityEvent]';
}
};
Object.defineProperty(UserProximityEvent.prototype, 'toString', props);
//set up the prototype object's constructor
UserProximityEvent.constructor = iObj;
props = {
writable: true,
enumerable: false,
configurable: true
};
Object.defineProperty(UserProximityEvent, 'constructor', props);
//Add UserProximityEvent to global object (i.e., to Window)
props = {
writable: true,
enumerable: false,
configurable: true,
value: iObj
};
Object.defineProperty(globalObject, 'UserProximityEvent', props);
//Set up prototype for interface
iObj.prototype = new UserProximityEvent();
props = {
writable: false,
enumerable: false,
configurable: false
};
Object.defineProperty(iObj, 'prototype', props);
//redefine toString() for interface object
stringify = 'function UserProximityEvent() { [native code] }';
props = {
writable: true,
enumerable: false,
configurable: true,
value: function toString() {
return stringify;
}
};
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, 'onuserproximity', props);
}(this, new FakeUserProximitySensor().sense()));