editor.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.*/
  2. $(document).ready(function () {
  3. window.initI18N(function(){
  4. initPage();
  5. bindEvents();
  6. sidebarScrollFix();
  7. });
  8. });
  9. var aceEditor;
  10. var containExamples = true;
  11. function initPage() {
  12. initSideBar();
  13. initEditor();
  14. screenResize();
  15. }
  16. function initSideBar() {
  17. var config = exampleConfig;
  18. var sideBar = $("ul#sidebar-menu");
  19. for (var key in config) {
  20. sideBar.append(createSideBarMenuItem(key, config[key], containExamples));
  21. }
  22. $(sideBar).ready(function () {
  23. initSelect();
  24. });
  25. }
  26. function screenResize() {
  27. window.onresize = function () {
  28. mapHeight();
  29. };
  30. }
  31. //初始化编辑器
  32. function initCodeEditor() {
  33. if (!aceEditor) {
  34. aceEditor = ace.edit("editor");
  35. aceEditor.setTheme("ace/theme/textmate");
  36. aceEditor.getSession().setMode("ace/mode/html");
  37. aceEditor.getSession().setUseWrapMode(true);
  38. aceEditor.setShowPrintMargin(false);
  39. aceEditor.$blockScrolling = Infinity;
  40. }
  41. aceEditor.setValue($('#editor').val());
  42. aceEditor.clearSelection();
  43. aceEditor.moveCursorTo(0, 0);
  44. }
  45. //初始化编辑器以及预览内容
  46. function initEditor() {
  47. loadExampleHtml();
  48. initCodeEditor();
  49. }
  50. function loadExampleHtml() {
  51. var locationParam = getLocationParam();
  52. if (!locationParam) {
  53. return;
  54. }
  55. var href = window.location.toString();
  56. var mapUrl = href.substr(0, href.lastIndexOf('/') + 1);
  57. mapUrl = mapUrl + locationParam + ".html";
  58. if (!mapUrl) {
  59. return;
  60. }
  61. var html = $.ajax({
  62. url: mapUrl,
  63. async: false,
  64. error: function (error) {
  65. alert(resources.editor.envTips);
  66. html = "";
  67. }
  68. }).responseText;
  69. if (html && html != "") {
  70. $('#editor').val(html);
  71. loadPreview(html);
  72. }
  73. }
  74. function getLocationParam() {
  75. var param = window.location.toString();
  76. if (param.indexOf("#") === -1) {
  77. return null;
  78. }
  79. param = param.split("#");
  80. if (param && param.length > 0) {
  81. return param[1];
  82. }
  83. }
  84. //运行代码
  85. function run() {
  86. var iframeContent = $("#editor").val();
  87. if (editor) {
  88. iframeContent = aceEditor.getValue();
  89. }
  90. loadPreview(iframeContent);
  91. }
  92. //填充预览效果内容
  93. function loadPreview(content) {
  94. var iFrame = createIFrame(),
  95. iframeDocument = iFrame.contentWindow.document;
  96. iFrame.contentWindow.resources=window.resources?window.resources.resources:{};
  97. iframeDocument.open();
  98. iframeDocument.write(content);
  99. iframeDocument.close();
  100. var doc = document;
  101. iFrame.addEventListener('load', function () {
  102. mapHeight();
  103. setTimeout(function () {
  104. doc.title = iframeDocument.title;
  105. }, 100);
  106. });
  107. mapHeight();
  108. }
  109. function createIFrame() {
  110. var preViewPane = $("#previewPane");
  111. preViewPane.empty();
  112. var iframe = document.createElement("iframe");
  113. $(iframe).attr("id", "innerPage");
  114. $(iframe).attr("name", "innerPage");
  115. preViewPane.append(iframe);
  116. return iframe;
  117. }
  118. //重置编辑器
  119. function refresh() {
  120. initEditor();
  121. run();
  122. }
  123. function initSelect() {
  124. var hash = window.location.hash;
  125. var id;
  126. if (hash.indexOf("#") === -1) {
  127. id = $("section#sidebar .thirdMenu a.link").first().attr('id');
  128. window.location.hash = (id) ? "#" + id : window.location.hash;
  129. } else {
  130. id = hash.split("#")[1];
  131. }
  132. selectMenu(id);
  133. }
  134. function mapHeight() {
  135. var doc = $("#innerPage").contents();
  136. doc.find("html").height("100%");
  137. doc.find("body").height("100%");
  138. }
  139. function bindEvents() {
  140. $("#sidebar ul.third-menu a").click(function (evt) {
  141. var target = $(evt.target).parent().parent();
  142. var nodeId = evt.target.id;
  143. //如果点击的是span节点还要往上一层
  144. if (evt.target.localName === "span") {
  145. nodeId = target.attr('id');
  146. }
  147. if (nodeId) {
  148. //阻止冒泡防止上层事件响应导致修改url hash值
  149. evt.preventDefault();
  150. window.location.hash = "#" + nodeId;
  151. initEditor();
  152. evt.stopPropagation();
  153. }
  154. });
  155. var codePane = $("#codePane");
  156. var previewPane = $("#previewPane");
  157. var expand = !!1;
  158. $("#showCodeBtn").click(function () {
  159. if (expand) {
  160. //编辑器和预览宽度5:7
  161. $(this).text(resources.editor.expand);
  162. $(this).addClass("fa-arrows-alt");
  163. $(this).removeClass(" fa-compress");
  164. codePane.show(10, function () {
  165. previewPane.removeClass("col-md-12");
  166. previewPane.addClass("col-md-7");
  167. codePane.addClass("col-md-5");
  168. });
  169. } else {
  170. //预览独占一行
  171. $(this).text(resources.editor.source);
  172. $(this).addClass(" fa-compress");
  173. $(this).removeClass("fa-arrows-alt");
  174. codePane.hide(200, function () {
  175. codePane.removeClass("col-md-5");
  176. previewPane.removeClass("col-md-7");
  177. previewPane.addClass("col-md-12");
  178. });
  179. }
  180. expand = !expand;
  181. });
  182. window.addEventListener("hashchange", function () {
  183. var hash = window.location.hash;
  184. if (hash.indexOf("#") !== -1) {
  185. var id = hash.split("#")[1];
  186. selectMenu(id);
  187. }
  188. });
  189. }