menu.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var jsmenu = new Array();
  2. var ctrlobjclassName;
  3. jsmenu['active'] = new Array();
  4. jsmenu['timer'] = new Array();
  5. jsmenu['iframe'] = new Array();
  6. function initCtrl(ctrlobj, click, duration, timeout, layer) {
  7. if(ctrlobj && !ctrlobj.initialized) {
  8. ctrlobj.initialized = true;
  9. ctrlobj.unselectable = true;
  10. ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function' ? ctrlobj.onmouseout : null;
  11. ctrlobj.onmouseout = function() {
  12. if(this.outfunc) this.outfunc();
  13. if(duration < 3) jsmenu['timer'][ctrlobj.id] = setTimeout('hideMenu(' + layer + ')', timeout);
  14. }
  15. if(click && duration) {
  16. ctrlobj.clickfunc = typeof ctrlobj.onclick == 'function' ? ctrlobj.onclick : null;
  17. ctrlobj.onclick = function (e) {
  18. doane(e);
  19. if(jsmenu['active'][layer] == null || jsmenu['active'][layer].ctrlkey != this.id) {
  20. if(this.clickfunc) this.clickfunc();
  21. else showMenu(this.id, true);
  22. } else {
  23. hideMenu(layer);
  24. }
  25. }
  26. }
  27. ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
  28. ctrlobj.onmouseover = function(e) {
  29. doane(e);
  30. if(this.overfunc) this.overfunc();
  31. if(click) {
  32. clearTimeout(jsmenu['timer'][this.id]);
  33. } else {
  34. for(var id in jsmenu['timer']) {
  35. if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. function initMenu(ctrlid, menuobj, duration, timeout, layer) {
  42. if(menuobj && !menuobj.initialized) {
  43. menuobj.initialized = true;
  44. menuobj.ctrlkey = ctrlid;
  45. menuobj.onclick = ebygum;
  46. menuobj.style.position = 'absolute';
  47. if(duration < 3) {
  48. if(duration > 1) {
  49. menuobj.onmouseover = function() {
  50. clearTimeout(jsmenu['timer'][ctrlid]);
  51. }
  52. }
  53. if(duration != 1) {
  54. menuobj.onmouseout = function() {
  55. jsmenu['timer'][ctrlid] = setTimeout('hideMenu(' + layer + ')', timeout);
  56. }
  57. }
  58. }
  59. menuobj.style.zIndex = 50;
  60. if(is_ie) {
  61. menuobj.style.filter += "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#CCCCCC,strength=2)";
  62. }
  63. }
  64. }
  65. function showMenu(ctrlid, click, offset, duration, timeout, layer, showid, maxh) {
  66. e = window.event ? window.event : showMenu.caller.arguments[0];
  67. var ctrlobj = $(ctrlid);
  68. if(!ctrlobj) return;
  69. if(isUndefined(click)) click = false;
  70. if(isUndefined(offset)) offset = 0;
  71. if(isUndefined(duration)) duration = 2;
  72. if(isUndefined(timeout)) timeout = 500;
  73. if(isUndefined(layer)) layer = 0;
  74. if(isUndefined(showid)) showid = ctrlid;
  75. var showobj = $(showid);
  76. var menuobj = $(showid + '_menu');
  77. if(!showobj|| !menuobj) return;
  78. if(isUndefined(maxh)) maxh = 400;
  79. hideMenu(layer);
  80. for(var id in jsmenu['timer']) {
  81. if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
  82. }
  83. initCtrl(ctrlobj, click, duration, timeout, layer);
  84. ctrlobjclassName = ctrlobj.className;
  85. ctrlobj.className += ' hover';
  86. initMenu(ctrlid, menuobj, duration, timeout, layer);
  87. menuobj.style.display = '';
  88. if(!is_opera) {
  89. menuobj.style.clip = 'rect(auto, auto, auto, auto)';
  90. }
  91. setMenuPosition(showid, offset);
  92. if(is_ie && is_ie < 7) {
  93. if(!jsmenu['iframe'][layer]) {
  94. var iframe = document.createElement('iframe');
  95. iframe.style.display = 'none';
  96. iframe.style.position = 'absolute';
  97. iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
  98. $('append_parent') ? $('append_parent').appendChild(iframe) : menuobj.parentNode.appendChild(iframe);
  99. jsmenu['iframe'][layer] = iframe;
  100. }
  101. jsmenu['iframe'][layer].style.top = menuobj.style.top;
  102. jsmenu['iframe'][layer].style.left = menuobj.style.left;
  103. jsmenu['iframe'][layer].style.width = menuobj.w;
  104. jsmenu['iframe'][layer].style.height = menuobj.h;
  105. jsmenu['iframe'][layer].style.display = 'block';
  106. }
  107. if(maxh && menuobj.scrollHeight > maxh) {
  108. menuobj.style.height = maxh + 'px';
  109. if(is_opera) {
  110. menuobj.style.overflow = 'auto';
  111. } else {
  112. menuobj.style.overflowY = 'auto';
  113. }
  114. }
  115. if(!duration) {
  116. setTimeout('hideMenu(' + layer + ')', timeout);
  117. }
  118. jsmenu['active'][layer] = menuobj;
  119. }
  120. function setMenuPosition(showid, offset) {
  121. var showobj = $(showid);
  122. var menuobj = $(showid + '_menu');
  123. if(isUndefined(offset)) offset = 0;
  124. if(showobj) {
  125. showobj.pos = fetchOffset(showobj);
  126. showobj.X = showobj.pos['left'];
  127. showobj.Y = showobj.pos['top'];
  128. showobj.w = showobj.offsetWidth;
  129. showobj.h = showobj.offsetHeight;
  130. menuobj.w = menuobj.offsetWidth;
  131. menuobj.h = menuobj.offsetHeight;
  132. menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
  133. menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
  134. if(menuobj.style.clip && !is_opera) {
  135. menuobj.style.clip = 'rect(auto, auto, auto, auto)';
  136. }
  137. }
  138. }
  139. function hideMenu(layer) {
  140. if(isUndefined(layer)) layer = 0;
  141. if(jsmenu['active'][layer]) {
  142. try {
  143. $(jsmenu['active'][layer].ctrlkey).className = ctrlobjclassName;
  144. } catch(e) {}
  145. clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
  146. jsmenu['active'][layer].style.display = 'none';
  147. if(is_ie && is_ie < 7 && jsmenu['iframe'][layer]) {
  148. jsmenu['iframe'][layer].style.display = 'none';
  149. }
  150. jsmenu['active'][layer] = null;
  151. }
  152. }
  153. function fetchOffset(obj) {
  154. var left_offset = obj.offsetLeft;
  155. var top_offset = obj.offsetTop;
  156. while((obj = obj.offsetParent) != null) {
  157. left_offset += obj.offsetLeft;
  158. top_offset += obj.offsetTop;
  159. }
  160. return { 'left' : left_offset, 'top' : top_offset };
  161. }
  162. function ebygum(eventobj) {
  163. if(!eventobj || is_ie) {
  164. window.event.cancelBubble = true;
  165. return window.event;
  166. } else {
  167. if(eventobj.target.type == 'submit') {
  168. eventobj.target.form.submit();
  169. }
  170. eventobj.stopPropagation();
  171. return eventobj;
  172. }
  173. }
  174. function menuoption_onclick_function(e) {
  175. this.clickfunc();
  176. hideMenu();
  177. }
  178. function menuoption_onclick_link(e) {
  179. choose(e, this);
  180. }
  181. function menuoption_onmouseover(e) {
  182. this.className = 'popupmenu_highlight';
  183. }
  184. function menuoption_onmouseout(e) {
  185. this.className = 'popupmenu_option';
  186. }
  187. function choose(e, obj) {
  188. var links = obj.getElementsByTagName('a');
  189. if(links[0]) {
  190. if(is_ie) {
  191. links[0].click();
  192. window.event.cancelBubble = true;
  193. } else {
  194. if(e.shiftKey) {
  195. window.open(links[0].href);
  196. e.stopPropagation();
  197. e.preventDefault();
  198. } else {
  199. window.location = links[0].href;
  200. e.stopPropagation();
  201. e.preventDefault();
  202. }
  203. }
  204. hideMenu();
  205. }
  206. }