aboutsummaryrefslogtreecommitdiff
path: root/assets/js/home_page.js
blob: 1bc5ccf539d84b36b7cf496baf371c1383447146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
    $('.search-menu').bind('click', function(e) {
        e.preventDefault();
        $("#search-drop-down").click();
        var type = $(this).text();

        switch(type) {
            case "Courses":
                // 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":
                // 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);