Home Reference Source

cables_dev/cables_ui/src/ui/components/tabs/tab_spreadsheet.js

  1. import { Port, utils } from "cables";
  2. import { ele, Events, Logger } from "cables-shared-client";
  3. import TabPanel from "../../elements/tabpanel/tabpanel.js";
  4. import Tab from "../../elements/tabpanel/tab.js";
  5. import { gui } from "../../gui.js";
  6. import { editorSession } from "../../elements/tabpanel/editor_session.js";
  7. import undo from "../../utils/undo.js";
  8. /**
  9. * @typedef SpreadSheetOptions
  10. * @property {string} [title]
  11. * @property {number} [numColumns]
  12. * @property {function} [onchange]
  13. * @property {string[]} [colNames]
  14. * @property {[string[]]} [cells]
  15. */
  16. export default class SpreadSheetTab extends Events
  17. {
  18. static TABSESSION_NAME = "spreadsheet";
  19. #log = new Logger("SpreadSheetTab");
  20. #cellMate = null;
  21. #port = null;
  22. #options = null;
  23. #tab = null;
  24. /** @type {TabPanel} */
  25. #tabs = null;
  26. #currentId;
  27. /**
  28. * @param {TabPanel} tabs
  29. * @param {Port} port
  30. * @param {SpreadSheetOptions} [options]
  31. */
  32. constructor(tabs, port, options)
  33. {
  34. super();
  35. if (!port)
  36. {
  37. console.log("spreadsheettab missing args");
  38. return;
  39. }
  40. this.#tabs = tabs;
  41. this.#currentId = port.op.id + "_" + port.name;
  42. options = options || {};
  43. if (options.colNames && !options.numColumns)options.numColumns = options.colNames.length;
  44. this.#port = port;
  45. this.#options = options;
  46. this.#tab = new Tab(options.title || port.op.getTitle(), {
  47. "icon": "spreadsheet",
  48. "infotext": "tab_spreadsheet",
  49. "padding": true,
  50. "singleton": false });
  51. this.#tabs.addTab(this.#tab, true);
  52. this.#tab.on("close", () =>
  53. {
  54. editorSession.remove(SpreadSheetTab.TABSESSION_NAME, this.#currentId);
  55. this.#cellMate.dispose();
  56. });
  57. this.#tab.on(Tab.EVENT_RESIZE, () =>
  58. {
  59. this.#cellMate.resize();
  60. });
  61. this.#tab.on(Tab.EVENT_ACTIVATE, () =>
  62. {
  63. this.#cellMate.resize();
  64. });
  65. this.#tab.addButton("export csv", () =>
  66. {
  67. this.#cellMate.download("cables.csv", this.#cellMate.toCsv());
  68. });
  69. this.#tab.addButton("delete line", () =>
  70. {
  71. this.#cellMate.deleteCurrentLine();
  72. });
  73. this.#tab.addButton("insert line", () =>
  74. {
  75. this.#cellMate.insertLineAtCursor();
  76. });
  77. editorSession.rememberOpenEditor(SpreadSheetTab.TABSESSION_NAME, this.#currentId, {
  78. "portname": port.name,
  79. "opid": port.op.id,
  80. }, true);
  81. this.show();
  82. gui.maintabPanel.show(true);
  83. }
  84. show()
  85. {
  86. let html = "<div></div>";
  87. this.#tab.html(html);
  88. this.#cellMate = new CellMate(this.#tab.contentEle,
  89. {
  90. "undo": undo,
  91. "onChange": this.onChange.bind(this)
  92. });
  93. this.#cellMate.fromObj(this.#port.get());
  94. }
  95. onChange()
  96. {
  97. console.log(this.#cellMate.toObj());
  98. this.#port.setRef(this.#cellMate.toObj());
  99. }
  100. }
  101. editorSession.addListener(SpreadSheetTab.TABSESSION_NAME, (id, data) =>
  102. {
  103. const op = gui.corePatch().getOpById(data.opid);
  104. if (!op) return console.log("no spread op found..");
  105. new SpreadSheetTab(gui.mainTabs, op.getPortByName(data.portname));
  106. });