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.

119 lines
4.1 KiB

  1. /* class2context.js
  2. creator: Arnau
  3. July 2016
  4. version: v0.1
  5. usage:
  6. class2context(
  7. 'className',
  8. "title",
  9. [["option1", "functionoption1()"], ["option 2","functionoption2()"]]
  10. );
  11. */
  12. /* testing this, not necessary */
  13. var cmCallNum=0;
  14. $( document ).ready(function() {
  15. $(document).on('contextmenu', function (e) {
  16. //e.preventDefault();
  17. /*//testing only
  18. console.log('ContextMenus() called, num.' + cmCallNum);
  19. cmCallNum++;*/
  20. ContextMenus();
  21. });
  22. $(document).on('click', function (e) {
  23. //e.preventDefault();
  24. /*//testing only
  25. console.log('ContextMenus() called, num.' + cmCallNum);
  26. cmCallNum++;*/
  27. ContextMenus();
  28. });
  29. });
  30. /* MUTATION OBSERVER
  31. --detect changes in dom, and re-asign context menus*/
  32. // select the target node //MUTATION DOM
  33. var target = document.body;
  34. var mutNum=0;
  35. var observer = new MutationObserver(function(mutations) {
  36. console.log("class2context.js: mutation of dom, num." + mutNum);
  37. mutNum++;
  38. ContextMenus();
  39. });
  40. // configuration of the observer:
  41. var config = { attributes: true, childList: true, characterData: true };
  42. // pass in the target node, as well as the observer options
  43. observer.observe(target, config);
  44. /* </MUTATION OBSERVER */
  45. //creates the div where the context menus will place
  46. document.body.innerHTML+="<div id='contextMenus'></div>";
  47. //
  48. function class2context(classgiv, title, options){
  49. classgiv=JSON.parse(JSON.stringify(classgiv));
  50. if(document.getElementById("context"+classgiv))
  51. {
  52. //toastr.success("alreadyExist");
  53. }else{
  54. var aux="";
  55. aux+="<div id='context"+classgiv+"' class='contextMenu'>";
  56. aux+=" <ul class='c2c-dropdown c2c-border' style='display:block;position:static;margin-bottom:5px;'>";
  57. aux+="<div class='contextTitle'>" + title + "</div>";
  58. for(var i=0; i<options.length; i++)
  59. {
  60. //aux+=" <a onmousedown='"+options[i][1]+";' href='javascript:void(0);'>"+options[i][0]+"</a>";
  61. aux+=" <a onmousedown='"+options[i][1]+"; ContextMenus();' href='javascript:void(0);'>"+options[i][0]+"</a>";
  62. }
  63. aux+=" </ul>";
  64. aux+="</div>";
  65. document.getElementById('contextMenus').innerHTML+=aux; //adds the div context menu into the context menus div place
  66. }
  67. for(var i=0; i<document.getElementsByClassName(classgiv).length; i++)
  68. {
  69. classgiv=JSON.parse(JSON.stringify(classgiv));
  70. //toastr.info(document.getElementsByClassName(classgiv)[i].oncontextmenu);
  71. document.getElementsByClassName(classgiv)[i].addEventListener("contextmenu", function(e){
  72. // Avoid the real one
  73. e.preventDefault();
  74. e.stopPropagation();
  75. setTimeout(function(){
  76. //classaux=this.className.split(" ")[0];
  77. document.getElementById('context'+classgiv).style.display='block';
  78. document.getElementById('context'+classgiv).style.left=e.pageX + "px";
  79. document.getElementById('context'+classgiv).style.top=e.pageY + "px";
  80. }, 150);
  81. });
  82. /*document.getElementsByClassName(classgiv)[i].oncontextmenu+= function(e){
  83. // Avoid the real one
  84. e.preventDefault();
  85. setTimeout(function(){
  86. //classaux=this.className.split(" ")[0];
  87. document.getElementById('context'+classgiv).style.display='block';
  88. document.getElementById('context'+classgiv).style.left=e.pageX + "px";
  89. document.getElementById('context'+classgiv).style.top=e.pageY + "px";
  90. }, 150);
  91. };*/
  92. }
  93. document.body.addEventListener("mousedown", function(e){
  94. // Avoid the real one
  95. //e.preventDefault();
  96. if(document.getElementById('context'+classgiv))
  97. {
  98. setTimeout(function(){
  99. document.getElementById('context'+classgiv).style.display='none';
  100. }, 70);
  101. //ContextMenus();
  102. }
  103. return false;
  104. }, false);
  105. }