Menu.js 16 KB

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