aboutsummaryrefslogtreecommitdiff
path: root/assets/js/home_page.js
diff options
context:
space:
mode:
authorNana Nosirova <nargiza.nosirova@mail.utoronto.ca>2018-07-17 02:44:31 +0000
committerGitHub <noreply@github.com>2018-07-17 02:44:31 +0000
commit77168116122c0c5e37405fddaee785efca414f6e (patch)
tree32a4bcfc6b591e25ca86bb55fafc8f5e75c2a118 /assets/js/home_page.js
parentc9f4e0e9df821c305b9606bd9575d3092384205b (diff)
parent60a53ac331e33cab3173a6803dcf11de3ff47ae6 (diff)
Merge pull request #3 from kdam0/feat/SR-26qa
Feat/SR-26
Diffstat (limited to 'assets/js/home_page.js')
-rw-r--r--assets/js/home_page.js76
1 files changed, 71 insertions, 5 deletions
diff --git a/assets/js/home_page.js b/assets/js/home_page.js
index 449461d..1bc5ccf 100644
--- a/assets/js/home_page.js
+++ b/assets/js/home_page.js
@@ -1,4 +1,14 @@
function main(){
+ $("#go-button").on('click', function(e) {
+ e.preventDefault();
+ if ($("#drop-down-value").text() == "Courses") {
+ // $("#search-field").attr("action", "search/courses");
+ window.location += 'search/courses?search=' + $("#user-input-course").val();
+ } else {
+ // $("#search-field").attr("action", "search/users");
+ window.location += 'search/users?search=' + $("#user-input-users").val();
+ }
+ });
// Search bar drop down scripting (some of it is shared with nav panel
// in script.js but this code in this file is exclusive to search bar
@@ -9,18 +19,74 @@ function main(){
switch(type) {
case "Courses":
- $("#search-field").attr("action","/search/courses");
- $("#user-input").attr("placeholder", "Enter coure code");
+ // hide the input for user search
+ $("#user-input-users").css("display", "none");
+ // show the input for courses search
+ $("#custom-autocomplete").css("display", "block");
+
+ $("#search-field").attr("action", "/search/courses");
$("#drop-down-value").text("Courses");
break;
case "Users":
- $("#search-field").attr("action","/search/users");
- $("#user-input").attr("placeholder", "Enter user info");
+ // hide the input for courses search
+ $("#custom-autocomplete").css("display", "none");
+ // show the input for users search
+ $("#user-input-users").css("display", "block");
+
+ $("#search-field").attr("action", "/search/users");
$("#drop-down-value").text("Users");
break;
default:
console.log("Error: Click not registered");
}
});
+
+ // TODO: move this function to a general helper class??
+ // for auto-complete
+ var substringMatcher = function(strs) {
+ return function findMatches(q, cb) {
+ var matches, substringRegex;
+ // an array that will be populated with substring matches
+ matches = [];
+
+ // regex used to determine if a string contains the substring `q`
+ substrRegex = new RegExp(q, 'i');
+
+ // iterate through the pool of strings and for any string that
+ // contains the substring `q`, add it to the `matches` array
+ $.each(strs, function(i, str) {
+ if (substrRegex.test(str)) {
+ matches.push(str);
+ }
+ });
+
+ cb(matches);
+ };
+ };
+
+
+ // get array of all courses from controller
+ $.ajax({
+ url: '/auto-complete-courses',
+ type: 'GET',
+ success: function(data){
+ var jsonData = JSON.parse(data);
+ if (jsonData.success) {
+ var states = jsonData.data;
+
+ $('.typeahead').typeahead({
+ hint: true,
+ highlight: true,
+ minLength: 1
+ },
+ {
+ name: 'courseNames',
+ source: substringMatcher(states)
+ });
+ } else { // console log the error mssg
+ console.log(jsonData.data);
+ }
+ }
+ });
}
-$(document).ready(main); \ No newline at end of file
+$(document).ready(main);