Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/RFinance2014/libraries/frameworks/io2012/js/slide-controller.js
1436 views
1
(function(window) {
2
3
var ORIGIN_ = location.protocol + '//' + location.host;
4
5
function SlideController() {
6
this.popup = null;
7
this.isPopup = window.opener;
8
9
if (this.setupDone()) {
10
window.addEventListener('message', this.onMessage_.bind(this), false);
11
12
// Close popups if we reload the main window.
13
window.addEventListener('beforeunload', function(e) {
14
if (this.popup) {
15
this.popup.close();
16
}
17
}.bind(this), false);
18
}
19
}
20
21
SlideController.PRESENTER_MODE_PARAM = 'presentme';
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
var opts = 'menubar=no,location=yes,resizable=yes,scrollbars=no,status=no';
52
this.popup = window.open(location.href, 'mywindow', opts);
53
54
// Loading in the popup? Trigger the hotkey for turning presenter mode on.
55
this.popup.addEventListener('load', function(e) {
56
var evt = this.popup.document.createEvent('Event');
57
evt.initEvent('keydown', true, true);
58
evt.keyCode = 'P'.charCodeAt(0);
59
this.popup.document.dispatchEvent(evt);
60
// this.popup.document.body.classList.add('with-notes');
61
// document.body.classList.add('popup');
62
}.bind(this), false);
63
}
64
}
65
66
return true;
67
}
68
69
SlideController.prototype.onMessage_ = function(e) {
70
var data = e.data;
71
72
// Restrict messages to being from this origin. Allow local developmet
73
// from file:// though.
74
// TODO: It would be dope if FF implemented location.origin!
75
if (e.origin != ORIGIN_ && ORIGIN_.indexOf('file://') != 0) {
76
alert('Someone tried to postMessage from an unknown origin');
77
return;
78
}
79
80
// if (e.source.location.hostname != 'localhost') {
81
// alert('Someone tried to postMessage from an unknown origin');
82
// return;
83
// }
84
85
if ('keyCode' in data) {
86
var evt = document.createEvent('Event');
87
evt.initEvent('keydown', true, true);
88
evt.keyCode = data.keyCode;
89
document.dispatchEvent(evt);
90
}
91
};
92
93
SlideController.prototype.sendMsg = function(msg) {
94
// // Send message to popup window.
95
// if (this.popup) {
96
// this.popup.postMessage(msg, ORIGIN_);
97
// }
98
99
// Send message to main window.
100
if (this.isPopup) {
101
// TODO: It would be dope if FF implemented location.origin.
102
window.opener.postMessage(msg, '*');
103
}
104
};
105
106
window.SlideController = SlideController;
107
108
})(window);
109
110
111