Menu.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 ? MWF.xDesktop.zIndexPool.zIndex : 0;
  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. var p = this.node.getPosition(document.body);
  52. var size = this.node.getSize();
  53. var bodySize = document.body.getSize();
  54. if (p.y+size.y+10>bodySize.y){
  55. var y = bodySize.y-p.y-10;
  56. this.node.setStyle("height", ""+y+"px");
  57. this.node.addEvent("mousedown", function(e){ e.stopPropagation(); })
  58. }
  59. this.fireEvent("postShow");
  60. }
  61. }
  62. },
  63. hideIm: function(all){
  64. if (this.fireEvent("queryHide")){
  65. $(document.body).removeEvent("mousedown", this.hide);
  66. this.borderNode.set("styles", {
  67. "display": "none",
  68. "opacity": 0
  69. });
  70. this.show = false;
  71. document.body.oncontextmenu = this.tmpBodyOncontextmenu;
  72. this.tmpBodyOncontextmenu = null;
  73. if (all) if (this.topMenu) this.topMenu.hideIm();
  74. this.fireEvent("postHide");
  75. }
  76. },
  77. setPosition: function(e){
  78. var position = this.target.getPosition(this.target.getOffsetParent());
  79. var size = this.target.getSize();
  80. this.borderNode.show();
  81. var nodeSize = this.borderNode.getSize();
  82. var left=0, top=0;
  83. switch (this.options.where.x.toLowerCase()){
  84. case "right":
  85. left = position.x-nodeSize.x+size.x;
  86. break;
  87. default:
  88. left = position.x-0;
  89. }
  90. switch (this.options.where.y.toLowerCase()){
  91. case "top":
  92. top = position.y-nodeSize.y;
  93. break;
  94. default:
  95. top = position.y+size.y;
  96. }
  97. //(this.options.where)
  98. if (this.options.offsetY) top = top+this.options.offsetY;
  99. if (this.options.offsetX) left = left+this.options.offsetX;
  100. var bodySize = $(document.body).getSize();
  101. var borderSize = this.borderNode.getSize();
  102. if (left+borderSize.x>bodySize.x) left = bodySize.x-borderSize.x-10;
  103. this.borderNode.setStyle("top", top);
  104. this.borderNode.setStyle("left", left);
  105. }
  106. });