UIContextMenu.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. function UIContextMenu(options){
  20. $('.window-active .window-app-iframe').css('pointer-events', 'none');
  21. const menu_id = global_element_id++;
  22. let h = '';
  23. h += `<div
  24. id="context-menu-${menu_id}"
  25. data-is-submenu="${options.is_submenu ? 'true' : 'false'}"
  26. data-element-id="${menu_id}"
  27. data-id="${options.id ?? ''}"
  28. ${options.parent_id ? `data-parent-id="${options.parent_id}"` : ``}
  29. ${!options.parent_id && options.parent_element ? `data-parent-id="${$(options.parent_element).attr('data-element-id')}"` : ``}
  30. class="context-menu context-menu-active ${options.is_submenu ? 'context-menu-submenu-open' : ''}"
  31. >`;
  32. for(let i=0; i < options.items.length; i++){
  33. // item
  34. if(!options.items[i].is_divider && options.items[i] !== '-'){
  35. // single item
  36. if(options.items[i].items === undefined){
  37. h += `<li data-action="${i}"
  38. class="context-menu-item ${options.items[i].disabled ? ' context-menu-item-disabled' : ''}"
  39. >`;
  40. // icon
  41. h += `<span class="context-menu-item-icon">${options.items[i].icon ?? ''}</span>`;
  42. h += `<span class="context-menu-item-icon-active">${options.items[i].icon_active ?? (options.items[i].icon ?? '')}</span>`;
  43. // label
  44. h += `<span class="contextmenu-label">${options.items[i].html}</span>`;
  45. h += `<span class="contextmenu-label-active">${options.items[i].html_active ?? options.items[i].html}</span>`;
  46. h += `</li>`;
  47. }
  48. // submenu
  49. else{
  50. h += `<li data-action="${i}"
  51. data-menu-id="${menu_id}-${i}"
  52. data-has-submenu="true"
  53. data-parent-element-id="${menu_id}"
  54. class="context-menu-item-submenu context-menu-item${options.items[i].disabled ? ' context-menu-item-disabled' : ''}"
  55. >`;
  56. // icon
  57. h += `<span class="context-menu-item-icon">${options.items[i].icon ?? ''}</span>`;
  58. h += `<span class="context-menu-item-icon-active">${options.items[i].icon_active ?? (options.items[i].icon ?? '')}</span>`;
  59. // label
  60. h += `${html_encode(options.items[i].html)}`;
  61. // arrow
  62. h += `<img class="submenu-arrow" src="${html_encode(window.icons['chevron-right.svg'])}"><img class="submenu-arrow submenu-arrow-active" src="${html_encode(window.icons['chevron-right-active.svg'])}">`;
  63. h += `</li>`;
  64. }
  65. }
  66. // divider
  67. else if(options.items[i].is_divider || options.items[i] === '-')
  68. h += `<li class="context-menu-divider"><hr></li>`;
  69. }
  70. h += `</div>`
  71. $('body').append(h)
  72. const contextMenu = document.getElementById(`context-menu-${menu_id}`);
  73. const menu_width = $(contextMenu).width();
  74. const menu_height = $(contextMenu).outerHeight();
  75. let start_x, start_y;
  76. //--------------------------------
  77. // Auto position
  78. //--------------------------------
  79. if(!options.position){
  80. if(isMobile.phone || isMobile.tablet){
  81. start_x = window.last_touch_x;
  82. start_y = window.last_touch_y;
  83. }else{
  84. start_x = window.mouseX;
  85. start_y = window.mouseY;
  86. }
  87. }
  88. //--------------------------------
  89. // custom position
  90. //--------------------------------
  91. else{
  92. start_x = options.position.left;
  93. start_y = options.position.top;
  94. }
  95. // X position
  96. let x_pos;
  97. if( start_x + menu_width > window.innerWidth){
  98. x_pos = start_x - menu_width;
  99. // if this is a child menu, the width of parent must be also considered
  100. if(options.parent_id){
  101. x_pos -= $(`.context-menu[data-element-id="${options.parent_id}"]`).width() + 30;
  102. }
  103. }else
  104. x_pos = start_x
  105. // Y position
  106. let y_pos;
  107. // is the menu going to go out of the window from the bottom?
  108. if( (start_y + menu_height) > (window.innerHeight - taskbar_height - 10))
  109. y_pos = window.innerHeight - menu_height - taskbar_height - 10;
  110. else
  111. y_pos = start_y;
  112. // Show ContextMenu
  113. $(contextMenu).delay(100).show(0)
  114. // In the right position (the mouse)
  115. .css({
  116. top: y_pos + "px",
  117. left: x_pos + "px"
  118. });
  119. // mark other context menus as inactive
  120. $('.context-menu').not(contextMenu).removeClass('context-menu-active');
  121. // An item is clicked
  122. $(`#context-menu-${menu_id} > li:not(.context-menu-item-disabled)`).on('click', function (e) {
  123. // onClick
  124. if(options.items[$(this).attr("data-action")].onClick && typeof options.items[$(this).attr("data-action")].onClick === 'function'){
  125. let event = e;
  126. event.value = options.items[$(this).attr("data-action")]['val'] ?? undefined;
  127. options.items[$(this).attr("data-action")].onClick(event);
  128. }
  129. // close menu and, if exists, its parent
  130. if(!$(this).hasClass('context-menu-item-submenu')){
  131. $(`#context-menu-${menu_id}, .context-menu[data-element-id="${$(this).closest('.context-menu').attr('data-parent-id')}"]`).fadeOut(200, function(){
  132. $(contextMenu).remove();
  133. });
  134. }
  135. return false;
  136. });
  137. // when mouse is over an item
  138. $(contextMenu).find('.context-menu-item').on('mouseover', function (e) {
  139. // mark other items as inactive
  140. $(contextMenu).find('.context-menu-item').removeClass('context-menu-item-active');
  141. // mark this item as active
  142. $(this).addClass('context-menu-item-active');
  143. // close any submenu that doesn't belong to this item
  144. $(`.context-menu[data-parent-id="${menu_id}"]`).remove();
  145. // mark this context menu as active
  146. $(contextMenu).addClass('context-menu-active');
  147. })
  148. // open submenu if applicable
  149. $(`#context-menu-${menu_id} > li.context-menu-item-submenu`).on('mouseover', function (e) {
  150. // open submenu only if it's not already open
  151. if($(`.context-menu[data-id="${menu_id}-${$(this).attr('data-action')}"]`).length === 0){
  152. let item_rect_box = this.getBoundingClientRect();
  153. // close other submenus
  154. $(`.context-menu[parent-element-id="${menu_id}"]`).remove();
  155. // open the new submenu
  156. UIContextMenu({
  157. items: options.items[parseInt($(this).attr('data-action'))].items,
  158. parent_id: menu_id,
  159. is_submenu: true,
  160. id: menu_id + '-' + $(this).attr('data-action'),
  161. position:{
  162. top: item_rect_box.top - 5,
  163. left: x_pos + item_rect_box.width + 15,
  164. }
  165. })
  166. }
  167. return false;
  168. });
  169. // useful in cases such as where a menue item is over a window, this prevents from the mousedown event
  170. // reaching the window underneath
  171. $(`#context-menu-${menu_id} > li:not(.context-menu-item-disabled)`).on('mousedown', function (e) {
  172. e.preventDefault();
  173. e.stopPropagation();
  174. return false;
  175. })
  176. //disable parent scroll
  177. if(options.parent_element){
  178. $(options.parent_element).css('overflow', 'hidden');
  179. $(options.parent_element).parent().addClass('children-have-open-contextmenu');
  180. $(options.parent_element).addClass('has-open-contextmenu');
  181. }
  182. $(contextMenu).on("remove", function () {
  183. // when removing, make parent scrollable again
  184. if(options.parent_element){
  185. $(options.parent_element).parent().removeClass('children-have-open-contextmenu');
  186. $(options.parent_element).css('overflow', 'scroll');
  187. $(options.parent_element).removeClass('has-open-contextmenu');
  188. if($(options.parent_element).hasClass('taskbar-item')){
  189. make_taskbar_sortable()
  190. }
  191. }
  192. })
  193. $(contextMenu).on("contextmenu", function (e) {
  194. e.preventDefault();
  195. e.stopPropagation();
  196. return false;
  197. })
  198. }
  199. window.select_ctxmenu_item = function ($ctxmenu_item){
  200. // remove active class from other items
  201. $($ctxmenu_item).siblings('.context-menu-item').removeClass('context-menu-item-active');
  202. // add active class to the selected item
  203. $($ctxmenu_item).addClass('context-menu-item-active');
  204. }
  205. export default UIContextMenu;