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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Tabulator v4.8.2 (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. this.escEvent = this.escMenu.bind(this);
  7. this.nestedMenuBlock = false;
  8. };
  9. Menu.prototype.initializeColumnHeader = function (column) {
  10. var _this = this;
  11. var headerMenuEl;
  12. if (column.definition.headerContextMenu) {
  13. column.getElement().addEventListener("contextmenu", this.LoadMenuEvent.bind(this, column, column.definition.headerContextMenu));
  14. this.tapHold(column, column.definition.headerContextMenu);
  15. }
  16. // if(column.definition.headerClickMenu){
  17. // column.getElement().addEventListener("click", this.LoadMenuEvent.bind(this, column, column.definition.headerClickMenu));
  18. // }
  19. if (column.definition.headerMenu) {
  20. headerMenuEl = document.createElement("span");
  21. headerMenuEl.classList.add("tabulator-header-menu-button");
  22. headerMenuEl.innerHTML = "⋮";
  23. headerMenuEl.addEventListener("click", function (e) {
  24. var menu = typeof column.definition.headerMenu == "function" ? column.definition.headerMenu(column.getComponent(), e) : column.definition.headerMenu;
  25. e.stopPropagation();
  26. e.preventDefault();
  27. _this.loadMenu(e, column, menu);
  28. });
  29. column.titleElement.insertBefore(headerMenuEl, column.titleElement.firstChild);
  30. }
  31. };
  32. Menu.prototype.LoadMenuEvent = function (component, menu, e) {
  33. menu = typeof menu == "function" ? menu(component.getComponent(), e) : menu;
  34. // if(component instanceof Cell){
  35. // e.stopImmediatePropagation();
  36. // }
  37. this.loadMenu(e, component, menu);
  38. };
  39. Menu.prototype.tapHold = function (component, menu) {
  40. var _this2 = this;
  41. var element = component.getElement(),
  42. tapHold = null,
  43. loaded = false;
  44. element.addEventListener("touchstart", function (e) {
  45. clearTimeout(tapHold);
  46. loaded = false;
  47. tapHold = setTimeout(function () {
  48. clearTimeout(tapHold);
  49. tapHold = null;
  50. loaded = true;
  51. _this2.LoadMenuEvent(component, menu, e);
  52. }, 1000);
  53. }, { passive: true });
  54. element.addEventListener("touchend", function (e) {
  55. clearTimeout(tapHold);
  56. tapHold = null;
  57. if (loaded) {
  58. e.preventDefault();
  59. }
  60. });
  61. };
  62. Menu.prototype.initializeCell = function (cell) {
  63. if (cell.column.definition.contextMenu) {
  64. cell.getElement().addEventListener("contextmenu", this.LoadMenuEvent.bind(this, cell, cell.column.definition.contextMenu));
  65. this.tapHold(cell, cell.column.definition.contextMenu);
  66. }
  67. if (cell.column.definition.clickMenu) {
  68. cell.getElement().addEventListener("click", this.LoadMenuEvent.bind(this, cell, cell.column.definition.clickMenu));
  69. }
  70. };
  71. Menu.prototype.initializeRow = function (row) {
  72. if (this.table.options.rowContextMenu) {
  73. row.getElement().addEventListener("contextmenu", this.LoadMenuEvent.bind(this, row, this.table.options.rowContextMenu));
  74. this.tapHold(row, this.table.options.rowContextMenu);
  75. }
  76. if (this.table.options.rowClickMenu) {
  77. row.getElement().addEventListener("click", this.LoadMenuEvent.bind(this, row, this.table.options.rowClickMenu));
  78. }
  79. };
  80. Menu.prototype.initializeGroup = function (group) {
  81. if (this.table.options.groupContextMenu) {
  82. group.getElement().addEventListener("contextmenu", this.LoadMenuEvent.bind(this, group, this.table.options.groupContextMenu));
  83. this.tapHold(group, this.table.options.groupContextMenu);
  84. }
  85. if (this.table.options.groupClickMenu) {
  86. group.getElement().addEventListener("click", this.LoadMenuEvent.bind(this, group, this.table.options.groupClickMenu));
  87. }
  88. };
  89. Menu.prototype.loadMenu = function (e, component, menu) {
  90. var _this3 = this;
  91. var docHeight = Math.max(document.body.offsetHeight, window.innerHeight),
  92. touch = !(e instanceof MouseEvent);
  93. if (!touch) {
  94. e.preventDefault();
  95. }
  96. //abort if no menu set
  97. if (!menu || !menu.length) {
  98. return;
  99. }
  100. if (this.nestedMenuBlock) {
  101. //abort if child menu already open
  102. if (this.isOpen()) {
  103. return;
  104. }
  105. } else {
  106. this.nestedMenuBlock = setTimeout(function () {
  107. _this3.nestedMenuBlock = false;
  108. }, 100);
  109. }
  110. this.hideMenu();
  111. this.menuEl = document.createElement("div");
  112. this.menuEl.classList.add("tabulator-menu");
  113. menu.forEach(function (item) {
  114. var itemEl = document.createElement("div");
  115. var label = item.label;
  116. var disabled = item.disabled;
  117. if (item.separator) {
  118. itemEl.classList.add("tabulator-menu-separator");
  119. } else {
  120. itemEl.classList.add("tabulator-menu-item");
  121. if (typeof label == "function") {
  122. label = label(component.getComponent());
  123. }
  124. if (label instanceof Node) {
  125. itemEl.appendChild(label);
  126. } else {
  127. itemEl.innerHTML = label;
  128. }
  129. if (typeof disabled == "function") {
  130. disabled = disabled(component.getComponent());
  131. }
  132. if (disabled) {
  133. itemEl.classList.add("tabulator-menu-item-disabled");
  134. itemEl.addEventListener("click", function (e) {
  135. e.stopPropagation();
  136. });
  137. } else {
  138. itemEl.addEventListener("click", function (e) {
  139. _this3.hideMenu();
  140. item.action(e, component.getComponent());
  141. });
  142. }
  143. }
  144. _this3.menuEl.appendChild(itemEl);
  145. });
  146. this.menuEl.style.top = (touch ? e.touches[0].pageY : e.pageY) + "px";
  147. this.menuEl.style.left = (touch ? e.touches[0].pageX : e.pageX) + "px";
  148. setTimeout(function () {
  149. _this3.table.rowManager.element.addEventListener("scroll", _this3.blurEvent);
  150. document.body.addEventListener("click", _this3.blurEvent);
  151. document.body.addEventListener("contextmenu", _this3.blurEvent);
  152. document.body.addEventListener("keydown", _this3.escEvent);
  153. }, 100);
  154. document.body.appendChild(this.menuEl);
  155. //move menu to start on right edge if it is too close to the edge of the screen
  156. if (e.pageX + this.menuEl.offsetWidth >= document.body.offsetWidth) {
  157. this.menuEl.style.left = "";
  158. this.menuEl.style.right = document.body.offsetWidth - e.pageX + "px";
  159. }
  160. //move menu to start on bottom edge if it is too close to the edge of the screen
  161. if (e.pageY + this.menuEl.offsetHeight >= docHeight) {
  162. this.menuEl.style.top = "";
  163. this.menuEl.style.bottom = docHeight - e.pageY + "px";
  164. }
  165. };
  166. Menu.prototype.isOpen = function () {
  167. return !!this.menuEl.parentNode;
  168. };
  169. Menu.prototype.escMenu = function (e) {
  170. if (e.keyCode == 27) {
  171. this.hideMenu();
  172. }
  173. };
  174. Menu.prototype.hideMenu = function () {
  175. if (this.menuEl.parentNode) {
  176. this.menuEl.parentNode.removeChild(this.menuEl);
  177. }
  178. if (this.escEvent) {
  179. document.body.removeEventListener("keydown", this.escEvent);
  180. }
  181. if (this.blurEvent) {
  182. document.body.removeEventListener("click", this.blurEvent);
  183. document.body.removeEventListener("contextmenu", this.blurEvent);
  184. this.table.rowManager.element.removeEventListener("scroll", this.blurEvent);
  185. }
  186. };
  187. //default accessors
  188. Menu.prototype.menus = {};
  189. Tabulator.prototype.registerModule("menu", Menu);