|
1 | 1 | const Browser = { |
2 | | - canPushState(){ return (typeof (history.pushState) !== "undefined") }, |
| 2 | + canPushState() { |
| 3 | + return typeof history.pushState !== "undefined"; |
| 4 | + }, |
3 | 5 |
|
4 | | - dropLocal(localStorage, namespace, subkey){ |
5 | | - return localStorage.removeItem(this.localKey(namespace, subkey)) |
| 6 | + dropLocal(localStorage, namespace, subkey) { |
| 7 | + return localStorage.removeItem(this.localKey(namespace, subkey)); |
6 | 8 | }, |
7 | 9 |
|
8 | | - updateLocal(localStorage, namespace, subkey, initial, func){ |
9 | | - const current = this.getLocal(localStorage, namespace, subkey) |
10 | | - const key = this.localKey(namespace, subkey) |
11 | | - const newVal = current === null ? initial : func(current) |
12 | | - localStorage.setItem(key, JSON.stringify(newVal)) |
13 | | - return newVal |
| 10 | + updateLocal(localStorage, namespace, subkey, initial, func) { |
| 11 | + const current = this.getLocal(localStorage, namespace, subkey); |
| 12 | + const key = this.localKey(namespace, subkey); |
| 13 | + const newVal = current === null ? initial : func(current); |
| 14 | + localStorage.setItem(key, JSON.stringify(newVal)); |
| 15 | + return newVal; |
14 | 16 | }, |
15 | 17 |
|
16 | | - getLocal(localStorage, namespace, subkey){ |
17 | | - return JSON.parse(localStorage.getItem(this.localKey(namespace, subkey))) |
| 18 | + getLocal(localStorage, namespace, subkey) { |
| 19 | + return JSON.parse(localStorage.getItem(this.localKey(namespace, subkey))); |
18 | 20 | }, |
19 | 21 |
|
20 | | - updateCurrentState(callback){ |
21 | | - if(!this.canPushState()){ return } |
22 | | - history.replaceState(callback(history.state || {}), "", window.location.href) |
| 22 | + updateCurrentState(callback) { |
| 23 | + if (!this.canPushState()) { |
| 24 | + return; |
| 25 | + } |
| 26 | + history.replaceState( |
| 27 | + callback(history.state || {}), |
| 28 | + "", |
| 29 | + window.location.href, |
| 30 | + ); |
23 | 31 | }, |
24 | 32 |
|
25 | | - pushState(kind, meta, to){ |
26 | | - if(this.canPushState()){ |
27 | | - if(to !== window.location.href){ |
28 | | - if(meta.type == "redirect" && meta.scroll){ |
| 33 | + pushState(kind, meta, to) { |
| 34 | + if (this.canPushState()) { |
| 35 | + if (to !== window.location.href) { |
| 36 | + if (meta.type == "redirect" && meta.scroll) { |
29 | 37 | // If we're redirecting store the current scrollY for the current history state. |
30 | | - const currentState = history.state || {} |
31 | | - currentState.scroll = meta.scroll |
32 | | - history.replaceState(currentState, "", window.location.href) |
| 38 | + const currentState = history.state || {}; |
| 39 | + currentState.scroll = meta.scroll; |
| 40 | + history.replaceState(currentState, "", window.location.href); |
33 | 41 | } |
34 | 42 |
|
35 | | - delete meta.scroll // Only store the scroll in the redirect case. |
36 | | - history[kind + "State"](meta, "", to || null) // IE will coerce undefined to string |
| 43 | + delete meta.scroll; // Only store the scroll in the redirect case. |
| 44 | + history[kind + "State"](meta, "", to || null); // IE will coerce undefined to string |
37 | 45 |
|
38 | 46 | // when using navigate, we'd call pushState immediately before patching the DOM, |
39 | 47 | // jumping back to the top of the page, effectively ignoring the scrollIntoView; |
40 | 48 | // therefore we wait for the next frame (after the DOM patch) and only then try |
41 | 49 | // to scroll to the hashEl |
42 | 50 | window.requestAnimationFrame(() => { |
43 | | - const hashEl = this.getHashTargetEl(window.location.hash) |
44 | | - |
45 | | - if(hashEl){ |
46 | | - hashEl.scrollIntoView() |
47 | | - } else if(meta.type === "redirect"){ |
48 | | - window.scroll(0, 0) |
| 51 | + const hashEl = this.getHashTargetEl(window.location.hash); |
| 52 | + |
| 53 | + if (hashEl) { |
| 54 | + hashEl.scrollIntoView(); |
| 55 | + } else if (meta.type === "redirect") { |
| 56 | + window.scroll(0, 0); |
49 | 57 | } |
50 | | - }) |
| 58 | + }); |
51 | 59 | } |
52 | 60 | } else { |
53 | | - this.redirect(to) |
| 61 | + this.redirect(to); |
54 | 62 | } |
55 | 63 | }, |
56 | 64 |
|
57 | | - setCookie(name, value, maxAgeSeconds){ |
58 | | - const expires = typeof(maxAgeSeconds) === "number" ? ` max-age=${maxAgeSeconds};` : "" |
59 | | - document.cookie = `${name}=${value};${expires} path=/` |
| 65 | + setCookie(name, value, maxAgeSeconds) { |
| 66 | + const expires = |
| 67 | + typeof maxAgeSeconds === "number" ? ` max-age=${maxAgeSeconds};` : ""; |
| 68 | + document.cookie = `${name}=${value};${expires} path=/`; |
60 | 69 | }, |
61 | 70 |
|
62 | | - getCookie(name){ |
63 | | - return document.cookie.replace(new RegExp(`(?:(?:^|.*;\s*)${name}\s*\=\s*([^;]*).*$)|^.*$`), "$1") |
| 71 | + getCookie(name) { |
| 72 | + return document.cookie.replace( |
| 73 | + new RegExp(`(?:(?:^|.*;\s*)${name}\s*\=\s*([^;]*).*$)|^.*$`), |
| 74 | + "$1", |
| 75 | + ); |
64 | 76 | }, |
65 | 77 |
|
66 | | - deleteCookie(name){ |
67 | | - document.cookie = `${name}=; max-age=-1; path=/` |
| 78 | + deleteCookie(name) { |
| 79 | + document.cookie = `${name}=; max-age=-1; path=/`; |
68 | 80 | }, |
69 | 81 |
|
70 | | - redirect(toURL, flash){ |
71 | | - if(flash){ this.setCookie("__phoenix_flash__", flash, 60) } |
72 | | - window.location.href = toURL |
| 82 | + redirect(toURL, flash) { |
| 83 | + if (flash) { |
| 84 | + this.setCookie("__phoenix_flash__", flash, 60); |
| 85 | + } |
| 86 | + window.location.href = toURL; |
73 | 87 | }, |
74 | 88 |
|
75 | | - localKey(namespace, subkey){ return `${namespace}-${subkey}` }, |
| 89 | + localKey(namespace, subkey) { |
| 90 | + return `${namespace}-${subkey}`; |
| 91 | + }, |
76 | 92 |
|
77 | | - getHashTargetEl(maybeHash){ |
78 | | - const hash = maybeHash.toString().substring(1) |
79 | | - if(hash === ""){ return } |
80 | | - return document.getElementById(hash) || document.querySelector(`a[name="${hash}"]`) |
81 | | - } |
82 | | -} |
| 93 | + getHashTargetEl(maybeHash) { |
| 94 | + const hash = maybeHash.toString().substring(1); |
| 95 | + if (hash === "") { |
| 96 | + return; |
| 97 | + } |
| 98 | + return ( |
| 99 | + document.getElementById(hash) || |
| 100 | + document.querySelector(`a[name="${hash}"]`) |
| 101 | + ); |
| 102 | + }, |
| 103 | +}; |
83 | 104 |
|
84 | | -export default Browser |
| 105 | +export default Browser; |
0 commit comments