diff options
author | Eric Bidelman | 2012-04-18 14:45:38 -0700 |
---|---|---|
committer | Eric Bidelman | 2012-04-18 14:45:38 -0700 |
commit | a8e534158525f69db7276cded5f79e6fb6819754 (patch) | |
tree | fd56a680306561c6c289b4d6b19b2ceab2d46627 /js/slide-controller.js | |
parent | 6ca44eaadf9787260a87be1baabcd9bc9a306afd (diff) | |
download | io-slides-remote-a8e534158525f69db7276cded5f79e6fb6819754.tar.gz |
Using history.replaceState. Presenter mode code cleanup
Diffstat (limited to 'js/slide-controller.js')
-rw-r--r-- | js/slide-controller.js | 79 |
1 files changed, 60 insertions, 19 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 | ||
3 | var ORIGIN = location.protocol + '//' + location.host; | 3 | var ORIGIN_ = location.protocol + '//' + location.host; |
4 | 4 | ||
5 | function SlideController(slideDeck) { | 5 | function 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 | ||
22 | SlideController.MOVE_LEFT = -1; | 21 | SlideController.PRESENTER_MODE_PARAM = 'presentme'; |
23 | SlideController.MOVE_RIGHT = 1; | 22 | |
23 | SlideController.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 | ||
25 | SlideController.prototype.onMessage_ = function(e) { | 66 | SlideController.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 | ||
49 | SlideController.prototype.sendMsg = function(msg) { | 90 | SlideController.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 | } |