aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Bidelman2012-04-18 14:45:38 -0700
committerEric Bidelman2012-04-18 14:45:38 -0700
commita8e534158525f69db7276cded5f79e6fb6819754 (patch)
treefd56a680306561c6c289b4d6b19b2ceab2d46627
parent6ca44eaadf9787260a87be1baabcd9bc9a306afd (diff)
downloadio-slides-remote-a8e534158525f69db7276cded5f79e6fb6819754.tar.gz
Using history.replaceState. Presenter mode code cleanup
-rw-r--r--js/slide-controller.js79
-rw-r--r--js/slides.js69
2 files changed, 79 insertions, 69 deletions
diff --git a/js/slide-controller.js b/js/slide-controller.js
index bdbb670..594ee47 100644
--- a/js/slide-controller.js
+++ b/js/slide-controller.js
@@ -1,26 +1,67 @@
1(function(window) { 1(function(window) {
2 2
3var ORIGIN = location.protocol + '//' + location.host; 3var ORIGIN_ = location.protocol + '//' + location.host;
4 4
5function SlideController(slideDeck) { 5function SlideController() {
6 this.deck_ = slideDeck; 6 this.popup = null;
7 this.win_ = null; 7 this.isPopup = window.opener;
8 8
9 window.addEventListener('message', this.onMessage_.bind(this), false); 9 if (this.setupDone()) {
10 window.addEventListener('message', this.onMessage_.bind(this), false);
10 11
11 // Close popups if we reload the main window. 12 // Close popups if we reload the main window.
12 window.addEventListener('beforeunload', function(e) { 13 window.addEventListener('beforeunload', function(e) {
13 this.win_.close() 14 if (this.popup) {
14 }.bind(this), false); 15 this.popup.close();
15 16 }
16 // Only open one new popup. The recursion popup opening! 17 }.bind(this), false);
17 if (!window.opener) {
18 this.win_ = window.open(location.href, 'mywindow');
19 } 18 }
20} 19}
21 20
22SlideController.MOVE_LEFT = -1; 21SlideController.PRESENTER_MODE_PARAM = 'presentme';
23SlideController.MOVE_RIGHT = 1; 22
23SlideController.prototype.setupDone = function() {
24 var params = location.search.substring(1).split('&').map(function(el) {
25 return el.split('=');
26 });
27
28 var presentMe = null;
29 for (var i = 0, param; param = params[i]; ++i) {
30 if (param[0].toLowerCase() == SlideController.PRESENTER_MODE_PARAM) {
31 presentMe = param[1] == 'true';
32 break;
33 }
34 }
35
36 if (presentMe !== null) {
37 localStorage.ENABLE_PRESENTOR_MODE = presentMe;
38 // TODO: use window.history.pushState to update URL instead of the redirect.
39 if (window.history.replaceState) {
40 window.history.replaceState({}, '', location.pathname);
41 } else {
42 location.replace(location.pathname);
43 return false;
44 }
45 }
46
47 var enablePresenterMode = localStorage.getItem('ENABLE_PRESENTOR_MODE');
48 if (enablePresenterMode && JSON.parse(enablePresenterMode)) {
49 // Only open popup from main deck. Don't want recursive popup opening!
50 if (!this.isPopup) {
51 this.popup = window.open(location.href, 'mywindow');
52
53 // Loading in the popup? Trigger the hotkey for turning presenter mode on.
54 this.popup.addEventListener('load', function(e) {
55 var evt = this.popup.document.createEvent('Event');
56 evt.initEvent('keydown', true, true);
57 evt.keyCode = 'P'.charCodeAt(0);
58 this.popup.document.dispatchEvent(evt);
59 }.bind(this), false);
60 }
61 }
62
63 return true;
64}
24 65
25SlideController.prototype.onMessage_ = function(e) { 66SlideController.prototype.onMessage_ = function(e) {
26 var data = e.data; 67 var data = e.data;
@@ -28,7 +69,7 @@ SlideController.prototype.onMessage_ = function(e) {
28 // Restrict messages to being from this origin. Allow local developmet 69 // Restrict messages to being from this origin. Allow local developmet
29 // from file:// though. 70 // from file:// though.
30 // TODO: It would be dope if FF implemented location.origin! 71 // TODO: It would be dope if FF implemented location.origin!
31 if (e.origin != ORIGIN && ORIGIN != 'file://') { 72 if (e.origin != ORIGIN_ && ORIGIN_ != 'file://') {
32 alert('Someone tried to postMessage from an unknown origin'); 73 alert('Someone tried to postMessage from an unknown origin');
33 return; 74 return;
34 } 75 }
@@ -48,12 +89,12 @@ SlideController.prototype.onMessage_ = function(e) {
48 89
49SlideController.prototype.sendMsg = function(msg) { 90SlideController.prototype.sendMsg = function(msg) {
50 // // Send message to popup window. 91 // // Send message to popup window.
51 // if (this.win_) { 92 // if (this.popup) {
52 // this.win_.postMessage(msg, ORIGIN); 93 // this.popup.postMessage(msg, ORIGIN_);
53 // } 94 // }
54 95
55 // Send message to main window. 96 // Send message to main window.
56 if (window.opener) { 97 if (this.isPopup) {
57 // TODO: It would be dope if FF implemented location.origin. 98 // TODO: It would be dope if FF implemented location.origin.
58 window.opener.postMessage(msg, '*'); 99 window.opener.postMessage(msg, '*');
59 } 100 }
diff --git a/js/slides.js b/js/slides.js
index 7afe0cf..cd9e646 100644
--- a/js/slides.js
+++ b/js/slides.js
@@ -13,8 +13,7 @@ function SlideDeck() {
13 this.prevSlide_ = 0; 13 this.prevSlide_ = 0;
14 this.slides = []; 14 this.slides = [];
15 this.config_ = null; 15 this.config_ = null;
16 this.controller_ = null; 16 this.controller = null;
17 this.IS_POPUP_ = window.opener;
18 17
19 this.getCurrentSlideFromHash_(); 18 this.getCurrentSlideFromHash_();
20 19
@@ -50,39 +49,6 @@ SlideDeck.prototype.getCurrentSlideFromHash_ = function() {
50 } 49 }
51}; 50};
52 51
53SlideDeck.prototype.loadPresenterMode = function() {
54 var params = location.search.substring(1).split('&').map(function(el) {
55 return el.split('=');
56 });
57
58 var presentMe = null;
59 for (var i = 0, param; param = params[i]; ++i) {
60 if (param[0].toLowerCase() == 'presentme') {
61 presentMe = param[1] == 'true';
62 break;
63 }
64 }
65
66 if (presentMe !== null) {
67 localStorage.ENABLE_PRESENTOR_MODE = presentMe;
68 location.href = location.pathname;
69 }
70
71 // Turn on presenter mode?
72 if (localStorage.getItem('ENABLE_PRESENTOR_MODE') &&
73 JSON.parse(localStorage.getItem('ENABLE_PRESENTOR_MODE'))) {
74 this.controller_ = new SlideController(this);
75
76 // Loading in the popup? Trigger the hotkey for turning presenter mode on.
77 if (this.IS_POPUP_) {
78 var evt = document.createEvent('Event');
79 evt.initEvent('keydown', true, true);
80 evt.keyCode = 'P'.charCodeAt(0);
81 document.dispatchEvent(evt);
82 }
83 }
84}
85
86/** 52/**
87 * @private 53 * @private
88 */ 54 */
@@ -120,8 +86,11 @@ SlideDeck.prototype.onDomLoaded_ = function(e) {
120 }); 86 });
121 87
122 // Note: this needs to come after addEventListeners_(), which adds a 88 // Note: this needs to come after addEventListeners_(), which adds a
123 // 'keydown' listener that this method relies on. 89 // 'keydown' listener that this controller relies on.
124 this.loadPresenterMode(); 90 // Also, no need to set this up if we're on mobile.
91 if (!Modernizr.touch) {
92 this.controller = new SlideController(this);
93 }
125}; 94};
126 95
127/** 96/**
@@ -152,9 +121,9 @@ SlideDeck.prototype.onBodyKeyDown_ = function(e) {
152 return; 121 return;
153 } 122 }
154 123
155 // Forward keydown to the main slides if we're the popup. 124 // Forward keydowns to the main slides if we're the popup.
156 if (this.controller_ && this.IS_POPUP_) { 125 if (this.controller && this.controller.isPopup) {
157 this.controller_.sendMsg({keyCode: e.keyCode}); 126 this.controller.sendMsg({keyCode: e.keyCode});
158 } 127 }
159 128
160 switch (e.keyCode) { 129 switch (e.keyCode) {
@@ -195,9 +164,9 @@ SlideDeck.prototype.onBodyKeyDown_ = function(e) {
195 break; 164 break;
196 165
197 case 80: // P 166 case 80: // P
198 if (this.controller_ && this.IS_POPUP_) { 167 if (this.controller && this.controller.isPopup) {
199 document.body.classList.toggle('with-notes'); 168 document.body.classList.toggle('with-notes');
200 } else if (!this.controller_) { 169 } else if (this.controller && !this.controller.popup) {
201 document.body.classList.toggle('with-notes'); 170 document.body.classList.toggle('with-notes');
202 } 171 }
203 break; 172 break;
@@ -392,14 +361,14 @@ SlideDeck.prototype.prevSlide = function(opt_dontPush) {
392 361
393 // Toggle off speaker notes if they're showing when we move backwards on the 362 // Toggle off speaker notes if they're showing when we move backwards on the
394 // main slides. If we're the speaker notes popup, leave them up. 363 // main slides. If we're the speaker notes popup, leave them up.
395 if (this.controller_ && !this.IS_POPUP_) { 364 if (this.controller && !this.controller.isPopup) {
396 bodyClassList.remove('with-notes'); 365 bodyClassList.remove('with-notes');
397 } else if (!this.controller_) { 366 } else if (!this.controller) {
398 bodyClassList.remove('with-notes'); 367 bodyClassList.remove('with-notes');
399 } 368 }
400 369
401 // if (this.controller_) { 370 // if (this.controller) {
402 // this.controller_.sendMsg({slideDirection: SlideController.MOVE_LEFT}); 371 // this.controller.sendMsg({slideDirection: SlideController.MOVE_LEFT});
403 // } 372 // }
404 373
405 this.prevSlide_ = this.curSlide_; 374 this.prevSlide_ = this.curSlide_;
@@ -414,8 +383,8 @@ SlideDeck.prototype.prevSlide = function(opt_dontPush) {
414 */ 383 */
415SlideDeck.prototype.nextSlide = function(opt_dontPush) { 384SlideDeck.prototype.ne