Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions mfr/server/static/js/mfr.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,61 @@
self.spinner = _createSpinner(url, imgName);

self.init = function () {
self.pymParent = new pym.Parent(self.id, self.url, self.config);
self.pymParent.iframe.setAttribute('allowfullscreen', '');
self.pymParent.iframe.setAttribute('webkitallowfullscreen', '');
self.pymParent.iframe.setAttribute('scrolling', 'yes');
self.pymParent.iframe.setAttribute('sandbox', 'allow-scripts allow-popups allow-same-origin');

self.pymParent.el.appendChild(self.spinner);
$(self.pymParent.iframe).on('load', function () {
self.pymParent.el.removeChild(self.spinner);
});

self.pymParent.onMessage('embed', function(message) {
_addClass(self.pymParent.el, 'embed-responsive');
_addClass(self.pymParent.el, message);
_addClass(self.pymParent.iframe, 'embed-responsive-item');
});

self.pymParent.onMessage('location', function(message) {
window.location = message;
});

const wb_url = window.contextVars.waterbutlerURL;
const node_id = window.contextVars.node.id;
const provider = window.contextVars.file.provider;
const file_id = window.contextVars.file.id;
const file_name = window.contextVars.file.name;

const wb_file_url = `${wb_url}/v1/resources/${node_id}/providers/${provider}/${file_id}`;

const handlers = {

default: function() {

self.pymParent = new pym.Parent(self.id, self.url, self.config);
self.pymParent.iframe.setAttribute('allowfullscreen', '');
self.pymParent.iframe.setAttribute('webkitallowfullscreen', '');
self.pymParent.iframe.setAttribute('scrolling', 'yes');
self.pymParent.iframe.setAttribute('sandbox', 'allow-scripts allow-popups allow-same-origin');

self.pymParent.el.appendChild(self.spinner);
$(self.pymParent.iframe).on('load', function () {
self.pymParent.el.removeChild(self.spinner);
});

self.pymParent.onMessage('embed', function(message) {
_addClass(self.pymParent.el, 'embed-responsive');
_addClass(self.pymParent.el, message);
_addClass(self.pymParent.iframe, 'embed-responsive-item');
});

self.pymParent.onMessage('location', function(message) {
window.location = message;
});
},

html: function() {
var xhr = new XMLHttpRequest()
xhr.open('GET', wb_file_url, true);
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const frame = document.createElement("iframe");
frame.id = "mfrframe";
document.getElementById("mfrIframe").appendChild(frame);
document.getElementById("mfrframe").src = "data:text/html;charset=utf-8," + escape(xhr.responseText);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are issues using a data url, we also have the srcdoc attribute available https://www.w3schools.com/tags/att_iframe_srcdoc.asp

document.getElementById("mfrframe").sandbox = 'allow-forms allow-scripts'
}
}
xhr.send()
}
}

if (!(self.file_ext in handlers)) self.file_ext = "default";
handlers[self.file_ext]();

};

self.init();
Expand Down