summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/wwwroot/Content/js/log-parser.js
blob: c730309baf7c5ffdf011db1864872cd20cc64129 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/* globals $, Vue */

/**
 * The global SMAPI module.
 */
var smapi = smapi || {};

/**
 * The Vue app for the current page.
 * @type {Vue}
 */
var app;

// Use a scroll event to apply a sticky effect to the filters / pagination
// bar. We can't just use "position: sticky" due to how the page is structured
// but this works well enough.
$(function () {
    let sticking = false;

    document.addEventListener("scroll", function () {
        const filters = document.getElementById("filters");
        const holder = document.getElementById("filterHolder");
        if (!filters || !holder)
            return;

        const offset = holder.offsetTop;
        const shouldStick = window.pageYOffset > offset;
        if (shouldStick === sticking)
            return;

        sticking = shouldStick;
        if (sticking) {
            holder.style.marginBottom = `calc(1em + ${filters.offsetHeight}px)`;
            filters.classList.add("sticky");
        }
        else {
            filters.classList.remove("sticky");
            holder.style.marginBottom = "";
        }
    });
});

/**
 * Initialize a log parser view on the current page.
 * @param {object} state The state options to use.
 * @returns {void}
 */
smapi.logParser = function (state) {
    if (!state)
        state = {};

    // internal helpers
    const helpers = {
        /**
         * Get a handler which invokes the callback after a set delay, resetting the delay each time it's called.
         * @param {(...*) => void} action The callback to invoke when the delay ends.
         * @param {number} delay The number of milliseconds to delay the action after each call.
         * @returns {() => void}
         */
        getDebouncedHandler(action, delay) {
            let timeoutId = null;

            return function () {
                clearTimeout(timeoutId);

                const args = arguments;
                const self = this;

                timeoutId = setTimeout(
                    function () {
                        action.apply(self, args);
                    },
                    delay
                );
            }
        },

        /**
         * Escape regex special characters in the given string.
         * @param {string} text
         * @returns {string}
         */
        escapeRegex(text) {
            return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
        },

        /**
         * Format a number for the user's locale.
         * @param {number} value The number to format.
         * @returns {string}
         */
        formatNumber(value) {
            const formatter = window.Intl && Intl.NumberFormat && new Intl.NumberFormat();
            return formatter && formatter.format
                ? formatter.format(value)
                : `${value}`;
        },

        /**
         * Try parsing the value as a base-10 integer.
         * @param {string} value The value to parse.
         * @param {number} defaultValue The value to return if parsing fails.
         * @param {() => boolean} criteria An optional callback to check whether a parsed number is valid.
         * @returns {number} The parsed number if it's valid, else the default value.
         */
        tryParseNumber(value, defaultValue, criteria = null) {
            value = parseInt(value, 10);
            return !isNaN(value) && isFinite(value) && (!criteria || criteria(value))
                ? value
                : defaultValue;
        },

        /**
         * Get whether two objects are equivalent based on their top-level properties.
         * @param {Object} left The first value to compare.
         * @param {Object} right The second value to compare.
         * @returns {Boolean}
         */
        shallowEquals(left, right) {
            if (typeof left !== "object" || typeof right !== "object")
                return left === right;

            if (left == null || right == null)
                return left == null && right == null;

            if (Array.isArray(left) !== Array.isArray(right))
                return false;

            const leftKeys = Object.keys(left);
            const rightKeys = Object.keys(right);

            if (leftKeys.length != rightKeys.length)
                return false;

            for (const key of leftKeys) {
                if (!rightKeys.includes(key) || left[key] !== right[key])
                    return false;
            }

            return true;
        }
    };

    // internal event handlers
    const handlers = {
        /**
         * Method called when the user clicks a log line to toggle the visibility of a section. Binding methods is problematic with functional components so we just use the `data-section` parameter and our global reference to the app.
         * @param {any} event
         * @returns {false}
         */
        clickLogLine(event) {
            app.toggleSection(event.currentTarget.dataset.section);
            event.preventDefault();
            return false;
        },

        /**
         * Navigate to the previous page of messages in the log.
         * @returns {void}
         */
        prevPage() {
            app.prevPage();
        },

        /**
         * Navigate to the next page of messages in the log.
         * @returns {void}
         */
        nextPage() {
            app.nextPage();
        },

        /**
         * Handle a click on a page number element.
         * @param {number | Event} event
         * @returns {void}
         */
        changePage(event) {
            if (typeof event === "number")
                app.changePage(event);
            else if (event) {
                const page = parseInt(event.currentTarget.dataset.page);
                if (!isNaN(page) && isFinite(page))
                    app.changePage(page);
            }
        }
    };

    // internal filter counts
    const stats = state.stats = {
        modsShown: 0,
        modsHidden: 0
    };

    // load raw log data
    {
        const dataElement = document.querySelector(state.dataElement);
        state.data = JSON.parse(dataElement.textContent.trim());
        dataElement.remove(); // let browser unload the data element since we won't need it anymore
    }

    // preprocess data for display
    state.messages = state.data.messages || [];
    if (state.messages.length) {
        const levels = state.data.logLevels;
        const sections = state.data.sections;
        const modSlugs = state.data.modSlugs;

        for (let i = 0, length = state.messages.length; i < length; i++) {
            const message = state.messages[i];

            // add unique ID
            message.id = i;

            // add display values
            message.LevelName = levels[message.Level];
            message.SectionName = sections[message.Section];
            message.ModSlug = modSlugs[message.Mod] || message.Mod;

            // For repeated messages, since our <log-line /> component
            // can't return two rows, just insert a second message
            // which will display as the message repeated notice.
            if (message.Repeated > 0 && !message.isRepeated) {
                const repeatNote = {
                    id: i + 1,
                    Level: message.Level,
                    Section: message.Section,
                    Mod: message.Mod,
                    Repeated: message.Repeated,
                    isRepeated: true
                };

                state.messages.splice(i + 1, 0, repeatNote);
                length++;
            }

            // let Vue know the message won't change, so it doesn't need to monitor it
            Object.freeze(message);
        }
    }
    Object.freeze(state.messages);

    // set local time started
    if (state.logStarted)
        state.localTimeStarted = ("0" + state.logStarted.getHours()).slice(-2) + ":" + ("0" + state.logStarted.getMinutes()).slice(-2);

    // add the properties we're passing to Vue
    state.totalMessages = state.messages.length;
    state.filterText = "";
    state.filterRegex = "";
    state.showContentPacks = true;
    state.useHighlight = true;
    state.useRegex = false;
    state.useInsensitive = true;
    state.useWord = false;
    state.perPage = 1000;
    state.page = 1;

    state.defaultMods = { ...state.showMods };
    state.defaultSections = { ...state.showSections };
    state.defaultLevels = { ...state.showLevels };

    // load saved values, if any
    if (localStorage.settings) {
        try {
            const saved = JSON.parse(localStorage.settings);

            state.showContentPacks = saved.showContentPacks ?? state.showContentPacks;
            state.useHighlight = saved.useHighlight ?? state.useHighlight;
            state.useRegex = saved.useRegex ?? state.useRegex;
            state.useInsensitive = saved.useInsensitive ?? state.useInsensitive;
            state.useWord = saved.useWord ?? state.useWord;
        }
        catch (error) {
            // ignore settings if invalid
        }
    }

    // add a number formatter so our numbers look nicer
    Vue.filter("number", handlers.formatNumber);

    // Strictly speaking, we don't need this. However, due to the way our
    // Vue template is living in-page the browser is "helpful" and moves
    // our <log-line />s outside of a basic <table> since obviously they
    // aren't table rows and don't belong inside a table. By using another
    // Vue component, we avoid that.
    Vue.component("log-table", {
        functional: true,
        render: function (createElement, context) {
            return createElement(
                "table",
                {
                    attrs: {
                        id: "log"
                    }
                },
                context.children
            );
        }
    });

    // The <filter-stats /> component draws a nice message under the filters
    // telling a user how many messages match their filters, and also expands
    // on how many of them they're seeing because of pagination.
    Vue.component("filter-stats", {
        functional: true,
        render: function (createElement, context) {
            const props = context.props;
            return createElement(
                "div",
                { class: "stats" },
                [
                    createElement(
                        "abbr",
                        {
                            attrs: {
                                title: "These numbers may be inaccurate when using filtering with sections collapsed."
                            }
                        },
                        [
                            "showing ",
                            createElement("strong", helpers.formatNumber(props.start + 1)),
                            " to ",
                            createElement("strong", helpers.formatNumber(props.end)),
                            " of ",
                            createElement("strong", helpers.formatNumber(props.filtered))
                        ]
                    ),
                    " (total: ",
                    createElement("strong", helpers.formatNumber(props.total)),
                    ")"
                ]
            );
        }
    });

    // Next up we have <pager /> which renders the pagination list. This has a
    // helper method to make building the list of links easier.
    function addPageLink(page, links, visited, createElement, currentPage) {
        if (visited.has(page))
            return;

        if (page > 1 && !visited.has(page - 1))
            links.push(" … ");

        visited.add(page);
        links.push(createElement(
            "span",
            {
                class: page === currentPage ? "active" : null,
                attrs: {
                    "data-page": page
                },
                on: {
                    click: handlers.changePage
                }
            },
            helpers.formatNumber(page)
        ));
    }

    Vue.component("pager", {
        functional: true,
        render: function (createElement, context) {
            const props = context.props;
            if (props.pages <= 1)
                return null;

            const visited = new Set();
            const pageLinks = [];

            for (let i = 1; i <= 2; i++)
                addPageLink(i, pageLinks, visited, createElement, props.page);

            for (let i = props.page - 2; i <= props.page + 2; i++) {
                if (i >= 1 && i <= props.pages)
                    addPageLink(i, pageLinks, visited, createElement, props.page);
            }

            for (let i = props.pages - 2; i <= props.pages; i++) {
                if (i >= 1)
                    addPageLink(i, pageLinks, visited, createElement, props.page);
            }

            return createElement(
                "div",
                { class: "pager" },
                [
                    createElement(
                        "span",
                        {
                            class: props.page <= 1 ? "disabled" : null,
                            on: {
                                click: handlers.prevPage
                            }
                        },
                        "Prev"
                    ),
                    " ",
                    "Page ",
                    helpers.formatNumber(props.page),
                    " of ",
                    helpers.formatNumber(props.pages),
                    " ",
                    createElement(
                        "span",
                        {
                            class: props.page >= props.pages ? "disabled" : null,
                            on: {
                                click: handlers.nextPage
                            }
                        },
                        "Next"
                    ),
                    createElement("div", {}, pageLinks)
                ]
            );
        }
    });

    // Our <log-line /> functional component draws each log line.
    Vue.component("log-line", {
        functional: true,
        props: {
            showScreenId: {
                type: Boolean,
                required: true
            },
            message: {
                type: Object,
                required: true
            },
            highlight: {
                type: Boolean,
                required: false
            }
        },
        render: function (createElement, context) {
            const message = context.props.message;
            const level = message.LevelName;

            if (message.isRepeated)
                return createElement(
                    "tr",
                    {
                        class: [
                            "mod",
                            level,
                            "mod-repeat"
                        ]
                    },
                    [
                        createElement(
                            "td",
                            {
                                attrs: {
                                    colspan: context.props.showScreenId ? 4 : 3
                                }
                            },
                            ""
                        ),
                        createElement("td", `repeats ${message.Repeated} times`)
                    ]
                );

            const events = {};
            let toggleMessage;
            if (message.IsStartOfSection) {
                const visible = message.SectionName && window.app && app.sectionsAllow(message.SectionName);
                events.click = handlers.clickLogLine;
                toggleMessage = visible
                    ? "This section is shown. Click here to hide it."
                    : "This section is hidden. Click here to show it.";
            }

            let text = message.Text;
            const filter = window.app && app.filterRegex;
            if (text && filter && context.props.highlight) {
                text = [];
                let match;
                let consumed = 0;
                let index = 0;
                filter.lastIndex = -1;

                // Our logic to highlight the text is a bit funky because we
                // want to group consecutive matches to avoid a situation
                // where a ton of single characters are in their own elements
                // if the user gives us bad input.

                while (true) {
                    match = filter.exec(message.Text);
                    if (!match)
                        break;

                    // Do we have an area of non-matching text? This
                    // happens if the new match's index is further
                    // along than the last index.
                    if (match.index > index) {
                        // Alright, do we have a previous match? If
                        // we do, we need to consume some text.
                        if (consumed < index)
                            text.push(createElement("strong", {}, message.Text.slice(consumed, index)));

                        text.push(message.Text.slice(index, match.index));
                        consumed = match.index;
                    }

                    index = match.index + match[0].length;
                }

                // Add any trailing text after the last match was found.
                if (consumed < message.Text.length) {
                    if (consumed < index)
                        text.push(createElement("strong", {}, message.Text.slice(consumed, index)));

                    if (index < message.Text.length)
                        text.push(message.Text.slice(index));
                }
            }

            return createElement(
                "tr",
                {
                    class: [
                        "mod",
                        level,
                        message.IsStartOfSection ? "section-start" : null
                    ],
                    attrs: {
                        "data-section": message.SectionName
                    },
                    on: events
                },
                [
                    createElement("td", message.Time),
                    context.props.showScreenId ? createElement("td", message.ScreenId) : null,
                    createElement("td", level.toUpperCase()),
                    createElement(
                        "td",
                        {
                            attrs: {
                                "data-title": message.Mod
                            }
                        },
                        message.Mod
                    ),
                    createElement(
                        "td",
                        [
                            createElement(
                                "span",
                                { class: "log-message-text" },
                                text
                            ),
                            message.IsStartOfSection
                                ? createElement(
                                    "span",
                                    { class: "section-toggle-message" },
                                    [
                                        " ",
                                        toggleMessage
                                    ]
                                )
                                : null
                        ]
                    )
                ]
            );
        }
    });

    // init app
    app = new Vue({
        el: "#output",
        data: state,
        computed: {
            anyModsHidden: function () {
                return stats.modsHidden > 0;
            },
            anyModsShown: function () {
                return stats.modsShown > 0;
            },
            showScreenId: function () {
                return this.data.screenIds.length > 1;
            },

            // Maybe not strictly necessary, but the Vue template is being
            // weird about accessing data entries on the app rather than
            // computed properties.
            hideContentPacks: function () {
                return !state.showContentPacks;
            },

            // Filter messages for visibility.
            filterUseRegex: function () {
                return state.useRegex;
            },
            filterInsensitive: function () {
                return state.useInsensitive;
            },
            filterUseWord: function () {
                return state.useWord;
            },
            shouldHighlight: function () {
                return state.useHighlight;
            },

            filteredMessages: function () {
                if (!state.messages)
                    return [];

                //const start = performance.now();
                const filtered = [];

                let total = 0;

                // This is slightly faster than messages.filter(), which is
                // important when working with absolutely huge logs.
                for (let i = 0, length = state.messages.length; i < length; i++) {
                    const msg = state.messages[i];
                    if (!this.filtersAllow(msg.ModSlug, msg.LevelName))
                        continue;

                    if (this.filterRegex) {
                        const text = msg.Text || (i > 0 ? state.messages[i - 1].Text : null);
                        this.filterRegex.lastIndex = -1;
                        if (!text || !this.filterRegex.test(text))
                            continue;
                    }

                    total++;

                    if (msg.SectionName && !msg.IsStartOfSection && !this.sectionsAllow(msg.SectionName))
                        continue;

                    filtered.push(msg);
                }

                filtered.total = total;

                Object.freeze(filtered);

                //const end = performance.now();
                //console.log(`applied ${(this.useRegex ? "regex" : "text")} filter '${this.filterRegex}' in ${end - start}ms`);

                return filtered;
            },

            // And the rest are about pagination.
            start: function () {
                return (this.page - 1) * state.perPage;
            },
            end: function () {
                return this.start + this.visibleMessages.length;
            },
            totalPages: function () {
                return Math.ceil(this.filteredMessages.length / state.perPage);
            },
            // 
            visibleMessages: function () {
                if (this.totalPages <= 1)
                    return this.filteredMessages;

                const start = this.start;
                const end = start + state.perPage;

                return this.filteredMessages.slice(start, end);
            }
        },
        created: function () {
            window.addEventListener("popstate", () => this.loadFromUrl());
            this.loadFromUrl();
        },
        methods: {
            loadFromUrl: function () {
                const params = new URL(location).searchParams;

                state.perPage = helpers.tryParseNumber(params.get("PerPage"), 1000, n => n > 0);
                this.page = helpers.tryParseNumber(params.get("Page"), 1, n => n > 0);
                state.filterText = params.get("Filter") || "";

                if (params.has("FilterMode")) {
                    const values = params.get("FilterMode").split("~");
                    state.useRegex = values.includes("Regex");
                    state.useInsensitive = !values.includes("Sensitive");
                    state.useWord = values.includes("Word");
                }
                else {
                    state.useRegex = false;
                    state.useInsensitive = true;
                    state.useWord = false;
                }

                if (params.has("Mods")) {
                    const value = params.get("Mods").split("~");
                    for (const key of Object.keys(this.showMods))
                        this.showMods[key] = value.includes(key);

                }
                else {
                    for (const key of Object.keys(this.showMods))
                        this.showMods[key] = state.defaultMods[key];
                }

                if (params.has("Levels")) {
                    const values = params.get("Levels").split("~");
                    for (const key of Object.keys(this.showLevels))
                        this.showLevels[key] = values.includes(key);

                }
                else {
                    const keys = Object.keys(this.showLevels);
                    for (const key of Object.keys(this.showLevels))
                        this.showLevels[key] = state.defaultLevels[key];
                }

                if (params.has("Sections")) {
                    const values = params.get("Sections").split("~");
                    for (const key of Object.keys(this.showSections))
                        this.showSections[key] = values.includes(key);

                }
                else {
                    for (const key of Object.keys(this.showSections))
                        this.showSections[key] = state.defaultSections[key];
                }

                this.updateModFilters();
                this.updateFilterText();
            },

            /**
             * Update the page URL to track non-default filter values.
             */
            updateUrl: function () {
                const url = new URL(location);
                url.searchParams.set("Page", state.page);
                url.searchParams.set("PerPage", state.perPage);

                if (!helpers.shallowEquals(this.showMods, state.defaultMods))
                    url.searchParams.set("Mods", Object.entries(this.showMods).filter(p => p[1]).map(p => p[0]).join("~"));
                else
                    url.searchParams.delete("Mods");

                if (!helpers.shallowEquals(this.showLevels, state.defaultLevels))
                    url.searchParams.set("Levels", Object.entries(this.showLevels).filter(p => p[1]).map(p => p[0]).join("~"));
                else
                    url.searchParams.delete("Levels");

                if (!helpers.shallowEquals(this.showSections, state.defaultSections))
                    url.searchParams.set("Sections", Object.entries(this.showSections).filter(p => p[1]).map(p => p[0]).join("~"));
                else
                    url.searchParams.delete("Sections");

                if (state.filterText?.length) {
                    url.searchParams.set("Filter", state.filterText);

                    const modes = [];
                    if (state.useRegex)
                        modes.push("Regex");
                    if (!state.useInsensitive)
                        modes.push("Sensitive");
                    if (state.useWord)
                        modes.push("Word");

                    if (modes.length)
                        url.searchParams.set("FilterMode", modes.join("~"));
                    else
                        url.searchParams.delete("FilterMode");

                }
                else {
                    url.searchParams.delete("Filter");
                    url.searchParams.delete("FilterMode");
                }

                window.history.replaceState(null, document.title, url.toString()); // use replaceState instead of pushState to avoid filling the tab history with history steps the user probably doesn't care about
            },

            toggleLevel: function (id) {
                if (!state.enableFilters)
                    return;

                this.showLevels[id] = !this.showLevels[id];
                this.updateUrl();
            },

            toggleContentPacks: function () {
                state.showContentPacks = !state.showContentPacks;
                this.saveSettings();
            },

            toggleFilterUseRegex: function () {
                state.useRegex = !state.useRegex;
                this.saveSettings();
                this.updateFilterText();
            },

            toggleFilterInsensitive: function () {
                state.useInsensitive = !state.useInsensitive;
                this.saveSettings();
                this.updateFilterText();
            },

            toggleFilterWord: function () {
                state.useWord = !state.useWord;
                this.saveSettings();
                this.updateFilterText();
            },

            toggleHighlight: function () {
                state.useHighlight = !state.useHighlight;
                this.saveSettings();
            },

            prevPage: function () {
                if (this.page <= 1)
                    return;
                this.page--;
                this.updateUrl();
            },

            nextPage: function () {
                if (this.page >= this.totalPages)
                    return;
                this.page++;
                this.updateUrl();
            },

            changePage: function (page) {
                if (page < 1 || page > this.totalPages)
                    return;
                this.page = page;
                this.updateUrl();
            },

            /**
             * Persist settings into localStorage for use the next time the user opens a log.
             */
            saveSettings: function () {
                localStorage.settings = JSON.stringify({
                    showContentPacks: state.showContentPacks,
                    useRegex: state.useRegex,
                    useInsensitive: state.useInsensitive,
                    useWord: state.useWord,
                    useHighlight: state.useHighlight
                });
            },

            // We don't want to update the filter text often, so use a debounce with
            // a quarter second delay. We basically always build a regular expression
            // since we use it for highlighting, and it also make case insensitivity
            // much easier.
            updateFilterText: helpers.getDebouncedHandler(
                function () {
                    let text = state.filterText;
                    if (!text || !text.length) {
                        this.filterText = "";
                        this.filterRegex = null;
                    }
                    else {
                        if (!state.useRegex)
                            text = helpers.escapeRegex(text);
                        this.filterRegex = new RegExp(
                            state.useWord ? `\\b${text}\\b` : text,
                            state.useInsensitive ? "ig" : "g"
                        );
                    }

                    this.updateUrl();
                },
                250
            ),

            updateModFilters: function () {
                // counts
                stats.modsShown = 0;
                stats.modsHidden = 0;
                for (let key in state.showMods) {
                    if (state.showMods.hasOwnProperty(key)) {
                        if (state.showMods[key])
                            stats.modsShown++;
                        else
                            stats.modsHidden++;
                    }
                }
            },

            toggleMod: function (id) {
                if (!state.enableFilters)
                    return;

                const curShown = this.showMods[id];

                // first filter: only show this by default
                if (stats.modsHidden === 0) {
                    this.hideAllMods();
                    this.showMods[id] = true;
                }

                // unchecked last filter: reset
                else if (stats.modsShown === 1 && curShown)
                    this.showAllMods();

                // else toggle
                else
                    this.showMods[id] = !this.showMods[id];

                this.updateModFilters();
                this.updateUrl();
            },

            toggleSection: function (name) {
                if (!state.enableFilters)
                    return;

                this.showSections[name] = !this.showSections[name];
                this.updateUrl();
            },

            showAllMods: function () {
                if (!state.enableFilters)
                    return;

                for (let key in this.showMods) {
                    if (this.showMods.hasOwnProperty(key)) {
                        this.showMods[key] = true;
                    }
                }
                this.updateModFilters();
                this.updateUrl();
            },

            hideAllMods: function () {
                if (!state.enableFilters)
                    return;

                for (let key in this.showMods) {
                    if (this.showMods.hasOwnProperty(key)) {
                        this.showMods[key] = false;
                    }
                }
                this.updateModFilters();
                this.updateUrl();
            },

            filtersAllow: function (modId, level) {
                return this.showMods[modId] !== false && this.showLevels[level] !== false;
            },

            sectionsAllow: function (section) {
                return this.showSections[section] !== false;
            }
        }
    });

    /**********
    ** Upload form
    *********/
    const input = $("#input");
    if (input.length) {
        // file upload
        smapi.fileUpload({
            chooseFileLink: $("#choose-file-link"),
            chooseFileInput: $("#inputFile"),
            contentArea: input,
            submitButton: $("#submit")
        });
    }
};