diff --git a/pkg/lib/cockpit/location.ts b/pkg/lib/cockpit/location.ts index 40ad9c35f97..cc221741e37 100644 --- a/pkg/lib/cockpit/location.ts +++ b/pkg/lib/cockpit/location.ts @@ -16,8 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck import { url_root, calculate_application } from './_internal/location-utils'; @@ -31,9 +29,8 @@ export function get_window_location_hash() { } type Options = { [name: string]: string | Array }; +type Path = string | string[] | Location; -/* eslint prefer-rest-params: 0 */ -/* eslint prefer-spread: 0 */ export class Location { path: string[]; href: string; @@ -81,17 +78,15 @@ export class Location { return out; } - #href_for_go_or_replace(/* ... */) { - let href; - if (arguments.length == 1 && arguments[0] instanceof Location) { - href = String(arguments[0]); - } else if (typeof arguments[0] == "string") { - const options = arguments[1] || { }; - href = this.encode(this.decode(arguments[0], options), options); + #href_for_go_or_replace(path: Path, options?: Options) { + options = options || {}; + if (typeof path === "string") { + return this.encode(this.decode(path, options), options); + } else if (path instanceof Location) { + return path.href; } else { - href = this.encode.apply(this, arguments); + return this.encode(path, options); } - return href; } #decode_path(input: string) { @@ -155,10 +150,10 @@ export class Location { decode(href: string, options: Options) { if (href[0] == '#') - href = href.substr(1); + href = href.substring(1); const pos = href.indexOf('?'); - const first = (pos === -1) ? href : href.substr(0, pos); + const first = (pos === -1) ? href : href.substring(0, pos); const path = this.#decode_path(first); if (pos !== -1 && options) { href.substring(pos + 1).split("&") @@ -168,7 +163,7 @@ export class Location { const value = decodeURIComponent(parts[1]); if (options[name]) { let last = options[name]; - if (!Array.isArray(value)) + if (!Array.isArray(last)) last = options[name] = [last]; last.push(value); } else { @@ -180,19 +175,17 @@ export class Location { return path; } - // replace with string[] | string, options - replace(/* ... */) { + replace(path: Path, options?: Options) { if (this.#hash_changed) return; - const href = this.#href_for_go_or_replace.apply(this, arguments); + const href = this.#href_for_go_or_replace(path, options); window.location.replace(window.location.pathname + '#' + href); } - // replace with string[] | string, options? - go(/* ... */) { + go(path: Path, options?: Options) { if (this.#hash_changed) return; - const href = this.#href_for_go_or_replace.apply(this, arguments); + const href = this.#href_for_go_or_replace(path, options); window.location.hash = '#' + href; }