Rename data -> global_context

This commit is contained in:
Adrian Vollmer 2022-10-03 15:45:37 +02:00
parent 9bb1e6f3ae
commit b01c034420
4 changed files with 20 additions and 20 deletions

View File

@ -35,37 +35,37 @@ var createIframe = function() {
} }
var load_virtual_page = (function (path, get_params, anchor) { var load_virtual_page = (function (path, get_params, anchor) {
const data = window.data.file_tree[path]; const data = window.global_context.file_tree[path];
var iframe = createIframe(); var iframe = createIframe();
window.data.get_parameters = get_params; window.global_context.get_parameters = get_params;
iframe.contentDocument.write(data); iframe.contentDocument.write(data);
if (anchor) { if (anchor) {
iframe.contentDocument.location.hash = anchor; iframe.contentDocument.location.hash = anchor;
} }
window.data.current_path = path; window.global_context.current_path = path;
window.history.pushState({path, get_params, anchor}, '', '#'); window.history.pushState({path, get_params, anchor}, '', '#');
}); });
window.onload = function() { window.onload = function() {
// Set up the virtual file tree // Set up the virtual file tree
var FT = window.data.file_tree; var FT = window.global_context.file_tree;
FT = _base64ToArrayBuffer(FT); FT = _base64ToArrayBuffer(FT);
FT = pako.inflate(FT) FT = pako.inflate(FT)
FT = new TextDecoder("utf-8").decode(FT); FT = new TextDecoder("utf-8").decode(FT);
FT = JSON.parse(FT); FT = JSON.parse(FT);
window.data.file_tree = FT; window.global_context.file_tree = FT;
// Set up message listener // Set up message listener
window.addEventListener("message", (evnt) => { window.addEventListener("message", (evnt) => {
console.log("Received message in parent", evnt); console.log("Received message in parent", evnt);
if (evnt.data.action == 'set_title') { if (evnt.data.action == 'set_title') {
// iframe has finished loading and sent us its title // iframe has finished loading and sent us its title
// parent sets the title and responds with the data object // parent sets the title and responds with the global_context object
window.document.title = evnt.data.argument; window.document.title = evnt.data.argument;
var iframe = document.getElementById(iFrameId); var iframe = document.getElementById(iFrameId);
iframe.contentWindow.postMessage({ iframe.contentWindow.postMessage({
action: "set_data", action: "set_data",
argument: window.data, argument: window.global_context,
}, "*"); }, "*");
} else if (evnt.data.action == 'virtual_click') { } else if (evnt.data.action == 'virtual_click') {
// user has clicked on a link in the iframe // user has clicked on a link in the iframe
@ -94,5 +94,5 @@ window.onload = function() {
}); });
// Load first page // Load first page
load_virtual_page(window.data.current_path, "", ""); load_virtual_page(window.global_context.current_path, "", "");
} }

View File

@ -141,14 +141,14 @@ var is_virtual = function(url) {
var retrieve_file = function(path) { var retrieve_file = function(path) {
// console.log("Retrieving file: " + path); // console.log("Retrieving file: " + path);
var file_tree = window.data.file_tree; var file_tree = window.global_context.file_tree;
var file = file_tree[path]; var file = file_tree[path];
return file; return file;
}; };
var normalize_path = function(path) { var normalize_path = function(path) {
// make relative paths absolute // make relative paths absolute
var result = window.data.current_path; var result = window.global_context.current_path;
result = result.split('/'); result = result.split('/');
result.pop(); result.pop();
result = result.concat(path.split('/')); result = result.concat(path.split('/'));
@ -167,7 +167,7 @@ var normalize_path = function(path) {
}); });
result = array.join('/'); result = array.join('/');
// console.log(`Normalized path: ${path} -> ${result} (@${window.data.current_path})`); // console.log(`Normalized path: ${path} -> ${result} (@${window.global_context.current_path})`);
return result; return result;
}; };
@ -185,8 +185,8 @@ var fix_document = function() {
window.addEventListener("message", (evnt) => { window.addEventListener("message", (evnt) => {
console.log("Received message in iframe", evnt); console.log("Received message in iframe", evnt);
if (evnt.data.action == 'set_data') { if (evnt.data.action == 'set_data') {
window.data = evnt.data.argument; window.global_context = evnt.data.argument;
console.log("Received data from parent", window.data); console.log("Received data from parent", window.global_context);
// dynamically fix elements on this page // dynamically fix elements on this page
try { try {
fix_document(); fix_document();

View File

@ -7,7 +7,7 @@
* creative. * creative.
* *
* Here, we patch the `URLSearchParams` class so it returns the information * Here, we patch the `URLSearchParams` class so it returns the information
* stored in `window.data.get_parameters`. * stored in `window.global_context.get_parameters`.
* *
*/ */
@ -17,11 +17,11 @@ var myGet = function (arg) {
const originalResult = originalGet.apply(this, [arg]); const originalResult = originalGet.apply(this, [arg]);
// If searchtools.js of sphinx is used // If searchtools.js of sphinx is used
if ( if (
window.data.get_parameters && window.global_context.get_parameters &&
(window.location.search === "") && (window.location.search === "") &&
(Array.from(this.entries()).length == 0) (Array.from(this.entries()).length == 0)
) { ) {
const params = new URLSearchParams('?' + window.data.get_parameters); const params = new URLSearchParams('?' + window.global_context.get_parameters);
const result = params.get(arg); const result = params.get(arg);
// console.log("Return virtual get parameter:", arg, result); // console.log("Return virtual get parameter:", arg, result);
return result; return result;

View File

@ -61,19 +61,19 @@ def embed_assets(index_file):
remote_resources = [] remote_resources = []
data = { global_context = {
'current_path': base_name, 'current_path': base_name,
'file_tree': file_tree, 'file_tree': file_tree,
'remote_resources': remote_resources, 'remote_resources': remote_resources,
} }
data = json.dumps(data) global_context = json.dumps(global_context)
result = """ result = """
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head><style>{style}</style></head> <head><style>{style}</style></head>
<body>{body} <body>{body}
<script>window.data = {data}</script> <script>window.global_context = {global_context}</script>
<script>{pako} //# sourceURL=pako.js</script> <script>{pako} //# sourceURL=pako.js</script>
<script>{init_js}</script> <script>{init_js}</script>
</body></html> </body></html>
@ -82,7 +82,7 @@ def embed_assets(index_file):
init_js=init_files['init.js'], init_js=init_files['init.js'],
pako=init_files['pako.min.js'], pako=init_files['pako.min.js'],
body=init_files['init.html'], body=init_files['init.html'],
data=data, global_context=global_context,
) )
with open(result_file, 'w') as fp: with open(result_file, 'w') as fp: