example.js 7.2 KB

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