This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
/
index.pug
143 lines (125 loc) · 6.14 KB
/
index.pug
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
extends layout
block content
div(id="app")
p Choose result type:
form
span(:title="pga.titles.position", style="margin-right:10px; cursor: default;")
input(id="position", type="radio", name="endPoint", v-model="pga.resultType", value="position", @change="getResult(value)", checked)
|
label(for="position") Position
|
span(:title="pga.titles.object", style="margin-right:10px; cursor: default;")
input(id="object", type="radio", name="endPoint", v-model="pga.resultType", value="object", @change="getResult(value)")
|
label(for="object") Object
span(:title="pga.titles.cities", style="margin-right:10px; cursor: default;")
input(id="cities", type="radio", name="endPoint", v-model="pga.resultType", value="names", @change="getResult(value)")
|
label(for="cities") Cities
span(:title="pga.titles.distance", style="cursor: default;")
input(id="distance", type="radio", name="endPoint", v-model="pga.resultType", value="distance", :title="pga.titles.distance", @change="getResult(value)")
|
label(for="distance") Distance
select(id="unit", v-if="multiple === true", :title="pga.titles.unit", v-model="pga.distanceUnit", @change="getResult(value)")
option(value="m") Meter
option(value="km") Kilometer
option(value="ft") Feet
option(value="mi") Miles
p(v-if="multiple") Choose two postcodes:
p(v-else) Choose postcode:
multiselect(id="zip", :title="title", v-model="value", :options="options", :multiple="multiple", :max="max", :placeholder="placeholder", @search-change="getOptions", @input="getResult")
br
br
textarea(id="result", readonly="false", placeholder="Result", v-model="pga.result", rows="10", class="multiselect__input multiselect__tags")
script.
'use strict'
var app = new Vue({
el: '#app',
components: { 'Multiselect': VueMultiselect.default },
data () {
return {
value: null,
options: [],
multiple: false,
max: false,
placeholder: 'Enter PLZ or city',
title:'The city name or postcode (PLZ - Postleitzahl) of interest, e.g. 45130',
pga: {
titles: {
position: 'Returns the geoposition for one postcode',
object: 'Returns a JSON object with details on the given postcode',
cities: 'Returns all (city) names for all postcodes within the given lead region, e.g. 45',
distance: 'Returns the linear distance in the given unit between the central points of the two given postcodes',
unit: 'The unit to be used for the distance value (defaults to meter)',
},
proxy: {
path: `${!{JSON.stringify(proxyFullPath)}}`,
qs: {
options: '',
results: '',
},
_csrf: `${!{JSON.stringify(csrfToken)}}`,
},
resultType: 'position',
distanceUnit: 'm',
result: '',
},
};
},
methods: {
getOptions(currentInput) {
if(currentInput.length === 2 && ! isNaN(currentInput)) {
this.pga.proxy.qs.options = `?endPoint=names&leadRegion=${encodeURIComponent(currentInput)}`;
this.sendRequest(this.pga.proxy.qs.options, 'options');
}
if(currentInput.length === 4 && currentInput.match(/[0-9\sa-zA-ZäAöÖüÜß]{4,4}/)) {
this.pga.proxy.qs.options = `?endPoint=names&namesPattern=${encodeURIComponent(currentInput)}`;
this.sendRequest(this.pga.proxy.qs.options, 'options');
}
},
getResult(currentInput) {
this.pga.result = '';
switch(this.pga.resultType) {
case 'position':
case 'object':
this.multiple = false;
this.max = false;
this.pga.proxy.qs.results = (typeof(currentInput) === 'string' || Array.isArray(currentInput)) ? `?endPoint=${this.pga.resultType}&postcode=${encodeURIComponent(currentInput).substring(0,5)}` : '';
break;
case 'names':
this.multiple = false;
this.max = false;
this.pga.proxy.qs.results = (typeof(currentInput) === 'string' || Array.isArray(currentInput)) ? `?endPoint=${this.pga.resultType}&leadRegion=${encodeURIComponent(currentInput).substring(0,2)}` : '';
break;
case 'distance':
this.multiple = true;
this.max = 2;
this.pga.proxy.qs.results = (Array.isArray(currentInput)) ? `?endPoint=${this.pga.resultType}&start=${encodeURIComponent(currentInput[0]).substring(0,5)}&end=${encodeURIComponent(currentInput[1]).substring(0,5)}&unit=${this.pga.distanceUnit}` : '';
break;
default:
this.pga.proxy.qs.results = '';
}
if(typeof(currentInput) === 'string' && this.pga.resultType === 'position' ||
Array.isArray(currentInput) && typeof(currentInput[0]) === 'string' && this.pga.resultType === 'position' ||
typeof(currentInput) === 'string' && this.pga.resultType === 'object' ||
Array.isArray(currentInput) && typeof(currentInput[0]) === 'string' && this.pga.resultType === 'object' ||
typeof(currentInput) === 'string' && this.pga.resultType === 'names' ||
Array.isArray(currentInput) && typeof(currentInput[0]) === 'string' && this.pga.resultType === 'names' ||
Array.isArray(currentInput) && currentInput.length === 2 && this.pga.resultType === 'distance') {
this.sendRequest(this.pga.proxy.qs.results, 'result');
}
},
sendRequest(qs, key) {
var xhr = new XMLHttpRequest();
xhr.open('GET', `${this.pga.proxy.path}${qs}&_csrf=${this.pga.proxy._csrf}`);
xhr.onload = function() {
if(key === 'options') {
app[key] = JSON.parse(xhr.responseText);
} else {
app.pga[key] = xhr.responseText;
}
};
xhr.send();
},
},
});