Reference Source

src/contextMenus/ModelsContextMenu.js

  1. import {ContextMenu} from "@xeokit/xeokit-sdk/dist/xeokit-sdk.es.js";
  2.  
  3. /**
  4. * @private
  5. * @param {*} cfg Configs
  6. * @param {Boolean} [cfg.enableEditModels=false] Set true to show Add/Edit/Delete options in the menu.
  7. */
  8. class ModelsContextMenu extends ContextMenu {
  9.  
  10. constructor(cfg = {}) {
  11.  
  12. const enableEditModels = (!!cfg.enableEditModels);
  13.  
  14. const items = [
  15. [
  16. {
  17. getTitle: (context) => {
  18. return context.viewer.localeService.translate("modelsContextMenu.loadModel") || "Load";
  19. },
  20. getEnabled: (context) => {
  21. return (!context.bimViewer.isModelLoaded(context.modelId));
  22. },
  23. doAction: (context) => {
  24. context.bimViewer.loadModel(context.modelId);
  25. }
  26. },
  27. {
  28. getTitle: (context) => {
  29. return context.viewer.localeService.translate("modelsContextMenu.unloadModel") || "Unload";
  30. },
  31. getEnabled: (context) => {
  32. return context.bimViewer.isModelLoaded(context.modelId);
  33. },
  34. doAction: (context) => {
  35. context.bimViewer.unloadModel(context.modelId);
  36. }
  37. }
  38. ]
  39. ];
  40.  
  41. if (enableEditModels) {
  42.  
  43. items.push([
  44. {
  45. getTitle: (context) => {
  46. return context.viewer.localeService.translate("modelsContextMenu.editModel") || "Edit";
  47. },
  48. getEnabled: (context) => {
  49. return true;
  50. },
  51. doAction: (context) => {
  52. context.bimViewer.editModel(context.modelId);
  53. }
  54. },
  55. {
  56. getTitle: (context) => {
  57. return context.viewer.localeService.translate("modelsContextMenu.deleteModel") || "Delete";
  58. },
  59. getEnabled: (context) => {
  60. return true;
  61. },
  62. doAction: (context) => {
  63. context.bimViewer.deleteModel(context.modelId);
  64. }
  65. }
  66. ]);
  67. }
  68.  
  69. items.push([
  70. {
  71. getTitle: (context) => {
  72. return context.viewer.localeService.translate("modelsContextMenu.loadAllModels") || "Load All";
  73. },
  74. getEnabled: (context) => {
  75. const bimViewer = context.bimViewer;
  76. const modelIds = bimViewer.getModelIds();
  77. const loadedModelIds = bimViewer.getLoadedModelIds();
  78. return (loadedModelIds.length < modelIds.length);
  79. },
  80. doAction: (context) => {
  81. context.bimViewer.loadAllModels();
  82. }
  83. },
  84. {
  85. getTitle: (context) => {
  86. return context.viewer.localeService.translate("modelsContextMenu.unloadAllModels") || "Unload All";
  87. },
  88. getEnabled: (context) => {
  89. const loadedModelIds = context.bimViewer.getLoadedModelIds();
  90. return (loadedModelIds.length > 0);
  91. },
  92. doAction: (context) => {
  93. context.bimViewer.unloadAllModels();
  94. }
  95. }
  96. ]);
  97.  
  98. items.push([
  99. {
  100. getTitle: (context) => {
  101. return context.viewer.localeService.translate("modelsContextMenu.clearSlices") || "Clear Slices";
  102. },
  103. getEnabled: (context) => {
  104. return (context.bimViewer.getNumSections() > 0);
  105. },
  106. doAction: (context) => {
  107. context.bimViewer.clearSections();
  108. }
  109. }
  110. ]);
  111.  
  112. super({
  113. hideOnAction: cfg.hideOnAction,
  114. context: cfg.context,
  115. items: items
  116. });
  117. }
  118. }
  119.  
  120. export {ModelsContextMenu};