UNPKG

wolfram-notebook-embedder

Version:

Library to embed Wolfram Cloud notebooks on websites.

1 lines 5.51 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).WolframNotebookEmbedder={})}(this,function(e){"use strict";function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n,r=arguments[t];for(n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var v={},g={},b=0,s=!1,h=[],p=[];function y(n,e){function t(e,t){t=t&&!e.didInsertWithoutCloning?e:e.cloneNode(!0);e.didInsertWithoutCloning=!0,n.appendChild(t)}function r(t,n){return function(t,e){var n=t.tagName.toLowerCase();if("link"===n&&("stylesheet"===t.rel||"text/css"===t.type))return e.some(function(e){return t.href.startsWith(e)});if("style"===n){if(t.dataset.isWolframNotebookStyle||0<=t.innerText.indexOf("._ccc"))return 1;if("erd_scroll_detection_scrollbar_style"===t.id)return 1}}(t,e)&&(h.push(t),p.forEach(function(e){e(t,n)}),1)}if(!s){for(var o=document.getElementsByTagName("link"),a=0;a<o.length;++a)r(o[a],!1);for(var i=document.getElementsByTagName("style"),l=0;l<i.length;++l)r(i[l],!1);var c,d=document.getElementsByTagName("head")[0];d&&(c=d.appendChild,d.appendChild=function(e){r(e,!0)||c.call(this,e)}),s=!0}h.forEach(function(e){t(e,!1)}),p.push(t);var u=p.length-1;return function(){p.splice(u,1)}}function i(e){var a=null,t="",i=!1,l=null,c={};if("string"==typeof e){var n=function(e,t){for(var n=null,r=null,o=0;o<t.length;++o){var a=t[o],i=e.indexOf(a);0<=i&&(null===n||i<n.length)&&(n=e.substring(0,i),r=e.substring(i+a.length))}return[n,r]}(e,["/obj/","/objects/"]),r=n[0],o=n[1];if(!r||!o)throw new Error("InvalidCloudObjectURL");a=r,n=o.split("?",1)[0];if(!n)throw new Error("InvalidCloudObjectURL");t="path="+encodeURIComponent(n)}else if(e&&"object"==typeof e){a=e.cloudBase||"https://www.wolframcloud.com",c.cloudBase=a;r=void 0!==e.path,o=void 0!==e.expr,n=void 0!==e.url;if(1!=(r?1:0)+(o?1:0)+(n?1:0))throw new Error("InvalidSourceParameters");r?(t="path="+encodeURIComponent(e.path),c.path=e.path):n?(t="url="+encodeURIComponent(e.url),c.url=e.url):o&&(l=e.expr,t="expr="+encodeURIComponent(e.expr),i=!0,c.expr=e.expr)}if(!a)throw new Error("InvalidSource");var d=(a="/"===a.charAt(a.length-1)?a.substring(0,a.length-1):a)+"/notebooks/embedding";return i||(d+="?"+t),new Promise(function(n,r){var o=new XMLHttpRequest;o.addEventListener("load",function(){var e,t;200===o.status?(t=o.responseText,(t=(e=JSON.parse(t)).extraData||[]).notebookSource=c,n({notebookID:e.notebookID,mainScript:a+e.mainScript,otherScripts:(e.otherScripts||[]).map(function(e){return a+e}),stylesheetDomains:[a].concat(e.stylesheetDomains),notebookExpr:l,extraData:t})):r(new Error("ResolveError"))}),o.addEventListener("error",function(){r(new Error("RequestError"))}),o.addEventListener("abort",function(){r(new Error("RequestAborted"))}),i?(o.open("POST",d,!0),o.setRequestHeader("Content-type","application/x-www-form-urlencoded")):o.open("GET",d,!0),i?o.send(t):o.send()})}function l(e,t){return void 0===e?t:e}function t(t,h,e){var p,f,o=e||{},e=o.useShadowDOM,m=void 0!==e&&e&&h.attachShadow,r=[h],w=[];function a(e){var t=e.width,n=e.height;r.forEach(function(e){null!=t&&(e.style.width=t+"px"),null!=n&&(e.style.height=n+"px")})}return Promise.resolve().then(function(){var e;return m?((e=document.createElement("div")).style.width="100%",e.style.height="100%",f=e.attachShadow({mode:"open"}),(p=document.createElement("div")).style.width="100%",p.style.height="100%",h.appendChild(e),Object.defineProperty(p,"ownerDocument",{value:f}),f.createElement=function(){var e;return(e=document).createElement.apply(e,arguments)},f.createElementNS=function(){var e;return(e=document).createElementNS.apply(e,arguments)},f.createTextNode=function(){var e;return(e=document).createTextNode.apply(e,arguments)},f.createDocumentFragment=function(){var e;return(e=document).createDocumentFragment.apply(e,arguments)},f.documentElement=p,f.defaultView=window,r.push(e),r.push(p),f.appendChild(p)):p=h,i(t)}).then(function(e){var t=e.notebookID,n=e.mainScript,r=e.otherScripts,o=e.stylesheetDomains,a=e.notebookExpr,i=e.extraData;if(m){o=y(f,o);w.push(o);for(var l=0;l<h.children.length-1;++l)p.appendChild(h.children[l])}for(var c,d,u,s=0;s<r.length;++s)c=r[s],d=void 0,v[c]||((d=document.createElement("script")).type="text/javascript",d.src=c,document.getElementsByTagName("head")[0].appendChild(d),v[c]=!0);return g[u=n]||(g[u]=new Promise(function(t,e){var n,r=document.createElement("script");for(r.type="text/javascript",r.onerror=e;n="_wolframNotebookEmbedderCallback"+ ++b,window[n];);window[n]=function(e){delete window[n],t(e)},r.src=u+"?callback="+n,document.getElementsByTagName("head")[0].appendChild(r)})),g[u].then(function(e){return[t,e,a,i]})}).then(function(e){var t=e[0],n=e[1],r=e[2],e=e[3];return n.embed(t,p,{width:l(o.width,null),maxHeight:l(o.maxHeight,1/0),showBorder:l(o.showBorder,null),allowInteract:l(o.allowInteract,!0),showRenderProgress:l(o.showRenderProgress,!0),notebookExpr:r,extraData:e,onContainerDimensionsChange:a})}).then(function(a){return n({},a,{detach:function(){a.detach(),w.forEach(function(e){e()})},setAttributes:function(e){var t=e.width,n=e.maxHeight,r=e.showBorder,o=e.allowInteract,e=e.showRenderProgress;a.setAttributes({width:t,maxHeight:n,showBorder:r,allowInteract:o,showRenderProgress:e})}})})}e.default={embed:t},e.embed=t,Object.defineProperty(e,"__esModule",{value:!0})});