diff options
Diffstat (limited to 'slides/intermediaire/js/slide-controller.js')
-rw-r--r-- | slides/intermediaire/js/slide-controller.js | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/slides/intermediaire/js/slide-controller.js b/slides/intermediaire/js/slide-controller.js new file mode 100644 index 0000000..2fb2560 --- /dev/null +++ b/slides/intermediaire/js/slide-controller.js | |||
@@ -0,0 +1,227 @@ | |||
1 | /** | ||
2 | * Remote control by: | ||
3 | * | ||
4 | * @authors Pacien TRAN-GIRARD | ||
5 | */ | ||
6 | (function(window) { | ||
7 | |||
8 | var ORIGIN_ = location.protocol + '//' + location.host; | ||
9 | |||
10 | function SlideController(deck) { | ||
11 | this.deck = deck; | ||
12 | |||
13 | this.mode = null; | ||
14 | this.remoteSocket = null; | ||
15 | this.isPresenter = false; | ||
16 | this.isController = false; | ||
17 | |||
18 | this.keyLock = null; | ||
19 | |||
20 | this.setup(); | ||
21 | }; | ||
22 | |||
23 | SlideController.MODES = ['local', 'remote', 'controller', 'presenter']; | ||
24 | |||
25 | SlideController.prototype.setup = function() { | ||
26 | |||
27 | var self = this; | ||
28 | |||
29 | // find the current mode | ||
30 | var params = location.search.substring(1).split('&').map(function(el) { | ||
31 | return el.split('='); | ||
32 | }); | ||
33 | |||
34 | var paramKeys = params[0]; | ||
35 | |||
36 | SlideController.MODES.forEach(function(element, index, array) { | ||
37 | if (paramKeys.indexOf(element) > -1) { | ||
38 | self.mode = element; | ||
39 | return; | ||
40 | } | ||
41 | }); | ||
42 | |||
43 | console.log("Control mode: " + this.mode); | ||
44 | |||
45 | // clean the location bar | ||
46 | // if (this.mode !== null) { | ||
47 | // // localStorage.ENABLE_PRESENTOR_MODE = presentMe; | ||
48 | // if (window.history.pushState) { | ||
49 | // window.history.pushState({}, '', location.pathname); | ||
50 | // } else if (window.history.replaceState) { | ||
51 | // window.history.replaceState({}, '', location.pathname); | ||
52 | // } | ||
53 | // // else { | ||
54 | // // location.replace(location.pathname); | ||
55 | // // return false; | ||
56 | // // } | ||
57 | // } | ||
58 | |||
59 | // activate the mode specific behaviour | ||
60 | switch (this.mode) { | ||
61 | |||
62 | case 'local': | ||
63 | // Only open popup from main deck. Avoid recursive popupception. | ||
64 | if (!this.isPresenter) { | ||
65 | var opts = 'menubar=no,location=yes,resizable=yes,scrollbars=no,status=no'; | ||
66 | var localPresenter = window.open(location.href, 'mywindow', opts); | ||
67 | |||
68 | // Loading in the popup? Turn the presenter mode on. | ||
69 | localPresenter.addEventListener('load', function(e) { | ||
70 | localPresenter.document.body.classList.add('popup'); | ||
71 | localPresenter.document.body.classList.add('with-notes'); | ||
72 | }.bind(this), false); | ||
73 | |||
74 | window.addEventListener('message', this.onMessage_.bind(this), false); | ||
75 | |||
76 | // Close popups if we reload the main window. | ||
77 | window.addEventListener('beforeunload', function(e) { | ||
78 | localPresenter.close(); | ||
79 | }.bind(this), false); | ||
80 | } | ||
81 | |||
82 | break; | ||
83 | |||
84 | case 'presenter': | ||
85 | this.isPresenter = true; | ||
86 | document.body.classList.add('popup'); | ||
87 | |||
88 | case 'controller': | ||
89 | this.isController = true; | ||
90 | document.body.classList.add('with-notes'); | ||
91 | var password = prompt("Broadcaster password"); | ||
92 | |||
93 | case 'remote': | ||
94 | |||
95 | var addr = this.deck.config_.settings.remoteSocket; | ||
96 | var channel = this.deck.config_.settings.remoteChannel; | ||
97 | var password = (password != null) ? password : ''; | ||
98 | |||
99 | require(['humane-themed', addr + 'socket.io/socket.io.js'], function(humane, io) { | ||
100 | |||
101 | self.remoteSocket = io.connect(addr, { | ||
102 | 'query' : 'channel=' + channel + '&password=' + password, | ||
103 | 'force new connection' : true, | ||
104 | }); | ||
105 | |||
106 | self.remoteSocket.on('connecting', function() { | ||
107 | console.log('Connecting to ' + channel + '@' + addr); | ||
108 | humane.remove(); | ||
109 | humane.log('Connecting...', { | ||
110 | timeout : 0 | ||
111 | }); | ||
112 | }); | ||
113 | |||
114 | self.remoteSocket.on('connect', function() { | ||
115 | console.log('Connected to ' + channel + '@' + addr); | ||
116 | humane.remove(); | ||
117 | humane.log('Connected'); | ||
118 | }); | ||
119 | |||
120 | self.remoteSocket.on('connect_failed', function() { | ||
121 | console.log('Error connecting to ' + channel + '@' + addr); | ||
122 | humane.remove(); | ||
123 | humane.log('Connection failed', { | ||
124 | timeout : 0 | ||
125 | }); | ||
126 | }); | ||
127 | |||
128 | self.remoteSocket.on('error', function() { | ||
129 | console.log('Error on ' + channel + '@' + addr); | ||
130 | humane.remove(); | ||
131 | humane.log('Error', { | ||
132 | timeout : 0 | ||
133 | }); | ||
134 | }); | ||
135 | |||
136 | self.remoteSocket.on('disconnect', function() { | ||
137 | console.log('Diconnected from' + channel + '@' + addr); | ||
138 | humane.remove(); | ||
139 | humane.log('Disconnected'); | ||
140 | }); | ||
141 | |||
142 | self.remoteSocket.on('message', function(message) { | ||
143 | console.log('Received from remote: ' + message); | ||
144 | self.onMessage_({ | ||
145 | data : { | ||
146 | keyCode : parseInt(message[0]) | ||
147 | } | ||
148 | }); | ||
149 | }); | ||
150 | }); | ||
151 | |||
152 | break; | ||
153 | |||
154 | } | ||
155 | |||
156 | return true; | ||
157 | }; | ||
158 | |||
159 | SlideController.prototype.onMessage_ = function(e) { | ||
160 | |||
161 | var data = e.data; | ||
162 | |||
163 | console.log("Received event: " + JSON.stringify(data)); | ||
164 | |||
165 | // Restrict messages to being from this origin. Allow local developmet | ||
166 | // from file:// though. | ||
167 | // TODO: It would be dope if FF implemented location.origin! | ||
168 | if (this.mode === 'local' && e.origin != ORIGIN_ && ORIGIN_.indexOf('file://') != 0) { | ||
169 | alert('Someone tried to postMessage from an unknown origin'); | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | // if (e.source.location.hostname != 'localhost') { | ||
174 | // alert('Someone tried to postMessage from an unknown origin'); | ||
175 | // return; | ||
176 | // } | ||
177 | |||
178 | if ('keyCode' in data) { | ||
179 | if (isNaN(data.keyCode)) { | ||
180 | return; | ||
181 | } | ||
182 | |||
183 | this.keyLock = data.keyCode; | ||
184 | |||
185 | var evt = document.createEvent('Event'); | ||
186 | evt.initEvent('keydown', true, true); | ||
187 | evt.keyCode = data.keyCode; | ||
188 | document.dispatchEvent(evt); | ||
189 | } | ||
190 | }; | ||
191 | |||
192 | SlideController.prototype.sendMsg = function(msg) { | ||
193 | |||
194 | if (msg.keyCode === this.keyLock) { | ||
195 | this.keyLock = null; | ||
196 | return; | ||
197 | } | ||
198 | |||
199 | // don't toggle speaker's notes for viewers | ||
200 | if (msg.keyCode === 80) { | ||
201 | return; | ||
202 | } | ||
203 | |||
204 | console.log("Sending: " + JSON.stringify(msg)); | ||
205 | |||
206 | // // Send message to popup window. | ||
207 | // if (this.localPresenter) { | ||
208 | // this.localPresenter.postMessage(msg, ORIGIN_); | ||
209 | // } | ||
210 | |||
211 | // Send message to main window. | ||
212 | if (this.isController) { | ||
213 | switch (this.mode) { | ||
214 | case 'local': | ||
215 | // TODO: It would be dope if FF implemented location.origin. | ||
216 | window.opener.postMessage(msg, '*'); | ||
217 |