Passed
Push — master ( e7326f...65e227 )
by Sergii
01:22
created

template/static/scripts/fulltext-search-ui.js   A

Complexity

Total Complexity 13
Complexity/F 1.44

Size

Lines of Code 89
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 0
c 1
b 0
f 1
nc 2
dl 0
loc 89
rs 10
noi 0
wmc 13
mnd 1
bc 13
fnc 9
bpm 1.4443
cpm 1.4443
1
window.SearcherDisplay = (function($) {
2
    /**
3
     * This class provides support for displaying quick search text results to users.
4
     */
5
    function SearcherDisplay() { }
6
7
    SearcherDisplay.prototype.init = function() {
8
        this._displayQuickSearch();
9
    };
10
11
    /**
12
     * This method creates the quick text search entry in navigation menu and wires all required events.
13
     */
14
    SearcherDisplay.prototype._displayQuickSearch = function() {
15
            var quickSearch = $(document.createElement("iframe")),
16
                   body = $("body"),
17
                   self = this;
18
19
            quickSearch.attr("src", "quicksearch.html");
20
            quickSearch.css("width", "0px");
21
            quickSearch.css("height", "0px");
22
23
            body.append(quickSearch);
24
25
            $(window).on("message", function(msg) {
26
                var msgData = msg.originalEvent.data;
27
28
                if (msgData.msgid != "docstrap.quicksearch.done") {
29
                    return;
30
                }
31
32
                var results = msgData.results || [];
33
34
                self._displaySearchResults(results);
35
            });
36
37
            function startSearch() {
38
              var searchTerms = $('#search-input').prop("value");
39
              if (searchTerms) {
40
                quickSearch[0].contentWindow.postMessage({
41
                  "searchTerms": searchTerms,
42
                  "msgid": "docstrap.quicksearch.start"
43
                }, "*");
44
              }
45
            }
46
47
            $('#search-input').on('keyup', function(evt) {
48
              if (evt.keyCode != 13) {
49
                return;
50
              }
51
              startSearch();
52
              return false;
53
            });
54
            $('#search-submit').on('click', function() {
55
              startSearch();
56
              return false;
57
            });
58
    };
59
60
    /**
61
     * This method displays the quick text search results in a modal dialog.
62
     */
63
    SearcherDisplay.prototype._displaySearchResults = function(results) {
64
            var resultsHolder = $($("#searchResults").find(".modal-body")),
65
                  fragment = document.createDocumentFragment(),
66
                  resultsList = document.createElement("ul");
67
68
            resultsHolder.empty();
69
70
            for (var idx = 0; idx < results.length; idx++) {
71
                var result = results[idx],
72
                       item = document.createElement("li"),
73
                       link = document.createElement("a");
74
75
                link.href = result.id;
76
                link.innerHTML = result.title;
77
78
                item.appendChild(link)
79
                resultsList.appendChild(item);
80
            }
81
82
            fragment.appendChild(resultsList);
83
            resultsHolder.append(fragment);
84
85
            $("#searchResults").modal({"show": true});
86
    };
87
88
    return new SearcherDisplay();
89
})($);
90