example.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. //
  179. if (title == "三维场景发布与浏览") {
  180. link = $("<a class='chart-link' target='_blank' href='http://121.43.55.7:2102?token=" + location.search.replace("?token=", "") + "'></a>");
  181. }
  182. let thumb = $("<img class='chart-area' src='" + thumbnail + "' style='display: inline'>");
  183. let chartTitle = $("<h5 class='chart-title' title='" + msg + "' >" + title + "</h5>");
  184. if (example.plugins) {
  185. msg += "\n该功能属于独立" + example.plugins + "插件功能,在额外的js中。";
  186. chartTitle = $(
  187. "<h5 class='chart-title' title='" +
  188. msg +
  189. "' >" +
  190. title +
  191. "<span style='color:rgba(0, 147, 255, 0.7)'>[" +
  192. example.plugins +
  193. "插件]</span></h5>"
  194. );
  195. }
  196. if (isWidget) {
  197. msg += "\n该功能属于项目内功能,此处仅做演示,具体交付依赖是否选择对应项目。";
  198. chartTitle = $("<h5 class='chart-title' title='" + msg + "' >" + title + "<span style='color:rgba(0, 147, 255, 0.7)'>[项目widget]</span></h5>");
  199. }
  200. //最新加的示例
  201. // if (window.mars3d.version == example.version) {
  202. // $('<span class="newTitle" title="新添加示例">新</span>').appendTo(chart);
  203. // }
  204. chartDiv.attr("title", msg);
  205. thumb.appendTo(link)
  206. chartTitle.appendTo(link);
  207. link.appendTo(chart);
  208. chart.appendTo(chartDiv);
  209. return chartDiv;
  210. }
  211. function imgerrorfun() {
  212. let img = event.srcElement;
  213. img.src = "img/mapicon.jpg";
  214. img.onerror = null;
  215. }
  216. function openExampleView(href, title) {
  217. let width = document.documentElement.clientWidth - 230 + "px";
  218. let height = document.documentElement.clientHeight - 60 + "px";
  219. let _layerIdx = layer.open({
  220. type: 2,
  221. title: title,
  222. fix: true,
  223. maxmin: true,
  224. shadeClose: true,
  225. offset: ["60px", "230px"],
  226. area: [width, height],
  227. content: href,
  228. skin: "layer-mars-dialog animation-scale-up",
  229. success: function (layero) { },
  230. });
  231. //$("#layui-layer" + _layerIdx).css({
  232. // "width": "calc(100% - 230px)",
  233. // "height": "calc(100% - 80px)",
  234. //});
  235. $("#layui-layer" + _layerIdx + " .layui-layer-title").css({
  236. background: "rgba(30, 36, 50, 1)",
  237. "border-color": "rgba(32, 160, 255, 1)",
  238. });
  239. }
  240. function getThumbLocation() {
  241. let param = window.location.toString();
  242. return param.substr(0, param.lastIndexOf("/"));
  243. }
  244. //chart宽高自适应
  245. function resizeCharts() {
  246. let charts = $("#charts-list .chart .chart-area");
  247. if (charts[0] && charts[0].offsetWidth) {
  248. charts.height(charts[0].offsetWidth * 0.8);
  249. } else {
  250. charts.height(260 * 0.8);
  251. }
  252. window.onresize = function () {
  253. resizeCharts();
  254. };
  255. }
  256. //根据url滚动到页面相应的位置
  257. function scroll() { }
  258. //绑定点击事件
  259. function bindEvents() {
  260. let child = $("ul#sidebar-menu>li.treeview>ul>li");
  261. // var parent = $('ul.sidebar-menu>li').parent('ul')
  262. // //因为iManager只有1级所以,iManager点击的时候相当于一级菜单,其他的二级都要关闭.
  263. // if ($('ul.sidebar-menu>li#firstMenuiManager').find('ul').length == 0) {
  264. // if (
  265. // $('ul.sidebar-menu>li#firstMenuiManager').click(function () {
  266. // $('ul#sidebar-menu>li>ul').slideUp(500)
  267. // })
  268. // ) {
  269. // //
  270. // }
  271. // }
  272. //一级菜单跳转
  273. child
  274. .parent("ul")
  275. .siblings("a")
  276. .click(function (evt) {
  277. if ($(this).siblings("ul").is(":visible") && $(this).siblings("ul").children("li").hasClass("active")) {
  278. evt.stopPropagation(); //阻止点击事件触发折叠的冒泡
  279. }
  280. window.location = evt.currentTarget.href;
  281. });
  282. //二级菜单跳转,不用 boot自带
  283. window.addEventListener("hashchange", function () {
  284. scroll();
  285. });
  286. }
  287. let openTimer; // 定义展开的延时
  288. let animationSpeed = 500;
  289. $(window).on("scroll", function () {
  290. if ($("ul.sidebar-menu>li").hasClass("active")) {
  291. let parent = $("ul.sidebar-menu>li").parent("ul");
  292. //设置0.1秒后再打开,目的是为了防止滚轮拉快 中途经过的展开和折叠效果还来不及完成而产生的重叠效果;
  293. if (openTimer) {
  294. clearTimeout(openTimer);
  295. }
  296. openTimer = setTimeout(function () {
  297. parent
  298. .children("li.active")
  299. .children("ul")
  300. .slideDown(animationSpeed, function () {
  301. parent.children("li.active").children("ul").css("display", "block");
  302. });
  303. }, 100);
  304. }
  305. $("ul.sidebar-menu>li").not("li.active").children("ul").css("display", "none");
  306. });
  307. function getVerDiff(oldver) {
  308. let index = 0;
  309. let arrNew = "序号,分类,子分类,功能名称,版本\n";
  310. exConfig.forEach((item, index1) => {
  311. if (!item.children) {
  312. return;
  313. }
  314. item.children.forEach((item2, index2) => {
  315. if (!item2.children) {
  316. return;
  317. }
  318. item2.children.forEach((item3, index3) => {
  319. if (!oldver || item3.version > oldver) {
  320. arrNew += `${++index},${item.name},${item2.name},${item3.name},${item3.version}\n`;
  321. }
  322. });
  323. });
  324. });
  325. return arrNew;
  326. }
  327. function getAllName() {
  328. let arrNew = "Mars3D功能清单:";
  329. let qianzhui = "1.";
  330. exConfig.forEach((item, index1) => {
  331. if (!item.children) {
  332. return;
  333. }
  334. arrNew += `\n\n${qianzhui}${index1 + 1} ${item.name}`;
  335. item.children.forEach((item2, index2) => {
  336. if (!item2.children) {
  337. return;
  338. }
  339. arrNew += `\n${qianzhui}${index1 + 1}.${index2 + 1} ${item2.name}\n`;
  340. item2.children.forEach((item3, index3) => {
  341. if (index3 === 0) {
  342. arrNew += `\t${item3.name}`;
  343. } else {
  344. arrNew += `,${item3.name}`;
  345. }
  346. });
  347. arrNew += `\n`;
  348. });
  349. });
  350. return arrNew;
  351. }