LocalStorage.js
2 years ago
LocationManager.js
2 years ago
RenderCurrentPage.js
2 years ago
resolveRestUrl.js
2 years ago
LocationManager.js
81 lines
| 1 | export function getCurrentPath() { |
| 2 | return window.location.pathname; |
| 3 | } |
| 4 | |
| 5 | export function getRawSearch() { |
| 6 | return window.location.search; |
| 7 | } |
| 8 | |
| 9 | export function getSearch() { |
| 10 | const args = {}; |
| 11 | getRawSearch().replace( '?', '' ).split( '&' ).forEach( pair => { |
| 12 | const [ name, value ] = pair.split( '=' ); |
| 13 | |
| 14 | args[ name ] = value; |
| 15 | } ); |
| 16 | |
| 17 | return args; |
| 18 | } |
| 19 | |
| 20 | function prepareQueryArgs( key, value ) { |
| 21 | if ( 'object' !== typeof value ) { |
| 22 | return [ |
| 23 | [ key, value ], |
| 24 | ]; |
| 25 | } |
| 26 | |
| 27 | const response = []; |
| 28 | |
| 29 | for ( let [ valueKey, valueItem ] of Object.entries( value ) ) { |
| 30 | valueKey = `${ key }[${ valueKey }]`; |
| 31 | |
| 32 | response.push( ...prepareQueryArgs( valueKey, valueItem ) ); |
| 33 | } |
| 34 | |
| 35 | return response; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param args {Object} |
| 40 | * @param url {module:url.URL} |
| 41 | * @returns {string} |
| 42 | */ |
| 43 | export function addQueryArgs( args, url ) { |
| 44 | url = new URL( url ); |
| 45 | |
| 46 | const params = new URLSearchParams( url.search ); |
| 47 | const pairs = []; |
| 48 | |
| 49 | for ( const [ key, value ] of Object.entries( args ) ) { |
| 50 | pairs.push( ...prepareQueryArgs( key, value ) ); |
| 51 | } |
| 52 | |
| 53 | for ( const [ key, value ] of pairs ) { |
| 54 | if ( !value ) { |
| 55 | continue; |
| 56 | } |
| 57 | params.append( key, value ); |
| 58 | } |
| 59 | |
| 60 | return url.origin + url.pathname + '?' + params; |
| 61 | } |
| 62 | |
| 63 | export function createPath( queryArgs = {}, hashes = {}, clearArgs = [] ) { |
| 64 | const params = []; |
| 65 | queryArgs = { |
| 66 | ...getSearch(), |
| 67 | ...queryArgs, |
| 68 | }; |
| 69 | |
| 70 | for ( const [ key, value ] of Object.entries( queryArgs ) ) { |
| 71 | if ( clearArgs.includes( key ) ) { |
| 72 | continue; |
| 73 | } |
| 74 | params.push( `${ key }=${ encodeURIComponent( value ) }` ); |
| 75 | } |
| 76 | |
| 77 | const urlParts = [ getCurrentPath(), params.join( '&' ) ]; |
| 78 | |
| 79 | return urlParts.filter( part => part ).join( '?' ); |
| 80 | |
| 81 | } |