sidebar.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* 2017-10-20 13:00:05 | 修改 木遥(微信: http://marsgis.cn/weixin.html ) */
  2. //左侧层级是否包含示例
  3. let containExample = false;
  4. //侧边栏滚动支持
  5. function sidebarScrollFix() {
  6. $("ul#sidebar-menu>li").hover(
  7. function (evt) {
  8. if (!$("body").hasClass("sidebar-collapse")) {
  9. return;
  10. }
  11. //调整一级菜单li下标题的布局位置至右侧
  12. let $titleBar = $(this).children("a").children(".sidebar-title-bar");
  13. $titleBar.css({
  14. top: $(this).offset().top - $(window).scrollTop() + "px",
  15. //fix由于侧边栏滚动条宽度引起的减少的宽度
  16. width: "233px",
  17. });
  18. //如果底部空间不够,动态增加侧边栏高度
  19. let visibleOffsetTop = $(this).offset().top - $(window).scrollTop();
  20. let offsetBottom = $(".sidebar-menu").height() - visibleOffsetTop;
  21. let requireVisibleHeight = $(this).height() + $(this).children("ul").height();
  22. if (offsetBottom <= requireVisibleHeight) {
  23. $(".sidebar-menu").css({
  24. height: requireVisibleHeight + $(window).height() + "px",
  25. });
  26. }
  27. //调整一级菜单li下子列表的布局位置至右侧
  28. let offsetTop = visibleOffsetTop + $(this).height();
  29. $(this)
  30. .children("ul")
  31. .css({
  32. top: offsetTop + "px",
  33. });
  34. //fix小尺寸屏幕下二级菜单高度高于窗口高度时显示不全的情况
  35. let $activeList = $(this).children("ul");
  36. let activeListOffsetBottom = Math.abs($(window).height() - visibleOffsetTop - $(this).height());
  37. let requireActiveListHeight = $activeList.height();
  38. if (activeListOffsetBottom < requireActiveListHeight) {
  39. $activeList.css({ height: requireActiveListHeight });
  40. //滚动条样式
  41. $activeList.addClass("scroll-list");
  42. }
  43. },
  44. function (evt) {
  45. if (!$("body").hasClass("sidebar-collapse")) {
  46. return;
  47. }
  48. //滚动条
  49. $(this).children("ul").removeClass("scroll-list");
  50. //恢复原来的高度
  51. $(this).children("ul").css({ height: "auto" });
  52. }
  53. );
  54. $(".main-sidebar").on("scroll", function (evt) {
  55. evt.stopPropagation();
  56. });
  57. $(window).on("resize", function () {
  58. $(".sidebar-menu").css({ height: "100%" });
  59. });
  60. }
  61. //创建菜单项
  62. function createSideBarMenuItem(config, containAll) {
  63. if (!config) {
  64. return;
  65. }
  66. containExample = containAll;
  67. let li = $("<li id='bar_" + config.id + "' class='treeview '></li>");
  68. if (config.children) {
  69. createSideBarMenuTitle(config, true).appendTo(li);
  70. createSideBarSecondMenu(config.children).appendTo(li);
  71. } else {
  72. createSideBarMenuTitle(config, false).appendTo(li);
  73. }
  74. return li;
  75. }
  76. //创建二级菜单
  77. function createSideBarSecondMenu(children) {
  78. let ul = $("<ul class='treeview-menu second-menu '></ul>");
  79. children.forEach((item, index) => {
  80. let li = $("<li class='menuTitle ' id='bar_" + item.id + "' ></li>");
  81. li.appendTo(ul);
  82. if (containExample && item.children) {
  83. createSideBarMenuSecondTitle(item, true).appendTo(li);
  84. createSideBarThirdMenu(item.children).appendTo(li);
  85. } else {
  86. createSideBarMenuSecondTitle(item, false).appendTo(li);
  87. }
  88. });
  89. return ul;
  90. }
  91. function fileName2Id(fileName) {
  92. let value = (fileName || "").replace(".html", "");
  93. return value;
  94. }
  95. function id2FileName(id) {
  96. let fileName = id + ".html";
  97. return fileName;
  98. }
  99. //创建三级菜单
  100. function createSideBarThirdMenu(examples) {
  101. let ul = $("<ul class='treeview-menu third-menu'></ul>");
  102. let len = examples && examples.length ? examples.length : 0;
  103. for (let i = 0; i < len; i++) {
  104. let example = examples[i];
  105. let _id = fileName2Id(example.fileName);
  106. let li = $("<li class='menuTitle' id='bar_" + _id + "' ></li>");
  107. li.appendTo(ul);
  108. if (_id != "" && example.name) {
  109. createSideBarMenuThirdTitle(_id, example, false).appendTo(li);
  110. }
  111. }
  112. return ul;
  113. }
  114. function createSideBarMenuTitle(config, collapse) {
  115. let id = config.id || "";
  116. let icon = "",
  117. iconName = config.icon;
  118. if (iconName) {
  119. icon = "<i class='fa " + iconName + " iconName'></i>";
  120. }
  121. let href = "";
  122. if (location.href.indexOf("editor.html") != -1) {
  123. href = "../examples.html#" + id;
  124. } else {
  125. href = "#" + id;
  126. }
  127. let div = $("<a href='" + href + "' >" + icon + "<span class='firstMenuTitle'>" + config.name + "(" + config.count + ")</span></a>");
  128. if (collapse) {
  129. div.append(createCollapsedIcon());
  130. }
  131. return div;
  132. }
  133. function createSideBarMenuSecondTitle(item, collapse) {
  134. let id = item.id || "";
  135. let icon = "",
  136. iconName = item.icon;
  137. if (iconName) {
  138. icon = "<i class='fa " + iconName + "'></i>";
  139. }
  140. let href = "";
  141. if (location.href.indexOf("editor.html") != -1) {
  142. href = "../examples.html#" + id;
  143. } else {
  144. href = "#" + id;
  145. }
  146. let div = $(
  147. "<a href='" + href + "' id='bar_" + item.id + "'>" + icon + "<span class='secondMenuTitle'>" + item.name + "(" + item.count + ")</span></a>"
  148. );
  149. if (collapse) {
  150. div.append(createCollapsedIcon());
  151. }
  152. return div;
  153. }
  154. function createSideBarMenuThirdTitle(id, item, collapse) {
  155. id = id || "";
  156. let icon = "",
  157. iconName = item.icon;
  158. if (iconName) {
  159. icon = "<i class='fa " + iconName + "'></i>";
  160. }
  161. let div = $("<a href='#' id='bar_" + id + "'>" + icon + "<span class='thirdMenuTitle'>" + item.name + "</span></a>");
  162. if (collapse) {
  163. div.append(createCollapsedIcon());
  164. }
  165. return div;
  166. }
  167. //创建右侧折叠菜单
  168. function createCollapsedIcon() {
  169. return $("<span class='pull-right-container'> <i class='fa fa-angle-left pull-right'></i> </span>");
  170. }
  171. //只处理三层节点,后续可优化
  172. function selectMenu(id, length) {
  173. let target = _getTarget(id, length);
  174. if (length !== 1) {
  175. //控制editor页面左侧导航栏一级菜单高亮
  176. _selectTarget(target.parent().parent().parent().parent());
  177. //控制示例页面左侧导航栏一级菜单高亮
  178. _selectTarget(target.parent().parent());
  179. //控制左侧导航栏最低级菜单高亮
  180. _selectTarget(target.parent());
  181. _selectTarget(target.find("ul"));
  182. }
  183. }
  184. function _getTarget(id, length) {
  185. let target;
  186. if (length) {
  187. if (length === 1) {
  188. $("section#sidebar li.active").removeClass("active");
  189. target = $("section#sidebar li#bar_" + id);
  190. target.children("ul").show();
  191. }
  192. if (length === 2) {
  193. $("section#sidebar li.active ul.active li").removeClass("active");
  194. target = $("section#sidebar li.treeview")
  195. .children("ul")
  196. .children("li#bar_" + id);
  197. }
  198. } else {
  199. $("section#sidebar #ul").addClass("active");
  200. $("section#sidebar li.active").removeClass("active");
  201. target = $("section#sidebar li#bar_" + id);
  202. }
  203. target && target.addClass("active");
  204. return target;
  205. }
  206. function _selectTarget(target) {
  207. if (!target || target.length < 1) {
  208. return;
  209. }
  210. let className = target.attr("class");
  211. if (className && className.indexOf("treeview-menu") > -1 && className.indexOf("menu-open") === -1) {
  212. target.addClass("menu-open");
  213. target.css("display", "block");
  214. }
  215. if (className && className.indexOf("treeview") > -1) {
  216. target.addClass("active");
  217. }
  218. }