{"version":3,"sources":["https://lively-kernel.org/lively4/BP2019RH1/src/internal/individuals-as-points/group-chaining/grouping-menu.js"],"names":["GroupingMenu","constructor","grouper","attributes","currentGroupings","initGroupingSelectBox","containerElement","world","addGrouping","removeGrouping","selectContainer","groupingBar","id","appendChild","uniqueValues","textElement","selectElement","buttonElement","sort","innerHTML","value","options","length","Option","menu","addEventListener","selectedIndex","includes","addGroupingLegendAndRemoveGroupingButton","removeCurrentLegend","groupingLegend","generateGroupingLegend","removeGroupingButton","generateRemoveGroupingButton","disablePreviousRemoveGroupingButton","push","currentLegend","lively","query","parentElement","removeChild","e","console","log","setAttribute","clickedGroupingKey","getAttribute","filter","groupingKey","addLegendFromLastGrouping","enableLastRemoveGroupingButton","key","uniqueValuesForKey","getUniqueValuesForKey","groupingLegendTable","forEach","tableRow","valueTableCell","d3Color","groupingLayouter","getColorForValue","style","backgroundColor","getRGBValueFromD3Color","padding","toString","getFilteredData","points","activeFilterExpr","getLastButtonOfButtonBar","disabled","previousGroupingKey","slice"],"mappings":";;;;;;;;;;;;;;;;AAAO,YAAMA,YAAN,CAAmB;;AAExBC,oBAAYC,OAAZ,EAAqBC,UAArB,EAAiC;AAC/B,eAAKD,OAAL,GAAeA,OAAf;AACA,eAAKC,UAAL,GAAkBA,UAAlB;AACA,eAAKC,gBAAL,GAAwB,EAAxB;AACD;;AAEDC,8BAAsBC,gBAAtB,EAAwCC,KAAxC,EAA+CC,WAA/C,EAA4DC,cAA5D,EAA4E;AAC1E,cAAIC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAJ;AACA,cAAIC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAJ;AACA,eAAKL,gBAAL,GAAwBA,gBAAxB;AACAI,0BAAgBE,EAAhB,GAAqB,iBAArB;AACAD,sBAAYC,EAAZ,GAAiB,YAAjB;AACA,eAAKL,KAAL,GAAaA,KAAb;;AAEAD,2BAAi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,CAA0B,EAA7F;AACD;;AAEDC,wBAAgBC,MAAhB,EAAwB;AACtB,iBAAOA,OAAOpB,MAAP,CAAc,KAAKqB,gBAAnB,CAAP;AACD;;AAEDlC,8CAAqC;AACnC,cAAG,KAAK9B,gBAAL,CAAsBkB,MAAtB,IAAgC,CAAnC,EAAsC;AACpC,gBAAIU,uBAAuB,KAAKqC,wBAAL,EAA3B;AACArC,iCAAqBsC,QAArB,GAAgC,IAAhC;AACD;AACF;;AAEDpB,yCAAgC;AAC9B,cAAG,KAAK9C,gBAAL,CAAsBkB,MAAtB,IAAgC,CAAnC,EAAsC;AACpC,gBAAIU,uBAAwB,KAAKqC,wBAAL,EAA5B;AACArC,iCAAqBsC,QAArB,GAAgC,KAAhC;AACD;AACF;;AAEDD,mCAA2B;AACzB,cAAIE,sBAAsB,KAAKnE,gBAAL,CAAsBoE,KAAtB,CAA4B,CAAC,CAA7B,EAAgC,CAAhC,CAA1B;AACA,cAAIxC,uBAAuBK,OAAOC,KAAP,CAAa,KAAK/B,KAAlB,EAA0B,IAAGgE,mBAAoB,EAAjD,CAA3B;AACA,iBAAOvC,oBAAP;AACD;;AAtJuB","file":"grouping-menu.js","sourcesContent":["export class GroupingMenu {\n  \n  constructor(grouper, attributes) {\n    this.grouper = grouper;\n    this.attributes = attributes;\n    this.currentGroupings = [];\n  }\n  \n  initGroupingSelectBox(containerElement, world, addGrouping, removeGrouping) {\n    let selectContainer = <div></div>\n    let groupingBar = <div></div>\n    this.containerElement = containerElement;\n    selectContainer.id = \"selectContainer\"\n    groupingBar.id = \"filter-bar\"\n    this.world = world;\n    \n    containerElement.appendChild(selectContainer)\n    containerElement.appendChild(groupingBar)\n    \n    \n    let uniqueValues = this.attributes;\n    let textElement = <div></div>\n    let selectElement = <select></select>\n    let buttonElement = <button>Group</button>\n\n    uniqueValues.sort()\n\n    selectElement.id = \"group-select\"\n    buttonElement.id = \"group-button\"\n    textElement.innerHTML =  \"Group 6: \"\n\n    for (let value of uniqueValues) {\n      selectElement.options[selectElement.options.length] = new Option(value)\n    }\n\n    selectContainer.appendChild(textElement)\n    selectContainer.appendChild(selectElement)\n    selectContainer.appendChild(buttonElement)\n    \n    let menu = this;\n\n    buttonElement.addEventListener(\"click\", () => {\n\n      let value = selectElement.options[selectElement.selectedIndex].value\n      if (!this.currentGroupings.includes(value)) {\n        menu.addGroupingLegendAndRemoveGroupingButton(value, groupingBar, removeGrouping)\n        menu.grouper.addGrouping(value);\n      }\n      \n\n    })\n  }\n  \n  addGroupingLegendAndRemoveGroupingButton(value, groupingBar, removeGrouping) {\n    \n    this.removeCurrentLegend();\n        \n    let groupingLegend = this.generateGroupingLegend(value)\n    let removeGroupingButton = this.generateRemoveGroupingButton(value, groupingBar, removeGrouping, groupingLegend);\n    this.disablePreviousRemoveGroupingButton();\n    this.currentGroupings.push(value);\n    \n    groupingBar.appendChild(removeGroupingButton);\n    this.containerElement.appendChild(groupingLegend);\n  }\n  \n  removeCurrentLegend(){\n    try {\n      let currentLegend = lively.query(this.world, \"#legend\");\n      currentLegend.parentElement.removeChild(currentLegend);\n    } catch(e) {\n      console.log(e);\n    }\n    \n  }\n  \n  generateRemoveGroupingButton(value, groupingBar, removeGrouping, groupingLegend){\n    let removeGroupingButton = <button></button>;\n    removeGroupingButton.innerHTML = \"Grouping: \" + value;\n    removeGroupingButton.setAttribute(\"value\", value);\n    removeGroupingButton.id = value;\n    \n    removeGroupingButton.addEventListener(\"click\", () => {\n      \n      let clickedGroupingKey = removeGroupingButton.getAttribute(\"value\");\n      this.currentGroupings= this.currentGroupings\n        .filter(groupingKey => groupingKey != clickedGroupingKey);\n      this.grouper.removeGrouping(clickedGroupingKey);\n      groupingBar.removeChild(removeGroupingButton);\n      this.containerElement.removeChild(groupingLegend);\n      this.addLegendFromLastGrouping();  \n      this.enableLastRemoveGroupingButton();\n    });\n    \n    return removeGroupingButton;\n    \n  }\n  \n  addLegendFromLastGrouping(){\n      this.containerElement.appendChild(\n        this.generateGroupingLegend(this.currentGroupings[this.currentGroupings.length - 1]));\n  }  \n  \n  generateGroupingLegend(key){\n    let uniqueValuesForKey = this.grouper.getUniqueValuesForKey(key);\n    let groupingLegendTable = <tabel></tabel>;\n    groupingLegendTable.id = \"legend\";\n        \n    uniqueValuesForKey.forEach((value) => {\n      let tableRow = <th></th>;\n      let valueTableCell = <td>{value}</td>;\n      let d3Color = this.grouper.groupingLayouter.getColorForValue(value);\n      valueTableCell.style.backgroundColor = this.getRGBValueFromD3Color(d3Color);\n      valueTableCell.style.padding = \"5px\";\n\n      tableRow.appendChild(valueTableCell);\n      groupingLegendTable.appendChild(tableRow);\n    });\n    \n    return groupingLegendTable;\n    \n    \n  }\n  \n  getRGBValueFromD3Color(d3Color){\n    return `#${d3Color[\"r\"].toString(16)}${d3Color[\"g\"].toString(16)}${d3Color[\"b\"].toString(16)}`;\n  }\n  \n  getFilteredData(points) {\n    return points.filter(this.activeFilterExpr)\n  }\n  \n  disablePreviousRemoveGroupingButton(){\n    if(this.currentGroupings.length >= 1) {\n      let removeGroupingButton = this.getLastButtonOfButtonBar();\n      removeGroupingButton.disabled = true;\n    }\n  }\n  \n  enableLastRemoveGroupingButton(){\n    if(this.currentGroupings.length >= 1) {\n      let removeGroupingButton =  this.getLastButtonOfButtonBar();\n      removeGroupingButton.disabled = false;\n    }\n  }\n  \n  getLastButtonOfButtonBar() {\n    let previousGroupingKey = this.currentGroupings.slice(-1)[0];\n    let removeGroupingButton = lively.query(this.world, `#${previousGroupingKey}`);\n    return removeGroupingButton;\n  }\n  \n}\n"]}