zhaoxing
2022-06-16 935c5a8382bb2ef60ed6bf1d05c1122eb1d6f46a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* 2017-12-4 15:15:45 | 修改 木遥(微信:  http://marsgis.cn/weixin.html ) */
 
var protocol = window.location.protocol;
if (protocol == "https:") {
  window.location.href = window.location.href.replace("https", "http");
}
 
$(document).ready(function () {
  initPage();
  bindEvents();
  // sidebarScrollFix()
});
 
let aceEditor;
let containExamples = true;
 
function initPage () {
  initEditor();
  screenResize();
}
 
function screenResize () {
  window.onresize = function () {
    mapHeight();
  };
}
 
function findConfig (locationParam, search) {
  let exConfig = haoutil.storage.get("mars3d-es5-example");
  if (!exConfig) {
    return;
  }
 
  exConfig = JSON.parse(exConfig);
  if (!exConfig || !exConfig.length) {
    return;
  }
 
  for (let index1 = 0; index1 < exConfig.length; index1++) {
    let item = exConfig[index1];
    if (!item.children) {
      return;
    }
    for (let index2 = 0; index2 < item.children.length; index2++) {
      let item2 = item.children[index2];
      if (!item2.children) {
        return;
      }
      for (let index3 = 0; index3 < item2.children.length; index3++) {
        let item3 = item2.children[index3];
        if (item3.fileName == locationParam) {
          console.log(item, item2, item3, "itemitemite")
          document.title = item3.name + " 【" + item2.name + " " + item.name + "】 | Mars3D示例 | 合肥火星科技有限公司";
          return;
        }
      }
    }
  }
}
 
//初始化编辑器
function initCodeEditor () {
  if (!aceEditor) {
    aceEditor = ace.edit("editor");
    aceEditor.setTheme("ace/theme/xcode");
    aceEditor.getSession().setMode("ace/mode/html");
    aceEditor.getSession().setUseWrapMode(true);
    aceEditor.setShowPrintMargin(false);
    aceEditor.$blockScrolling = Infinity;
  }
  aceEditor.setValue($("#editor").val());
  aceEditor.clearSelection();
  aceEditor.moveCursorTo(0, 0);
}
 
//初始化编辑器以及预览内容
function initEditor () {
  loadExampleHtml();
  initCodeEditor();
}
 
function loadExampleHtml () {
  let locationParam = getLocationParam();
  if (!locationParam) {
    return;
  }
 
  findConfig(locationParam, window.location.search);
 
  locationParam = id2FileName(locationParam);
 
  let href = window.location.pathname;
  let mapUrl = href.substr(0, href.lastIndexOf("/") + 1);
  mapUrl = mapUrl + locationParam + window.location.search;
  if (!mapUrl) {
    return;
  }
 
  let cacheVersion = "20210501";
  if (mapUrl.indexOf("?") == -1) {
    mapUrl += "?time=" + cacheVersion;
  } else if (mapUrl.indexOf("time=" + cacheVersion) == -1) {
    mapUrl += "&time=" + cacheVersion;
  }
 
  console.log("加载示例页面:" + mapUrl);
 
  var html = $.ajax({
    url: mapUrl,
    async: false,
    error: function (error) {
      haoutil.msg("该页面不存在,请检查地址!");
      html = "";
    },
  }).responseText;
  if (html && html != "") {
    html = html.replace(new RegExp(/[\s]*<meta[^>]*?\/?>/, "gm"), "");
 
    $("#editor").val(html);
    loadPreview(html);
  }
}
 
function getLocationParam () {
  let id = haoutil.system.getRequestByName("id");
  if (id) {
    return id;
  }
 
  let param = window.location.toString();
  if (param.indexOf("#") === -1) {
    return "";
  }
  param = param.split("#");
  if (param && param.length > 0) {
    return param[1];
  }
}
 
//运行代码
function run () {
  let iframeContent = $("#editor").val();
  if (editor) {
    iframeContent = aceEditor.getValue();
  }
  loadPreview(iframeContent);
}
 
//填充预览效果内容
function loadPreview (content) {
  let iFrame = createIFrame(),
    iframeDocument = iFrame.contentWindow.document;
 
  iframeDocument.open();
  iframeDocument.write(content);
  iframeDocument.close();
 
  let doc = document;
  iFrame.addEventListener("load", function () {
    mapHeight();
    //setTimeout(function () {
    //    doc.title = iframeDocument.title;
    //}, 100);
  });
  mapHeight();
}
 
function loadIFrameForSrc (url) {
  createIFrame();
  $("#innerPage").attr("src", url);
 
  mapHeight();
}
 
function createIFrame () {
  let preViewPane = $("#previewPane");
  preViewPane.empty();
  let iframe = document.createElement("iframe");
  $(iframe).attr("id", "innerPage");
  $(iframe).attr("name", "innerPage");
  preViewPane.append(iframe);
  return iframe;
}
 
//重置编辑器
function refresh () {
  initEditor();
  run();
}
 
// function initSelect() {
//   let hash = window.location.hash
//   let id
//   if (hash.indexOf('#') === -1) {
//     id = $('section#sidebar .thirdMenu a.link').first().attr('id')
//     window.location.hash = id ? '#' + id : window.location.hash
//   } else {
//     id = hash.split('#')[1]
//   }
//   selectMenu(id)
// }
 
function mapHeight () {
  let doc = $("#innerPage").contents();
  doc.find("html").height("100%");
  doc.find("body").height("100%");
}
 
function bindEvents () {
  $("#sidebar ul.third-menu a").click(function (evt) {
    let target = $(evt.target).parent().parent();
    let nodeId = evt.target.id;
    //如果点击的是span节点还要往上一层
    if (evt.target.localName === "span") {
      nodeId = target.attr("id");
    }
 
    if (nodeId) {
      //阻止冒泡防止上层事件响应导致修改url hash值
      evt.preventDefault();
      window.location.hash = "#" + nodeId;
      initEditor();
      evt.stopPropagation();
    }
  });
  let codePane = $("#codePane");
  let previewPane = $("#previewPane");
  let expand = !!1;
  $("#showCodeBtn").click(function () {
    if (expand) {
      //编辑器和预览宽度5:7
      $(this).text(" 收缩").css({ left: "500px" });
      $(this).addClass(" fa-compress");
      $(this).removeClass("fa-arrows-alt");
      codePane.show(10, function () {
        previewPane.removeClass("col-md-12");
        previewPane.addClass("col-md-7");
        codePane.addClass("col-md-5");
 
        $("#showCodeBtn").css({ left: $("#codePane").width() + 5 + "px" });
        if (aceEditor) {
          aceEditor.resize();
        }
      });
    } else {
      //预览独占一行
      $(this).text(" 源码").css({ left: "0px" });
      $(this).addClass("fa-arrows-alt");
      $(this).removeClass(" fa-compress");
      codePane.hide(200, function () {
        codePane.removeClass("col-md-5");
        previewPane.removeClass("col-md-7");
        previewPane.addClass("col-md-12");
      });
    }
    expand = !expand;
  });
 
  if (haoutil.system.getRequestByName("code")) {
    $("#showCodeBtn").click();
  }
 
  // window.addEventListener('hashchange', function () {
  //   let hash = window.location.hash
  //   if (hash.indexOf('#') !== -1) {
  //     let id = hash.split('#')[1]
  //     selectMenu(id)
  //   }
  // })
}