| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { onWillRender, reactive, useState } from "@odoo/owl"; |
| 4 |
import { isIterable } from "@web/../lib/hoot-dom/hoot_dom_utils"; |
| 5 |
import { debounce, ensureArray, isNil } from "../hoot_utils"; |
| 6 |
import { CONFIG_KEYS, CONFIG_SCHEMA, FILTER_KEYS, FILTER_SCHEMA } from "./config"; |
| 7 |
|
| 8 |
/** |
| 9 |
* @typedef {{ |
| 10 |
* debug?: boolean; |
| 11 |
* ignore?: boolean; |
| 12 |
* }} CreateUrlFromIdOptions |
| 13 |
* |
| 14 |
* @typedef {typeof import("./config").DEFAULT_CONFIG} DEFAULT_CONFIG |
| 15 |
* |
| 16 |
* @typedef {typeof import("./config").DEFAULT_FILTERS} DEFAULT_FILTERS |
| 17 |
*/ |
| 18 |
|
| 19 |
//----------------------------------------------------------------------------- |
| 20 |
// Global |
| 21 |
//----------------------------------------------------------------------------- |
| 22 |
|
| 23 |
const { |
| 24 |
history, |
| 25 |
location, |
| 26 |
Object: { entries: $entries }, |
| 27 |
Set, |
| 28 |
URIError, |
| 29 |
URL, |
| 30 |
URLSearchParams, |
| 31 |
} = globalThis; |
| 32 |
|
| 33 |
//----------------------------------------------------------------------------- |
| 34 |
// Internal |
| 35 |
//----------------------------------------------------------------------------- |
| 36 |
|
| 37 |
const debouncedUpdateUrl = debounce(function updateUrl() { |
| 38 |
const url = createUrl({}); |
| 39 |
url.search = ""; |
| 40 |
for (const [key, value] of $entries(urlParams)) { |
| 41 |
if (isIterable(value)) { |
| 42 |
for (const val of value) { |
| 43 |
if (val) { |
| 44 |
url.searchParams.append(key, val); |
| 45 |
} |
| 46 |
} |
| 47 |
} else if (value) { |
| 48 |
url.searchParams.set(key, String(value)); |
| 49 |
} |
| 50 |
} |
| 51 |
const path = url.toString(); |
| 52 |
history.replaceState({ path }, "", path); |
| 53 |
}, 20); |
| 54 |
|
| 55 |
//----------------------------------------------------------------------------- |
| 56 |
// Exports |
| 57 |
//----------------------------------------------------------------------------- |
| 58 |
|
| 59 |
/** |
| 60 |
* @param {Partial<DEFAULT_CONFIG & DEFAULT_FILTERS>} params |
| 61 |
*/ |
| 62 |
export function createUrl(params) { |
| 63 |
const url = new URL(location.href); |
| 64 |
for (const key in params) { |
| 65 |
url.searchParams.delete(key); |
| 66 |
if (!CONFIG_KEYS.includes(key) && !FILTER_KEYS.includes(key)) { |
| 67 |
throw new URIError(`unknown URL param key: "${key}"`); |
| 68 |
} |
| 69 |
if (isIterable(params[key])) { |
| 70 |
for (const value of params[key]) { |
| 71 |
url.searchParams.append(key, value); |
| 72 |
} |
| 73 |
} else if (!isNil(params[key])) { |
| 74 |
url.searchParams.set(key, params[key]); |
| 75 |
} |
| 76 |
} |
| 77 |
return url; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param {Record<keyof DEFAULT_FILTERS, string | Iterable<string>>} specs |
| 82 |
* @param {CreateUrlFromIdOptions} [options] |
| 83 |
*/ |
| 84 |
export function createUrlFromId(specs, options) { |
| 85 |
const nextParams = {}; |
| 86 |
for (const key of FILTER_KEYS) { |
| 87 |
nextParams[key] = new Set(ensureArray((options?.ignore && urlParams[key]) || [])); |
| 88 |
} |
| 89 |
for (const [type, id] of $entries(specs)) { |
| 90 |
const ids = ensureArray(id); |
| 91 |
switch (type) { |
| 92 |
case "id": { |
| 93 |
if (options?.ignore) { |
| 94 |
for (const id of ids) { |
| 95 |
const exludedId = EXCLUDE_PREFIX + id; |
| 96 |
if (nextParams.id.has(exludedId) || urlParams.id?.includes(exludedId)) { |
| 97 |
nextParams.id.delete(exludedId); |
| 98 |
} else { |
| 99 |
nextParams.id.add(exludedId); |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
for (const id of ids) { |
| 104 |
nextParams.id.add(id); |
| 105 |
} |
| 106 |
} |
| 107 |
break; |
| 108 |
} |
| 109 |
case "tag": { |
| 110 |
if (options?.ignore) { |
| 111 |
for (const id of ids) { |
| 112 |
const exludedId = EXCLUDE_PREFIX + id; |
| 113 |
if (urlParams.tag?.includes(exludedId)) { |
| 114 |
nextParams.tag.delete(exludedId); |
| 115 |
} else { |
| 116 |
nextParams.tag.add(exludedId); |
| 117 |
} |
| 118 |
} |
| 119 |
} else { |
| 120 |
for (const id of ids) { |
| 121 |
nextParams.tag.add(id); |
| 122 |
} |
| 123 |
} |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
for (const key in nextParams) { |
| 130 |
if (!nextParams[key].size) { |
| 131 |
nextParams[key] = null; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
nextParams.debugTest = options?.debug ? true : null; |
| 136 |
|
| 137 |
return createUrl(nextParams); |
| 138 |
} |
| 139 |
|
| 140 |
export function refresh() { |
| 141 |
history.go(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param {Partial<DEFAULT_CONFIG & DEFAULT_FILTERS>} params |
| 146 |
*/ |
| 147 |
export function setParams(params) { |
| 148 |
for (const [key, value] of $entries(params)) { |
| 149 |
if (!CONFIG_KEYS.includes(key) && !FILTER_KEYS.includes(key)) { |
| 150 |
throw new URIError(`unknown URL param key: "${key}"`); |
| 151 |
} |
| 152 |
if (value) { |
| 153 |
urlParams[key] = isIterable(value) ? [...value] : value; |
| 154 |
} else { |
| 155 |
delete urlParams[key]; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
debouncedUpdateUrl(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @param {...(keyof DEFAULT_CONFIG | keyof DEFAULT_FILTERS | "*")} keys |
| 164 |
*/ |
| 165 |
export function subscribeToURLParams(...keys) { |
| 166 |
const state = useState(urlParams); |
| 167 |
if (keys.length) { |
| 168 |
const observedKeys = keys.includes("*") ? [...CONFIG_KEYS, ...FILTER_KEYS] : keys; |
| 169 |
onWillRender(() => observedKeys.forEach((key) => state[key])); |
| 170 |
} |
| 171 |
return state; |
| 172 |
} |
| 173 |
|
| 174 |
export const EXCLUDE_PREFIX = "-"; |
| 175 |
|
| 176 |
/** @type {Partial<DEFAULT_CONFIG & DEFAULT_FILTERS>} */ |
| 177 |
export const urlParams = reactive({}); |
| 178 |
|
| 179 |
// Update URL params immediatly |
| 180 |
|
| 181 |
const searchParams = new URLSearchParams(location.search); |
| 182 |
const searchKeys = new Set(searchParams.keys()); |
| 183 |
for (const [configKey, { aliases, parse }] of $entries({ |
| 184 |
...CONFIG_SCHEMA, |
| 185 |
...FILTER_SCHEMA, |
| 186 |
})) { |
| 187 |
const configKeys = [configKey, ...(aliases || [])]; |
| 188 |
/** @type {string[]} */ |
| 189 |
const values = []; |
| 190 |
let hasKey = false; |
| 191 |
for (const key of configKeys) { |
| 192 |
if (searchKeys.has(key)) { |
| 193 |
hasKey = true; |
| 194 |
values.push(...searchParams.getAll(key).filter(Boolean)); |
| 195 |
} |
| 196 |
} |
| 197 |
if (hasKey) { |
| 198 |
urlParams[configKey] = parse(values); |
| 199 |
} else { |
| 200 |
delete urlParams[configKey]; |
| 201 |
} |
| 202 |
} |