Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menu.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  2. var Menu = function Menu(table) {
  3. this.table = table; //hold Tabulator object
  4. this.menuEl = false;
  5. this.blurEvent = this.hideMenu.bind(this);
  6. };
  7. Menu.prototype.initializeColumnHeader = function (column) {
  8. var _this = this;
  9. var headerMenuEl;
  10. if (column.definition.headerContextMenu) {
  11. column.getElement().addEventListener("contextmenu", function (e) {
  12. var menu = typeof column.definition.headerContextMenu == "function" ? column.definition.headerContextMenu(column.getComponent()) : column.definition.headerContextMenu;
  13. e.preventDefault();
  14. _this.loadMenu(e, column, menu);
  15. });
  16. }
  17. if (column.definition.headerMenu) {
  18. headerMenuEl = document.createElement("span");
  19. headerMenuEl.classList.add("tabulator-header-menu-button");
  20. headerMenuEl.innerHTML = "⋮";
  21. headerMenuEl.addEventListener("click", function (e) {
  22. var menu = typeof column.definition.headerMenu == "function" ? column.definition.headerMenu(column.getComponent()) : column.definition.headerMenu;
  23. e.stopPropagation();
  24. e.preventDefault();
  25. _this.loadMenu(e, column, menu);
  26. });
  27. column.titleElement.insertBefore(headerMenuEl, column.titleElement.firstChild);
  28. }
  29. };
  30. Menu.prototype.initializeCell = function (cell) {
  31. var _this2 = this;
  32. cell.getElement().addEventListener("contextmenu", function (e) {
  33. var menu = typeof cell.column.definition.contextMenu == "function" ? cell.column.definition.contextMenu(cell.getComponent()) : cell.column.definition.contextMenu;
  34. e.preventDefault();
  35. _this2.loadMenu(e, cell, menu);
  36. });
  37. };
  38. Menu.prototype.initializeRow = function (row) {
  39. var _this3 = this;
  40. row.getElement().addEventListener("contextmenu", function (e) {
  41. var menu = typeof _this3.table.options.rowContextMenu == "function" ? _this3.table.options.rowContextMenu(row.getComponent()) : _this3.table.options.rowContextMenu;
  42. e.preventDefault();
  43. _this3.loadMenu(e, row, menu);
  44. });
  45. };
  46. Menu.prototype.loadMenu = function (e, component, menu) {
  47. var _this4 = this;
  48. var docHeight = Math.max(document.body.offsetHeight, window.innerHeight);
  49. //abort if no menu set
  50. if (!menu || !menu.length) {
  51. return;
  52. }
  53. this.hideMenu();
  54. this.menuEl = document.createElement("div");
  55. this.menuEl.classList.add("tabulator-menu");
  56. menu.forEach(function (item) {
  57. var itemEl = document.createElement("div");
  58. var label = item.label;
  59. var disabled = item.disabled;
  60. if (item.separator) {
  61. itemEl.classList.add("tabulator-menu-separator");
  62. } else {
  63. itemEl.classList.add("tabulator-menu-item");
  64. if (typeof label == "function") {
  65. label = label(component.getComponent());
  66. }
  67. if (label instanceof Node) {
  68. itemEl.appendChild(label);
  69. } else {
  70. itemEl.innerHTML = label;
  71. }
  72. if (typeof disabled == "function") {
  73. disabled = disabled(component.getComponent());
  74. }
  75. if (disabled) {
  76. itemEl.classList.add("tabulator-menu-item-disabled");
  77. itemEl.addEventListener("click", function (e) {
  78. e.stopPropagation();
  79. });
  80. } else {
  81. itemEl.addEventListener("click", function (e) {
  82. _this4.hideMenu();
  83. item.action(e, component.getComponent());
  84. });
  85. }
  86. }
  87. _this4.menuEl.appendChild(itemEl);
  88. });
  89. this.menuEl.style.top = e.pageY + "px";
  90. this.menuEl.style.left = e.pageX + "px";
  91. document.body.addEventListener("click", this.blurEvent);
  92. this.table.rowManager.element.addEventListener("scroll", this.blurEvent);
  93. setTimeout(function () {
  94. document.body.addEventListener("contextmenu", _this4.blurEvent);
  95. }, 100);
  96. document.body.appendChild(this.menuEl);
  97. //move menu to start on right edge if it is too close to the edge of the screen
  98. if (e.pageX + this.menuEl.offsetWidth >= document.body.offsetWidth) {
  99. this.menuEl.style.left = "";
  100. this.menuEl.style.right = document.body.offsetWidth - e.pageX + "px";
  101. }
  102. //move menu to start on bottom edge if it is too close to the edge of the screen
  103. if (e.pageY + this.menuEl.offsetHeight >= docHeight) {
  104. this.menuEl.style.top = "";
  105. this.menuEl.style.bottom = docHeight - e.pageY + "px";
  106. }
  107. };
  108. Menu.prototype.hideMenu = function () {
  109. if (this.menuEl.parentNode) {
  110. this.menuEl.parentNode.removeChild(this.menuEl);
  111. }
  112. if (this.blurEvent) {
  113. document.body.removeEventListener("click", this.blurEvent);
  114. document.body.removeEventListener("contextmenu", this.blurEvent);
  115. this.table.rowManager.element.removeEventListener("scroll", this.blurEvent);
  116. }
  117. };
  118. //default accessors
  119. Menu.prototype.menus = {};
  120. Tabulator.prototype.registerModule("menu", Menu);