00001 (function() {
00002
00003 "use strict";
00004
00007
00008
00009
00012
00021 function clickInsideElement( e, className ) {
00022 var el = e.srcElement || e.target;
00023
00024 if ( el.classList.contains(className) ) {
00025 return el;
00026 } else {
00027 while ( el = el.parentNode ) {
00028 if ( el.classList && el.classList.contains(className) ) {
00029 return el;
00030 }
00031 }
00032 }
00033
00034 return false;
00035 }
00036
00043 function getPosition(e) {
00044 var posx = 0;
00045 var posy = 0;
00046
00047 if (!e) var e = window.event;
00048
00049 if (e.pageX || e.pageY) {
00050 posx = e.pageX;
00051 posy = e.pageY;
00052 } else if (e.clientX || e.clientY) {
00053 posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
00054 posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
00055 }
00056
00057 return {
00058 x: posx,
00059 y: posy
00060 }
00061 }
00062
00065
00066
00067
00070
00074 var contextMenuClassName = "context-menu";
00075 var contextMenuItemClassName = "context-menu__item";
00076 var contextMenuLinkClassName = "context-menu__link";
00077 var contextMenuActive = "context-menu--active";
00078
00079 var taskItemClassName = "macroDiv";
00080 var taskItemInContext;
00081
00082 var clickCoords;
00083 var clickCoordsX;
00084 var clickCoordsY;
00085
00086 var menu = document.querySelector(".context-menu");
00087 var menuItems = menu.querySelectorAll(".context-menu__item");
00088 var menuState = 0;
00089 var menuWidth;
00090 var menuHeight;
00091 var menuPosition;
00092 var menuPositionX;
00093 var menuPositionY;
00094
00095 var windowWidth;
00096 var windowHeight;
00097
00101 function initContextMenu() {
00102 contextListener();
00103 clickListener();
00104 keyupListener();
00105 resizeListener();
00106 }
00107
00111 function contextListener() {
00112 document.addEventListener( "contextmenu", function(e) {
00113 taskItemInContext = clickInsideElement( e, taskItemClassName );
00114
00115 if ( taskItemInContext ) {
00116 e.preventDefault();
00117 toggleMenuOn();
00118 positionMenu(e);
00119 } else {
00120 taskItemInContext = null;
00121 toggleMenuOff();
00122 }
00123 });
00124 }
00125
00129 function clickListener() {
00130 document.addEventListener( "click", function(e) {
00131 var clickeElIsLink = clickInsideElement( e, contextMenuLinkClassName );
00132
00133 if ( clickeElIsLink ) {
00134 e.preventDefault();
00135 menuItemListener( clickeElIsLink );
00136 } else {
00137 var button = e.which || e.button;
00138 if ( button === 1 ) {
00139 toggleMenuOff();
00140 }
00141 }
00142 });
00143 }
00144
00148 function keyupListener() {
00149 window.onkeyup = function(e) {
00150 if ( e.keyCode === 27 ) {
00151 toggleMenuOff();
00152 }
00153 }
00154 }
00155
00159 function resizeListener() {
00160 window.onresize = function(e) {
00161 toggleMenuOff();
00162 };
00163 }
00164
00168 function toggleMenuOn() {
00169 if ( menuState !== 1 ) {
00170 menuState = 1;
00171 menu.classList.add( contextMenuActive );
00172 }
00173 }
00174
00178 function toggleMenuOff() {
00179 if ( menuState !== 0 ) {
00180 menuState = 0;
00181 menu.classList.remove( contextMenuActive );
00182 }
00183 }
00184
00190 function positionMenu(e) {
00191 clickCoords = getPosition(e);
00192 clickCoordsX = clickCoords.x;
00193 clickCoordsY = clickCoords.y;
00194
00195 menuWidth = menu.offsetWidth + 4;
00196 menuHeight = menu.offsetHeight + 4;
00197
00198 windowWidth = window.innerWidth;
00199 windowHeight = window.innerHeight;
00200
00201 if ( (windowWidth - clickCoordsX) < menuWidth ) {
00202 menu.style.left = windowWidth - menuWidth + "px";
00203 } else {
00204 menu.style.left = clickCoordsX + "px";
00205 }
00206
00207 if ( (windowHeight - clickCoordsY) < menuHeight ) {
00208 menu.style.top = windowHeight - menuHeight + "px";
00209 } else {
00210 menu.style.top = clickCoordsY + "px";
00211 }
00212 }
00213
00219 function menuItemListener( link ) {
00220 console.log( "Macro name - " + taskItemInContext.getAttribute("data-id") + ", Macro action - " + link.getAttribute("data-action"));
00221 toggleMenuOff();
00222 macroActionOnRightClick(taskItemInContext.getAttribute("data-id"), link.getAttribute("data-action"),
00223 taskItemInContext.getAttribute("data-sequence"), taskItemInContext.getAttribute("data-notes"),
00224 taskItemInContext.getAttribute("data-time"), taskItemInContext.getAttribute("data-LSBF"));
00225 }
00226
00230 initContextMenu();
00231
00232
00233
00234 })();