summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/wwwroot/Content/js/mods.js
blob: 690b81df0faddcdcde060cc826b5b5f0e643da6b (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
/* globals $ */

var smapi = smapi || {};
var app;
smapi.modList = function (mods) {
    // init data
    var data = {
        mods: mods,
        showAllFields: false,
        search: ""
    };
    for (var i = 0; i < data.mods.length; i++) {
        var mod = mods[i];

        // set initial visibility
        mod.Visible = true;

        // concatenate searchable text
        mod.SearchableText = [mod.Name, mod.AlternateNames, mod.Author, mod.AlternateAuthors, mod.Compatibility.Summary, mod.BrokeIn];
        if (mod.Compatibility.UnofficialVersion)
            mod.SearchableText.push(mod.Compatibility.UnofficialVersion);
        if (mod.BetaCompatibility) {
            mod.SearchableText.push(mod.BetaCompatibility.Summary);
            if (mod.BetaCompatibility.UnofficialVersion)
                mod.SearchableText.push(mod.BetaCompatibility.UnofficialVersion);
        }
        for (var p = 0; p < mod.ModPages; p++)
            mod.SearchableField.push(mod.ModPages[p].Text);
        mod.SearchableText = mod.SearchableText.join(" ").toLowerCase();
    }

    // init app
    app = new Vue({
        el: "#app",
        data: data,
        methods: {
            /**
             * Update the visibility of all mods based on the current search text.
             */
            applySearch: function () {
                // get search terms
                var words = data.search.toLowerCase().split(" ");

                // make sure all words match
                for (var i = 0; i < data.mods.length; i++) {
                    var mod = data.mods[i];
                    var match = true;
                    for (var w = 0; w < words.length; w++) {
                        if (mod.SearchableText.indexOf(words[w]) === -1) {
                            match = false;
                            break;
                        }
                    }

                    mod.Visible = match;
                }
            }
        }
    });

    // put focus in textbox for quick search
    if (!location.hash)
        $("#search-box").focus();

    // enable table sorting
    $("#mod-list").tablesorter({
        cssHeader: "header",
        cssAsc: "headerSortUp",
        cssDesc: "headerSortDown"
    });
};