| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | const $contextMenuContainer = $("#context-menu-container"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 20:54:17 +01:00
										 |  |  | let dateContextMenuOpenedMs = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 21:34:01 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param event - originating click event (used to get coordinates to display menu at position) | 
					
						
							|  |  |  |  * @param {object} contextMenu - needs to have getContextMenuItems() and selectContextMenuItem(e, cmd) | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | async function initContextMenu(event, contextMenu) { | 
					
						
							| 
									
										
										
										
											2018-12-28 22:05:04 +01:00
										 |  |  |     event.stopPropagation(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  |     $contextMenuContainer.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |     function addItems($parent, items) { | 
					
						
							|  |  |  |         for (const item of items) { | 
					
						
							|  |  |  |             if (item.title === '----') { | 
					
						
							|  |  |  |                 $parent.append($("<div>").addClass("dropdown-divider")); | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                 const $icon = $("<span>"); | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                 if (item.uiIcon) { | 
					
						
							| 
									
										
										
										
											2019-11-02 12:17:00 +01:00
										 |  |  |                     $icon.addClass("bx bx-" + item.uiIcon); | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     $icon.append(" "); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                 const $link = $("<span>") | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                     .append($icon) | 
					
						
							|  |  |  |                     .append("   ") // some space between icon and text
 | 
					
						
							|  |  |  |                     .append(item.title); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                 const $item = $("<li>") | 
					
						
							|  |  |  |                     .addClass("dropdown-item") | 
					
						
							|  |  |  |                     .append($link) | 
					
						
							|  |  |  |                     .attr("data-cmd", item.cmd) | 
					
						
							|  |  |  |                     .click(function (e) { | 
					
						
							|  |  |  |                         const cmd = $(e.target).closest(".dropdown-item").attr("data-cmd"); | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                         e.originalTarget = event.target; | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                         contextMenu.selectContextMenuItem(e, cmd); | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |                         hideContextMenu(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                         // it's important to stop the propagation especially for sub-menus, otherwise the event
 | 
					
						
							|  |  |  |                         // might be handled again by top-level menu
 | 
					
						
							|  |  |  |                         return false; | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                 if (item.enabled !== undefined && !item.enabled) { | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  |                     $item.addClass("disabled"); | 
					
						
							| 
									
										
										
										
											2019-04-01 21:11:20 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-11-09 22:18:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                 if (item.items) { | 
					
						
							|  |  |  |                     $item.addClass("dropdown-submenu"); | 
					
						
							|  |  |  |                     $link.addClass("dropdown-toggle"); | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  |                     const $subMenu = $("<ul>").addClass("dropdown-menu"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     addItems($subMenu, item.items); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     $item.append($subMenu); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $parent.append($item); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |     addItems($contextMenuContainer, await contextMenu.getContextMenuItems()); | 
					
						
							| 
									
										
										
										
											2019-03-17 11:38:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  |     // code below tries to detect when dropdown would overflow from page
 | 
					
						
							|  |  |  |     // in such case we'll position it above click coordinates so it will fit into client
 | 
					
						
							|  |  |  |     const clickPosition = event.pageY; | 
					
						
							|  |  |  |     const clientHeight = document.documentElement.clientHeight; | 
					
						
							|  |  |  |     const contextMenuHeight = $contextMenuContainer.height(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let top; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (clickPosition + contextMenuHeight > clientHeight) { | 
					
						
							|  |  |  |         top = clientHeight - contextMenuHeight - 10; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         top = event.pageY - 10; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 20:54:17 +01:00
										 |  |  |     dateContextMenuOpenedMs = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  |     $contextMenuContainer.css({ | 
					
						
							|  |  |  |         display: "block", | 
					
						
							|  |  |  |         top: top, | 
					
						
							|  |  |  |         left: event.pageX - 20 | 
					
						
							|  |  |  |     }).addClass("show"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 20:54:17 +01:00
										 |  |  | $(document).click(() => hideContextMenu()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function hideContextMenu() { | 
					
						
							|  |  |  |     // this date checking comes from change in FF66 - https://github.com/zadam/trilium/issues/468
 | 
					
						
							|  |  |  |     // "contextmenu" event also triggers "click" event which depending on the timing can close just opened context menu
 | 
					
						
							|  |  |  |     // we might filter out right clicks, but then it's better if even right clicks close the context menu
 | 
					
						
							|  |  |  |     if (Date.now() - dateContextMenuOpenedMs > 300) { | 
					
						
							|  |  |  |         $contextMenuContainer.hide(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							| 
									
										
										
										
											2019-03-28 20:54:17 +01:00
										 |  |  |     initContextMenu, | 
					
						
							|  |  |  |     hideContextMenu | 
					
						
							| 
									
										
										
										
											2018-11-09 17:11:45 +01:00
										 |  |  | } |