Advertisement
inanutshellus

Dojo Toolkit ContextMenu

May 20th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. define([
  2. "dijit/Menu",
  3. "dijit/MenuItem",
  4. "dijit/MenuSeparator",
  5. "dijit/registry",
  6.  
  7. "dojo/_base/declare",
  8. "dojo/dom-style",
  9. "dojo/query!css2"
  10. ], function(
  11. Menu,
  12. MenuItem,
  13. MenuSeparator,
  14. registry,
  15. declare,
  16. domStyle
  17. ){
  18. var ContextMenu = declare([Menu], {
  19.  
  20. addMenuItem: function(label, callback, insertIndex) {
  21. // summary:
  22. // Create a clickable row in the context menu,
  23. // and pass to the callback the event AND clicked widget.
  24. // label: string
  25. // The displayed value of the menu item
  26. // callback: function
  27. // When the menu item is clicked, this function
  28. // will be called with two parameters:
  29. // * The original click event
  30. // * The clicked widget
  31. // insertIndex: Number
  32. // Optional. Inserts the created menu item at a specific Menu position.
  33.  
  34. var menuItem = new MenuItem({
  35. label: label,
  36. onClick: function(evt) {
  37.  
  38. var node = this.getParent().currentTarget;
  39.  
  40. var widget;
  41. while (!widget && node !== document.body) {
  42. widget = registry.byNode(node);
  43. node = node.parentNode;
  44. }
  45.  
  46. if (widget) {
  47. callback(evt, widget);
  48. } else {
  49. console.error("ContextMenu - No widget found from clicked node.");
  50. }
  51. }
  52. });
  53. this.addChild(menuItem, insertIndex);
  54.  
  55. return menuItem;
  56. },
  57.  
  58. addMenuSeparator: function(insertIndex) {
  59.  
  60. // summary:
  61. // Create a horizontal line between menu items
  62. // insertIndex: Number
  63. // Optional. Inserts the created menu item at a specific Menu position.
  64.  
  65. var menuItem = new MenuSeparator();
  66. this.addChild(menuItem, insertIndex);
  67.  
  68. return menuItem;
  69. },
  70.  
  71. updateMenuItemLabel : function(menuItemId, label){
  72. var menuItems = this.getChildren();
  73. for(var i=0; i < menuItems.length; ++i){
  74. if(menuItemId === menuItems[i].id){
  75. menuItems[i].set("label", label);
  76. break;
  77. }
  78. }
  79. },
  80.  
  81. hideMenuItem :function(menuItemId){
  82. this._hideMenuItem(menuItemId);
  83. },
  84.  
  85. showMenuItem : function(menuItemId){
  86. this._showMenuItem(menuItemId);
  87. },
  88.  
  89. toggleMenuItem: function(menuItemId, show) {
  90. if (show) {
  91. this._showMenuItem(menuItemId);
  92. } else {
  93. this._hideMenuItem(menuItemId);
  94. }
  95. },
  96.  
  97. /*
  98. * Blur event does not happen on the element we created the context menu on, this
  99. * hides the menu when the creating element is clicked.
  100. * This forces a blur fire to when desired.
  101. * There is no public method to do this on Menu.
  102. */
  103. hideContextMenu: function(){
  104. this._onBlur();
  105. },
  106.  
  107. _hideMenuItem :function(menuItemId){
  108. var menuItems = this.getChildren();
  109. for(var i=0; i < menuItems.length; ++i){
  110. if(menuItemId === menuItems[i].id){
  111. domStyle.set(menuItems[i].domNode, "display", "none");
  112. break;
  113. }
  114. }
  115. },
  116.  
  117. _showMenuItem : function(menuItemId){
  118. var menuItems = this.getChildren();
  119. for(var i=0; i < menuItems.length; ++i){
  120. if(menuItemId === menuItems[i].id){
  121. domStyle.set(menuItems[i].domNode, "display", "");
  122. break;
  123. }
  124. }
  125. },
  126.  
  127. });
  128. return ContextMenu;
  129. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement