vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.1 KiB

  1. $(document).ready(function() {
  2. vis.createBreadcrumbs($(".container.full").first());
  3. vis.initSiteSearch();
  4. vis.initKeywords();
  5. $("#tipue_search_input").keyup(checkInput)
  6. vis.typingTimeout = 0;
  7. });
  8. function checkInput() {
  9. if (document.getElementById("tipue_search_input").value.length > 3) {
  10. clearTimeout(vis.typingTimeout);
  11. vis.typingTimeout = setTimeout(vis.initSiteSearch(true),300);
  12. }
  13. else {
  14. document.getElementById("search-results-wrapper").style.display = "none";
  15. }
  16. }
  17. // namespace
  18. var vis = {};
  19. /**
  20. * Adds a breadcrumb as first child to the specified container.
  21. *
  22. * @author felixhayashi
  23. */
  24. vis.createBreadcrumbs = function(container) {
  25. // use the url to infer the path
  26. var crumbs = location.pathname.split('/');
  27. // number of ancestor directories
  28. var stepbackIndex = crumbs.length-1;
  29. var breadcrumbs = $.map(crumbs, function(crumb, i) {
  30. // first and last element of the split
  31. if(!crumb) return;
  32. stepbackIndex--;
  33. if(/\.html$/.test(crumb)) {
  34. // strip the .html to make it look prettier
  35. return "<span>" + crumb.replace(/\.html$/, "") + "</span>";
  36. } else {
  37. // calculate the relative url
  38. for(var ref=crumb+"/", j=0; j<stepbackIndex; j++, ref="../"+ref);
  39. return "<a href='" + ref + "'>" + crumb + "</a>";
  40. }
  41. }).join("") || "Home";
  42. // insert into the container at the beginning.
  43. $(container).prepend("<div id=\"breadcrumbs\">" + breadcrumbs + "</div>");
  44. };
  45. /**
  46. * Will load tipue search field.
  47. * If the search has already begun, we also display the results.
  48. *
  49. * For information how it works:
  50. * @see https://github.com/almende/vis/issues/909#issuecomment-120119414
  51. * @see https://github.com/almende/vis/issues/909#issuecomment-120397562
  52. *
  53. * @author felixhayashi
  54. */
  55. vis.initSiteSearch = function(dynamic) { // Added dynamic flag for live update ~ Alex
  56. $("#tipue_search_input").tipuesearch({
  57. "mode": "live",
  58. "show": 3,
  59. },dynamic);
  60. var hasSearchMessage = $("#tipue_search_content").children().length > 0;
  61. if(hasSearchMessage) {
  62. // show result panel
  63. if ($("#search-results-wrapper").css("display") === 'none') {
  64. $("#search-results-wrapper").css("display", "block");
  65. }
  66. // encode the keywords that were entered by the user
  67. var keywords = $("#tipue_search_input").val().replace(/\s/g, ",");
  68. // add keywords to result-urls
  69. $(".tipue_search_content_url a, .tipue_search_content_title a").each(function() {
  70. $(this).attr("href", $(this).attr("href") + "?keywords=" + keywords);
  71. });
  72. } else {
  73. $("#search-results-wrapper").css("display", "none");
  74. }
  75. };
  76. /**
  77. * Will highlight the keywords that are passed as url get-parameters.
  78. * All keywords are higlighted and a panel is displayed to jump to the
  79. * first keyword found.
  80. *
  81. * For information how it works:
  82. * @see https://github.com/almende/vis/issues/909#issuecomment-120119414
  83. * @see https://github.com/almende/vis/issues/909#issuecomment-120397562
  84. *
  85. * @author felixhayashi
  86. */
  87. vis.initKeywords = function() {
  88. // extract keywords from get-variable
  89. var keywords = url("?keywords");
  90. if(keywords) {
  91. // highlighting all keywords
  92. keywords = keywords.split(",");
  93. for(var i = 0; i < keywords.length; i++) {
  94. $("body").highlight(keywords[i]);
  95. }
  96. // nasty hack: programmatically open full options tab
  97. // because no browser allows scrolling to hidden elements!
  98. $("[role=presentation][targetnode=fullOptions]").click();
  99. $("tr.toggle:not(collapsible)").click();
  100. // init keyword info panel
  101. $("#keyword-info").css("display", "block");
  102. $("#keyword-count").text($(".highlight").length);
  103. $("#keyword-jumper-button").on('click', function(event) {
  104. event.preventDefault();
  105. // do not cache hits outside the handler; creates problems with prettyfy lib
  106. // we use the first visible(!) hit at the time the button is clicked
  107. var firstHit = $(".highlight:visible").first();
  108. if(firstHit.length) {
  109. $("html, body").animate({ scrollTop: $(firstHit).offset().top }, 2000);
  110. }
  111. });
  112. }
  113. };