aboutsummaryrefslogtreecommitdiff
path: root/js/slide-controller.js
blob: 64186ebe65b07d3b9805df639363e4026e9da8f6 (plain)
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
/**
 * Remote control by:
 *
 * @authors Pacien TRAN-GIRARD
 */
(function(window) {

	var ORIGIN_ = location.protocol + '//' + location.host;

	function SlideController(deck) {
		this.deck = deck;

		this.mode = null;
		this.remoteSocket = null;
		this.isPresenter = false;
		this.isController = false;

		this.keyLock = null;

		this.setup();
	};

	SlideController.MODES = ['local', 'remote', 'controller', 'presenter'];

	SlideController.prototype.setup = function() {

		var self = this;

		// find the current mode
		var params = location.search.substring(1).split('&').map(function(el) {
			return el.split('=');
		});

		var paramKeys = params[0];

		SlideController.MODES.forEach(function(element, index, array) {
			if (paramKeys.indexOf(element) > -1) {
				self.mode = element;
				return;
			}
		});

		console.log("Control mode: " + this.mode);

		// clean the location bar
		// if (this.mode !== null) {
		// // localStorage.ENABLE_PRESENTOR_MODE = presentMe;
		// if (window.history.pushState) {
		// window.history.pushState({}, '', location.pathname);
		// } else if (window.history.replaceState) {
		// window.history.replaceState({}, '', location.pathname);
		// }
		// // else {
		// // location.replace(location.pathname);
		// // return false;
		// // }
		// }

		// activate the mode specific behaviour
		switch (this.mode) {

			case 'local':
				// Only open popup from main deck. Avoid recursive popupception.
				if (!this.isPresenter) {
					var opts = 'menubar=no,location=yes,resizable=yes,scrollbars=no,status=no';
					var localPresenter = window.open(location.href, 'mywindow', opts);

					// Loading in the popup? Turn the presenter mode on.
					localPresenter.addEventListener('load', function(e) {
						localPresenter.document.body.classList.add('popup');
						localPresenter.document.body.classList.add('with-notes');
					}.bind(this), false);

					window.addEventListener('message', this.onMessage_.bind(this), false);

					// Close popups if we reload the main window.
					window.addEventListener('beforeunload', function(e) {
						localPresenter.close();
					}.bind(this), false);
				}

				break;

			case 'presenter':
				this.isPresenter = true;
				document.body.classList.add('popup');

			case 'controller':
				this.isController = true;
				document.body.classList.add('with-notes');
				var password = prompt("Broadcaster password");

			case 'remote':

				var addr = this.deck.config_.settings.remoteSocket;
				var channel = this.deck.config_.settings.remoteChannel;
				var password = (password != null) ? password : '';

				require(['humane-themed', addr + 'socket.io/socket.io.js'], function(humane, io) {

					self.remoteSocket = io.connect(addr, {
						'query' : 'channel=' + channel + '&password=' + password,
						'force new connection' : true,
					});

					self.remoteSocket.on('connecting', function() {
						console.log('Connecting to ' + channel + '@' + addr);
						humane.remove();
						humane.log('Connecting...', {
							timeout : 0
						});
					});

					self.remoteSocket.on('connect', function() {
						console.log('Connected to ' + channel + '@' + addr);
						humane.remove();
						humane.log('Connected');
					});

					self.remoteSocket.on('connect_failed', function() {
						console.log('Error connecting to ' + channel + '@' + addr);
						humane.remove();
						humane.log('Connection failed', {
							timeout : 0
						});
					});

					self.remoteSocket.on('error', function() {
						console.log('Error on ' + channel + '@' + addr);
						humane.remove();
						humane.log('Error', {
							timeout : 0
						});
					});

					self.remoteSocket.on('disconnect', function() {
						console.log('Diconnected from' + channel + '@' + addr);
						humane.remove();
						humane.log('Disconnected');
					});

					self.remoteSocket.on('message', function(message) {
						console.log('Received from remote: ' + message);
						self.onMessage_({
							data : {
								keyCode : parseInt(message)
							}
						});
					});
				});

				break;

		}

		return true;
	};

	SlideController.prototype.onMessage_ = function(e) {

		var data = e.data;

		console.log("Received event: " + JSON.stringify(data));

		// Restrict messages to being from this origin. Allow local developmet
		// from file:// though.
		// TODO: It would be dope if FF implemented location.origin!
		if (this.mode === 'local' && e.origin != ORIGIN_ && ORIGIN_.indexOf('file://') != 0) {
			alert('Someone tried to postMessage from an unknown origin');
			return;
		}

		// if (e.source.location.hostname != 'localhost') {
		// alert('Someone tried to postMessage from an unknown origin');
		// return;
		// }

		if ('keyCode' in data) {
			if (isNaN(data.keyCode)) {
				return;
			}

			this.keyLock = data.keyCode;

			var evt = document.createEvent('Event');
			evt.initEvent('keydown', true, true);
			evt.keyCode = data.keyCode;
			document.dispatchEvent(evt);
		}
	};

	SlideController.prototype.sendMsg = function(msg) {

		if (msg.keyCode === this.keyLock) {
			this.keyLock = null;
			return;
		}

		// don't toggle speaker's notes for viewers
		if (msg.keyCode === 80) {
			return;
		}

		console.log("Sending: " + JSON.stringify(msg));

		// // Send message to popup window.
		// if (this.localPresenter) {
		// this.localPresenter.postMessage(msg, ORIGIN_);
		// }

		// Send message to main window.
		if (this.isController) {
			switch (this.mode) {
				case 'local':
					// TODO: It would be dope if FF implemented location.origin.
					window.opener.postMessage(msg, '*');
					break;
				case 'controller':
				case 'presenter':
					this.remoteSocket.emit('message', msg.keyCode);
			}
		}
	};

	window.SlideController = SlideController;

})(window);