Path: blob/master/sandbox/RFinance2014/libraries/frameworks/io2012/js/slide-controller.js
1436 views
(function(window) {12var ORIGIN_ = location.protocol + '//' + location.host;34function SlideController() {5this.popup = null;6this.isPopup = window.opener;78if (this.setupDone()) {9window.addEventListener('message', this.onMessage_.bind(this), false);1011// Close popups if we reload the main window.12window.addEventListener('beforeunload', function(e) {13if (this.popup) {14this.popup.close();15}16}.bind(this), false);17}18}1920SlideController.PRESENTER_MODE_PARAM = 'presentme';2122SlideController.prototype.setupDone = function() {23var params = location.search.substring(1).split('&').map(function(el) {24return el.split('=');25});2627var presentMe = null;28for (var i = 0, param; param = params[i]; ++i) {29if (param[0].toLowerCase() == SlideController.PRESENTER_MODE_PARAM) {30presentMe = param[1] == 'true';31break;32}33}3435if (presentMe !== null) {36localStorage.ENABLE_PRESENTOR_MODE = presentMe;37// TODO: use window.history.pushState to update URL instead of the redirect.38if (window.history.replaceState) {39window.history.replaceState({}, '', location.pathname);40} else {41location.replace(location.pathname);42return false;43}44}4546var enablePresenterMode = localStorage.getItem('ENABLE_PRESENTOR_MODE');47if (enablePresenterMode && JSON.parse(enablePresenterMode)) {48// Only open popup from main deck. Don't want recursive popup opening!49if (!this.isPopup) {50var opts = 'menubar=no,location=yes,resizable=yes,scrollbars=no,status=no';51this.popup = window.open(location.href, 'mywindow', opts);5253// Loading in the popup? Trigger the hotkey for turning presenter mode on.54this.popup.addEventListener('load', function(e) {55var evt = this.popup.document.createEvent('Event');56evt.initEvent('keydown', true, true);57evt.keyCode = 'P'.charCodeAt(0);58this.popup.document.dispatchEvent(evt);59// this.popup.document.body.classList.add('with-notes');60// document.body.classList.add('popup');61}.bind(this), false);62}63}6465return true;66}6768SlideController.prototype.onMessage_ = function(e) {69var data = e.data;7071// Restrict messages to being from this origin. Allow local developmet72// from file:// though.73// TODO: It would be dope if FF implemented location.origin!74if (e.origin != ORIGIN_ && ORIGIN_.indexOf('file://') != 0) {75alert('Someone tried to postMessage from an unknown origin');76return;77}7879// if (e.source.location.hostname != 'localhost') {80// alert('Someone tried to postMessage from an unknown origin');81// return;82// }8384if ('keyCode' in data) {85var evt = document.createEvent('Event');86evt.initEvent('keydown', true, true);87evt.keyCode = data.keyCode;88document.dispatchEvent(evt);89}90};9192SlideController.prototype.sendMsg = function(msg) {93// // Send message to popup window.94// if (this.popup) {95// this.popup.postMessage(msg, ORIGIN_);96// }9798// Send message to main window.99if (this.isPopup) {100// TODO: It would be dope if FF implemented location.origin.101window.opener.postMessage(msg, '*');102}103};104105window.SlideController = SlideController;106107})(window);108109110111