aboutsummaryrefslogtreecommitdiff
path: root/js/slide-controller.js
blob: bdbb6708c793ce82e842df6be83fed2e52940ff5 (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
(function(window) {

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

function SlideController(slideDeck) {
  this.deck_ = slideDeck;
  this.win_ = null;

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

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

  // Only open one new popup. The recursion popup opening!
  if (!window.opener) {
    this.win_ = window.open(location.href, 'mywindow');
  }
}

SlideController.MOVE_LEFT = -1;
SlideController.MOVE_RIGHT = 1;

SlideController.prototype.onMessage_ = function(e) {
  var data = e.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 (e.origin != ORIGIN && ORIGIN != 'file://') {
    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) {
    var evt = document.createEvent('Event');
    evt.initEvent('keydown', true, true);
    evt.keyCode = data.keyCode;
    document.dispatchEvent(evt);
  }
};

SlideController.prototype.sendMsg = function(msg) {
  // // Send message to popup window.
  // if (this.win_) {
  //   this.win_.postMessage(msg, ORIGIN);
  // }

  // Send message to main window.
  if (window.opener) {
    // TODO: It would be dope if FF implemented location.origin.
    window.opener.postMessage(msg, '*');
  }
};

window.SlideController = SlideController;

})(window);