-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-params.js
101 lines (78 loc) · 2.84 KB
/
url-params.js
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
/**
@license
Copyright (c) 2019 elifents. All rights reserved.
*/
import {html, PolymerElement} from '@polymer/polymer/polymer-element';
/**
* @customElement
* @polymer
* @elifent
*/
/*
url-params is a polymer element which is the best way to read params from params directly from url.
If you employ app-route or app-location, these are two way binded elements and soon as value changes it
gets mutated to url, which effects in the desired performance of web app.
url-params is specifically build for multipage app approach, because in single page app everything is passed
to inner page via parent routes.
There are two ways to use url-params.
1. To get tails
2. To read GET params
To read tails, simply give the pattern that the page will be loaded and then tails that need to be read.
eg pattern: cars/:build/:year
eg url: cars/volvo/2017
You can read value in build through params.build
Any tail that comes after /:year will be neglected
To read GET params from url give the patten as '?', all GET will be converted to key value pairs.
eg pattern: '?'
eg url: cars?build=volovo&year=2017
*/
class UrlParams extends PolymerElement {
static get properties() {
return {
pattern: {
type: String,
value: null
},
data:{
type: Object,
value: null,
notify: true,
}
};
}
ready(){
//If pattern to be tested is given as /path/:key/:anotherkey
if(this.pattern.search(':') != -1){
const url = window.location.pathname;
//find the position of : given in pattern
const pos = (this.pattern).indexOf(':')-1;
//now test whether pattern given and substring of url is same or not
if(url.substring(0, pos) == (this.pattern).substring(0, pos)){
//If pattern and url is same then split other parts of pattern and url and then push each as key: value
//on to data.
let keys = (this.pattern).substring(pos);
let values = url.substring(pos);
keys = keys.split('/:');
values = values.split('/');
let data = {};
for(let i=1;i<keys.length;i++){
data[keys[i]] = values[i]
}
this.data = data;
}
//If pattern to be tested is given as ?, then split everything in url as key value pair
}else if(this.pattern == '?'){
const url = window.location.href;
const pos = (url).indexOf('?')+1;
const urlValues = url.substring(pos);
const keyValuePairs = urlValues.split('&');
let data = {};
for(let i=0;i<keyValuePairs.length;i++){
let keyValue = keyValuePairs[i].split('=');
data[keyValue[0]] = keyValue[1]
}
this.data = data;
}
}
}
window.customElements.define('url-params', UrlParams);