example.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. $(document).ready(function () {
  2. initPage();
  3. bindEvents();
  4. //懒加载
  5. var timeout = setTimeout(function () {
  6. $("img.chart-thumb").lazyload();
  7. }, 1000);
  8. });
  9. var exConfig = exampleConfig,
  10. containExamples = false,
  11. thumbLocation = getThumbLocation();
  12. //左侧层级不包含例子,只包含分类
  13. function initPage() {
  14. var sideBar = $("ul#sidebar-menu");
  15. var chartList = $("#charts-list");
  16. for (var key in exConfig) {
  17. sideBar.append(createSideBarMenuItem(key, exConfig[key], containExamples));
  18. chartList.append(createGalleryItem(key, exConfig[key]));
  19. }
  20. resizeCharts();
  21. initSelect();
  22. sidebarScrollFix();
  23. }
  24. //初始化页面第一次加载
  25. function initSelect() {
  26. var hash = window.location.hash;
  27. if (hash.indexOf("#") === -1) {
  28. var id = $('#sidebar li').first().children('a')[0].hash;
  29. window.location.hash = (id) ? id : window.location.hash;
  30. }
  31. scroll();
  32. }
  33. //初始化示例面板
  34. function createGalleryItem(id, config) {
  35. if (!config) {
  36. return;
  37. }
  38. var categoryLi = $("<li class='category' id='" + id + "'></li>");
  39. var title = utils.getLocalPairs(config, "name");
  40. if (title) {
  41. createGalleryItemTitle(id, title).appendTo(categoryLi);
  42. }
  43. if (config.content) {
  44. createSubGalleryItem(config.content, id).appendTo(categoryLi);
  45. }
  46. return categoryLi;
  47. }
  48. function createSubGalleryItem(config, name) {
  49. var categoryContentDiv = $("<div class='category-content'></div>");
  50. for (var key in config) {
  51. var configItem = config[key];
  52. var content = $("<div class='box box-default color-palette-box' id='" + name + '-' + key + "'></div>");
  53. var title = utils.getLocalPairs(configItem, "name");
  54. createSubGalleryItemTitle(key, title).appendTo(content);
  55. if (configItem.content) {
  56. createGalleryCharts(configItem.content).appendTo(content);
  57. }
  58. content.appendTo(categoryContentDiv);
  59. }
  60. return categoryContentDiv;
  61. }
  62. function createGalleryItemTitle(id, title) {
  63. var menuItemIcon = exampleIconConfig[id];
  64. return $("<h3 class='category-title' id='title_" + id + "'>" + "<i class='fa " + menuItemIcon + "'></i>" + "&nbsp;&nbsp;" + title + "</h3>");
  65. }
  66. function createSubGalleryItemTitle(id, title) {
  67. return $("<div class='box-header'>" + "<h3 class='box-title' id='category-type-" + id + "'>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + title + "</h4>" + "</h3>" + "</div>");
  68. }
  69. function createGalleryCharts(examples) {
  70. var chartsDiv = $("<div class='box-body'></div>");
  71. var len = (examples && examples.length) ? examples.length : 0;
  72. for (var i = 0; i < len; i++) {
  73. createGalleryChart(examples[i]).appendTo(chartsDiv);
  74. }
  75. return chartsDiv;
  76. }
  77. function createGalleryChart(example) {
  78. var target = "editor.html",
  79. defaultThumb = "./images/thumb.png",
  80. title = utils.getLocalPairs(example, "name"),
  81. href = example.fileName ? example.fileName : "",
  82. thumbnail = example.thumbnail ? thumbLocation + "/img/" + example.thumbnail : "";
  83. var chartDiv = $("<div class='col-xlg-2 col-lg-3 col-md-4 col-sm-6 col-xs-12'></div>");
  84. var chart = $("<div class='chart'></div>");
  85. var link = $("<a class='chart-link' target='_blank' href='" + target + "#" + href + "'></a>");
  86. var chartTitle = $("<h5 class='chart-title'>" + title + "</h5>");
  87. var thumb = $("<img class='chart-thumb' src='" + defaultThumb + "' data-original='" + thumbnail + "' style='display: inline'>");
  88. chartTitle.appendTo(link);
  89. thumb.appendTo(link);
  90. link.appendTo(chart);
  91. chart.appendTo(chartDiv);
  92. return chartDiv;
  93. }
  94. function getThumbLocation() {
  95. var param = window.location.toString();
  96. return param.substr(0, param.lastIndexOf('/'));
  97. }
  98. //chart宽高自适应
  99. function resizeCharts() {
  100. var charts = $("#charts-list .chart .chart-thumb");
  101. if (charts[0] && charts[0].offsetWidth) {
  102. charts.height(charts[0].offsetWidth * 0.8);
  103. } else {
  104. charts.height(260 * 0.8);
  105. }
  106. window.onresize = function () {
  107. charts.height(charts[0].offsetWidth * 0.8);
  108. }
  109. }
  110. //根据url滚动到页面相应的位置
  111. function scroll() {
  112. var hash = window.location.hash;
  113. var ele;
  114. if (hash && hash.indexOf("#") !== -1) {
  115. var param = hash.split("#")[1].split("-");
  116. if (param.length === 1) {
  117. ele = $(".category-title#title_" + param[0]);
  118. selectMenu(param[0], param.length);
  119. }
  120. if (param.length == 2) {
  121. //二级菜单里面的li
  122. ele = $("#category-type-" + param[1]);
  123. selectMenu(param[1], param.length);
  124. }
  125. }
  126. if (ele && ele.offset()) {
  127. $(window).animate({scrollTop: ele.offset().top - 60}, 0);
  128. }
  129. }
  130. //绑定点击事件
  131. function bindEvents() {
  132. var child = $("ul#sidebar-menu>li.treeview>ul>li");
  133. var parent = $('ul.sidebar-menu>li').parent("ul");
  134. //因为iManager只有1级所以,iManager点击的时候相当于一级菜单,其他的二级都要关闭.
  135. if ($('ul.sidebar-menu>li#firstMenuiManager').find('ul').length == 0) {
  136. if ($('ul.sidebar-menu>li#firstMenuiManager').click(function () {
  137. $('ul#sidebar-menu>li>ul').slideUp(500);
  138. })) ;
  139. }
  140. //一级菜单跳转
  141. child.parent('ul').siblings('a').click(function (evt) {
  142. if ($(this).siblings('ul').is(':visible') && $(this).siblings('ul').children('li').hasClass('active')) {
  143. evt.stopPropagation();//阻止点击事件触发折叠的冒泡
  144. }
  145. window.location = evt.currentTarget.href;
  146. });
  147. //二级菜单跳转,不用 boot自带
  148. window.addEventListener("hashchange", function () {
  149. scroll();
  150. });
  151. }
  152. var openTimer; // 定义展开的延时
  153. var animationSpeed = 500;
  154. $(window).on('scroll', function () {
  155. if ($('ul.sidebar-menu>li').hasClass('active')) {
  156. var parent = $('ul.sidebar-menu>li').parent("ul");
  157. //设置0.1秒后再打开,目的是为了防止滚轮拉快 中途经过的展开和折叠效果还来不及完成而产生的重叠效果;
  158. if (openTimer) {
  159. clearTimeout(openTimer);
  160. }
  161. openTimer = setTimeout(function () {
  162. parent.children('li.active').children('ul').slideDown(animationSpeed, function () {
  163. parent.children('li.active').children('ul').css('display', 'block');
  164. })
  165. }, 100);
  166. }
  167. $('ul.sidebar-menu>li').not("li.active").children('ul').css('display', 'none');
  168. });