jquery.menu.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * jQuery EasyUI 1.4.1
  3. *
  4. * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
  7. * To use it on other terms please contact us at info@jeasyui.com
  8. *
  9. */
  10. (function($){
  11. function _1(_2){
  12. $(_2).appendTo("body");
  13. $(_2).addClass("menu-top");
  14. $(document).unbind(".menu").bind("mousedown.menu",function(e){
  15. var m=$(e.target).closest("div.menu,div.combo-p");
  16. if(m.length){
  17. return;
  18. }
  19. $("body>div.menu-top:visible").menu("hide");
  20. });
  21. var _3=_4($(_2));
  22. for(var i=0;i<_3.length;i++){
  23. _5(_3[i]);
  24. }
  25. function _4(_6){
  26. var _7=[];
  27. _6.addClass("menu");
  28. _7.push(_6);
  29. if(!_6.hasClass("menu-content")){
  30. _6.children("div").each(function(){
  31. var _8=$(this).children("div");
  32. if(_8.length){
  33. _8.insertAfter(_2);
  34. this.submenu=_8;
  35. var mm=_4(_8);
  36. _7=_7.concat(mm);
  37. }
  38. });
  39. }
  40. return _7;
  41. };
  42. function _5(_9){
  43. var wh=$.parser.parseOptions(_9[0],["width","height"]);
  44. _9[0].originalHeight=wh.height||0;
  45. if(_9.hasClass("menu-content")){
  46. _9[0].originalWidth=wh.width||_9._outerWidth();
  47. }else{
  48. _9[0].originalWidth=wh.width||0;
  49. _9.children("div").each(function(){
  50. var _a=$(this);
  51. var _b=$.extend({},$.parser.parseOptions(this,["name","iconCls","href",{separator:"boolean"}]),{disabled:(_a.attr("disabled")?true:undefined)});
  52. if(_b.separator){
  53. _a.addClass("menu-sep");
  54. }
  55. if(!_a.hasClass("menu-sep")){
  56. _a[0].itemName=_b.name||"";
  57. _a[0].itemHref=_b.href||"";
  58. var _c=_a.addClass("menu-item").html();
  59. _a.empty().append($("<div class=\"menu-text\"></div>").html(_c));
  60. if(_b.iconCls){
  61. $("<div class=\"menu-icon\"></div>").addClass(_b.iconCls).appendTo(_a);
  62. }
  63. if(_b.disabled){
  64. _d(_2,_a[0],true);
  65. }
  66. if(_a[0].submenu){
  67. $("<div class=\"menu-rightarrow\"></div>").appendTo(_a);
  68. }
  69. _e(_2,_a);
  70. }
  71. });
  72. $("<div class=\"menu-line\"></div>").prependTo(_9);
  73. }
  74. _f(_2,_9);
  75. _9.hide();
  76. _10(_2,_9);
  77. };
  78. };
  79. function _f(_11,_12){
  80. var _13=$.data(_11,"menu").options;
  81. var _14=_12.attr("style")||"";
  82. _12.css({display:"block",left:-10000,height:"auto",overflow:"hidden"});
  83. var el=_12[0];
  84. var _15=el.originalWidth||0;
  85. if(!_15){
  86. _15=0;
  87. _12.find("div.menu-text").each(function(){
  88. if(_15<$(this)._outerWidth()){
  89. _15=$(this)._outerWidth();
  90. }
  91. $(this).closest("div.menu-item")._outerHeight($(this)._outerHeight()+2);
  92. });
  93. _15+=40;
  94. }
  95. _15=Math.max(_15,_13.minWidth);
  96. var _16=el.originalHeight||0;
  97. if(!_16){
  98. _16=_12.outerHeight();
  99. if(_12.hasClass("menu-top")&&_13.alignTo){
  100. var at=$(_13.alignTo);
  101. var h1=at.offset().top-$(document).scrollTop();
  102. var h2=$(window)._outerHeight()+$(document).scrollTop()-at.offset().top-at._outerHeight();
  103. _16=Math.min(_16,Math.max(h1,h2));
  104. }else{
  105. if(_16>$(window)._outerHeight()){
  106. _16=$(window).height();
  107. _14+=";overflow:auto";
  108. }else{
  109. _14+=";overflow:hidden";
  110. }
  111. }
  112. }
  113. var _17=Math.max(el.originalHeight,_12.outerHeight())-2;
  114. _12._outerWidth(_15)._outerHeight(_16);
  115. _12.children("div.menu-line")._outerHeight(_17);
  116. _14+=";width:"+el.style.width+";height:"+el.style.height;
  117. _12.attr("style",_14);
  118. };
  119. function _10(_18,_19){
  120. var _1a=$.data(_18,"menu");
  121. _19.unbind(".menu").bind("mouseenter.menu",function(){
  122. if(_1a.timer){
  123. clearTimeout(_1a.timer);
  124. _1a.timer=null;
  125. }
  126. }).bind("mouseleave.menu",function(){
  127. if(_1a.options.hideOnUnhover){
  128. _1a.timer=setTimeout(function(){
  129. _1b(_18);
  130. },_1a.options.duration);
  131. }
  132. });
  133. };
  134. function _e(_1c,_1d){
  135. if(!_1d.hasClass("menu-item")){
  136. return;
  137. }
  138. _1d.unbind(".menu");
  139. _1d.bind("click.menu",function(){
  140. if($(this).hasClass("menu-item-disabled")){
  141. return;
  142. }
  143. if(!this.submenu){
  144. _1b(_1c);
  145. var _1e=this.itemHref;
  146. if(_1e){
  147. location.href=_1e;
  148. }
  149. }
  150. var _1f=$(_1c).menu("getItem",this);
  151. $.data(_1c,"menu").options.onClick.call(_1c,_1f);
  152. }).bind("mouseenter.menu",function(e){
  153. _1d.siblings().each(function(){
  154. if(this.submenu){
  155. _22(this.submenu);
  156. }
  157. $(this).removeClass("menu-active");
  158. });
  159. _1d.addClass("menu-active");
  160. if($(this).hasClass("menu-item-disabled")){
  161. _1d.addClass("menu-active-disabled");
  162. return;
  163. }
  164. var _20=_1d[0].submenu;
  165. if(_20){
  166. $(_1c).menu("show",{menu:_20,parent:_1d});
  167. }
  168. }).bind("mouseleave.menu",function(e){
  169. _1d.removeClass("menu-active menu-active-disabled");
  170. var _21=_1d[0].submenu;
  171. if(_21){
  172. if(e.pageX>=parseInt(_21.css("left"))){
  173. _1d.addClass("menu-active");
  174. }else{
  175. _22(_21);
  176. }
  177. }else{
  178. _1d.removeClass("menu-active");
  179. }
  180. });
  181. };
  182. function _1b(_23){
  183. var _24=$.data(_23,"menu");
  184. if(_24){
  185. if($(_23).is(":visible")){
  186. _22($(_23));
  187. _24.options.onHide.call(_23);
  188. }
  189. }
  190. return false;
  191. };
  192. function _25(_26,_27){
  193. var _28,top;
  194. _27=_27||{};
  195. var _29=$(_27.menu||_26);
  196. $(_26).menu("resize",_29[0]);
  197. if(_29.hasClass("menu-top")){
  198. var _2a=$.data(_26,"menu").options;
  199. $.extend(_2a,_27);
  200. _28=_2a.left;
  201. top=_2a.top;
  202. if(_2a.alignTo){
  203. var at=$(_2a.alignTo);
  204. _28=at.offset().left;
  205. top=at.offset().top+at._outerHeight();
  206. if(_2a.align=="right"){
  207. _28+=at.outerWidth()-_29.outerWidth();
  208. }
  209. }
  210. if(_28+_29.outerWidth()>$(window)._outerWidth()+$(document)._scrollLeft()){
  211. _28=$(window)._outerWidth()+$(document).scrollLeft()-_29.outerWidth()-5;
  212. }
  213. if(_28<0){
  214. _28=0;
  215. }
  216. top=_2b(top,_2a.alignTo);
  217. }else{
  218. var _2c=_27.parent;
  219. _28=_2c.offset().left+_2c.outerWidth()-2;
  220. if(_28+_29.outerWidth()+5>$(window)._outerWidth()+$(document).scrollLeft()){
  221. _28=_2c.offset().left-_29.outerWidth()+2;
  222. }
  223. top=_2b(_2c.offset().top-3);
  224. }
  225. function _2b(top,_2d){
  226. if(top+_29.outerHeight()>$(window)._outerHeight()+$(document).scrollTop()){
  227. if(_2d){
  228. top=$(_2d).offset().top-_29._outerHeight();
  229. }else{
  230. top=$(window)._outerHeight()+$(document).scrollTop()-_29.outerHeight();
  231. }
  232. }
  233. if(top<0){
  234. top=0;
  235. }
  236. return top;
  237. };
  238. _29.css({left:_28,top:top});
  239. _29.show(0,function(){
  240. if(!_29[0].shadow){
  241. _29[0].shadow=$("<div class=\"menu-shadow\"></div>").insertAfter(_29);
  242. }
  243. _29[0].shadow.css({display:"block",zIndex:$.fn.menu.defaults.zIndex++,left:_29.css("left"),top:_29.css("top"),width:_29.outerWidth(),height:_29.outerHeight()});
  244. _29.css("z-index",$.fn.menu.defaults.zIndex++);
  245. if(_29.hasClass("menu-top")){
  246. $.data(_29[0],"menu").options.onShow.call(_29[0]);
  247. }
  248. });
  249. };
  250. function _22(_2e){
  251. if(!_2e){
  252. return;
  253. }
  254. _2f(_2e);
  255. _2e.find("div.menu-item").each(function(){
  256. if(this.submenu){
  257. _22(this.submenu);
  258. }
  259. $(this).removeClass("menu-active");
  260. });
  261. function _2f(m){
  262. m.stop(true,true);
  263. if(m[0].shadow){
  264. m[0].shadow.hide();
  265. }
  266. m.hide();
  267. };
  268. };
  269. function _30(_31,_32){
  270. var _33=null;
  271. var tmp=$("<div></div>");
  272. function _34(_35){
  273. _35.children("div.menu-item").each(function(){
  274. var _36=$(_31).menu("getItem",this);
  275. var s=tmp.empty().html(_36.text).text();
  276. if(_32==$.trim(s)){
  277. _33=_36;
  278. }else{
  279. if(this.submenu&&!_33){
  280. _34(this.submenu);
  281. }
  282. }
  283. });
  284. };
  285. _34($(_31));
  286. tmp.remove();
  287. return _33;
  288. };
  289. function _d(_37,_38,_39){
  290. var t=$(_38);
  291. if(!t.hasClass("menu-item")){
  292. return;
  293. }
  294. if(_39){
  295. t.addClass("menu-item-disabled");
  296. if(_38.onclick){
  297. _38.onclick1=_38.onclick;
  298. _38.onclick=null;
  299. }
  300. }else{
  301. t.removeClass("menu-item-disabled");
  302. if(_38.onclick1){
  303. _38.onclick=_38.onclick1;
  304. _38.onclick1=null;
  305. }
  306. }
  307. };
  308. function _3a(_3b,_3c){
  309. var _3d=$(_3b);
  310. if(_3c.parent){
  311. if(!_3c.parent.submenu){
  312. var _3e=$("<div class=\"menu\"><div class=\"menu-line\"></div></div>").appendTo("body");
  313. _3e.hide();
  314. _3c.parent.submenu=_3e;
  315. $("<div class=\"menu-rightarrow\"></div>").appendTo(_3c.parent);
  316. }
  317. _3d=_3c.parent.submenu;
  318. }
  319. if(_3c.separator){
  320. var _3f=$("<div class=\"menu-sep\"></div>").appendTo(_3d);
  321. }else{
  322. var _3f=$("<div class=\"menu-item\"></div>").appendTo(_3d);
  323. $("<div class=\"menu-text\"></div>").html(_3c.text).appendTo(_3f);
  324. }
  325. if(_3c.iconCls){
  326. $("<div class=\"menu-icon\"></div>").addClass(_3c.iconCls).appendTo(_3f);
  327. }
  328. if(_3c.id){
  329. _3f.attr("id",_3c.id);
  330. }
  331. if(_3c.name){
  332. _3f[0].itemName=_3c.name;
  333. }
  334. if(_3c.href){
  335. _3f[0].itemHref=_3c.href;
  336. }
  337. if(_3c.onclick){
  338. if(typeof _3c.onclick=="string"){
  339. _3f.attr("onclick",_3c.onclick);
  340. }else{
  341. _3f[0].onclick=eval(_3c.onclick);
  342. }
  343. }
  344. if(_3c.handler){
  345. _3f[0].onclick=eval(_3c.handler);
  346. }
  347. if(_3c.disabled){
  348. _d(_3b,_3f[0],true);
  349. }
  350. _e(_3b,_3f);
  351. _10(_3b,_3d);
  352. _f(_3b,_3d);
  353. };
  354. function _40(_41,_42){
  355. function _43(el){
  356. if(el.submenu){
  357. el.submenu.children("div.menu-item").each(function(){
  358. _43(this);
  359. });
  360. var _44=el.submenu[0].shadow;
  361. if(_44){
  362. _44.remove();
  363. }
  364. el.submenu.remove();
  365. }
  366. $(el).remove();
  367. };
  368. var _45=$(_42).parent();
  369. _43(_42);
  370. _f(_41,_45);
  371. };
  372. function _46(_47,_48,_49){
  373. var _4a=$(_48).parent();
  374. if(_49){
  375. $(_48).show();
  376. }else{
  377. $(_48).hide();
  378. }
  379. _f(_47,_4a);
  380. };
  381. function _4b(_4c){
  382. $(_4c).children("div.menu-item").each(function(){
  383. _40(_4c,this);
  384. });
  385. if(_4c.shadow){
  386. _4c.shadow.remove();
  387. }
  388. $(_4c).remove();
  389. };
  390. $.fn.menu=function(_4d,_4e){
  391. if(typeof _4d=="string"){
  392. return $.fn.menu.methods[_4d](this,_4e);
  393. }
  394. _4d=_4d||{};
  395. return this.each(function(){
  396. var _4f=$.data(this,"menu");
  397. if(_4f){
  398. $.extend(_4f.options,_4d);
  399. }else{
  400. _4f=$.data(this,"menu",{options:$.extend({},$.fn.menu.defaults,$.fn.menu.parseOptions(this),_4d)});
  401. _1(this);
  402. }
  403. $(this).css({left:_4f.options.left,top:_4f.options.top});
  404. });
  405. };
  406. $.fn.menu.methods={options:function(jq){
  407. return $.data(jq[0],"menu").options;
  408. },show:function(jq,pos){
  409. return jq.each(function(){
  410. _25(this,pos);
  411. });
  412. },hide:function(jq){
  413. return jq.each(function(){
  414. _1b(this);
  415. });
  416. },destroy:function(jq){
  417. return jq.each(function(){
  418. _4b(this);
  419. });
  420. },setText:function(jq,_50){
  421. return jq.each(function(){
  422. $(_50.target).children("div.menu-text").html(_50.text);
  423. });
  424. },setIcon:function(jq,_51){
  425. return jq.each(function(){
  426. $(_51.target).children("div.menu-icon").remove();
  427. if(_51.iconCls){
  428. $("<div class=\"menu-icon\"></div>").addClass(_51.iconCls).appendTo(_51.target);
  429. }
  430. });
  431. },getItem:function(jq,_52){
  432. var t=$(_52);
  433. var _53={target:_52,id:t.attr("id"),text:$.trim(t.children("div.menu-text").html()),disabled:t.hasClass("menu-item-disabled"),name:_52.itemName,href:_52.itemHref,onclick:_52.onclick};
  434. var _54=t.children("div.menu-icon");
  435. if(_54.length){
  436. var cc=[];
  437. var aa=_54.attr("class").split(" ");
  438. for(var i=0;i<aa.length;i++){
  439. if(aa[i]!="menu-icon"){
  440. cc.push(aa[i]);
  441. }
  442. }
  443. _53.iconCls=cc.join(" ");
  444. }
  445. return _53;
  446. },findItem:function(jq,_55){
  447. return _30(jq[0],_55);
  448. },appendItem:function(jq,_56){
  449. return jq.each(function(){
  450. _3a(this,_56);
  451. });
  452. },removeItem:function(jq,_57){
  453. return jq.each(function(){
  454. _40(this,_57);
  455. });
  456. },enableItem:function(jq,_58){
  457. return jq.each(function(){
  458. _d(this,_58,false);
  459. });
  460. },disableItem:function(jq,_59){
  461. return jq.each(function(){
  462. _d(this,_59,true);
  463. });
  464. },showItem:function(jq,_5a){
  465. return jq.each(function(){
  466. _46(this,_5a,true);
  467. });
  468. },hideItem:function(jq,_5b){
  469. return jq.each(function(){
  470. _46(this,_5b,false);
  471. });
  472. },resize:function(jq,_5c){
  473. return jq.each(function(){
  474. _f(this,$(_5c));
  475. });
  476. }};
  477. $.fn.menu.parseOptions=function(_5d){
  478. return $.extend({},$.parser.parseOptions(_5d,[{minWidth:"number",duration:"number",hideOnUnhover:"boolean"}]));
  479. };
  480. $.fn.menu.defaults={zIndex:110000,left:0,top:0,alignTo:null,align:"left",minWidth:120,duration:100,hideOnUnhover:true,onShow:function(){
  481. },onHide:function(){
  482. },onClick:function(_5e){
  483. }};
  484. })(jQuery);