Debian Codegraph @

inst/rmd/h/rsiframe-1.1/rsiframe.js

raw symbols SCIP
1 /*jshint browser:true, strict:false, curly:false, indent:3*/
2 (function(){
3
4 // returns the origin we should post messages to (if on same host), or null if
5 // no suitable origin is found
6 var getOrigin = function() {
7 var origin = null;
8
9 // if an origin is supplied by RStudio, use it directly.
10 var rstudio_origin_meta = $('meta[name="rstudio_origin"]');
11 if (rstudio_origin_meta.length > 0) {
12 var rstudio_origin = rstudio_origin_meta.attr('content');
13 return window.location.protocol + "//" + rstudio_origin;
14 }
15
16 // function to normalize hostnames
17 var normalize = function(hostname) {
18 if (hostname == "127.0.0.1")
19 return "localhost";
20 else
21 return hostname;
22 };
23
24 // construct the parent origin if the hostnames match
25 var parentUrl = (parent !== window) ? document.referrer : null;
26
27 if (parentUrl) {
28 // parse the parent href
29 var a = document.createElement('a');
30 a.href = parentUrl;
31
32 if (normalize(a.hostname) == normalize(window.location.hostname)) {
33 var protocol = a.protocol.replace(':',''); // browser compatability
34 origin = protocol + '://' + a.hostname;
35 if (a.port)
36 origin = origin + ':' + a.port;
37 }
38 }
39 return origin;
40 };
41
42 // property application ------------------------------------------------------
43
44 // sets the location hash
45 var setHash = function(hash) {
46 // special case for ioslides: if the hash is numeric and we have a slide
47 // controller, use it to change the slides (ioslides doesn't respond to
48 // onhashchange)
49 if (/[0-9]+/.test(hash) && window.slidedeck) {
50 window.slidedeck.loadSlide(parseInt(hash));
51 } else {
52 location.hash = hash;
53 }
54 };
55
56 // sets the document scroll position. this could be called before that part of
57 // the document has loaded, so if the position specified is not yet available,
58 // wait a few ms and try again.
59 var setScrollPos = function(pos) {
60 if (pos > document.body.scrollHeight) {
61 window.setTimeout(function() { setScrollPos(pos); }, 100);
62 } else {
63 document.body.scrollTop = pos;
64 }
65 };
66
67 // cross-domain communication ------------------------------------------------
68
69 // obtain and validate the origin
70 var origin = getOrigin();
71 if (origin === null)
72 return;
73
74 // set up cross-domain send/receive
75 var send = function(data) {
76 data.type = "ShinyFrameEvent";
77 // only send the message if we have a parent
78 if (parent && (parent !== window))
79 parent.postMessage(data, origin);
80 };
81
82 var recv = function(evt) {
83 // validate that message is from expected origin (i.e. our parent)
84 if (evt.origin !== origin)
85 return;
86
87 switch (evt.data.method) {
88 case "rs_set_scroll_pos":
89 setScrollPos(evt.data.arg);
90 break;
91 case "rs_set_hash":
92 setHash(evt.data.arg);
93 break;
94 }
95 };
96
97 window.addEventListener("message", recv, false);
98
99 // document event handlers ---------------------------------------------------
100
101 // notify parent when scroll stops changing for ~0.25 s (don't spam during
102 // continuous/smooth scrolling)
103 var scrollTimer = 0;
104 var onScroll = function(pos) {
105 if (scrollTimer !== 0)
106 window.clearTimeout(scrollTimer);
107 scrollTimer = window.setTimeout(function() {
108 send({ event: "doc_scroll_change", data: document.body.scrollTop });
109 }, 250);
110 };
111
112 // test the href for changes every 100ms, and notify parent if it has changed.
113 // ordinarily we'd hook an event handler to 'hashchange' here, but in some
114 // cases (e.g. ioslides) the document can change the hash without triggering
115 // a hashchange event.
116 var currentHref = location.href;
117 var testHrefChange = function() {
118 if (currentHref !== location.href) {
119 currentHref = location.href;
120 send({ event: "doc_hash_change", data: location.href });
121 }
122 };
123 window.setInterval(testHrefChange, 100);
124
125 window.addEventListener("scroll", onScroll, false);
126
127 // let parent know we're ready once the event loop finishes
128 window.setTimeout(function() {
129 send({ event: "doc_ready", data: null });
130 }, 0);
131
132 // mathjax setup -------------------------------------------------------------
133
134 // if this is a Qt-based browser on Windows, inject the MathJax configuration
135 // data. at the time this runs it's not possible to know whether the document
136 // uses MathJax or not.
137 if (window.navigator.userAgent.indexOf(" Qt/") > 0 &&
138 window.navigator.userAgent.indexOf("Windows") > 0) {
139 var s = document.createElement("script");
140 s.type = "text/x-mathjax-config";
141 s.textContent =
142 'MathJax.Hub.Config({' +
143 ' "HTML-CSS": { minScaleAdjust: 125, availableFonts: [] } ' +
144 '});';
145 document.head.appendChild(s);
146 }
147 })();
148