-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation_control.js
144 lines (135 loc) · 5.26 KB
/
presentation_control.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
// The PresentationController encapsulates the basic flow to start, rejoin and
// stop presentations. It requires that the Presentation API polyfill be
// available and initialized. Two buttons with ids 'show' and 'stop' will be
// used to start and stop presentation.
//
// Usage:
//
// var p = new PresentationController();
// window['__OnPresentationAPIAvailable'] = function() {
// p.initialize();
// };
// ...
// p.startPresent(url) // e.g., in onclick for 'show' button
// p.stopPresent() // e.g., in onclick for 'stop' button
//
// TODO: Support multiple simultaneous sessions.
function() {
var presentation = navigator.presentation;
var PresentationController = function() {
this.session = null;
this.screenAvailable = false;
this.presentationUrl = localStorage['presentationUrl'] || null;
this.presentationId = localStorage['presentationId'] || null;
this.showButton = null;
this.stopButton = null;
};
// Initializes the controller.
PresentationController.prototype.initialize = function() {
presentation.onavailablechange = function(e) {
log.info('Screen availability changed from ' + this.screenAvailable +
' to ' + e.available);
this.screenAvailable = e.available;
updateButtons();
}.bind(this);
this.setupPage_();
this.checkExisting_();
};
// Binds UI elements for the page.
PresentationController.prototype.setupPage_ = function() {
this.showButton = document.getElementById('show'),
this.stopButton = document.getElementById('stop'),
if (!this.showButton || !this.stopButton) {
log.error('Show or stop button not found');
}
};
// Attempt to join an existing presentation, if one was remembered.
PresentationController.prototype.checkExisting_ = function() {
if (this.presentationUrl && this.presentationId) {
presentation.joinSession(this.presentationUrl, this.presentationId).then(
function(existingSession) {
log.info('Found existing session for [' + this.presentationUrl + ', ' +
this.presentationId + ']');
this.setSession(existingSession);
this.updateButtons();
}.bind(this),
function() {
log.info('No session found for [' + this.presentationUrl + ', ' +
this.presentationId + ']');
this.resetSession();
this.updateButtons();
}.bind(this));
}
};
// Updates the state of the buttons based on screen availability and the
// status of any presentation session.
PresentationController.prototype.updateButtons_ = function() {
var hasSession = this.session && this.session.state == 'connected';
if (this.stopButton) {
this.stopButton.disabled = !hasSession;
this.stopButton.onClick = hasSession ? this.stopPresent : null;
}
if (this.showButton) {
this.showButton.disabled = !this.screenAvailable;
this.showButton.onclick = this.screenAvailable ? this.startPresent : null;
}
};
// Requests new presentation of |url|. Returns a Promise that resolves
// to the new PresentationSession or rejects if the user cancelled presentation.
PresentationController.prototype.startPresent = function(url) {
if (!this.screenAvailable) {
log.warn('Presentation requested but no screens available!?');
return;
}
return presentation.requestSession(url).then(
function(newSession) {
log.info('New session obtained for [' + this.newSession.url +
', ' + this.newSession.id + ']');
this.setSession_(newSession);
this.updateButtons_();
newSession.onstatechange = function() {
log.info('Presentation [' + newSession.url + ', ' + newSession.id +
'], new state = ' + newSession.state);
if (newSession == this.session) {
this.updateButtons_();
}
}.bind(this);
return newSession;
}.bind(this),
function() {
log.warn('User cancelled session request for ' + url);
return Error('User cancelled session request');
});
};
// Stops the current presentation session, if any.
PresentationController.prototype.stopPresent = function() {
if (this.session) {
log.info('Stopping presentation of [' + this.presentationUrl +
', ' + this.presentationId + ']');
this.session.close();
} else {
log.warn('Stopping was requested, but no session available');
}
this.resetSession_();
};
// Removes data stored for the current session.
PresentationController.prototype.resetSession_ = function() {
delete localStorage['presentationId'];
delete localStorage['presentationUrl'];
this.presentationId = null;
this.presentationUrl = null;
};
// Sets the current presentation session for the controller.
PresentationController.prototype.setSession_(theSession) {
log.info('Setting current presentation to: [' + theSession.url +
', ' + theSession.id + '], state = ' + theSession.state);
if (this.session) {
this.stopPresent();
}
this.session = theSession;
this.presentationUrl = this.session.url
this.presentationId = this.session.id
localStorage['presentationUrl'] = this.session.url;
localStorage['presentationId'] = this.session.id;
}
}