summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/wwwroot/Content/js/mods.js
blob: 0394ac4fcb3ffc3e49225d0c5710a8c40e0a122b (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* globals $ */

var smapi = smapi || {};
var app;
smapi.modList = function (mods, enableBeta) {
    // init data
    var defaultStats = {
        total: 0,
        compatible: 0,
        workaround: 0,
        soon: 0,
        broken: 0,
        abandoned: 0,
        invalid: 0
    };
    var data = {
        mods: mods,
        showAdvanced: false,
        visibleStats: $.extend({}, defaultStats),
        filters: {
            source: {
                value: {
                    open: { value: true },
                    closed: { value: true }
                }
            },
            status: {
                label: enableBeta ? "main status" : "status",
                value: {
                    // note: keys must match status returned by the API
                    ok: { value: true },
                    optional: { value: true },
                    unofficial: { value: true },
                    workaround: { value: true },
                    broken: { value: true },
                    abandoned: { value: true },
                    obsolete: { value: true }
                }
            },
            betaStatus: {
                label: "beta status",
                value: {} // cloned from status field if needed
            },
            download: {
                value: {
                    chucklefish: { value: true, label: "Chucklefish" },
                    curseforge: { value: true, label: "CurseForge" },
                    moddrop: { value: true, label: "ModDrop" },
                    nexus: { value: true, label: "Nexus" },
                    custom: { value: true }
                }
            }
        },
        search: ""
    };

    // init filters
    Object.entries(data.filters).forEach(([groupKey, filterGroup]) => {
        filterGroup.label = filterGroup.label || groupKey;
        Object.entries(filterGroup.value).forEach(([filterKey, filter]) => {
            filter.id = ("filter_" + groupKey + "_" + filterKey).replace(/[^a-zA-Z0-9]/g, "_");
            filter.label = filter.label || filterKey;
        });
    });

    // init beta filters
    if (enableBeta) {
        var filterGroup = data.filters.betaStatus;
        $.extend(true, filterGroup.value, data.filters.status.value);
        Object.entries(filterGroup.value).forEach(([filterKey, filter]) => {
            filter.id = "beta_" + filter.id;
        });
    }
    else
        delete data.filters.betaStatus;

    // init mods
    for (var i = 0; i < data.mods.length; i++) {
        var mod = mods[i];

        // set initial visibility
        mod.Visible = true;

        // set overall compatibility
        mod.LatestCompatibility = mod.BetaCompatibility || mod.Compatibility;

        // 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,
        mounted: function() {
            // enable table sorting
            $("#mod-list").tablesorter({
                cssHeader: "header",
                cssAsc: "headerSortUp",
                cssDesc: "headerSortDown"
            });

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

            // jump to anchor (since table is added after page load)
            if (location.hash) {
                var row = $(location.hash).get(0);
                if (row)
                    row.scrollIntoView();
            }
        },
        methods: {
            /**
             * Update the visibility of all mods based on the current search text and filters.
             */
            applyFilters: function () {
                // get search terms
                var words = data.search.toLowerCase().split(" ");

                // apply criteria
                var stats = data.visibleStats = $.extend({}, defaultStats);
                for (var i = 0; i < data.mods.length; i++) {
                    var mod = data.mods[i];
                    mod.Visible = true;

                    // check filters
                    mod.Visible = this.matchesFilters(mod, words);
                    if (mod.Visible) {
                        stats.total++;
                        stats[this.getCompatibilityGroup(mod)]++;
                    }
                }
            },


            /**
             * Get whether a mod matches the current filters.
             * @param {object} mod The mod to check.
             * @param {string[]} searchWords The search words to match.
             * @returns {bool} Whether the mod matches the filters.
             */
            matchesFilters: function(mod, searchWords) {
                var filters = data.filters;

                // check hash
                if (location.hash === "#" + mod.Slug)
                    return true;

                // check source
                if (!filters.source.value.open.value && mod.SourceUrl)
                    return false;
                if (!filters.source.value.closed.value && !mod.SourceUrl)
                    return false;

                // check status
                var mainStatus = mod.Compatibility.Status;
                if (filters.status.value[mainStatus] && !filters.status.value[mainStatus].value)
                    return false;

                // check beta status
                if (enableBeta) {
                    var betaStatus = mod.LatestCompatibility.Status;
                    if (filters.betaStatus.value[betaStatus] && !filters.betaStatus.value[betaStatus].value)
                        return false;
                }

                // check download sites
                var ignoreSites = [];

                if (!filters.download.value.chucklefish.value)
                    ignoreSites.push("Chucklefish");
                if (!filters.download.value.curseforge.value)
                    ignoreSites.push("CurseForge");
                if (!filters.download.value.moddrop.value)
                    ignoreSites.push("ModDrop");
                if (!filters.download.value.nexus.value)
                    ignoreSites.push("Nexus");
                if (!filters.download.value.custom.value)
                    ignoreSites.push("custom");

                if (ignoreSites.length) {
                    var anyLeft = false;
                    for (var i = 0; i < mod.ModPageSites.length; i++) {
                        if (ignoreSites.indexOf(mod.ModPageSites[i]) === -1) {
                            anyLeft = true;
                            break;
                        }
                    }

                    if (!anyLeft)
                        return false;
                }

                // check search terms
                for (var w = 0; w < searchWords.length; w++) {
                    if (mod.SearchableText.indexOf(searchWords[w]) === -1)
                        return false;
                }

                return true;
            },

            /**
             * Get a mod's compatibility group for mod metrics.
             * @param {object} mod The mod to check.
             * @returns {string} The compatibility group (one of 'compatible', 'workaround', 'soon', 'broken', 'abandoned', or 'invalid').
             */
            getCompatibilityGroup: function (mod) {
                var status = mod.LatestCompatibility.Status;
                switch (status) {
                    // obsolete
                    case "abandoned":
                    case "obsolete":
                        return "abandoned";

                    // compatible
                    case "ok":
                    case "optional":
                        return "compatible";

                    // workaround
                    case "workaround":
                    case "unofficial":
                        return "workaround";

                    // soon/broken
                    case "broken":
                        if (mod.SourceUrl)
                            return "soon";
                        else
                            return "broken";

                    default:
                        return "invalid";
                }
            }
        }
    });
    app.applyFilters();
    window.addEventListener("hashchange", function () {
        app.applyFilters();
    });
};