diff options
Diffstat (limited to 'js/slide-controller.js')
-rw-r--r-- | js/slide-controller.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/js/slide-controller.js b/js/slide-controller.js new file mode 100644 index 0000000..e2f8bf2 --- /dev/null +++ b/js/slide-controller.js | |||
@@ -0,0 +1,54 @@ | |||
1 | function SlideController(slideDeck) { | ||
2 | this.deck_ = slideDeck; | ||
3 | this.win_ = null; | ||
4 | |||
5 | window.addEventListener('message', this.onMessage_.bind(this), false); | ||
6 | |||
7 | // Close popups if we reload the main window. | ||
8 | window.addEventListener('beforeunload', function(e) { | ||
9 | this.win_.close() | ||
10 | }.bind(this), false); | ||
11 | |||
12 | // Only open one new popup. The recursion popup opening! | ||
13 | if (!window.opener) { | ||
14 | this.win_ = window.open(location.href, 'mywindow'); | ||
15 | } | ||
16 | } | ||
17 | |||
18 | SlideController.MOVE_LEFT = -1; | ||
19 | SlideController.MOVE_RIGHT = 1; | ||
20 | |||
21 | SlideController.prototype.onMessage_ = function(e) { | ||
22 | var data = e.data; | ||
23 | |||
24 | // It would be dope if FF implemented location.origin. | ||
25 | if (e.origin != location.protocol + '//' + location.host) { | ||
26 | alert('Someone tried to postMessage from an unknown origin'); | ||
27 | return; | ||
28 | } | ||
29 | |||
30 | if (e.source.location.hostname != 'localhost') { | ||
31 | alert('Someone tried to postMessage from an unknown origin'); | ||
32 | return; | ||
33 | } | ||
34 | |||
35 | if ('slideDirection' in data) { | ||
36 | if (data.slideDirection == SlideController.MOVE_LEFT) { | ||
37 | this.deck_.prevSlide(); | ||
38 | } else { | ||
39 | this.deck_.nextSlide(); | ||
40 | } | ||
41 | } | ||
42 | }; | ||
43 | |||
44 | SlideController.prototype.sendMsg = function(msg) { | ||
45 | // // Send message to popup window. | ||
46 | // if (this.win_) { | ||
47 | // this.win_.postMessage(msg, location.protocol + '//' + location.host); | ||
48 | // } | ||
49 | // Send message to main window. | ||
50 | if (window.opener) { | ||
51 | // It would be dope if FF implemented location.origin. | ||
52 | window.opener.postMessage(msg, location.protocol + '//' + location.host); | ||
53 | } | ||
54 | }; | ||