21 function clickInsideElement( e, className ) {
22 var el = e.srcElement || e.target;
24 if ( el.classList.contains(className) ) {
27 while ( el = el.parentNode ) {
28 if ( el.classList && el.classList.contains(className) ) {
43 function getPosition(e) {
47 if (!e) var e = window.event;
49 if (e.pageX || e.pageY) {
52 }
else if (e.clientX || e.clientY) {
53 posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
54 posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
74 var contextMenuClassName =
"context-menu";
75 var contextMenuItemClassName =
"context-menu__item";
76 var contextMenuLinkClassName =
"context-menu__link";
77 var contextMenuActive =
"context-menu--active";
79 var taskItemClassName =
"macroDiv";
80 var taskItemInContext;
86 var menu = document.querySelector(
".context-menu");
87 var menuItems = menu.querySelectorAll(
".context-menu__item");
101 function initContextMenu() {
111 function contextListener() {
112 document.addEventListener(
"contextmenu",
function(e) {
113 taskItemInContext = clickInsideElement( e, taskItemClassName );
115 if ( taskItemInContext ) {
120 taskItemInContext = null;
129 function clickListener() {
130 document.addEventListener(
"click",
function(e) {
131 var clickeElIsLink = clickInsideElement( e, contextMenuLinkClassName );
133 if ( clickeElIsLink ) {
135 menuItemListener( clickeElIsLink );
137 var button = e.which || e.button;
138 if ( button === 1 ) {
148 function keyupListener() {
149 window.onkeyup =
function(e) {
150 if ( e.keyCode === 27 ) {
159 function resizeListener() {
160 window.onresize =
function(e) {
168 function toggleMenuOn() {
169 if ( menuState !== 1 ) {
171 menu.classList.add( contextMenuActive );
178 function toggleMenuOff() {
179 if ( menuState !== 0 ) {
181 menu.classList.remove( contextMenuActive );
190 function positionMenu(e) {
191 clickCoords = getPosition(e);
192 clickCoordsX = clickCoords.x;
193 clickCoordsY = clickCoords.y;
195 menuWidth = menu.offsetWidth + 4;
196 menuHeight = menu.offsetHeight + 4;
198 windowWidth = window.innerWidth;
199 windowHeight = window.innerHeight;
201 if ( (windowWidth - clickCoordsX) < menuWidth ) {
202 menu.style.left = windowWidth - menuWidth +
"px";
204 menu.style.left = clickCoordsX +
"px";
207 if ( (windowHeight - clickCoordsY) < menuHeight ) {
208 menu.style.top = windowHeight - menuHeight +
"px";
210 menu.style.top = clickCoordsY +
"px";
219 function menuItemListener( link ) {
220 console.log(
"Macro name - " + taskItemInContext.getAttribute(
"data-id") +
", Macro action - " + link.getAttribute(
"data-action"));
222 macroActionOnRightClick(taskItemInContext.getAttribute(
"data-id"), link.getAttribute(
"data-action"),
223 taskItemInContext.getAttribute(
"data-sequence"), taskItemInContext.getAttribute(
"data-notes"),
224 taskItemInContext.getAttribute(
"data-time"), taskItemInContext.getAttribute(
"data-LSBF"));