Menu.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. MWF.xDesktop = MWF.xDesktop || {};
  2. MWF.require("MWF.widget.Menu", null, false);
  3. MWF.xDesktop.Menu = new Class({
  4. Extends: MWF.widget.Menu,
  5. Implements: [Options, Events],
  6. options: {
  7. "style": "default",
  8. "event": "contextmenu",
  9. "disable": false,
  10. "top": -1,
  11. "left": -1,
  12. "container": null,
  13. "where": {"x": "left", "y": "bottom"}
  14. },
  15. load: function(){
  16. if (this.fireEvent("queryLoad")){
  17. this.node = new Element("div#menu");
  18. this.node.set("styles", this.css.container);
  19. if (this.options.event){
  20. if (this.target) this.target.addEvent(this.options.event, this.showIm.bind(this));
  21. }
  22. this.borderNode = new Element("div.MWFMenu", {
  23. "styles": this.css.borderNode
  24. }).inject(this.options.container || $(document.body));
  25. this.node.inject(this.borderNode);
  26. this.hide = this.hideMenu.bind(this);
  27. this.fireEvent("postLoad");
  28. }
  29. },
  30. showIm: function(e){
  31. if (!this.options.disable){
  32. this.hide = this.hideIm.bind(this);
  33. if (this.fireEvent("queryShow", [e])){
  34. this.tmpBodyOncontextmenu = document.body.oncontextmenu;
  35. document.body.oncontextmenu = function(){return false;};
  36. if (this.pauseCount<=0){
  37. this.setItemWidth();
  38. var i = MWF.xDesktop.zIndexPool.zIndex;
  39. this.borderNode.setStyles({
  40. "display": "block",
  41. "opacity": this.options.opacity || 1,
  42. "z-index": i
  43. });
  44. this.setPosition(e);
  45. $(document.body).removeEvent("mousedown", this.hide);
  46. $(document.body).addEvent("mousedown", this.hide);
  47. this.show = true;
  48. }else{
  49. this.pauseCount--;
  50. }
  51. this.fireEvent("postShow");
  52. }
  53. }
  54. },
  55. hideIm: function(all){
  56. if (this.fireEvent("queryHide")){
  57. $(document.body).removeEvent("mousedown", this.hide);
  58. this.borderNode.set("styles", {
  59. "display": "none",
  60. "opacity": 0
  61. });
  62. this.show = false;
  63. document.body.oncontextmenu = this.tmpBodyOncontextmenu;
  64. this.tmpBodyOncontextmenu = null;
  65. if (all) if (this.topMenu) this.topMenu.hideIm();
  66. this.fireEvent("postHide");
  67. }
  68. },
  69. setPosition: function(e){
  70. var position = this.target.getPosition(this.target.getOffsetParent());
  71. var size = this.target.getSize();
  72. this.borderNode.show();
  73. var nodeSize = this.borderNode.getSize();
  74. var left=0, top=0;
  75. switch (this.options.where.x.toLowerCase()){
  76. case "right":
  77. left = position.x-nodeSize.x+size.x;
  78. break;
  79. default:
  80. left = position.x-0;
  81. }
  82. switch (this.options.where.y.toLowerCase()){
  83. case "top":
  84. top = position.y-nodeSize.y;
  85. break;
  86. default:
  87. top = position.y+size.y;
  88. }
  89. //(this.options.where)
  90. if (this.options.offsetY) top = top+this.options.offsetY;
  91. if (this.options.offsetX) left = left+this.options.offsetX;
  92. var bodySize = $(document.body).getSize();
  93. var borderSize = this.borderNode.getSize();
  94. if (left+borderSize.x>bodySize.x) left = bodySize.x-borderSize.x-10;
  95. this.borderNode.setStyle("top", top);
  96. this.borderNode.setStyle("left", left);
  97. }
  98. });