menubar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. }
  45. group.CaptionClick(clickHandler);
  46. }
  47. this.Reload();
  48. }
  49. }
  50. this.ScrollMenuBar = function(index)
  51. {
  52. if (this.SelectedIndex == index)
  53. {
  54. return;
  55. }
  56. var preGroup = this.Groups[this.SelectedIndex];
  57. var group = this.Groups[index];
  58. if (preGroup != null && group != null)
  59. {
  60. var preGroupHeight = preGroup.GetHeight();
  61. var preGroupCaptionHeight = preGroup.GetCaptionHeight();
  62. var groupHeight = group.GetHeight();
  63. if (preGroupHeight > preGroupCaptionHeight)
  64. {
  65. var indent = this.MoveIndent;
  66. if (preGroupHeight - this.MoveIndent < 0)
  67. {
  68. indent = preGroupHeight - preGroupCaptionHeight;
  69. }
  70. preGroup.SetHeight(preGroupHeight - indent);
  71. group.SetHeight(groupHeight + indent);
  72. var handler = function (){owner.ScrollMenuBar(index)}
  73. timerId = window.setTimeout(handler, 10);
  74. }
  75. else
  76. {
  77. this.SelectedIndex = index;
  78. this.Reload();
  79. }
  80. }
  81. }
  82. // 重新计算位置
  83. //
  84. this.Reload = function()
  85. {
  86. this.ClearTimer();
  87. // 计算选中组高度
  88. //
  89. var selectedGroupHeight = this.Container.clientHeight-37;
  90. for (var i=0; i<this.Groups.length; i++)
  91. {
  92. var group = this.Groups[i];
  93. if (group == null)
  94. break;
  95. if (i != this.SelectedIndex)
  96. {
  97. var captionHeight = group.GetCaptionHeight();
  98. selectedGroupHeight -= captionHeight;
  99. group.SetHeight(captionHeight);
  100. }
  101. }
  102. // 设置选中项高度
  103. //
  104. var selectedGroup = this.Groups[this.SelectedIndex];
  105. if (selectedGroupHeight > 0 && selectedGroup != null)
  106. {
  107. selectedGroup.SetHeight(selectedGroupHeight);
  108. selectedGroup.ShowDrowdownButton();
  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 leftTitlecontent = document.createElement("div");
  120. leftTitlecontent.className="left-title-container";
  121. obj.appendChild(leftTitlecontent);
  122. imgcontent = document.createElement("span");
  123. leftTitlecontent.appendChild(imgcontent);
  124. textcontent = document.createElement("h1");
  125. textcontent.innerHTML = "菜单列表";
  126. leftTitlecontent.appendChild(textcontent);
  127. clickcontent = document.createElement("div");
  128. clickcontent.className="clickcontent-title";
  129. leftTitlecontent.appendChild(clickcontent);
  130. for (var i=0; i<this.Groups.length; i++)
  131. {
  132. var group = this.Groups[i];
  133. if (group != null)
  134. {
  135. obj.appendChild(group.GetHtmlObject());
  136. }
  137. }
  138. htmlObject = obj;
  139. }
  140. return obj;
  141. }
  142. this.ClearTimer = function()
  143. {
  144. if (timerId != null)
  145. {
  146. window.clearTimeout(timerId);
  147. timerId = null;
  148. }
  149. }
  150. }
  151. // 菜单组
  152. //
  153. function MenuGroup(text)
  154. {
  155. this.Text = text;
  156. this.Items = new Array();
  157. this.MenuBar = null;
  158. this.Index = -1;
  159. var owner = this;
  160. var htmlObject = null;
  161. var objCaption = null;
  162. var objItems = null;
  163. var imgScrollUp = null;
  164. var imgScrollDown = null;
  165. var scrollTimerId = null;
  166. var topMenuIndex = 0;
  167. this.Add = function(menu)
  168. {
  169. this.Items[this.Items.length] = menu;
  170. }
  171. this.AddMenu = function(text, icon, href, target)
  172. {
  173. var menu = new Menu(text, icon, href, target);
  174. this.Add(menu);
  175. }
  176. this.SetIndex = function(i)
  177. {
  178. this.Index = i;
  179. if (objCaption != null)
  180. {
  181. objCaption.setAttribute("index", i);
  182. }
  183. }
  184. this.GetCaptionHeight = function()
  185. {
  186. if (objCaption != null)
  187. {
  188. return objCaption.clientHeight;
  189. }
  190. return 0;
  191. }
  192. this.GetHeight = function()
  193. {
  194. if (htmlObject != null)
  195. {
  196. return htmlObject.clientHeight;
  197. }
  198. return 0;
  199. }
  200. this.GetItemsHeight = function()
  201. {
  202. if (objItems != null)
  203. {
  204. return objItems.offsetHeight;
  205. }
  206. return 0;
  207. }
  208. this.GetMenusOffsetTop = function()
  209. {
  210. var top = 0;
  211. if (objItems != null)
  212. {
  213. if (objItems.style.pixelTop)
  214. {
  215. top = objItems.style.pixelTop;
  216. }
  217. else if (objItems.style.top != "")
  218. {
  219. top = parseInt(objItems.style.top.replace("px", ""));
  220. }
  221. }
  222. return top;
  223. }
  224. this.SetHeight = function(height)
  225. {
  226. if (htmlObject != null)
  227. {
  228. htmlObject.style.height = height;
  229. }
  230. }
  231. this.CaptionClick = function(clickHandler)
  232. {
  233. objCaption.onclick = clickHandler;
  234. }
  235. // 除掉标题栏的高度
  236. //
  237. this.GetClientHeight = function()
  238. {
  239. var height = this.GetHeight();
  240. var captionHeight = this.GetCaptionHeight();
  241. return (height - captionHeight);
  242. }
  243. this.CanScrollUp = function()
  244. {
  245. return (this.GetMenusOffsetTop() < 0)
  246. }
  247. this.CanScrollDown = function()
  248. {
  249. var clientHeight = this.GetClientHeight();
  250. var itemsHeight = this.GetItemsHeight();
  251. return (itemsHeight + this.GetMenusOffsetTop() > clientHeight)
  252. }
  253. this.ShowDrowdownButton = function()
  254. {
  255. var height = this.GetHeight();
  256. var captionHeight = this.GetCaptionHeight();
  257. var pos = getPostion(htmlObject);
  258. // 出现上箭头
  259. //
  260. if (this.CanScrollUp())
  261. {
  262. imgScrollUp.style.display = "block";
  263. imgScrollUp.style.left = objItems.offsetWidth - 16 - 4;
  264. imgScrollUp.style.top = pos.top + captionHeight - 24;
  265. imgScrollUp.onmousedown = function()
  266. {
  267. owner.MenuScrollUp();
  268. }
  269. imgScrollUp.onmouseup = function()
  270. {
  271. owner.MenuScrollStop();
  272. }
  273. }
  274. else
  275. {
  276. imgScrollUp.style.display = "none";
  277. this.MenuScrollStop();
  278. }
  279. // 出现下箭头
  280. //
  281. if (this.CanScrollDown())
  282. {
  283. imgScrollDown.style.display = "block";
  284. imgScrollDown.style.left = objItems.offsetWidth - 20;
  285. imgScrollDown.style.top = height - 20;
  286. imgScrollDown.onmousedown = function()
  287. {
  288. owner.MenuScrollDown();
  289. }
  290. imgScrollDown.onmouseup = function()
  291. {
  292. owner.MenuScrollStop();
  293. }
  294. }
  295. else
  296. {
  297. imgScrollDown.style.display = "none";
  298. this.MenuScrollStop();
  299. }
  300. }
  301. this.MenuScroll = function(direction)
  302. {
  303. if (this.MenuBar == null || this.Index != this.MenuBar.SelectedIndex)
  304. {
  305. return;
  306. }
  307. var top = 0;
  308. if (direction == "up")
  309. {
  310. if (!this.CanScrollUp() || topMenuIndex == 0)
  311. {
  312. return;
  313. }
  314. topMenuIndex--;
  315. }
  316. else
  317. {
  318. if (!this.CanScrollDown() || topMenuIndex == this.Items.length)
  319. {
  320. return;
  321. }
  322. topMenuIndex++;
  323. }
  324. for (var i=0; i<topMenuIndex; i++)
  325. {
  326. var menu = this.Items[i];
  327. if (menu != null)
  328. {
  329. top -= menu.GetHeight();
  330. }
  331. };
  332. if (objItems != null)
  333. {
  334. objItems.style.top = top;
  335. }
  336. this.ShowDrowdownButton();
  337. }
  338. this.MenuScrollUp = function() {
  339. this.MenuScroll("up");
  340. this.MenuScrollStop();
  341. var handler = function()
  342. {
  343. owner.MenuScrollUp();
  344. }
  345. scrollTimerId = window.setTimeout(handler, 100);
  346. }
  347. this.MenuScrollDown = function() {
  348. this.MenuScroll("down");
  349. this.MenuScrollStop();
  350. var handler = function()
  351. {
  352. owner.MenuScrollDown();
  353. }
  354. scrollTimerId = window.setTimeout(handler, 100);
  355. }
  356. this.MenuScrollStop = function() {
  357. if (scrollTimerId != null) {
  358. window.clearTimeout(scrollTimerId)
  359. scrollTimerId = null;
  360. }
  361. }
  362. this.GetHtmlObject = function()
  363. {
  364. if (htmlObject == null)
  365. {
  366. var obj = document.createElement("div");
  367. obj.className = "left-menuGroup";
  368. obj.onmousewheel = function()
  369. {
  370. if (event.wheelDelta >= 120)
  371. owner.MenuScroll("up");
  372. else if (event.wheelDelta <= -120)
  373. owner.MenuScroll("down");
  374. }
  375. objCaption = document.createElement("div");
  376. obj.appendChild(objCaption);
  377. objCaption.className = "left-nav-container";
  378. objCaption.onselectstart = function(){return false;}
  379. imgScrollDown = new Image();
  380. obj.appendChild(imgScrollDown);
  381. imgScrollDown.src = "../resources/images/fw/left/scrolldown.gif";
  382. imgScrollDown.className = "left-scrollButton";
  383. imgScrollUp = new Image();
  384. obj.appendChild(imgScrollUp);
  385. imgScrollUp.src = "../resources/images/fw/left/scrollup.gif";
  386. imgScrollUp.className = "left-scrollButton";
  387. imgcontent = document.createElement("span");
  388. objCaption.appendChild(imgcontent);
  389. textcontent = document.createElement("h1");
  390. textcontent.innerHTML = this.Text;
  391. objCaption.appendChild(textcontent);
  392. clickcontent = document.createElement("div");
  393. objCaption.appendChild(clickcontent);
  394. objItems = document.createElement("div");
  395. objItems.className = "left-menuItems";
  396. obj.appendChild(objItems);
  397. for (var i=0; i<this.Items.length; i++)
  398. {
  399. var item = this.Items[i];
  400. if (item != null && item.GetHtmlObject() != null)
  401. {
  402. objItems.appendChild(item.GetHtmlObject());
  403. }
  404. }
  405. htmlObject = obj;
  406. }
  407. return htmlObject;
  408. }
  409. }
  410. function Menu(text, icon, href, target)
  411. {
  412. this.Text = text;
  413. this.Icon = icon;
  414. this.Href = href;
  415. this.Target = target;
  416. var htmlObject = null;
  417. this.GetHeight = function()
  418. {
  419. if (htmlObject != null)
  420. {
  421. return htmlObject.offsetHeight;
  422. }
  423. return 0;
  424. }
  425. this.GetHtmlObject = function()
  426. {
  427. if (htmlObject == null)
  428. {
  429. var obj = document.createElement("div");
  430. obj.align = "center";
  431. obj.className = "left-menu";
  432. var link = document.createElement("a");
  433. link.href = this.Href;
  434. link.target = "doc2";
  435. link.onfocus = function() { this.blur(); }
  436. var link2 = link.cloneNode(true);
  437. obj.appendChild(link2);
  438. link2.onfocus = function() { this.blur(); }
  439. var img = new Image();
  440. link2.appendChild(img);
  441. img.src = this.Icon;
  442. img.alt = this.Text;
  443. img.className = "left-menuIcon";
  444. var objText = document.createElement("div");
  445. obj.appendChild(objText);
  446. objText.className = "left-menuText";
  447. link.innerHTML = this.Text;
  448. objText.appendChild(link);
  449. htmlObject = obj;
  450. }
  451. return htmlObject;
  452. }
  453. }
  454. function SetBorderColor(obj, borderColorLight, borderColorDark)
  455. {
  456. obj.style.borderLeftColor = borderColorLight;
  457. obj.style.borderTopColor = borderColorLight;
  458. obj.style.borderRightColor = borderColorDark;
  459. obj.style.borderBottomColor = borderColorDark;
  460. }
  461. function getPostion(e){
  462. var t=e.offsetTop;
  463. var l=e.offsetLeft;
  464. while(e=e.offsetParent){
  465. t+=e.offsetTop;
  466. l+=e.offsetLeft;
  467. }
  468. return {"top": t, "left" : l};
  469. }