{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-petrinet/src/client/reactive/active-group/view.js"],"names":["pushIfMissing","removeIfExisting","identity","View","constructor","items","downstream","enterCallbacks","exitCallbacks","layersByItem","Map","isActiveGroup","safeAdd","item","wasNewItem","console","log","forEach","enterCallback","ea","newItemFromUpstream","safeRemove","gotRemoved","exitCallback","destroyItemFromUpstream","now","arr","push","size","length","enter","callback","exit","layer","partialBehavior","has","set","Layer","refineObject","layerForItem","get","isGlobal","beGlobal","beNotGlobal"],"mappings":";;;;;;AAASA,mB,YAAAA,a;AAAeC,sB,YAAAA,gB;AAAkBC,c,YAAAA,Q;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE1C;;;;;AAKe,YAAMC,IAAN,CAAW;AACxBC,sBAAc;AACZ,eAAKC,KAAL,GAAa,EAAb;AACA,eAAKC,UAAL,GAAkB,EAAlB;AACA,eAAKC,cAAL,GAAsB,EAAtB;AACA,eAAKC,aAAL,GAAqB,EAArB;;AAEA,eAAKC,YAAL,GAAoB,IAAIC,GAAJ,EAApB;AACD;AACD,YAAIC,aAAJ,GAAoB;AAAE,iBAAO,IAAP;AAAc;;AAEpCC,gBAAQC,IAAR,EAAc;AACZ,cAAIC,aAAad,cAAc,KAAKK,KAAnB,EAA0BQ,IAA1B,CAAjB;AACA,cAAGC,UAAH,EAAe;AACbC,oBAAQC,GAAR,CAAY,oBAAZ,EAAkCH,IAAlC;AACA,iBAAKN,cAAL,CAAoBU,OAApB,CAA4B,UAASC,aAAT,EAAwB;AAAEA,4BAAcL,IAAd;AAAsB,aAA5E;AACA,iBAAKP,UAAL,CAAgBW,OAAhB,CAAwB,UAASE,EAAT,EAAa;AAAEA,iBAAGC,mBAAH,CAAuBP,IAAvB;AAA+B,aAAtE;AACD;AACF;;AAEDQ,mBAAWR,IAAX,EAAiB;AACf,cAAIS,aAAarB,iBAAiB,KAAKI,KAAtB,EAA6BQ,IAA7B,CAAjB;AACA,cAAGS,UA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B3B,IAAjB,CAAnB;AACA,gBAAG,CAAC0B,aAAaE,QAAb,EAAJ,EAA6B;AAC3BF,2BAAaG,QAAb;AACD;AACF,WAVD;;AAYA,eAAKV,IAAL,CAAU,UAASnB,IAAT,EAAe;AACvB,gBAAI0B,eAAe9B,aAAa+B,GAAb,CAAiB3B,IAAjB,CAAnB;AACA,gBAAG0B,gBAAgBA,aAAaE,QAAb,EAAnB,EAA4C;AAC1CF,2BAAaI,WAAb;AACD;AACF,WALD;;AAOA,iBAAO,IAAP;AACD;AA1GuB;;yBAALxC,I","file":"view.js","sourcesContent":["import { pushIfMissing, removeIfExisting, identity } from './utils.js';\n\n/**\n *\n * @class View\n * @classdesc This is the class representing a view.\n */\nexport default class View {\n  constructor() {\n    this.items = [];\n    this.downstream = [];\n    this.enterCallbacks = [];\n    this.exitCallbacks = [];\n\n    this.layersByItem = new Map();\n  }\n  get isActiveGroup() { return true; }\n  \n  safeAdd(item) {\n    var wasNewItem = pushIfMissing(this.items, item);\n    if(wasNewItem) {\n      console.log('added to selection', item);\n      this.enterCallbacks.forEach(function(enterCallback) { enterCallback(item); });\n      this.downstream.forEach(function(ea) { ea.newItemFromUpstream(item); });\n    }\n  }\n\n  safeRemove(item) {\n    var gotRemoved = removeIfExisting(this.items, item);\n    if(gotRemoved) {\n      console.log('removed from selection', item);\n      this.exitCallbacks.forEach(function(exitCallback) { exitCallback(item); });\n      this.downstream.forEach(function(ea) { ea.destroyItemFromUpstream(item); });\n    }\n  }\n\n  // Get persistent version of the current state of the Selection.\n  /**\n   * Returns an Array of the objects that are currently in the set. This Array does not update automatically.\n   * @function View#now\n   * @return {Array}\n   */\n  now() {\n    var arr = [];\n    this.items.forEach(function(item) {\n      arr.push(item);\n    });\n\n    return arr;\n  }\n\n  /**\n   * Returns the current number of objects the set contains.\n   * @function View#size\n   * @return {Number}\n   */\n  size() { return this.now().length; }\n\n  /**\n   * Takes a callback that consumes a single parameter. This callback is called whenever an object is added to the reactive set with that very object.\n   * @function View#enter\n   * @param {View~enterCallback} callback - this is executed everytime an object is added to the set\n   * @return {View} The callee of this method.\n   */\n  enter(callback) {\n    this.enterCallbacks.push(callback);\n    this.now().forEach(function(item) {  callback(item); });\n\n    return this;\n  }\n\n  /**\n   * Similar to \\texttt{enter}, but the callback is called everytime an object is removed from the set.\n   * @function View#exit\n   * @param {View~exitCallback} callback\n   * @return {View} The callee of this method.\n   */\n  exit(callback) {\n    this.exitCallbacks.push(callback);\n\n    return this;\n  }\n\n  /**\n   * Define partial behavior attached to each object while it is contained in the set.\n   * @function View#layer\n   * @param {Object} partialBehavior - the mixin to be applied.\n   * @returns {View} The callee of this method.\n   */\n  // TODO: is this currently limited to 1 layer per item-view combination?\n  layer(partialBehavior) {\n    var layersByItem = this.layersByItem;\n\n    this.enter(function(item) {\n      // lazy initialization\n      if(!layersByItem.has(item)) {\n        layersByItem.set(item, new Layer().refineObject(item, partialBehavior));\n      }\n\n      var layerForItem = layersByItem.get(item);\n      if(!layerForItem.isGlobal()) {\n        layerForItem.beGlobal();\n      }\n    });\n\n    this.exit(function(item) {\n      var layerForItem = layersByItem.get(item);\n      if(layerForItem && layerForItem.isGlobal()) {\n        layerForItem.beNotGlobal();\n      }\n    });\n\n    return this;\n  }\n}\n\n/**\n * This callback is call whenever an item is added to this {@link View}.\n * @callback View~enterCallback\n * @param {Object} item - the item that was just added to the {@link View}.\n */\n\n/**\n * This callback is call whenever an item is removed from this {@link View}.\n * @callback View~exitCallback\n * @param {Object} item - the item that was just removed from the {@link View}.\n */\n"]}