aboutsummaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/utilities.js
blob: 02af8b5d1d7845481358ef66788db3f4db343311 (plain) (blame)
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
import {toaster} from "./base";
import {messages} from "./messages";

export const utilites = {
    dom: {
        restrictInputToNumbers(el, specials, mergeSpecialsWithDefaults) {
            el.addEventListener("keydown", e => {
                const defaultSpecials = [
                    "Backspace",
                    "ArrowLeft",
                    "ArrowRight",
                    "Tab",
                ];
                let keys = specials ?? defaultSpecials;
                if (mergeSpecialsWithDefaults && specials) keys = [...specials, ...defaultSpecials];
                if (keys.indexOf(e.key) !== -1) {
                    return;
                }
                if (isNaN(parseInt(e.key))) {
                    e.preventDefault();
                }
            });
        },
        elementHasFocus: (el) => el === document.activeElement,
        moveFocus(element) {
            if (!element) element = document.getElementsByTagName("body")[0];
            element.focus();
            if (!this.elementHasFocus(element)) {
                element.setAttribute("tabindex", "-1");
                element.focus();
            }
        },
    },
    array: {
        isEmpty: (arr) => arr === undefined || !Array.isArray(arr) || arr.length <= 0,
        removeItemOnce(arr, value) {
            const index = arr.indexOf(value);
            if (index > -1) {
                arr.splice(index, 1);
            }
            return arr;
        },
        pushOrUpdate(arr, obj) {
            const index = arr.findIndex((e) => e.id === obj.id);
            if (index === -1) {
                arr.push(obj);
            } else {
                arr[index] = obj;
            }
        },
        removeItemAll(arr, value) {
            let i = 0;
            while (i < arr.length) {
                if (arr[i] === value) {
                    arr.splice(i, 1);
                } else {
                    ++i;
                }
            }
            return arr;
        },
        removeItemByIdAll(arr, value) {
            let i = 0;
            while (i < arr.length) {
                if (arr[i].id === value.id) {
                    arr.splice(i, 1);
                } else {
                    ++i;
                }
            }
            return arr;
        },
    },
    object: {
        // http://youmightnotneedjquery.com/#deep_extend
        extend(object, ext) {
            ext = ext || {};
            for (let i = 1; i < object.length; i++) {
                let obj = object[i];
                if (!obj) continue;
                for (let key in obj) {
                    if (obj.hasOwnProperty(key)) {
                        if (typeof obj[key] === "object") {
                            if (obj[key] instanceof Array)
                                ext[key] = obj[key].slice(0);
                            else
                                ext[key] = this.extend(ext[key], obj[key]);
                        } else
                            ext[key] = obj[key];
                    }
                }
            }
            return ext;
        },
        isEmptyObj(obj) {
            return obj !== undefined && Object.keys(obj).length > 0;
        },
    },
    validators: {
        isEmail(input) {
            const re = /\S+@\S+\.\S+/;
            return re.test(input);
        },
    },
    getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    isInternetExplorer() {
        const ua = navigator.userAgent;
        return ua.indexOf("MSIE") > -1 || ua.indexOf("Trident") > -1;
    },
    toReadableDateString(date) {
        date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
        return date.toLocaleString("nb-NO");
    },
    toReadableBytes(bytes) {
        const s = ["bytes", "kB", "MB", "GB", "TB", "PB"];
        const e = Math.floor(Math.log(bytes) / Math.log(1024));
        return (bytes / Math.pow(1024, e)).toFixed(2) + " " + s[e];
    },
    toReadablePriceSuffix(suffix) {
        switch (suffix) {
            case 0:
                return ",-";
            case 1:
                return ",- kg";
            case 2:
                return ",- stk";
        }
    },
    setSessionStorageJSON(key, object) {
        sessionStorage.setItem(key, JSON.stringify(object));
    },
    getSessionStorageJSON(key) {
        const dataString = sessionStorage.getItem(key);
        if (dataString === null) return undefined;
        return JSON.parse(dataString);
    },
    setLocalStorageJSON(key, object) {
        localStorage.setItem(key, JSON.stringify(object));
    },
    getLocalStorageJSON(key) {
        const dataString = localStorage.getItem(key);
        if (dataString === null) return undefined;
        return JSON.parse(dataString);
    },
    handleError: (httpResult, fallback) => {
        if (httpResult.headers !== undefined && httpResult.headers.get("ContentType") === "application/json") {
            httpResult.json().then(error => {
                console.error(error);
                toaster.errorObj(utilites.errorOrDefault(error, fallback));
            });
        } else {
            toaster.errorObj(fallback);
        }
    },
    getOrderStatusName: (status) => {
        switch (status) {
            case 0:
                return "Pågående";
            case 1:
                return "Kansellert";
            case 2:
                return "Feilet";
            case 3:
                return "Fullført";
            case 4:
                return "Venter på faktura";
            case 5:
                return "Venter på vipps";
            default:
                return "Ukjent";
        }
    },
    getOrderPaymentName: (status) => {
        switch (status) {
            case 0:
                return "Vipps";
            case 1:
                return "Faktura på mail";
            default:
                return "Ukjent";
        }
    },
    errorOrDefault: (res, fallback) => {
        let title;
        let message;

        if (res.title) title = res.title;
        else if (fallback.title) title = fallback.title;
        else title = messages.unknownError.title;

        if (res.message) message = res.message;
        else if (fallback.message) message = fallback.message;
        else message = messages.unknownError.message;

        return {
            title,
            message,
        };
    },
    // https://stackoverflow.com/a/15757499/11961742
    resolveReferences(json) {
        if (typeof json === "string")
            json = JSON.parse(json);

        const byid = {}, // all objects by id
            refs = []; // references to objects that could not be resolved
        json = (function recurse(obj, prop, parent) {
            if (typeof obj !== "object" || !obj) // a primitive value
                return obj;
            if (Object.prototype.toString.call(obj) === "[object Array]") {
                for (let i = 0; i < obj.length; i++) {
                    // check also if the array element is not a primitive value
                    if (typeof obj[i] !== "object" || !obj[i]) {// a primitive value
                        continue;
                    } else if ("$ref" in obj[i]) {
                        obj[i] = recurse(obj[i], i, obj);
                    } else {
                        obj[i] = recurse(obj[i], prop, obj);
                    }
                }
                return obj;
            }
            if ("$ref" in obj) { // a reference
                let ref = obj.$ref;
                if (ref in byid)
                    return byid[ref];
                // else we have to make it lazy:
                refs.push([parent, prop, ref]);
                return;
            } else if ("$id" in obj) {
                let id = obj.$id;
                delete obj.$id;
                if ("$values" in obj) // an array
                    obj = obj.$values.map(recurse);
                else // a plain object
                    for (let prop in obj)
                        obj[prop] = recurse(obj[prop], prop, obj);
                byid[id] = obj;
            }
            return obj;
        })(json); // run it!

        for (let i = 0; i < refs.length; i++) { // resolve previously unknown references
            let ref = refs[i];
            ref[0][ref[1]] = byid[ref[2]];
            // Notice that this throws if you put in a reference at top-level
        }
        return json;
    },
};