Home Reference Source

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

  1. import { ele, Logger, TalkerAPI } from "cables-shared-client";
  2. import Tab from "../../elements/tabpanel/tab.js";
  3. import { getHandleBarHtml } from "../../utils/handlebars.js";
  4. import ModalDialog from "../../dialogs/modaldialog.js";
  5. import { gui } from "../../gui.js";
  6. import { platform } from "../../platform.js";
  7. export default class ElectronOpDirs
  8. {
  9. constructor(tabs)
  10. {
  11. this._log = new Logger("ElectronOpDirsTab");
  12. this._count = 0;
  13. this._timeout = null;
  14. this._tab = new Tab("op directories", { "icon": "folder", "singleton": true, "infotext": "tab_profiler", "padding": true });
  15. tabs.addTab(this._tab, true);
  16. this.show();
  17. this._tab.on("onActivate", this.show);
  18. }
  19. show()
  20. {
  21. if (!this._tab) return;
  22. platform.talkerAPI.send(TalkerAPI.CMD_ELECTRON_GET_PROJECT_OPDIRS, {}, (err, r) =>
  23. {
  24. if (!err && r.data)
  25. {
  26. const html = getHandleBarHtml("tab_electron_opdirs", { "dirs": r.data });
  27. this._tab.html(html);
  28. const listEle = ele.byId("dirlist");
  29. const infoBlock = listEle.querySelector(".highlightBlock");
  30. const addButton = this._tab.contentEle.querySelector("#addOpProjectDir");
  31. if (addButton)
  32. {
  33. addButton.addEventListener("click", () =>
  34. {
  35. platform.talkerAPI.send(TalkerAPI.CMD_ELECTRON_ADD_PROJECT_OPDIR, {}, (dirErr, _dirRes) =>
  36. {
  37. if (!dirErr)
  38. {
  39. this.show();
  40. this._loadOpsInDirs();
  41. }
  42. else
  43. {
  44. new ModalDialog({ "showOkButton": true, "warning": true, "title": "Warning", "text": dirErr.msg });
  45. this._log.info(dirErr.msg);
  46. }
  47. });
  48. });
  49. }
  50. const packageButton = this._tab.contentEle.querySelector("#addOpPackage");
  51. if (packageButton)
  52. {
  53. packageButton.addEventListener("click", () =>
  54. {
  55. platform.talkerAPI.send(TalkerAPI.CMD_ADD_OP_PACKAGE, {}, (dirErr, _dirRes) =>
  56. {
  57. if (!dirErr)
  58. {
  59. this.show();
  60. this._loadOpsInDirs();
  61. }
  62. else
  63. {
  64. new ModalDialog({ "showOkButton": true, "warning": true, "title": "Warning", "text": dirErr.msg });
  65. this._log.info(dirErr.msg);
  66. }
  67. });
  68. });
  69. }
  70. const removeButtons = this._tab.contentEle.querySelectorAll(".removeOpProjectDir");
  71. removeButtons.forEach((removeButton) =>
  72. {
  73. removeButton.addEventListener("click", () =>
  74. {
  75. const dir = removeButton.dataset.dir;
  76. platform.talkerAPI.send(TalkerAPI.CMD_ELECTRON_REMOVE_PROJECT_OPDIR, dir, () =>
  77. {
  78. this.show();
  79. this._loadOpsInDirs();
  80. });
  81. });
  82. });
  83. ele.hide(infoBlock);
  84. new Sortable(listEle, {
  85. "animation": 150,
  86. "handle": ".handle",
  87. "ghostClass": "ghost",
  88. "dragClass": "dragActive",
  89. "onEnd": () =>
  90. {
  91. infoBlock.classList.add("info");
  92. infoBlock.classList.remove("error");
  93. const order = [];
  94. const dirs = listEle.querySelectorAll("[data-dir]");
  95. dirs.forEach((dirEle) =>
  96. {
  97. order.push(dirEle.dataset.dir);
  98. });
  99. platform.talkerAPI.send(TalkerAPI.CMD_ELECTRON_SAVE_PROJECT_OPDIRS_ORDER, order, (orderErr, orderRes) =>
  100. {
  101. if (orderRes && orderRes.success)
  102. {
  103. infoBlock.innerHTML = "Saved, please reload the patch to see the changes";
  104. ele.show(infoBlock);
  105. }
  106. else
  107. {
  108. infoBlock.classList.remove("info");
  109. infoBlock.classList.add("error");
  110. infoBlock.innerHTML = orderErr;
  111. ele.show(infoBlock);
  112. }
  113. });
  114. }
  115. });
  116. }
  117. });
  118. }
  119. _loadOpsInDirs()
  120. {
  121. platform.talkerAPI.send(TalkerAPI.CMD_GET_ALL_OPDOCS, { "projectId": gui.patchId }, (_err, _data) =>
  122. {
  123. if (_err)
  124. {
  125. this._log.error("preloading error", _err);
  126. }
  127. else
  128. {
  129. if (gui.opDocs)
  130. {
  131. gui.opDocs.addOpDocs(_data.opDocs);
  132. }
  133. gui.opSelect().reload();
  134. }
  135. }, (response) =>
  136. {
  137. this._log.error("preloading error", response);
  138. });
  139. }
  140. }