Menu.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. MWF.widget = MWF.widget || {};
  2. MWF.widget.Menu = new Class({
  3. Implements: [Options, Events],
  4. Extends: MWF.widget.Common,
  5. options: {
  6. "style": "default",
  7. "event": "contextmenu",
  8. "disable": false,
  9. "top": -1,
  10. "left": -1,
  11. "container": null
  12. },
  13. initialize: function(target, options){
  14. this.setOptions(options);
  15. this.items = [];
  16. this.path = MWF.defaultPath+"/widget/$Menu/";
  17. this.cssPath = MWF.defaultPath+"/widget/$Menu/"+this.options.style+"/css.wcss";
  18. this._loadCss();
  19. this.target = $(target);
  20. // if (this.target.onselectstart)
  21. if (this.target) this.target.onselectstart = function(){return false;};
  22. // if (this.target.oncontextmenu)
  23. if (this.target) this.target.oncontextmenu = function(){return false;};
  24. this.pauseCount = 0;
  25. },
  26. pause: function(count){
  27. this.pauseCount = count;
  28. },
  29. load: function(){
  30. if (this.fireEvent("queryLoad")){
  31. this.node = new Element("div");
  32. this.node.set("styles", this.css.container);
  33. if (this.options.event){
  34. if (this.target) this.target.addEvent(this.options.event, this.showIm.bind(this));
  35. }
  36. this.node.inject(this.options.container || $(document.body));
  37. this.hide = this.hideMenu.bind(this);
  38. this.fireEvent("postLoad");
  39. }
  40. },
  41. setItemWidth: function(){
  42. this.items.each(function(item){
  43. var w1 = this.node.getStyle("padding-left").toInt();
  44. var w2 = this.node.getStyle("padding-right").toInt();
  45. var w3 = this.node.getStyle("border-left-width").toInt();
  46. var w4 = this.node.getStyle("border-right-width").toInt();
  47. var w5 = item.item.getStyle("border-left-width").toInt();
  48. var w6 = item.item.getStyle("border-right-width").toInt();
  49. var w7 = item.item.getStyle("margin-left").toInt();
  50. // item.item.setStyle("width", this.node.getSize().x-w1-w2-w3-w4-w5-w6-w7);
  51. /*
  52. if (item.type=="line"){
  53. item.item.setStyle("width", this.node.getSize().x-8-28);
  54. }else{
  55. item.item.setStyle("width", this.node.getSize().x-8);
  56. }*/
  57. }.bind(this));
  58. },
  59. setPosition: function(e){
  60. var top;
  61. var left;
  62. if (this.options.top==-1){
  63. top = e.page.y;
  64. }else{
  65. top = this.options.top;
  66. }
  67. if (this.options.left==-1){
  68. left = e.page.x;
  69. }else{
  70. left = this.options.left;
  71. }
  72. var size = this.node.getSize();
  73. var bodySize = $(document.body).getSize();
  74. if (left+size.x>bodySize.x){
  75. left = left-size.x-5;
  76. if (left<0) left = 0;
  77. }
  78. var scrollTop = ($(document.body).getScroll().y.toFloat()) || 0;
  79. if (top+size.y>bodySize.y+scrollTop){
  80. top = top-size.y-5;
  81. if (top<0) top = 0;
  82. }
  83. this.node.setStyle("top", top);
  84. this.node.setStyle("left", left);
  85. },
  86. showIm: function(e){
  87. if (!this.options.disable){
  88. this.hide = this.hideIm.bind(this);
  89. if (this.fireEvent("queryShow", [e])){
  90. this.tmpBodyOncontextmenu = document.body.oncontextmenu;
  91. document.body.oncontextmenu = function(){return false;};
  92. if (this.pauseCount<=0){
  93. this.setItemWidth();
  94. this.node.setStyles({
  95. "display": "block",
  96. "opacity": this.options.opacity || 1
  97. });
  98. this.setPosition(e);
  99. $(document.body).removeEvent("mousedown", this.hide);
  100. $(document.body).addEvent("mousedown", this.hide);
  101. this.show = true;
  102. }else{
  103. this.pauseCount--;
  104. }
  105. this.fireEvent("postShow");
  106. }
  107. }
  108. },
  109. hideIm: function(all){
  110. if (this.fireEvent("queryHide")){
  111. $(document.body).removeEvent("mousedown", this.hide);
  112. this.node.set("styles", {
  113. "display": "none",
  114. "opacity": 0
  115. });
  116. this.show = false;
  117. document.body.oncontextmenu = this.tmpBodyOncontextmenu;
  118. this.tmpBodyOncontextmenu = null;
  119. if (all){
  120. var menu = this;
  121. while (menu.topMenu){
  122. menu = menu.topMenu;
  123. }
  124. menu.hideIm();
  125. //if (this.topMenu) this.topMenu.hideIm();
  126. } else {
  127. this.items.each(function(item){
  128. if (item.type=="menu"){
  129. item.subMenu.hideIm();
  130. }
  131. });
  132. }
  133. this.fireEvent("postHide");
  134. }
  135. },
  136. showMenu: function(e){
  137. if (!this.show){
  138. if (this.pauseCount<=0){
  139. if (!this.options.disable){
  140. this.hide = this.hideMenu.bind(this);
  141. if (this.fireEvent("queryShow", [e])){
  142. this.tmpBodyOncontextmenu = document.body.oncontextmenu;
  143. document.body.oncontextmenu = function(){return false;};
  144. this.node.setStyle("display", "block");
  145. this.setItemWidth();
  146. this.setPosition(e);
  147. if (!this.morph){
  148. this.morph = new Fx.Morph(this.node, {duration: 100});
  149. }
  150. this.morph.start({
  151. "opacity": this.options.opacity || 1
  152. }).chain(function(){
  153. $(document).removeEvent("click", this.hide);
  154. $(document).addEvent("click", this.hide);
  155. $(document).removeEvent("mousedown", this.hide);
  156. $(document).addEvent("mousedown", this.hide);
  157. this.show = true;
  158. this.fireEvent("postShow");
  159. }.bind(this));
  160. }
  161. }
  162. }else{
  163. this.pauseCount--;
  164. }
  165. }
  166. },
  167. hideMenu: function(){
  168. $(document).removeEvent("click", this.hide);
  169. if (this.show){
  170. if (!this.morph){
  171. this.morph = new Fx.Morph(this.node, {duration: 100});
  172. }
  173. if (this.fireEvent("queryHide")){
  174. this.morph.start({
  175. "opacity": 0
  176. }).chain(function(){
  177. this.node.set("styles", {
  178. "display": "none"
  179. });
  180. this.show = false;
  181. document.body.oncontextmenu = this.tmpBodyOncontextmenu;
  182. this.tmpBodyOncontextmenu = null;
  183. this.fireEvent("postHide");
  184. }.bind(this));
  185. }
  186. }
  187. },
  188. clearItems: function(){
  189. this.items.each(function(item){
  190. item.remove();
  191. });
  192. this.items = [];
  193. },
  194. addMenuItem: function(str, even, fun, img, disable){
  195. var item = new MWF.widget.MenuItem(this, {
  196. "text": str,
  197. "event": even,
  198. "action": fun,
  199. "img": img,
  200. "disable": disable
  201. });
  202. item.load();
  203. this.items.push(item);
  204. return item;
  205. },
  206. addMenuMenu: function(str, img, menu, disable){
  207. var item = new MWF.widget.MenuMenu(this, menu, {
  208. "text": str,
  209. "img": img,
  210. "disable": disable
  211. });
  212. item.load();
  213. this.items.push(item);
  214. return item;
  215. },
  216. addMenuLine: function(){
  217. var item = new MWF.widget.MenuLine(this);
  218. item.load();
  219. this.items.push(item);
  220. },
  221. _loadToolbarItemNode: function(){
  222. var subNodes = this.node.getChildren();
  223. subNodes.each(function(node, idx){
  224. var type = node.get("MWFnodetype");
  225. if (type){
  226. if (typeOf(this[type])=="array"){
  227. this[type].push(node);
  228. }else{
  229. this[type] = [];
  230. this[type].push(node);
  231. }
  232. }
  233. }.bind(this));
  234. },
  235. _loadMenuItems: function(){
  236. this._loadToolBarSeparator(this.MWFToolBarMenuItem);
  237. this._loadToolBarButton(this.MWFToolBarMenuLine);
  238. this._loadToolBarMenu(this.MWFToolBarMenuItem);
  239. },
  240. _loadToolBarSeparator: function(nodes){
  241. if (nodes) {
  242. nodes.each(function(node, idx){
  243. node.set("styles", this.css.toolbarSeparator);
  244. }.bind(this));
  245. }
  246. },
  247. _loadToolBarButton: function(nodes){
  248. if (nodes) {
  249. nodes.each(function(node, idx){
  250. var btn = new MWF.widget.ToolbarButton(node, this);
  251. btn.load();
  252. if (btn.buttonID){
  253. this.items[btn.buttonID] = btn;
  254. }
  255. this.children.push(btn);
  256. this.childrenButton.push(btn);
  257. }.bind(this));
  258. }
  259. },
  260. _loadToolBarMenu: function(nodes){
  261. if (nodes) {
  262. nodes.each(function(node, idx){
  263. var btn = new MWF.widget.ToolbarMenu(node, this);
  264. btn.load();
  265. if (btn.buttonID){
  266. this.items[btn.buttonID] = btn;
  267. }
  268. this.children.push(btn);
  269. this.childrenMenu.push(btn);
  270. }.bind(this));
  271. }
  272. }
  273. });
  274. MWF.widget.MenuItem = new Class({
  275. Implements: [Options],
  276. options: {
  277. "text": "",
  278. "event": "",
  279. "action": null,
  280. "img": "",
  281. "disable": false
  282. },
  283. initialize: function(menu, options){
  284. this.setOptions(options);
  285. this.menu = menu;
  286. this.type="item";
  287. this.createNode();
  288. },
  289. createNode: function(){
  290. this.item = new Element("div", {"styles": this.menu.css.menuItem});
  291. var imgDiv = new Element("div", {"styles": this.menu.css.menuItemImgDiv}).inject(this.item);
  292. if (this.options.img) var img = new Element("img", {"styles": this.menu.css.menuItemImg, "src": this.options.img}).inject(imgDiv);
  293. var separator = new Element("div", {"styles": this.menu.css.menuItemSeparator}).inject(this.item);
  294. this.text = new Element("div", {"styles": this.menu.css.menuItemText, "text": this.options.text}).inject(this.item);
  295. if (this.options.event) this.item.addEvent(this.options.event, this.doAction.bind(this));
  296. this.setDisable(this.options.disable);
  297. },
  298. setText: function(text){
  299. this.options.text = text;
  300. var textNode = this.item.getLast("div");
  301. if (textNode) textNode.set("text", text);
  302. },
  303. load: function(){
  304. this.item.inject(this.menu.node);
  305. this._addButtonEvent();
  306. },
  307. setDisable: function(flag){
  308. if (this.options.disable!=flag){
  309. this.options.disable = flag;
  310. if (flag){
  311. this.item.set("styles", this.menu.css.menuItemDisable);
  312. var img = this.item.getElement("img");
  313. if (img){
  314. var src = img.get("src");
  315. //src = src.substr(0, src.lastIndexOf("."));
  316. src = src.replace(".", "_gray.");
  317. img.set("src", src);
  318. }
  319. }else{
  320. this.item.set("styles", this.menu.css.menuItem);
  321. var img = this.item.getElement("img");
  322. if (img){
  323. var src = img.get("src");
  324. src = src.replace("_gray", "");
  325. img.set("src", src);
  326. }
  327. }
  328. }
  329. },
  330. _addButtonEvent: function(){
  331. this.item.addEvent("mouseover", this._menuItemMouseOver.bind(this));
  332. this.item.addEvent("mouseout", this._menuItemMouseOut.bind(this));
  333. this.item.addEvent("mousedown", this._menuItemMouseDown.bind(this));
  334. this.item.addEvent("mouseup", this._menuItemMouseUp.bind(this));
  335. //this.item.addEvent("click", this.doAction.bind(this));
  336. },
  337. _menuItemMouseOver: function(e){
  338. this.menu.items.each(function(item){
  339. if (item!=this) if (item.type!="line") if (!item.options.disable){item._menuItemMouseOut(e);};
  340. }.bind(this));
  341. if (!this.options.disable){this.item.set("styles", this.menu.css.menuOver); this.menu.current = this;};
  342. },
  343. _menuItemMouseOut: function(e){
  344. if (!this.options.disable){this.item.set("styles", this.menu.css.menuOut);};
  345. },
  346. _menuItemMouseDown: function(e){
  347. if (!this.options.disable){this.item.set("styles", this.menu.css.menuDown);};
  348. e.stopPropagation();
  349. },
  350. _menuItemMouseUp: function(e){
  351. if (!this.options.disable){this.item.set("styles", this.menu.css.menuUp);};
  352. },
  353. doAction: function(e){
  354. if (!this.options.disable){
  355. if (this.options.action){
  356. this.options.action.apply(this, [e]);
  357. }
  358. this.menu.hideIm(true);
  359. }
  360. },
  361. remove: function(){
  362. this.item.destroy();
  363. }
  364. });
  365. MWF.widget.MenuLine = new Class({
  366. initialize: function(menu, options){
  367. this.type="line";
  368. this.menu = menu;
  369. this.createNode();
  370. },
  371. createNode: function(){
  372. this.item = new Element("div", {"styles": this.menu.css.menuLine});
  373. },
  374. load: function(){
  375. this.item.inject(this.menu.node);
  376. },
  377. remove: function(){
  378. this.item.destroy();
  379. },
  380. setDisable: function(){}
  381. });
  382. MWF.widget.MenuMenu = new Class({
  383. Implements: [Options],
  384. Extends: MWF.widget.MenuItem,
  385. initialize: function(menu, submenu, options){
  386. //this.setOptions(options);
  387. this.subMenu = submenu;
  388. this.parent(menu, options);
  389. this.subMenu.topMenu = this.menu;
  390. this.type="menu";
  391. this.createIcon();
  392. this.subMenu.setPosition = function(){
  393. this.setPosition();
  394. }.bind(this);
  395. },
  396. createIcon: function(){
  397. var icon = new Element("div", {"styles": this.menu.css.menuItemSubmenuIcon});
  398. icon.inject(this.text, "before");
  399. this.text.setStyle("margin-right", "16px");
  400. },
  401. _menuItemMouseOver: function(e){
  402. this.menu.items.each(function(item){
  403. if (item!=this) if (item.type!="line") if (!item.options.disable){item._menuItemMouseOut(e);};
  404. }.bind(this));
  405. if (!this.options.disable){this.item.set("styles", this.menu.css.menuOver); this.menu.current = this;};
  406. this.subMenu.showIm();
  407. },
  408. _menuItemMouseOut: function(e){
  409. if (e.event.toElement!=this.subMenu.node && !this.subMenu.node.contains(e.event.toElement)){
  410. if (!this.options.disable){this.item.set("styles", this.menu.css.menuOut);};
  411. this.subMenu.hideIm();
  412. }
  413. },
  414. setPosition: function(e){
  415. var top;
  416. var left;
  417. var position = this.item.getPosition();
  418. var size = this.item.getSize();
  419. this.subMenu.node.setStyle("display", "block");
  420. var menuSize = this.subMenu.node.getSize();
  421. var bodySize = $(document.body).getSize();
  422. top = position.y;
  423. left = (position.x.toFloat()) + (size.x.toFloat())-3;
  424. if ((left.toFloat()) + (menuSize.x.toFloat())>bodySize.x){
  425. left = (position.x.toFloat()) - (menuSize.x.toFloat())+8;
  426. }
  427. var scrollTop = ($(document.body).getScroll().y.toFloat()) || 0;
  428. if (top+menuSize.y>bodySize.y+scrollTop){
  429. top = top-menuSize.y + size.y+3;
  430. if (top<0) top = 0;
  431. }
  432. if (this.subMenu.options.offsetX) left = left + this.subMenu.options.offsetX;
  433. if (this.subMenu.options.offsetY) top = top + this.subMenu.options.offsetY;
  434. var zIndex = this.menu.node.getStyle("z-index");
  435. this.subMenu.node.setStyle("z-index", zIndex+1);
  436. this.subMenu.node.setStyle("top", top);
  437. this.subMenu.node.setStyle("left", left);
  438. }
  439. });