example.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. let exConfig;
  2. let containExamples = false,
  3. thumbLocation = getThumbLocation();
  4. $(document).ready(function () {
  5. bindEvents();
  6. //懒加载
  7. let timeout = setTimeout(function () {
  8. $("img.chart-thumb").lazyload();
  9. }, 1000);
  10. //记录url传入参数
  11. let request = haoutil.system.getRequest();
  12. fetch(request.json|| window.exampleConfig || "../data/example.json")
  13. .then(function (response) {
  14. if (!response.ok) {
  15. let error = new Error(response.statusText);
  16. error.response = response;
  17. throw error;
  18. } else {
  19. return response.json();
  20. }
  21. })
  22. .then((json) => {
  23. exConfig = json.result;
  24. //赋值id
  25. exConfig.forEach((item, index1) => {
  26. item.id = "ex_" + index1;
  27. if (item.children) {
  28. item.children.forEach((item2, index2) => {
  29. item2.id = item.id + "_" + index2;
  30. if (item2.children) {
  31. item2.children.forEach((item3, index3) => {
  32. item3.id = item3.id + "_" + index3;
  33. });
  34. }
  35. });
  36. }
  37. });
  38. haoutil.storage.add("exConfig", JSON.stringify(exConfig));
  39. initPage();
  40. })
  41. .catch(function (error) {
  42. console.log("加载JSON出错", error);
  43. haoutil.alert(error?.message, "出错了");
  44. });
  45. });
  46. //左侧层级不包含例子,只包含分类
  47. function initPage() {
  48. let sideBar = $("ul#sidebar-menu");
  49. let chartList = $("#charts-list");
  50. exConfig.forEach((item, index1) => {
  51. let count = 0;
  52. if (item.children) {
  53. item.children.forEach((configItem, index) => {
  54. configItem.count = configItem.children ? configItem.children.length : 0;
  55. count += configItem.count;
  56. });
  57. }
  58. item.count = count;
  59. sideBar.append(createSideBarMenuItem(item, containExamples));
  60. chartList.append(createGalleryItem(item));
  61. });
  62. resizeCharts();
  63. initSelect();
  64. sidebarScrollFix();
  65. }
  66. //初始化页面第一次加载
  67. function initSelect() {
  68. let hash = window.location.hash;
  69. if (hash.indexOf("#") === -1) {
  70. // var id = $('#sidebar li').first().children('a')[0].hash;
  71. // window.location.hash = (id) ? id : window.location.hash;
  72. } else {
  73. scroll();
  74. }
  75. }
  76. //初始化示例面板
  77. function createGalleryItem(item) {
  78. if (!item) {
  79. return;
  80. }
  81. let categoryLi = $("<li class='category' id='" + item.id + "'></li>");
  82. if (item.name) {
  83. createGalleryItemTitle(item).appendTo(categoryLi);
  84. }
  85. if (item.children) {
  86. createSubGalleryItem(item.children).appendTo(categoryLi);
  87. }
  88. return categoryLi;
  89. }
  90. function createSubGalleryItem(config) {
  91. let categoryContentDiv = $("<div class='category-content'></div>");
  92. config.forEach((configItem, index) => {
  93. let content = $("<div class='box box-default color-palette-box' id='" + config.id + "'></div>");
  94. createSubGalleryItemTitle(configItem).appendTo(content);
  95. if (configItem.children) {
  96. createGalleryCharts(configItem.children).appendTo(content);
  97. }
  98. content.appendTo(categoryContentDiv);
  99. });
  100. return categoryContentDiv;
  101. }
  102. function createGalleryItemTitle(item) {
  103. return $(
  104. "<h3 class='category-title' id='" +
  105. item.id +
  106. "'>" +
  107. "<i class='fa " +
  108. item.icon +
  109. "'></i>" +
  110. "&nbsp;&nbsp;" +
  111. item.name +
  112. " (" +
  113. item.count +
  114. ")</h3>"
  115. );
  116. }
  117. function createSubGalleryItemTitle(configItem) {
  118. let details;
  119. if (configItem.details) {
  120. details = `<div class='box-title-details'>说明:${configItem.details}</div>`;
  121. } else {
  122. details = "";
  123. }
  124. return $(
  125. "<div class='box-header'>" +
  126. "<h3 class='box-title' id='" +
  127. configItem.id +
  128. "'>" +
  129. "&nbsp;&nbsp;&nbsp;&nbsp;" +
  130. configItem.name +
  131. " (" +
  132. configItem.count +
  133. ")</h4>" +
  134. "</h3>" +
  135. details +
  136. "" +
  137. "</div>"
  138. );
  139. }
  140. function createGalleryCharts(examples) {
  141. let chartsDiv = $("<div class='box-body'></div>");
  142. let len = examples && examples.length ? examples.length : 0;
  143. for (let i = 0; i < len; i++) {
  144. let html = createGalleryChart(examples[i]);
  145. if (html) {
  146. html.appendTo(chartsDiv);
  147. }
  148. }
  149. return chartsDiv;
  150. }
  151. function createGalleryChart(example) {
  152. let _path = window.examplePath || "example/";
  153. let _widgetpath = window.widgetPath || "//mars3d.cn/project/zhts/map.html";
  154. let target = _path + "editor.html",
  155. title = example.name,
  156. href = fileName2Id(example.fileName),
  157. thumbnail = (window.exampleIconPath || "../data/exampleIcon/") + (example.thumbnail || "");
  158. let isWidget = false;
  159. if (example.params) {
  160. target += "?" + (window.autoShowCode ? "code=true&" : "") + example.params;
  161. if (example.params.indexOf("widget=") != -1) {
  162. if (!window.showWidget) {
  163. return false;
  164. }
  165. isWidget = true;
  166. target = _widgetpath + "?onlyStart=true&name=" + title + "&" + example.params;
  167. }
  168. } else {
  169. target += "?" + (window.autoShowCode ? "code=true&" : "");
  170. }
  171. if (href) {
  172. target = target + "#" + href;
  173. }
  174. let msg = title + " v" + (example.version || "");
  175. let chartDiv = $("<div class='col-xlg-2 col-lg-3 col-md-4 col-sm-6 col-xs-12'><a target='_blank'href='" + _path + href + ".html'></a></div>");
  176. let chart = $('<div class="chart"></div>');
  177. let link = $("<a class='chart-link' target='_blank' href='" + target + "'></a>");
  178. let thumb = $("<img class='chart-area' src='" + thumbnail + "' style='display: inline'>");
  179. let chartTitle = $("<h5 class='chart-title' title='" + msg + "' >" + title + "</h5>");
  180. if (example.plugins) {
  181. msg += "\n该功能属于独立" + example.plugins + "插件功能,在额外的js中。";
  182. chartTitle = $(
  183. "<h5 class='chart-title' title='" +
  184. msg +
  185. "' >" +
  186. title +
  187. "<span style='color:rgba(0, 147, 255, 0.7)'>[" +
  188. example.plugins +
  189. "插件]</span></h5>"
  190. );
  191. }
  192. if (isWidget) {
  193. msg += "\n该功能属于项目内功能,此处仅做演示,具体交付依赖是否选择对应项目。";
  194. chartTitle = $("<h5 class='chart-title' title='" + msg + "' >" + title + "<span style='color:rgba(0, 147, 255, 0.7)'>[项目widget]</span></h5>");
  195. }
  196. //最新加的示例
  197. // if (window.mars3d.version == example.version) {
  198. // $('<span class="newTitle" title="新添加示例">新</span>').appendTo(chart);
  199. // }
  200. chartDiv.attr("title", msg);
  201. thumb.appendTo(link)
  202. chartTitle.appendTo(link);
  203. link.appendTo(chart);
  204. chart.appendTo(chartDiv);
  205. return chartDiv;
  206. }
  207. function imgerrorfun() {
  208. let img = event.srcElement;
  209. img.src = "img/mapicon.jpg";
  210. img.onerror = null;
  211. }
  212. function openExampleView(href, title) {
  213. let width = document.documentElement.clientWidth - 230 + "px";
  214. let height = document.documentElement.clientHeight - 60 + "px";
  215. let _layerIdx = layer.open({
  216. type: 2,
  217. title: title,
  218. fix: true,
  219. maxmin: true,
  220. shadeClose: true,
  221. offset: ["60px", "230px"],
  222. area: [width, height],
  223. content: href,
  224. skin: "layer-mars-dialog animation-scale-up",
  225. success: function (layero) {},
  226. });
  227. //$("#layui-layer" + _layerIdx).css({
  228. // "width": "calc(100% - 230px)",
  229. // "height": "calc(100% - 80px)",
  230. //});
  231. $("#layui-layer" + _layerIdx + " .layui-layer-title").css({
  232. background: "rgba(30, 36, 50, 1)",
  233. "border-color": "rgba(32, 160, 255, 1)",
  234. });
  235. }
  236. function getThumbLocation() {
  237. let param = window.location.toString();
  238. return param.substr(0, param.lastIndexOf("/"));
  239. }
  240. //chart宽高自适应
  241. function resizeCharts() {
  242. let charts = $("#charts-list .chart .chart-area");
  243. if (charts[0] && charts[0].offsetWidth) {
  244. charts.height(charts[0].offsetWidth * 0.8);
  245. } else {
  246. charts.height(260 * 0.8);
  247. }
  248. window.onresize = function () {
  249. resizeCharts();
  250. };
  251. }
  252. //根据url滚动到页面相应的位置
  253. function scroll() {}
  254. //绑定点击事件
  255. function bindEvents() {
  256. let child = $("ul#sidebar-menu>li.treeview>ul>li");
  257. // var parent = $('ul.sidebar-menu>li').parent('ul')
  258. // //因为iManager只有1级所以,iManager点击的时候相当于一级菜单,其他的二级都要关闭.
  259. // if ($('ul.sidebar-menu>li#firstMenuiManager').find('ul').length == 0) {
  260. // if (
  261. // $('ul.sidebar-menu>li#firstMenuiManager').click(function () {
  262. // $('ul#sidebar-menu>li>ul').slideUp(500)
  263. // })
  264. // ) {
  265. // //
  266. // }
  267. // }
  268. //一级菜单跳转
  269. child
  270. .parent("ul")
  271. .siblings("a")
  272. .click(function (evt) {
  273. if ($(this).siblings("ul").is(":visible") && $(this).siblings("ul").children("li").hasClass("active")) {
  274. evt.stopPropagation(); //阻止点击事件触发折叠的冒泡
  275. }
  276. window.location = evt.currentTarget.href;
  277. });
  278. //二级菜单跳转,不用 boot自带
  279. window.addEventListener("hashchange", function () {
  280. scroll();
  281. });
  282. }
  283. let openTimer; // 定义展开的延时
  284. let animationSpeed = 500;
  285. $(window).on("scroll", function () {
  286. if ($("ul.sidebar-menu>li").hasClass("active")) {
  287. let parent = $("ul.sidebar-menu>li").parent("ul");
  288. //设置0.1秒后再打开,目的是为了防止滚轮拉快 中途经过的展开和折叠效果还来不及完成而产生的重叠效果;
  289. if (openTimer) {
  290. clearTimeout(openTimer);
  291. }
  292. openTimer = setTimeout(function () {
  293. parent
  294. .children("li.active")
  295. .children("ul")
  296. .slideDown(animationSpeed, function () {
  297. parent.children("li.active").children("ul").css("display", "block");
  298. });
  299. }, 100);
  300. }
  301. $("ul.sidebar-menu>li").not("li.active").children("ul").css("display", "none");
  302. });
  303. function getVerDiff(oldver) {
  304. let index = 0;
  305. let arrNew = "序号,分类,子分类,功能名称,版本\n";
  306. exConfig.forEach((item, index1) => {
  307. if (!item.children) {
  308. return;
  309. }
  310. item.children.forEach((item2, index2) => {
  311. if (!item2.children) {
  312. return;
  313. }
  314. item2.children.forEach((item3, index3) => {
  315. if (!oldver || item3.version > oldver) {
  316. arrNew += `${++index},${item.name},${item2.name},${item3.name},${item3.version}\n`;
  317. }
  318. });
  319. });
  320. });
  321. return arrNew;
  322. }
  323. function getAllName() {
  324. let arrNew = "Mars3D功能清单:";
  325. let qianzhui = "1.";
  326. exConfig.forEach((item, index1) => {
  327. if (!item.children) {
  328. return;
  329. }
  330. arrNew += `\n\n${qianzhui}${index1 + 1} ${item.name}`;
  331. item.children.forEach((item2, index2) => {
  332. if (!item2.children) {
  333. return;
  334. }
  335. arrNew += `\n${qianzhui}${index1 + 1}.${index2 + 1} ${item2.name}\n`;
  336. item2.children.forEach((item3, index3) => {
  337. if (index3 === 0) {
  338. arrNew += `\t${item3.name}`;
  339. } else {
  340. arrNew += `,${item3.name}`;
  341. }
  342. });
  343. arrNew += `\n`;
  344. });
  345. });
  346. return arrNew;
  347. }