forked from fraunhoferfokus/cordova-plugin-presentation
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpresentation.js
360 lines (300 loc) · 8.98 KB
/
presentation.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2014 Fraunhofer FOKUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* AUTHORS: Louay Bassbouss <[email protected]>
* Martin Lasak <[email protected]>
*/
var execRaw = require('cordova/exec'),
cordova = require('cordova');
var makeAbs = function(url)
{
var absUrl = null;
try { absUrl = new URL(url, location.href).href; } catch(e) { }
if(!absUrl)
{
var a = document.createElement('a');
a.href = url;
absUrl = a.href;
}
if (!absUrl)
{
absUrl = url;
}
return absUrl;
}
var exec = function()
{
var args = arguments;
setTimeout(function()
{
execRaw.apply(undefined, args);
},
0);
}
/** WebIDL: NavigatorPresentation
interface NavigatorPresentation : EventTarget {
PresentationSession requestSession(DOMString url);
attribute EventHandler onavailablechange;
attribute EventHandler onpresent;
};
http://www.w3.org/2014/secondscreen/presentation-api/20140721/#NavigatorPresentation_interface
TODO(mla): EventTarget implementation.
*/
function NavigatorPresentation()
{
// In case of mirroring, display the following placeholder page
var defaultDisplay = makeAbs("presentation/display.html");
var c = document.getElementsByTagName("script");
for(var i = 0; i < c.length; i++)
{
if (c[i] && c[i].src && c[i].src.indexOf("/cordova.js") != -1)
{
defaultDisplay = c[i].src.replace("/cordova.js","/presentation/display.html");
}
}
exec(/*successCallback*/Function, /*errorCallback*/Function, "Presentation", "setDefaultDisplay", [ defaultDisplay ]);
}
Object.defineProperty(NavigatorPresentation.prototype, "showSecondScreen",
{
get: function ()
{
return function(doShow)
{
exec(/*successCallback*/Function, /*errorCallback*/Function, "Presentation", "setSecondScreen", [ doShow ? "activate" : "deactivate" ]);
};
}
});
Object.defineProperty(NavigatorPresentation.prototype,"requestSession",
{
get: function ()
{
return navigatorPresentationRequestSession;
}
});
Object.defineProperty(NavigatorPresentation.prototype, "onavailablechange",
{
get: function ()
{
return onavailablechange;
},
set: function(eventCallback)
{
if (typeof eventCallback == "function" || eventCallback == null || eventCallback == undefined)
{
onavailablechange = eventCallback;
if (onavailablechange)
{
//trigger the service to serve screen states
var scb = function(res)
{
if (typeof onavailablechange == "function")
{
var evt = new AvailableChangeEvent("availablechange",res);
onavailablechange(evt);
}
}
exec(scb, function(){}, "Presentation", "addWatchAvailableChange", []);
}
else
{
//stop the service serving screen states
exec(function(){}, function(){}, "Presentation", "clearWatchAvailableChange", []);
}
};
}
});
//hold the reference to the user defined callback
var onavailablechange = undefined;
// TODO(mla): check if on sender side this event can ever be expected
Object.defineProperty(NavigatorPresentation.prototype, "onpresent",
{
get: function () { }
});
var navigatorPresentationRequestSession = function(url)
{
var delSession = {
_id:"",
state: DISCONNECTED,
onstatechange: function(){},
onmessage: function(){}
};
delSession.postMessage = presentationSessionPostMessage(delSession);
delSession.close = presentationSessionClose(delSession);
var successCallback = function(result)
{
// result == { id: String, eventType: String, value: String }
delSession._id = result.id;
switch(result.eventType)
{
case "onstatechange":
delSession.state = result.value;
if (typeof delSession.onstatechange == "function")
{
delSession.onstatechange(result.value);
}
break;
case "onmessage":
if (typeof delSession.onmessage == "function")
{
delSession.onmessage(result.value);
}
break;
default:
break;
}
};
var errorCallback = function(){ };
exec(successCallback, errorCallback, "Presentation", "requestSession", [ makeAbs(url) ]);
return new PresentationSession(delSession);
};
/** WebIDL: AvailableChangeEvent
[Constructor(DOMString type, optional AvailableChangeEventInit eventInitDict)]
interface AvailableChangeEvent : Event {
readonly attribute boolean available;
};
dictionary AvailableChangeEventInit : EventInit {
boolean available;
};
http://www.w3.org/2014/secondscreen/presentation-api/20140721/#AvailableChangeEvent_interface
*/
var AvailableChangeEvent = function(type, eventInitDict)
{
this.type = type;
var available = eventInitDict && eventInitDict.available == true;
Object.defineProperty(this, "available",
{
get: function ()
{
return available;
}
});
};
AvailableChangeEvent.prototype = Event.prototype;
/** WebIDL: PresentationSession
enum PresentationSessionState { "connected", "disconnected" , "resumed" };
interface PresentationSession : EventTarget {
readonly attribute PresentationSessionState state;
void postMessage(DOMString message);
void close();
attribute EventHandler onmessage;
attribute EventHandler onstatechange;
};
http://www.w3.org/2014/secondscreen/presentation-api/20140721/#PresentationSession_interface
*/
var CONNECTED = "connected";
var DISCONNECTED = "disconnected";
var RESUMED = "resumed";
var CANCELLED = "cancelled";
var PresentationSessionState = [CONNECTED, DISCONNECTED, RESUMED, CANCELLED];
var PresentationSession = function(delSession)
{
var onmessage = null;
var onstatechange = null;
var self = this;
delSession.onstatechange = function()
{
if (typeof onstatechange == "function")
{
onstatechange.call(null);
};
};
delSession.onmessage = function(msg)
{
if (typeof onmessage == "function")
{
onmessage.call(null,msg);
};
};
Object.defineProperty(this, "state",
{
get: function ()
{
return (delSession && delSession.state) || null;
}
});
Object.defineProperty(this, "onmessage",
{
get: function ()
{
return onmessage;
},
set: function(value)
{
if (typeof value == "function" || value == null)
{
onmessage = value;
}
}
});
Object.defineProperty(this, "onstatechange",
{
get: function ()
{
return onstatechange;
},
set: function(value)
{
if (typeof value == "function" || value == null)
{
onstatechange = value;
}
}
});
Object.defineProperty(this, "postMessage",
{
get: function ()
{
return function(msg)
{
return delSession.postMessage(msg);
};
}
});
Object.defineProperty(this, "close",
{
get: function ()
{
return function()
{
return delSession.close();
};
}
});
};
var presentationSessionPostMessage = function(ds)
{
return function(message)
{
exec(/*successCallback*/Function, /*errorCallback*/Function, "Presentation", "presentationSessionPostMessage", [ ds._id, message ]);
};
};
var presentationSessionClose = function(ds)
{
return function()
{
exec(/*successCallback*/Function, /*errorCallback*/Function, "Presentation", "presentationSessionClose", [ ds._id ]);
};
};
/** WebIDL: PresentEvent
[Constructor(DOMString type, optional PresentEventInit eventInitDict)]
interface PresentEvent : Event {
readonly attribute PresentationSession session;
};
dictionary PresentEventInit : EventInit {
PresentationSession session;
};
http://www.w3.org/2014/secondscreen/presentation-api/20140721/#PresentEvent_interface
*/
module.exports = new NavigatorPresentation();