main.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var editor;
  2. var embedded_editor;
  3. $(function() {
  4. if (typeof ace !== "undefined") {
  5. ace.config.set("workerPath", "build/src-min");
  6. editor = ace.edit("ace_editor_demo");
  7. editor.container.style.opacity = "";
  8. embedded_editor = ace.edit("embedded_ace_code");
  9. embedded_editor.container.style.opacity = "";
  10. embedded_editor.session.setMode("ace/mode/html");
  11. embedded_editor.setAutoScrollEditorIntoView(true);
  12. embedded_editor.setOption("maxLines", 40);
  13. editor.setOptions({
  14. maxLines: 30,
  15. mode: "ace/mode/javascript",
  16. autoScrollEditorIntoView: true
  17. });
  18. ace.config.loadModule("ace/ext/emmet", function() {
  19. ace.require("ace/lib/net").loadScript("http://cloud9ide.github.io/emmet-core/emmet.js", function() {
  20. embedded_editor.setOption("enableEmmet", true);
  21. editor.setOption("enableEmmet", true);
  22. });
  23. });
  24. ace.config.loadModule("ace/ext/language_tools", function() {
  25. embedded_editor.setOptions({
  26. enableSnippets: true,
  27. enableBasicAutocompletion: true
  28. });
  29. editor.setOptions({
  30. enableSnippets: true,
  31. enableBasicAutocompletion: true
  32. });
  33. });
  34. } else {
  35. document.body.insertAdjacentHTML("afterbegin", '<div class="bs-docs-example">\
  36. <div class="alert alert-error">\
  37. <button type="button" class="close" data-dismiss="alert">\xd7</button>\
  38. <strong>Oh No!</strong> Couldn\'t load <code>build/src/ace.js</code>.<br>\
  39. You can build it by running <code>npm install ; node Makefile.dryice.js</code><br>\
  40. Or download older version by running <code>git submodule update --init --recursive</code><br>\
  41. </div>\
  42. </div>');
  43. }
  44. $("ul.menu-list").mousedown(function(e) {
  45. if (e.button === 1) {
  46. e.preventDefault();
  47. }
  48. });
  49. $("ul.menu-list li").click(function(e) {
  50. if (e.target.tagName !== "A") {
  51. var href = $(this).find("a").attr("href");
  52. if (e.button == 1)
  53. window.open(href, "_blank");
  54. else
  55. window.location = href;
  56. }
  57. });
  58. // used when page is access directly
  59. function magicClickInterceptor(e) {
  60. e.preventDefault();
  61. var state = {};
  62. state.api = $(this).attr("href").substring(6, $(this).attr("href").length - 5);
  63. $.bbq.pushState(state);
  64. var _self = $(this);
  65. $("#apiHolder").load($(this).attr("href") + " #documentation", function(){
  66. $("#apiHolder").removeClass("apiIntro").removeClass("span9");
  67. $("#documentation").removeClass("span9").addClass("span7");
  68. ux();
  69. setupClicker();
  70. // handles dropping in from new link
  71. var section = $.bbq.getState("section");
  72. if (section) {
  73. $("li#dropdown_" + section.replace(/\./g, '\\.') + " a").triggerHandler('click');
  74. }
  75. //setupDisqus(_self.attr("href"));
  76. });
  77. }
  78. $('.menu-item a').click(magicClickInterceptor);
  79. $('a.argument').click(magicClickInterceptor);
  80. $('a.external').click(function(e) {
  81. e.preventDefault();
  82. });
  83. var tabs = $("#tabnav"),
  84. tab_a_selector = "a";
  85. var firstLoad = true;
  86. tabs.find(tab_a_selector).click(function(e) {
  87. e.preventDefault();
  88. if ($(this).attr("href") === "/") {
  89. window.location = "http://ace.ajax.org";
  90. return;
  91. }
  92. if ($(this).attr("href").indexOf("#api") === 0) {
  93. $("#top_container").addClass("collapse");
  94. scrollIntoPosition(null, 0);
  95. }
  96. else if ($(this).is(":visible")) {
  97. if (firstLoad) {
  98. firstLoad = false;
  99. setTimeout(function() {
  100. $("#top_container").removeClass("collapse");
  101. scrollIntoPosition(e.target);
  102. }, 700);
  103. }
  104. else {
  105. $("#top_container").removeClass("collapse");
  106. scrollIntoPosition(e.target);
  107. }
  108. }
  109. function scrollIntoPosition(el, overridePosition) {
  110. if (typeof overridePosition !== "undefined") {
  111. $("body").stop().animate({
  112. scrollTop: overridePosition
  113. }, 400);
  114. }
  115. else if ($("body").scrollTop() > 345) {
  116. $("body").stop().animate({
  117. scrollTop: ($(el).offset().top - 10)
  118. }, 400);
  119. }
  120. }
  121. $(this).tab("show");
  122. var state = {};
  123. state.nav = $(this).attr("href").substr(1);
  124. $.bbq.pushState(state);
  125. });
  126. $('#tabnav a[data-toggle="tab"]').on('shown', function (e) {
  127. $(".tab-content .tab-pane.active .ace_editor").each(function(i, el){
  128. el.env.onResize();
  129. });
  130. });
  131. $(window).on("hashchange", function(e) {
  132. _gaq.push(['_trackPageview',location.pathname + location.search + location.hash]);
  133. tabs.each(function() {
  134. var idx = $.bbq.getState("nav") || "about";
  135. var section = e.fragment.split("&")[1] || "";
  136. $(this).find(tab_a_selector + "[href='#" + idx + "']").triggerHandler('click');
  137. // handles dropping in from new link
  138. var api = $.bbq.getState("api");
  139. if (api) {
  140. $(tab_a_selector + "[href='./api/" + api + ".html']").triggerHandler('click');
  141. }
  142. });
  143. }).trigger("hashchange");
  144. highlight();
  145. });
  146. function highlight() {
  147. var highlighter = ace.require("ace/ext/static_highlight")
  148. var dom = ace.require("ace/lib/dom")
  149. function qsa(sel) {
  150. var els = document.querySelectorAll(sel);
  151. var result = [];
  152. for (var i = 0, l = els.length; i < l; i++)
  153. result[i] = els[i];
  154. return result;
  155. }
  156. qsa("code[class]").forEach(function(el) {
  157. var m = el.className.match(/language-(\w+)|(javascript)/);
  158. if (!m) return
  159. var mode = "ace/mode/" + (m[1] || m[2]);
  160. highlighter.highlight(el, {mode: mode, theme: "ace/theme/xcode"})
  161. });
  162. }