index.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. var DndPanel = /** @class */ (function () {
  2. function DndPanel(_a) {
  3. var _this = this;
  4. var lf = _a.lf;
  5. this.lf = lf;
  6. this.lf.setPatternItems = function (shapeList) {
  7. _this.setPatternItems(shapeList);
  8. };
  9. }
  10. DndPanel.prototype.render = function (lf, domContainer) {
  11. var _this = this;
  12. this.destroy();
  13. if (!this.shapeList || this.shapeList.length === 0) {
  14. // 首次render后失败后,后续调用setPatternItems支持渲染
  15. this.domContainer = domContainer;
  16. return;
  17. }
  18. this.panelEl = document.createElement('div');
  19. this.panelEl.className = 'lf-dndpanel';
  20. this.shapeList.forEach(function (shapeItem) {
  21. _this.panelEl.appendChild(_this.createDndItem(shapeItem));
  22. });
  23. domContainer.appendChild(this.panelEl);
  24. this.domContainer = domContainer;
  25. };
  26. DndPanel.prototype.destroy = function () {
  27. if (this.domContainer && this.panelEl && this.domContainer.contains(this.panelEl)) {
  28. this.domContainer.removeChild(this.panelEl);
  29. }
  30. };
  31. DndPanel.prototype.setPatternItems = function (shapeList) {
  32. this.shapeList = shapeList;
  33. // 支持渲染后重新设置拖拽面板
  34. if (this.domContainer) {
  35. this.render(this.lf, this.domContainer);
  36. }
  37. };
  38. DndPanel.prototype.createDndItem = function (shapeItem) {
  39. var _this = this;
  40. var el = document.createElement('div');
  41. el.className = shapeItem.className ? "lf-dnd-item " + shapeItem.className : 'lf-dnd-item';
  42. var shape = document.createElement('div');
  43. shape.className = 'lf-dnd-shape';
  44. // if (typeof shapeItem.icon === 'string') {
  45. if (shapeItem.icon) {
  46. shape.style.backgroundImage = "url(" + shapeItem.icon + ")";
  47. // } else {
  48. // shape.appendChild(shapeItem.icon);
  49. }
  50. el.appendChild(shape);
  51. if (shapeItem.label) {
  52. var text = document.createElement('div');
  53. text.innerText = shapeItem.label;
  54. text.className = 'lf-dnd-text';
  55. el.appendChild(text);
  56. }
  57. el.onmousedown = function () {
  58. if (shapeItem.type) {
  59. _this.lf.dnd.startDrag({
  60. type: shapeItem.type,
  61. properties: shapeItem.properties,
  62. text: shapeItem.text,
  63. });
  64. }
  65. if (shapeItem.callback) {
  66. shapeItem.callback(_this.lf, _this.domContainer);
  67. }
  68. };
  69. el.ondblclick = function (e) {
  70. _this.lf.graphModel.eventCenter.emit('dnd:panel-dbclick', {
  71. e: e,
  72. data: shapeItem,
  73. });
  74. };
  75. el.onclick = function (e) {
  76. _this.lf.graphModel.eventCenter.emit('dnd:panel-click', {
  77. e: e,
  78. data: shapeItem,
  79. });
  80. };
  81. el.oncontextmenu = function (e) {
  82. _this.lf.graphModel.eventCenter.emit('dnd:panel-contextmenu', {
  83. e: e,
  84. data: shapeItem,
  85. });
  86. };
  87. return el;
  88. };
  89. DndPanel.pluginName = 'dndPanel';
  90. return DndPanel;
  91. }());
  92. export { DndPanel, };