ux.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. $(function () {
  2. 'use strict';
  3. var pagePath = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
  4. // select current page in sidenav and set up prev/next links if they exist
  5. var $selNavLink = $('#sidebar').find('a[href="' + pagePath + '"]');
  6. if ($selNavLink.length) {
  7. //$selListItem = $selNavLink.closest('li');
  8. $selNavLink.addClass('currentItem');
  9. }
  10. if (window.addEventListener) window.addEventListener('load', loadCallback, true);
  11. else window.attachEvent('load', loadCallback, true);
  12. function loadCallback(evt) {
  13. var form = document.getElementById("searchbox");
  14. if (form) {
  15. var input = form.query;
  16. form.onsubmit = function (evt) {
  17. var query = input.value;
  18. if (query) {
  19. input.value = "";
  20. input.blur();
  21. var url = "https://www.google.com/search?q=" + encodeURIComponent("site:ace.ajax.org" + " " + query);
  22. window.open(url);
  23. }
  24. return false;
  25. };
  26. }
  27. }
  28. // init search
  29. $('#search')
  30. // prevent from form submit
  31. .on('submit', function () {
  32. return false;
  33. }).find('input');
  34. });
  35. function ux() {
  36. var d = 'a.menu, .dropdown-toggle'
  37. function clearMenus() {
  38. $(d).parent('li').each(function () {
  39. $(this).removeClass('open')
  40. });
  41. }
  42. var s, sx;
  43. // scrolling offset calculation via www.quirksmode.org
  44. if (window.pageYOffset || window.pageXOffset) {
  45. s = window.pageYOffset;
  46. sx = window.pageXOffset;
  47. }
  48. else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
  49. s = document.documentElement.scrollTop;
  50. sx = document.documentElement.scrollLeft;
  51. }
  52. else if (document.body) {
  53. s = document.body.scrollTop;
  54. sx = document.body.scrollLeft;
  55. }
  56. if (document.documentElement.offsetWidth < 1010) {
  57. if (sx <= 0) sx = 0;
  58. else if (sx + document.documentElement.offsetWidth > 1010) sx = 1010 - document.documentElement.offsetWidth;
  59. }
  60. else sx = 0;
  61. $('span.methodClicker, article.article, i.methodClicker').each(function () {
  62. var a = $(this);
  63. var constructorPos = a.attr("id").indexOf("new ");
  64. var objName = a.attr("id");
  65. if (constructorPos >= 0) {
  66. objName = objName.substring(constructorPos + 4);
  67. objName += ".new";
  68. }
  69. a.attr("id", objName);
  70. });
  71. function showMethodContent() {
  72. var locationHash = location.hash.replace(/^#/, '').replace(/\./g, '\\.');
  73. var equalsPos = location.hash.indexOf("=");
  74. if (equalsPos >=0) {
  75. locationHash = locationHash.substring(0, location.hash.indexOf("="));
  76. }
  77. var $clickerEl = $('span#' + locationHash);
  78. if ($clickerEl.length > 0 && $clickerEl.hasClass('methodClicker')) {
  79. var p = $clickerEl.parent();
  80. p[0].force = true;
  81. p.trigger('click');
  82. p[0].force = false;
  83. }
  84. }
  85. if (location.hash.indexOf("section") >= 1) {
  86. showMethodContent();
  87. var data = location.hash;
  88. scrollTo(null, data.substr(1));
  89. }
  90. window.onhashchange = function () {
  91. showMethodContent();
  92. }
  93. };
  94. function scrollTo(el, data) {
  95. if (!data) {
  96. data = el.getAttribute("data-id");
  97. //location.hash = data;
  98. }
  99. var el = $("span#" + data.replace(/\./g, "\\."))[0];
  100. if (!el) return;
  101. var article = $(el).closest('.article')[0];
  102. var top = article.offsetTop - 100;
  103. if (document.body.scrollTop > top || document.body.scrollTop != top && document.body.scrollTop + (window.innerHeight || document.documentElement.offsetHeight) < top + article.offsetHeight) {
  104. $('body').animate({
  105. scrollTop: top
  106. }, {
  107. duration: 200,
  108. easing: "swing"
  109. });
  110. }
  111. }