menubar-2.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. 
  2. function MenuBar(container)
  3. {
  4. this.Container = container; // 容器
  5. this.Groups = new Array(); // Group集合
  6. this.SelectedIndex = 0; // 选中项
  7. this.MoveIndent = 100;
  8. var timerId = null;
  9. var htmlObject = null;
  10. var owner = this;
  11. // 增加Group
  12. //
  13. this.Add = function(group)
  14. {
  15. group.MenuBar = this;
  16. group.Index = this.Groups.length;
  17. this.Groups[this.Groups.length] = group;
  18. }
  19. // 初始化
  20. //
  21. this.Init = function()
  22. {
  23. if (this.Container != null)
  24. {
  25. if (htmlObject == null)
  26. {
  27. this.Container.appendChild(this.GetHtmlObject());
  28. }
  29. for (var i=0; i<this.Groups.length; i++)
  30. {
  31. var group = this.Groups[i];
  32. if (group == null)
  33. break;
  34. group.SetIndex(i);
  35. var clickHandler = function() {
  36. var index = parseInt(this.getAttribute("index"));
  37. if (isNaN(index))
  38. index = 0;
  39. if (this.SelectedIndex == index)
  40. return;
  41. owner.ClearTimer();
  42. owner.Reload();
  43. owner.ScrollMenuBar(index);
  44. clicked();
  45. }
  46. group.CaptionClick(clickHandler);
  47. }
  48. this.Reload();
  49. }
  50. }
  51. this.ScrollMenuBar = function(index)
  52. {
  53. if (this.SelectedIndex == index)
  54. {
  55. return;
  56. }
  57. var preGroup = this.Groups[this.SelectedIndex];
  58. var group = this.Groups[index];
  59. if (preGroup != null && group != null)
  60. {
  61. var preGroupHeight = preGroup.GetHeight();
  62. var preGroupCaptionHeight = preGroup.GetCaptionHeight();
  63. var groupHeight = group.GetHeight();
  64. if (preGroupHeight > preGroupCaptionHeight)
  65. {
  66. var indent = this.MoveIndent;
  67. if (preGroupHeight - this.MoveIndent < 0)
  68. {
  69. indent = preGroupHeight - preGroupCaptionHeight;
  70. }
  71. preGroup.SetHeight(preGroupHeight - indent);
  72. group.SetHeight(groupHeight + indent);
  73. var handler = function (){owner.ScrollMenuBar(index)}
  74. timerId = window.setTimeout(handler, 10);
  75. }
  76. else
  77. {
  78. this.SelectedIndex = index;
  79. this.Reload();
  80. }
  81. }
  82. }
  83. // 重新计算位置
  84. //
  85. this.Reload = function()
  86. {
  87. this.ClearTimer();
  88. // 计算选中组高度
  89. //
  90. var selectedGroupHeight = this.Container.clientHeight-37;
  91. for (var i=0; i<this.Groups.length; i++)
  92. {
  93. var group = this.Groups[i];
  94. if (group == null)
  95. break;
  96. if (i != this.SelectedIndex)
  97. {
  98. var captionHeight = group.GetCaptionHeight();
  99. selectedGroupHeight -= captionHeight;
  100. group.SetHeight(captionHeight);
  101. }
  102. }
  103. // 设置选中项高度
  104. //
  105. var selectedGroup = this.Groups[this.SelectedIndex];
  106. if (selectedGroupHeight > 0 && selectedGroup != null)
  107. {
  108. selectedGroup.SetHeight(selectedGroupHeight);
  109. }
  110. }
  111. // 获取Html对象
  112. //
  113. this.GetHtmlObject = function()
  114. {
  115. if (htmlObject == null)
  116. {
  117. var obj = document.createElement("div");
  118. obj.className = "left-menuBar";
  119. var leftheight=document.body.clientHeight;
  120. obj.style.height=leftheight-12+"px";
  121. var leftTitlecontent = document.createElement("div");
  122. leftTitlecontent.className="left-title-container";
  123. obj.appendChild(leftTitlecontent);
  124. imgcontent = document.createElement("span");
  125. leftTitlecontent.appendChild(imgcontent);
  126. textcontent = document.createElement("h1");
  127. textcontent.innerHTML = "菜单列表";
  128. leftTitlecontent.appendChild(textcontent);
  129. clickcontent = document.createElement("div");
  130. clickcontent.className="clickcontent-title";
  131. leftTitlecontent.appendChild(clickcontent);
  132. for (var i=0; i<this.Groups.length; i++)
  133. {
  134. var group = this.Groups[i];
  135. if (group != null)
  136. {
  137. obj.appendChild(group.GetHtmlObject());
  138. }
  139. }
  140. htmlObject = obj;
  141. }
  142. return obj;
  143. }
  144. this.ClearTimer = function()
  145. {
  146. if (timerId != null)
  147. {
  148. window.clearTimeout(timerId);
  149. timerId = null;
  150. }
  151. }
  152. }
  153. // 菜单组
  154. //
  155. function MenuGroup(text)
  156. {
  157. this.Text = text;
  158. this.Items = new Array();
  159. this.MenuBar = null;
  160. this.Index = -1;
  161. var owner = this;
  162. var htmlObject = null;
  163. var objCaption = null;
  164. var objItems = null;
  165. this.Add = function(menu)
  166. {
  167. this.Items[this.Items.length] = menu;
  168. }
  169. this.AddMenu = function(text, icon, href, target)
  170. {
  171. var menu = new Menu(text, icon, href, target);
  172. this.Add(menu);
  173. }
  174. this.SetIndex = function(i)
  175. {
  176. this.Index = i;
  177. if (objCaption != null)
  178. {
  179. objCaption.setAttribute("index", i);
  180. }
  181. }
  182. this.GetCaptionHeight = function()
  183. {
  184. if (objCaption != null)
  185. {
  186. return objCaption.clientHeight;
  187. }
  188. return 0;
  189. }
  190. this.GetHeight = function()
  191. {
  192. if (htmlObject != null)
  193. {
  194. return htmlObject.clientHeight;
  195. }
  196. return 0;
  197. }
  198. this.GetItemsHeight = function()
  199. {
  200. if (objItems != null)
  201. {
  202. return objItems.offsetHeight;
  203. }
  204. return 0;
  205. }
  206. this.GetMenusOffsetTop = function()
  207. {
  208. var top = 0;
  209. if (objItems != null)
  210. {
  211. if (objItems.style.pixelTop)
  212. {
  213. top = objItems.style.pixelTop;
  214. }
  215. else if (objItems.style.top != "")
  216. {
  217. top = parseInt(objItems.style.top.replace("px", ""));
  218. }
  219. }
  220. return top;
  221. }
  222. this.SetHeight = function(height)
  223. {
  224. if (htmlObject != null)
  225. {
  226. htmlObject.style.height = height;
  227. }
  228. }
  229. this.CaptionClick = function(clickHandler)
  230. {
  231. objCaption.onclick = clickHandler;
  232. }
  233. // 除掉标题栏的高度
  234. //
  235. this.GetClientHeight = function()
  236. {
  237. var height = this.GetHeight();
  238. var captionHeight = this.GetCaptionHeight();
  239. return (height - captionHeight);
  240. }
  241. clicked = function()
  242. {
  243. if (this.MenuBar == null || this.Index != this.MenuBar.SelectedIndex)
  244. {
  245. clickcontent.className="clickcontent-selecte";
  246. }
  247. }
  248. this.GetHtmlObject = function()
  249. {
  250. if (htmlObject == null)
  251. {
  252. var obj = document.createElement("div");
  253. obj.className = "left-menuGroup";
  254. objCaption = document.createElement("div");
  255. obj.appendChild(objCaption);
  256. objCaption.className = "left-nav-container";
  257. objCaption.onselectstart = function(){return false;}
  258. imgcontent = document.createElement("span");
  259. objCaption.appendChild(imgcontent);
  260. textcontent = document.createElement("h1");
  261. textcontent.innerHTML = this.Text;
  262. objCaption.appendChild(textcontent);
  263. clickcontent = document.createElement("div");
  264. objCaption.appendChild(clickcontent);
  265. objItems = document.createElement("div");
  266. objItems.className = "left-menuItems-2";
  267. obj.appendChild(objItems);
  268. for (var i=0; i<this.Items.length; i++)
  269. {
  270. var item = this.Items[i];
  271. if (item != null && item.GetHtmlObject() != null)
  272. {
  273. objItems.appendChild(item.GetHtmlObject());
  274. }
  275. }
  276. htmlObject = obj;
  277. }
  278. return htmlObject;
  279. }
  280. }
  281. function Menu(text, icon, href, target)
  282. {
  283. this.Text = text;
  284. this.Icon = icon;
  285. this.Href = href;
  286. this.Target = target;
  287. var htmlObject = null;
  288. this.GetHeight = function()
  289. {
  290. if (htmlObject != null)
  291. {
  292. return htmlObject.offsetHeight;
  293. }
  294. return 0;
  295. }
  296. this.GetHtmlObject = function()
  297. {
  298. if (htmlObject == null)
  299. {
  300. var obj = document.createElement("div");
  301. obj.align = "center";
  302. obj.className = "left-menu";
  303. var link = document.createElement("a");
  304. link.href = this.Href;
  305. link.target = this.Target;
  306. link.onfocus = function() { this.blur(); }
  307. var link2 = link.cloneNode(true);
  308. obj.appendChild(link2);
  309. link2.onfocus = function() { this.blur(); }
  310. var img = new Image();
  311. link2.appendChild(img);
  312. img.src = this.Icon;
  313. img.alt = this.Text;
  314. img.className = "left-menuIcon";
  315. // img.onmouseover = function()
  316. // {
  317. // SetBorderColor(this, "#CCCCCC", "#000000");
  318. // }
  319. // img.onmousedown = function()
  320. // {
  321. // SetBorderColor(this, "#000000", "#CCCCCC");
  322. // }
  323. // img.onmouseup = function()
  324. // {
  325. // SetBorderColor(this, "#CCCCCC", "#000000");
  326. // }
  327. // img.onmouseout = function()
  328. // {
  329. // SetBorderColor(this, "#FFFFFF", "#FFFFFF");
  330. // }
  331. var objText = document.createElement("div");
  332. obj.appendChild(objText);
  333. objText.className = "left-menuText";
  334. link.innerHTML = this.Text;
  335. objText.appendChild(link);
  336. htmlObject = obj;
  337. }
  338. return htmlObject;
  339. }
  340. }
  341. function SetBorderColor(obj, borderColorLight, borderColorDark)
  342. {
  343. obj.style.borderLeftColor = borderColorLight;
  344. obj.style.borderTopColor = borderColorLight;
  345. obj.style.borderRightColor = borderColorDark;
  346. obj.style.borderBottomColor = borderColorDark;
  347. }
  348. function getPostion(e){
  349. var t=e.offsetTop;
  350. var l=e.offsetLeft;
  351. while(e=e.offsetParent){
  352. t+=e.offsetTop;
  353. l+=e.offsetLeft;
  354. }
  355. return {"top": t, "left" : l};
  356. }