slide-controller.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function(window) {
  2. var ORIGIN_ = location.protocol + '//' + location.host;
  3. function SlideController() {
  4. this.popup = null;
  5. this.isPopup = window.opener;
  6. if (this.setupDone()) {
  7. window.addEventListener('message', this.onMessage_.bind(this), false);
  8. // Close popups if we reload the main window.
  9. window.addEventListener('beforeunload', function(e) {
  10. if (this.popup) {
  11. this.popup.close();
  12. }
  13. }.bind(this), false);
  14. }
  15. }
  16. SlideController.PRESENTER_MODE_PARAM = 'presentme';
  17. SlideController.prototype.setupDone = function() {
  18. var params = location.search.substring(1).split('&').map(function(el) {
  19. return el.split('=');
  20. });
  21. var presentMe = null;
  22. for (var i = 0, param; param = params[i]; ++i) {
  23. if (param[0].toLowerCase() == SlideController.PRESENTER_MODE_PARAM) {
  24. presentMe = param[1] == 'true';
  25. break;
  26. }
  27. }
  28. if (presentMe !== null) {
  29. localStorage.ENABLE_PRESENTOR_MODE = presentMe;
  30. // TODO: use window.history.pushState to update URL instead of the redirect.
  31. if (window.history.replaceState) {
  32. window.history.replaceState({}, '', location.pathname);
  33. } else {
  34. location.replace(location.pathname);
  35. return false;
  36. }
  37. }
  38. var enablePresenterMode = localStorage.getItem('ENABLE_PRESENTOR_MODE');
  39. if (enablePresenterMode && JSON.parse(enablePresenterMode)) {
  40. // Only open popup from main deck. Don't want recursive popup opening!
  41. if (!this.isPopup) {
  42. var opts = 'menubar=no,location=yes,resizable=yes,scrollbars=no,status=no';
  43. this.popup = window.open(location.href, 'mywindow', opts);
  44. // Loading in the popup? Trigger the hotkey for turning presenter mode on.
  45. this.popup.addEventListener('load', function(e) {
  46. var evt = this.popup.document.createEvent('Event');
  47. evt.initEvent('keydown', true, true);
  48. evt.keyCode = 'P'.charCodeAt(0);
  49. this.popup.document.dispatchEvent(evt);
  50. // this.popup.document.body.classList.add('with-notes');
  51. // document.body.classList.add('popup');
  52. }.bind(this), false);
  53. }
  54. }
  55. return true;
  56. }
  57. SlideController.prototype.onMessage_ = function(e) {
  58. var data = e.data;
  59. // Restrict messages to being from this origin. Allow local developmet
  60. // from file:// though.
  61. // TODO: It would be dope if FF implemented location.origin!
  62. if (e.origin != ORIGIN_ && ORIGIN_.indexOf('file://') != 0) {
  63. alert('Someone tried to postMessage from an unknown origin');
  64. return;
  65. }
  66. // if (e.source.location.hostname != 'localhost') {
  67. // alert('Someone tried to postMessage from an unknown origin');
  68. // return;
  69. // }
  70. if ('keyCode' in data) {
  71. var evt = document.createEvent('Event');
  72. evt.initEvent('keydown', true, true);
  73. evt.keyCode = data.keyCode;
  74. document.dispatchEvent(evt);
  75. }
  76. };
  77. SlideController.prototype.sendMsg = function(msg) {
  78. // // Send message to popup window.
  79. // if (this.popup) {
  80. // this.popup.postMessage(msg, ORIGIN_);
  81. // }
  82. // Send message to main window.
  83. if (this.isPopup) {
  84. // TODO: It would be dope if FF implemented location.origin.
  85. window.opener.postMessage(msg, '*');
  86. }
  87. };
  88. window.SlideController = SlideController;
  89. })(window);