{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-core/src/babylonian-programming-editor/custom-instance-template-editor.js"],"names":["Morph","debounce","InstanceList","AddButton","MinusButton","CustomInstance","DEFAULT_CODE","CustomInstanceTemplateEditor","initialize","windowTitle","_instances","_activeInstance","_callback","Function","initInstanceList","initEditor","initButtons","_instanceList","get","instance","activeInstance","_editor","addEventListener","editor","setOption","checkSyntax","on","value","code","_nameInput","name","render","buttons","innerHTML","appendChild","push","splice","indexOf","setup","instances","callback","includes","length"],"mappings":";;;;;;AAAOA,W;;AACEC,c,UAAAA,Q;;AACFC,kB;;AACEC,e,gBAAAA,S;AAAWC,iB,gBAAAA,W;;AACbC,oB;;;;;;;;;;;AAJAL,gD;;;;;;;;;;;;;AACEC,mD;;;;;;;;;;;;;AACFC,uD;;;;;;;;;;;;;AACaE,sD;;;;;;;;;;;;;AAAXD,oD;;;;;;;;;;;;;AACFE,yD;;;;;;;;;AAEP,YAAMC,eAAe,cAArB;;;;;;;;AAAMA,uD;;;;;;;AAES,YAAMC,4BAAN,SAA2CP,KAA3C,CAAiD;;AAE9DQ,qBAAa;AACX,eAAKC,WAAL,GAAmB,iCAAnB;;AAEA,eAAKC,UAAL,GAAkB,EAAlB;AACA,eAAKC,eAAL,GAAuB,IAAvB;AACA,eAAKC,SAAL,GAAiB,IAAIC,QAAJ,EAAjB;;AAEA,eAAKC,gBAAL;AACA,eAAKC,UAAL;AACA,eAAKC,WAAL;AACD;;AAED,oBAAYF,iBAAgB,oMAAhB,GAAuN;AACjO,oBAAU,KAAKG,aAAL,CAAkB,MAAlB,GAA2B,IAAIf,YAAJ,CACnC,KAAKgB,GAAL,CAAS,gBAAT,CADmC,EAEnC,KAAKR,UAF8B,EAGlCS,QAAD,IAAc,KAAKC,cAAL,GAAsBD,QAHD,CAA3B;AAKX;;AAEDJ,qBAAa;AACX,eAAKM,OAAL,GAAe,KAAKH,GAAL,CAAS,SAAT,CAAf;AACA,eAAKG,OAAL,CAAaC,gBAAb,CAA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OAAL,CAAaE,MAAb,CAAoBC,SAApB,CAA8B,UAA9B,EAA0C,IAA1C;AACD;AACD,eAAKP,aAAL,CAAmBG,cAAnB,GAAoC,KAAKT,eAAzC;AACD;;AAxG6D;;yBAA3CJ,4B;;;;;;;;6BAAAA,6D","file":"custom-instance-template-editor.js","sourcesContent":["import Morph from 'src/components/widgets/lively-morph.js';\nimport { debounce } from \"utils\";\nimport InstanceList from \"./ui/instance-list.js\";\nimport { AddButton, MinusButton } from \"./ui/buttons.js\";\nimport CustomInstance from \"./utils/custom-instance.js\";\n\nconst DEFAULT_CODE = \"return null;\";\n\nexport default class CustomInstanceTemplateEditor extends Morph {\n\n  initialize() {\n    this.windowTitle = \"Custom Instance Template Editor\";\n\n    this._instances = [];\n    this._activeInstance = null;\n    this._callback = new Function();\n\n    this.initInstanceList();\n    this.initEditor();\n    this.initButtons();\n  }\n  \n  /*example:*/initInstanceList/*{\"id\":\"3d56_9324_7687\",\"name\":{\"mode\":\"input\",\"value\":\"\"},\"color\":\"hsl(290, 30%, 70%)\",\"values\":{},\"instanceId\":{\"mode\":\"connect\",\"value\":\"3d56_9324_7687_this\"},\"prescript\":\"\",\"postscript\":\"\"}*/() {\n    /*probe:*/this._instanceList/*{}*/ = new InstanceList(\n      this.get(\"#instance-list\"),\n      this._instances,\n      (instance) => this.activeInstance = instance\n    );\n  }\n  \n  initEditor() {\n    this._editor = this.get(\"#editor\");\n    this._editor.addEventListener(\"editor-loaded\", () => {\n      // Disable linting and syntax checking (otherwise it warns about 'return outside of funcion')\n      this._editor.editor.setOption(\"lint\", false);\n      this._editor.checkSyntax = new Function();\n\n      // Send feedback to editor on change\n      this._editor.editor.on(\"change\", ((value) => {\n        if(this._activeInstance) {\n          this._activeInstance.code = this._editor.value;\n        }\n        if(this._callback) {\n          this._callback();\n        }\n      })::debounce(1000));\n    });\n    \n    this._nameInput = this.get(\"#name-input\");\n    this._nameInput.addEventListener(\"change\", () => {\n      if(this._activeInstance) {\n        this._activeInstance.name = this._nameInput.value;\n      }\n      if(this._callback) {\n        this._callback();\n      }\n      this._instanceList.render();\n    });\n  }\n  \n  initButtons() {\n    const buttons = this.get(\"#buttons\");\n    buttons.innerHTML = \"\";\n    \n    buttons.appendChild(AddButton(() => {\n      this._instances.push(new CustomInstance(\"New instance\"));\n      this._instanceList.render();\n    }));\n    \n    buttons.appendChild(MinusButton(() => {\n      if(this._activeInstance) {\n        this._instances.splice(this._instances.indexOf(this._activeInstance), 1);\n        \n        this.activeInstance = null;\n        this._instanceList.render();\n        \n        if(this._callback) {\n          this._callback();\n        }\n      }\n    }));\n  }\n  \n  \n\n  setup(instances, callback) {\n    this._callback = callback;\n    this._instances = instances;\n    this._instanceList.instances = instances;\n    this.activeInstance = this._instances[0];\n  }\n\n  get activeInstance() {\n    return this._activeInstance;\n  }\n\n  set activeInstance(instance) {\n    if(instance && this._instances.includes(instance)) {\n      this._activeInstance = instance;\n      this._nameInput.value = this._activeInstance.name;\n      this._editor.editor.setOption(\"readonly\", false);\n      if(this._activeInstance.code && this._activeInstance.code.length) {\n        this._editor.value = this._activeInstance.code;\n      } else {\n        this._editor.value = DEFAULT_CODE;\n      }\n    } else {\n      this._nameInput.value = \"\";\n      this._editor.value = \"\";\n      this._editor.editor.setOption(\"readonly\", true);\n    }\n    this._instanceList.activeInstance = this._activeInstance;\n  }\n\n}\n/* Context: {\"context\":{\"prescript\":\"\",\"postscript\":\"\"},\"customInstances\":[{\"id\":\"cca8_9ef5_45ae\",\"name\":\"Test Person\",\"code\":\"return null;\"}]} */"]}