Menu.js 16 KB

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