{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-petrinet/src/client/scoped-scripts.js"],"names":["proceed","layer","cop","Layers","lively","ScopedScripts","load","openPromises","documentRoot","lively4url","documentLocation","window","location","layers","url","optBody","toString","replace","URL","documentBody","document","body","ImportLayer","DocumentLayer","LocalLayer","PropagateLayerActicationLayer","refineClass","Promise","then","onresolve","onerror","currentLayers","newResolve","args","arguments","withLayers","apply","newError","undefined","refineObject","loadJavaScriptThroughDOM","name","force","globalLayers","forEach","ea","beGlobal","result","beNotGlobal","error","System","import","parentName","parentAddress","write","a","console","log","div","createElement","innerHTML","childNodes","myPromise","resolve","reject","tagName","s","src","getAttribute","match","addEventListener","push","appendChild"],"mappings":";;;;;;;;;AAAQA,a,qCAAAA,O;AAASC,W,qCAAAA,K;AACLC,S;;AACAC,Y;;AACLC,Y;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGQ,YAAMC,aAAN,CAAoB;;AAEjC,eAAOC,IAAP,GAAc;AACZ,eAAKC,YAAL,GAAoB,EAApB;AACA,eAAKC,YAAL,GAAoBC,UAApB;AACA,eAAKC,gBAAL,GAAwBC,OAAOC,QAA/B;AACD;;AAED,eAAOC,MAAP,CAAcC,GAAd,EAAmBC,OAAnB,EAA4B;AAC1B,eAAKP,YAAL,GAAoBM,IAAIE,QAAJ,GAAeC,OAAf,CAAuB,QAAvB,EAAgC,EAAhC,CAApB;AACA,eAAKP,gBAAL,GAAwB,IAAIQ,GAAJ,CAAQJ,GAAR,CAAxB;AACA,eAAKK,YAAL,GAAoBJ,WAAWK,SAASC,IAAxC;AACA,iBAAO,CAAC,KAAKC,WAAN,EAAmB,KAAKC,aAAxB,EAAuC,KAAKC,UAA5C,EAAwD,KAAKC,6BAA7D,CAAP;AACD;AAbgC;;yBAAdpB,a;;;;;;;;;;;;;;;;AAgBrB;;;AAGA;AACAJ,YAAMU,MAAN,EAAc,+BAAd,EAA+Ce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AA;AAAA;AAAA,kBAAJ;AACA,oBAAIQ,MAAMtB,GAAGuB,YAAH,CAAgB,KAAhB,CAAV;AACA,oBAAI,CAACD,IAAIE,KAAJ,CAAU,wBAAV,CAAL,EACEF,MAAMA,IAAIlD,OAAJ,CAAY,GAAZ,EAAiBZ,cAAcG,YAA/B,CAAN;AACF0D,kBAAEC,GAAF,GAAQA,GAAR;;AAEAX,wBAAQC,GAAR,CAAY,aAAZ,EAA2BZ;AAC3B;AADA,kBAEAA,KAAKqB,CAAL;AACArB,mBAAGyB,gBAAH,CAAoB,MAApB,EAA4B,MAAM;AAChCP;AACD,iBAFD;AAGAlB,mBAAGyB,gBAAH,CAAoB,OAApB,EAA6B,MAAM;AACjCN;AACD,iBAFD;AAGD;AACF,aAlBe,CAAhB;AAmBA3D,0BAAcE,YAAd,CAA2BgE,IAA3B,CAAgCT,SAAhC;AACAzD,0BAAcc,YAAd,CAA2BqD,WAA3B,CAAuC3B,EAAvC,EAtB4B,CAsBgB;AAC7C,WAvBD;AAwBD,SAlCmD;AAmCpDc,sBAAclB,IAAd,EAAoB;AAClB;AACA,iBAAOvC,IAAIF,OAAJ,CAAYoC,KAAZ,CAAkB,IAAlB,EAAwBF,SAAxB,CAAP;AACD;AAtCmD,OAAtD;;AA0CA;;;;;;AAMA7B,oBAAcC,IAAd","file":"scoped-scripts.js","sourcesContent":["import {proceed, layer} from \"src/client/ContextJS/src/contextjs.js\";\nimport * as cop  from \"src/client/ContextJS/src/contextjs.js\";\nimport * as Layers  from \"src/client/ContextJS/src/Layers.js\";\nimport lively from \"src/client/lively.js\";\n\n\nexport default class ScopedScripts {\n\n  static load() {\n    this.openPromises = []\n    this.documentRoot = lively4url;\n    this.documentLocation = window.location;\n  }\n\n  static layers(url, optBody) {\n    this.documentRoot = url.toString().replace(/[^/]*$/,\"\");\n    this.documentLocation = new URL(url);\n    this.documentBody = optBody || document.body;\n    return [this.ImportLayer, this.DocumentLayer, this.LocalLayer, this.PropagateLayerActicationLayer];\n  }\n}\n\n/*\n * Captures the layer activation on Promise definition and replays it when resolving the promise\n */\n// #TODO it seems, we cannot layer \"then\" because it will sometimes result in an maximum stack size exeception\nlayer(window, \"PropagateLayerActicationLayer\").refineClass(Promise, {\n\tthen(onresolve, onerror) {\n    // return cop.proceed(onresolve, onerror)\n    var layers = Layers.currentLayers();\n    // console.log(\"Promise.then ... \");\n\t\tvar newResolve = function() {\n      var args = arguments;\n      // console.log(\"replay layers...\" + layers);\n      return cop.withLayers(layers, () => onresolve.apply(window, args));\n\t\t};\n\t\tvar newError = function() {\n      var args = arguments;\n      return cop.withLayers(layers, () => onerror.apply(window, args));\n\t\t};\n\t\treturn cop.proceed(onresolve ? newResolve : undefined, onerror ? newError : undefined);\n\t}\n}).refineObject(lively, {\n  loadJavaScriptThroughDOM(name, url, force) {\n    var globalLayers = Layers.currentLayers();\n    globalLayers.forEach( ea => {\n      // console.log(\"Layer enable: \" + ea)\n      ea.beGlobal();\n    });\n    return cop.proceed(name, url, force).then( (result) => {\n      globalLayers.forEach( ea => {\n        // console.log(\"Layer disable: \" + ea)\n        ea.beNotGlobal();\n      });\n      return result;\n    }, (error) => {\n      globalLayers.forEach( ea => ea.beNotGlobal());\n      return error;\n    });\n  }\n});\n\n\nlayer(window, \"ImportLayer\").refineObject(System, {\n\timport(name, parentName, parentAddress) {\n    // console.log(\"System.import \" + name +', ' + parentName + \", \" + parentAddress)\n\t\tname = name.replace(/^.\\//, ScopedScripts.documentRoot);\n\t\t// lively.notify(\"import \"+ name + \", \" + parentName +\",\"+ parentAddress)\n\t\treturn cop.proceed(name, parentName, parentAddress);\n\t}\n});\n\nlayer(ScopedScripts, \"LocalLayer\").refineObject(lively, {\n\tget location() {\n    // lively.notify(\"get location\");\n\t\treturn new URL(ScopedScripts.documentLocation);\n\t},\n\tset location(url) {\n    // lively.notify(\"get location\");\n\t\treturn cop.proceed(url)\n\t}\n});\n\n\nlayer(window, \"DocumentLayer\").refineObject(document, {\n  get body() {\n    return ScopedScripts.documentBody\n  },\n\n\twrite(a) {\n    console.log(\"document.write \" + a);\n    // console.log(\"BEGIN\")\n    var div = document.createElement(\"div\");\n    div.innerHTML = a;\n    div.childNodes.forEach( ea => {\n      // console.log(\"append child: \" + ea);\n      var myPromise = new Promise((resolve, reject) => {\n        if (ea.tagName == \"SCRIPT\") {\n          var s = document.createElement(\"script\");\n          var src = ea.getAttribute(\"src\")\n          if (!src.match(/^(https?\\:\\/\\/)|(\\/\\/)/) )\n            src = src.replace(/^/, ScopedScripts.documentRoot);\n          s.src = src\n\n          console.log(\"old src... \", ea)\n          // s.async = false;\n          ea = s;\n          ea.addEventListener(\"load\", () => {\n            resolve();\n          });\n          ea.addEventListener(\"error\", () => {\n            reject();\n          });\n        }\n      });\n      ScopedScripts.openPromises.push(myPromise);\n      ScopedScripts.documentBody.appendChild(ea); // #TODO instanctiate layers\n    });\n  },\n  createElement(name) {\n    // console.log(\"document.createElement \" + name);\n    return cop.proceed.apply(this, arguments);\n  }\n});\n\n\n/* Workspace\nScopedScripts.documentLocation = \"https://lively-kernel.org/lively4/lively4-jens/demos/example.html\"\nScopedScripts.LocalLayer.beGlobal()\nScopedScripts.ImportLayer\n*/\n\nScopedScripts.load();\n"]}