editor.js 5.1 KB

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