A semantic search engine for source code https://bitshift.benkurtovic.com/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.advanced-search-form.js 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Toggle the presence of an input field.
  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. if($(".search-group#selected").children("div").length > 1)
  73. $(".search-group#selected #" + fieldId).remove()
  74. else
  75. $(this).prop("checked", true);
  76. }
  77. });
  78. var previousAdvancedQuery = "";
  79. var searchBar = $("form#search-bar input[name=query]");
  80. window.setInterval(function(){
  81. var currentQuery = assembleQuery();
  82. if(currentQuery != previousAdvancedQuery){
  83. previousAdvancedQuery = currentQuery;
  84. searchBar.val(assembleQuery());
  85. }
  86. }, 1e3 / 15);
  87. }());
  88. /*
  89. * Return an HTML string representing a new input field div in a search group.
  90. *
  91. * @param fieldId The id of the input field div, and its child elements.
  92. */
  93. function createSearchGroupInput(fieldId){
  94. return [
  95. "<div id='" + fieldId + "'>",
  96. "<div class='name'>" + fieldId.replace(/-/g, " ") + "</div>",
  97. "<input class='" + fieldId + "' name='" + fieldId + "'type='text'>",
  98. "<input type='checkbox' name='regex'>",
  99. "<span class='regex'>Regex</span>",
  100. "</div>"
  101. ].join("");
  102. }
  103. /*
  104. * Create a query from advanced-search groups.
  105. */
  106. function assembleQuery(){
  107. var groups = searchGroups.children(".search-group");
  108. var groupQueries = [];
  109. for(var group = 0; group < groups.length; group++){
  110. var inputFields = groups[group].querySelectorAll("input[type=text]");
  111. var regexCheckbox = groups[group].querySelectorAll("input[name=regex]");
  112. var groupQuery = [];
  113. for(var field = 0; field < inputFields.length; field++)
  114. if(inputFields[field].value.length > 0)
  115. groupQuery.push(genFieldQueryString(
  116. inputFields[field], regexCheckbox[field].checked));
  117. groupQueries.push(groupQuery.join(" AND "));
  118. }
  119. return groupQueries.join(" OR ");
  120. }
  121. /*
  122. * Generate a processed query string for an input field's value.
  123. *
  124. * @param field (DOM element) An `input[type=text]` element.
  125. * @param hasRegex (boolean) Whether or not the field's value has regex.
  126. */
  127. function genFieldQueryString(field, hasRegex){
  128. var terms = field.value.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
  129. var query = field.getAttribute("name") + ":" + (hasRegex?"re:":"") + terms;
  130. return '"' + query + '"';
  131. }