A semantic search engine for source code https://bitshift.benkurtovic.com/
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.

index.advanced-search-form.js 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * @file Manages all advanced search form logic.
  3. */
  4. var searchGroups = $("div#search-groups");
  5. /*
  6. * Load all advanced search form libraries.
  7. */
  8. (function loadInputFieldWidgets(){
  9. $(".search-group input#date-last-modified").datepicker();
  10. $(".search-group input#date-created").datepicker();
  11. $("#autocomplete").autocomplete({
  12. source: function(request, response){
  13. var matcher = new RegExp(
  14. $.ui.autocomplete.escapeRegex(request.term), "i");
  15. response($.grep(AUTOCOMPLETE_LANGUAGES, function(item){
  16. return matcher.test(item);
  17. }));
  18. }
  19. });
  20. }());
  21. /*
  22. * Set all advanced search form button callbacks.
  23. */
  24. (function setSearchFormCallbacks(){
  25. // Create a new search group, and update the `#sidebar` checklist.
  26. $("button#add-group").click(function(){
  27. $("div#sidebar input[type=checkbox]").prop("checked", false);
  28. searchGroups.children("#selected").removeAttr("id");
  29. var searchGroup = $("<div/>", {
  30. class : "search-group",
  31. id : "selected"
  32. });
  33. searchGroups.append(
  34. searchGroup.append(createSearchGroupInput("language")));
  35. $("div#sidebar input[type=checkbox]#language").prop("checked", true);
  36. });
  37. // Remove the currently selected group if it's not the only one, and mark
  38. // one of its siblings as selected.
  39. $("button#remove-group").click(function(){
  40. var currentGroup = $("div.search-group#selected");
  41. if($("div.search-group").length == 1)
  42. return;
  43. else {
  44. var nextGroup = currentGroup.prev();
  45. if(nextGroup.size() == 0)
  46. nextGroup = currentGroup.next();
  47. }
  48. currentGroup.remove();
  49. nextGroup.click();
  50. });
  51. // Select a search group, and update the `#sidebar` checklist accordingly.
  52. $(document).on("click", "div.search-group", function(){
  53. searchGroups.children("#selected").removeAttr("id");
  54. $(this).attr("id", "selected");
  55. $("div#sidebar input[type=checkbox]").prop("checked", false);
  56. $(this).find("input[type=text]").each(function(){
  57. var checkBoxSelector = "div#sidebar input[type=checkbox]";
  58. $(checkBoxSelector + "#" + $(this).attr("class").split(" ")[0]).
  59. prop("checked", true);
  60. })
  61. });
  62. // Add an input field to the currently selected search group.
  63. $("div#sidebar input[type=checkbox]").click(function(){
  64. var fieldId = $(this).prop("id");
  65. if($(this).is(":checked")){
  66. $("div.search-group#selected").append(
  67. $.parseHTML(createSearchGroupInput(fieldId)));
  68. if(fieldId.slice(0, 4) == "date")
  69. $(".search-group#selected ." + fieldId).datepicker();
  70. }
  71. else
  72. $("div.search-group#selected #" + fieldId).remove()
  73. });
  74. var previousAdvancedQuery = "";
  75. var searchBar = $("form#search-bar input[name=query]");
  76. window.setInterval(function(){
  77. var currentQuery = assembleQuery();
  78. if(currentQuery != previousAdvancedQuery){
  79. previousAdvancedQuery = currentQuery;
  80. searchBar.val(assembleQuery());
  81. }
  82. }, 1e3 / 15);
  83. }());
  84. /*
  85. * Return an HTML string representing a new input field div in a search group.
  86. *
  87. * @param fieldId The id of the input field div, and its child elements.
  88. */
  89. function createSearchGroupInput(fieldId){
  90. return [
  91. "<div id='" + fieldId + "'>",
  92. "<div class='name'>" + fieldId.replace(/-/g, " ") + "</div>",
  93. "<input class='" + fieldId + "' name='" + fieldId + "'type='text'>",
  94. "<input type='checkbox' name='regex'>",
  95. "<span class='regex'>Regex</span>",
  96. "</div>"
  97. ].join("");
  98. }
  99. /*
  100. * Create a query from advanced-search groups.
  101. */
  102. function assembleQuery(){
  103. var groups = searchGroups.children(".search-group");
  104. var groupQueries = [];
  105. for(var group = 0; group < groups.length; group++){
  106. var inputFields = groups[group].querySelectorAll("input[type=text]");
  107. var regexCheckbox = groups[group].querySelectorAll("input[name=regex]");
  108. var groupQuery = [];
  109. for(var field = 0; field < inputFields.length; field++)
  110. if(inputFields[field].value.length > 0)
  111. groupQuery.push(genFieldQueryString(
  112. inputFields[field], regexCheckbox[field].checked));
  113. groupQueries.push(groupQuery.join(" AND "));
  114. }
  115. return groupQueries.join(" OR ");
  116. }
  117. /*
  118. * Generate a processed query string for an input field's value.
  119. *
  120. * @param field (DOM element) An `input[type=text]` element.
  121. * @param hasRegex (boolean) Whether or not the field's value has regex.
  122. */
  123. function genFieldQueryString(field, hasRegex){
  124. var terms = field.value.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
  125. var query = field.getAttribute("name") + ":" + (hasRegex?"re:":"") + terms;
  126. if(field.value.indexOf('"') >= 0){
  127. return '"' + query + '"';
  128. }
  129. return query;
  130. }