Menu.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. },
  14. load: function(){
  15. if (this.fireEvent("queryLoad")){
  16. this.node = new Element("div");
  17. this.node.set("styles", this.css.container);
  18. if (this.options.event){
  19. if (this.target) this.target.addEvent(this.options.event, this.showIm.bind(this));
  20. }
  21. this.borderNode = new Element("div", {
  22. "styles": this.css.borderNode
  23. }).inject(this.options.container || $(document.body));
  24. this.node.inject(this.borderNode);
  25. this.hide = this.hideMenu.bind(this);
  26. this.fireEvent("postLoad");
  27. }
  28. },
  29. showIm: function(e){
  30. if (!this.options.disable){
  31. this.hide = this.hideIm.bind(this);
  32. if (this.fireEvent("queryShow", [e])){
  33. this.tmpBodyOncontextmenu = document.body.oncontextmenu;
  34. document.body.oncontextmenu = function(){return false;};
  35. if (this.pauseCount<=0){
  36. this.setItemWidth();
  37. var i = MWF.xDesktop.zIndexPool.zIndex;
  38. this.borderNode.setStyles({
  39. "display": "block",
  40. "opacity": this.options.opacity || 1,
  41. "z-index": i
  42. });
  43. this.setPosition(e);
  44. $(document.body).removeEvent("mousedown", this.hide);
  45. $(document.body).addEvent("mousedown", this.hide);
  46. this.show = true;
  47. }else{
  48. this.pauseCount--;
  49. }
  50. this.fireEvent("postShow");
  51. }
  52. }
  53. },
  54. hideIm: function(all){
  55. if (this.fireEvent("queryHide")){
  56. $(document.body).removeEvent("mousedown", this.hide);
  57. this.borderNode.set("styles", {
  58. "display": "none",
  59. "opacity": 0
  60. });
  61. this.show = false;
  62. document.body.oncontextmenu = this.tmpBodyOncontextmenu;
  63. this.tmpBodyOncontextmenu = null;
  64. if (all) if (this.topMenu) this.topMenu.hideIm();
  65. this.fireEvent("postHide");
  66. }
  67. },
  68. setPosition: function(e){
  69. var position = this.target.getPosition(this.target.getOffsetParent());
  70. var size = this.target.getSize();
  71. var top = position.y+size.y;
  72. var left = position.x-0;
  73. if (this.options.offsetY) top = top+this.options.offsetY;
  74. if (this.options.offsetX) left = left+this.options.offsetX;
  75. var bodySize = $(document.body).getSize();
  76. var borderSize = this.borderNode.getSize();
  77. if (left+borderSize.x>bodySize.x) left = bodySize.x-borderSize.x-10;
  78. this.borderNode.setStyle("top", top);
  79. this.borderNode.setStyle("left", left);
  80. }
  81. });