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.

94 lines
3.2 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. //if the div 'contextMenus' exist, deletes
  13. if(document.getElementById('contextMenus'))
  14. {
  15. removeElemById('contextMenus');
  16. }
  17. //creates the div where the context menus will place
  18. document.body.innerHTML+="<div id='contextMenus'></div>";
  19. //
  20. function removeElemById(id) {
  21. return (elem=document.getElementById(id)).parentNode.removeChild(elem);
  22. }
  23. function class2context(classgiv, title, options){
  24. classgiv=JSON.parse(JSON.stringify(classgiv));
  25. if(document.getElementById("context"+classgiv))
  26. {
  27. //toastr.success("alreadyExist");
  28. }else{
  29. var aux="";
  30. aux+="<div id='context"+classgiv+"' class='contextMenu'>";
  31. aux+=" <ul class='c2c-dropdown c2c-border' style='display:block;position:static;margin-bottom:5px;'>";
  32. aux+="<div class='contextTitle'>" + title + "</div>";
  33. for(var i=0; i<options.length; i++)
  34. {
  35. //aux+=" <a onmousedown='"+options[i][1]+";' href='javascript:void(0);'>"+options[i][0]+"</a>";
  36. aux+=" <a onmousedown='"+options[i][1]+"; ContextMenus();' href='javascript:void(0);'>"+options[i][0]+"</a>";
  37. }
  38. aux+=" </ul>";
  39. aux+="</div>";
  40. document.getElementById('contextMenus').innerHTML+=aux; //adds the div context menu into the context menus div place
  41. }
  42. for(var i=0; i<document.getElementsByClassName(classgiv).length; i++)
  43. {
  44. classgiv=JSON.parse(JSON.stringify(classgiv));
  45. //toastr.info(document.getElementsByClassName(classgiv)[i].oncontextmenu);
  46. document.getElementsByClassName(classgiv)[i].addEventListener("contextmenu", function(e){
  47. // Avoid the real one
  48. e.preventDefault();
  49. e.stopPropagation();
  50. setTimeout(function(){
  51. //classaux=this.className.split(" ")[0];
  52. document.getElementById('context'+classgiv).style.display='block';
  53. document.getElementById('context'+classgiv).style.left=e.pageX + "px";
  54. document.getElementById('context'+classgiv).style.top=e.pageY + "px";
  55. }, 150);
  56. });
  57. /*document.getElementsByClassName(classgiv)[i].oncontextmenu+= function(e){
  58. // Avoid the real one
  59. e.preventDefault();
  60. setTimeout(function(){
  61. //classaux=this.className.split(" ")[0];
  62. document.getElementById('context'+classgiv).style.display='block';
  63. document.getElementById('context'+classgiv).style.left=e.pageX + "px";
  64. document.getElementById('context'+classgiv).style.top=e.pageY + "px";
  65. }, 150);
  66. };*/
  67. }
  68. document.body.addEventListener("mousedown", function(e){
  69. // Avoid the real one
  70. //e.preventDefault();
  71. if(document.getElementById('context'+classgiv))
  72. {
  73. setTimeout(function(){
  74. document.getElementById('context'+classgiv).style.display='none';
  75. }, 70);
  76. //ContextMenus();
  77. }
  78. return false;
  79. }, false);
  80. }