{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-livelyenergy/src/components/demo/lively-simulation-cell.js"],"names":["Morph","_","DEFAULT_VIEW","LivelySimulationCell","initialize","hasAttribute","isMirrorCell","initializeTitleBar","initializeCodeView","initializeLogView","initializeViewSlot","addEventListener","bringToFront","titleBar","get","initializeName","codeView","initializeState","initializeSnippet","logView","initializeInterval","shouldSkip","viewSlot","setAttribute","switchViewTo","livelyPrepareSave","getName","stringify","getState","getSnippet","getActiveView","removeAttribute","onPointerMove","event","clientX","clientY","lastMove","pick","onPointerUp","anchor","document","removeEventListener","simulation","getSimulation","foregroundCell","getForegroundCell","selector","shadowRoot","querySelector","getNormalizedName","setName","name","highlight","setState","state","time","log","timestamp","clearLog","execute","scope","executeSingle","resolve","executeSelf","executeSingleCell","delete","removeMirrorCells","remove","clone","cloneCell","mirror","mirrorCell","target","views","map","forEach","view","toggleSkip","startGrabbing","initPosition","parentBounds","getBoundingClientRect","isFocused","cellRef"],"mappings":"AAAA;;;;AAIA;;;;;;;;;;;;;;;;;AAEOA,W;;AACAC,O;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,eAAe,UAArB;;;;;;;;;;;;;;;;;AAEe,YAAMC,oBAAN,SAAmCH,KAAnC,CAAyC;;AAEtD;AACAI,qBAAa;AACX,yEAAqB,IAArB,6BAA6C,IAA7C;AACA,uEAAmB,IAAnB,2BAAyC,IAAzC;AACA,yCAAkB,qFAAKC,YAAL,CAAkB,kBAAlB,sFAAyC,KAAKC,YAAL,EAAzC,CAAlB;AACA,4CAAqB,KAArB;AAJW;AAKX,eAAKC,kBAAL;AALW;AAMX,eAAKC,kBAAL;AANW;AAOX,eAAKC,iBAAL;AAPW;AAQX,eAAKC,kBAAL;AARW;AASX,eAAKC,gBAAL,CAAsB,WAAtB,EAAmC;AAAM,uGAAKC,YAAL;AAAN,WAAnC;AACD;;AAEDL,6BAAqB;AAAA;;AACnB,gBAAMM,kFAAW,KAAKC,GAA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aAAxB,aAAuC,IAAvC;AAFwC;AAGxC,qGAAOA,gBAAP,CAAwB,WAAxB,aAAqC,IAArC;AACA,4GAAgB,0EAAE0B,IAAF,kFAAOJ,KAAP,GAAc,CAAC,SAAD,EAAY,SAAZ,CAAd,CAAhB;AACA,4GAAI8C,YAAJ,EAAkB;AAAA;;AAChB,kBAAMrC,8FAAa,KAAKC,aAAL,EAAb,CAAN;AACA,kBAAMqC,8GAAe,uGAAWC,qBAAX,EAAf,CAAN;AACA,yDAAkB,GAAE,2NAAgBD,YAAhB,OAA+B,IAAnD;AACA,0DAAmB,GAAE,2NAAgBA,YAAhB,UAAiC,kCAAmB,CAAE,IAA3E;AACD;AACF;;AAEDE,oBAAY;AACV,iBAAO,8FAAKpE,GAAL,CAAS,WAAT,iHACJ,KAAKA,GAAL,CAAS,WAAT,CADI,gHAEJ,KAAKA,GAAL,CAAS,UAAT,CAFI,mBAAP;AAGD;;AAED6B,wBAAgB;AACd,4BAAO,IAAP;AACD;;AAEDb,wBAAgB;AACd,qHAAO,KAAKhB,GAAL,CAAS,WAAT,CAAP,qBAA2C,SAA3C;AACD;;AAEDsC,kBAAU+B,OAAV,EAAmB;AAAA;;AACjB,wGAAKrE,GAAL,CAAS,WAAT,kBAAgC,yMAAY,KAAKmC,iBAAL,EAAZ,qBAAhC;AACA,wGAAKnC,GAAL,CAAS,WAAT,uGAAgCqE,OAAhC;AACD;;AAED7E,uBAAe;AACb,gGAAO,KAAKD,YAAL,CAAkB,gBAAlB,CAAP;AACD;AAnMqD;;yBAAnCF,oB","file":"lively-simulation-cell.js","sourcesContent":["/*MD\n  ![](https://lively-kernel.org/lively4/lively4-core/demos/lively-simulation/screenshots/cell.png){width=500px}\nMD*/\n\n\"enable aexpr\";\n\nimport Morph from 'src/components/widgets/lively-morph.js';\nimport _ from 'src/external/lodash/lodash.js';\n\nconst DEFAULT_VIEW = 'codeView';\n\nexport default class LivelySimulationCell extends Morph {\n  \n  // life cycle\n  initialize() {\n    this.onPointerMove = this.onPointerMove.bind(this);\n    this.onPointerUp = this.onPointerUp.bind(this);\n    this.shouldSkip = this.hasAttribute('data-should-skip') || this.isMirrorCell();\n    this.executeSingle = false;\n    this.initializeTitleBar();\n    this.initializeCodeView();\n    this.initializeLogView();\n    this.initializeViewSlot();\n    this.addEventListener('mousedown', () => this.bringToFront());\n  }\n  \n  initializeTitleBar() {\n    const titleBar = this.get('#titleBar');\n    titleBar.initializeName(this.dataset['name']);\n  }\n  \n  initializeCodeView() {\n    const codeView = this.get('#codeView');\n    codeView.initializeState(this.dataset['state']);\n    codeView.initializeSnippet(this.dataset['snippet']);\n  }\n  \n  initializeLogView() {\n    const logView = this.get('#logView');\n    logView.initializeInterval(this.dataset['loginterval']);\n  }\n  \n  initializeViewSlot() {\n    const { shouldSkip } = this;\n    const viewSlot = this.get('#viewSlot');\n    viewSlot.setAttribute('disabled', shouldSkip);\n    this.switchViewTo(this.dataset['view']);\n  }\n  \n  livelyPrepareSave() {\n    this.dataset['name'] = this.getName();\n    this.dataset['state'] = JSON.stringify(this.getState());\n    this.dataset['snippet'] = this.getSnippet();\n    this.dataset['view'] = this.getActiveView();\n    this.dataset['loginterval'] = this.get('#logView').getInterval();\n    if (this.shouldSkip) this.setAttribute('data-should-skip', true);\n    else this.removeAttribute('data-should-skip');\n  }\n  \n  // event handler\n  onPointerMove(event) {\n    const { clientX, clientY } = event;\n    const { lastMove } = this;\n    this.style.left = `${this.offsetLeft + clientX - lastMove.clientX}px`;\n    this.style.top = `${this.offsetTop + clientY - lastMove.clientY}px`;\n    this.lastMove = _.pick(event, ['clientX', 'clientY']);\n  }\n\n  onPointerUp() {\n    const anchor = document.body.parentElement;\n    anchor.removeEventListener('pointermove', this.onPointerMove);\n    anchor.removeEventListener('pointerup', this.onPointerUp);\n  }\n  \n  // other\n  bringToFront() {\n    const simulation = this.getSimulation();\n    if (!simulation.getForegroundCell) return;\n    const foregroundCell = simulation.getForegroundCell();\n    if (foregroundCell === this) return;\n    this.style.zIndex = parseInt(foregroundCell.style.zIndex || 1) + 1;\n  }\n  \n  get(selector) {\n    const { shadowRoot } = this;\n    return shadowRoot.querySelector(selector);\n  }\n  \n  getName() {\n    return this.get('#titleBar').getName();\n  }\n  \n  getNormalizedName() {\n    return this.get('#titleBar').getNormalizedName();\n  }\n  \n  setName(name) {\n    this.get('#titleBar').setName(name);\n    const simulation = this.getSimulation();\n    this.highlight(simulation.currentHighlight);\n  }\n  \n  getState() {\n    return this.get('#codeView').getState();\n  }\n  \n  setState(state, time) {\n    if (time) this.log(time, state);\n    return this.get('#codeView').setState(state);\n  }\n  \n  getSnippet() {\n    return this.get('#codeView').getSnippet();\n  }\n  \n  log(timestamp, state) {\n    this.get('#chartView').append(timestamp, state);\n    this.get('#logView').log(timestamp, state);\n  }\n  \n  clearLog() {\n    this.get('#chartView').reset();\n    this.get('#logView').clearLog();\n  }\n  \n  execute(scope = {}) {\n    const { executeSingle, shouldSkip } = this;\n    if (!executeSingle && shouldSkip || this.isMirrorCell()) return Promise.resolve(scope);\n    return this.get('#codeView').execute(scope);\n  }\n  \n  executeSelf() {\n    if (this.executeSingle || this.isMirrorCell()) return\n    const simulation = this.getSimulation();\n    if (!simulation.executeSingleCell) return;\n    this.executeSingle = true;\n    simulation.executeSingleCell(this).finally(() => this.executeSingle = false);\n  }\n  \n  delete() {\n    const simulation = this.getSimulation();\n    if (simulation.removeMirrorCells && !this.isMirrorCell()) {\n      simulation.removeMirrorCells(this.getName());\n    }\n    this.remove();\n  }\n  \n  clone(event) {\n    const simulation = this.getSimulation();\n    if (!simulation.cloneCell) return;\n    simulation.cloneCell(event, this);\n  }\n  \n  mirror(event) {\n    const simulation = this.getSimulation();\n    if (!simulation.mirrorCell) return;\n    simulation.mirrorCell(event, this);\n  }\n  \n  switchViewTo(target = DEFAULT_VIEW) {\n    const views = _.map(['codeView', 'logView', 'chartView'], name => this.get(`#${name}`));\n    _.forEach(views, view => view.classList.remove('active'));\n    this.get(`#${target}`).classList.add('active');\n  }\n  \n  toggleSkip() {\n    if (this.isMirrorCell()) return;\n    this.shouldSkip = !this.shouldSkip;\n    const { shouldSkip } = this;\n    this.get('#viewSlot').setAttribute('disabled', shouldSkip);\n  }\n  \n  startGrabbing(event, initPosition = true) {\n    const anchor = document.body.parentElement;\n    anchor.addEventListener('pointermove', this.onPointerMove);\n    anchor.addEventListener('pointerup', this.onPointerUp);\n    this.lastMove = _.pick(event, ['clientX', 'clientY']);\n    if (initPosition) {\n      const simulation = this.getSimulation();\n      const parentBounds = simulation.getBoundingClientRect();\n      this.style.top = `${event.clientY - parentBounds.y}px`;\n      this.style.left = `${event.clientX - parentBounds.x - this.clientWidth / 2}px`;\n    }\n  }\n  \n  isFocused() {\n    return this.get('#codeView').isFocused() \n    || this.get('#titleBar').isFocused() \n    || this.get('#logView').isFocused();\n  }\n  \n  getSimulation() {\n    return this.parentElement;\n  }\n  \n  getActiveView() {\n    return this.get('#viewSlot').querySelector('.active').id;\n  }\n  \n  highlight(cellRef) {\n    this.get('#titleBar').highlight(cellRef === this.getNormalizedName().toLowerCase());\n    this.get('#codeView').highlight(cellRef);\n  }\n  \n  isMirrorCell() {\n    return this.hasAttribute('data-is-mirror');\n  }\n}\n"]}