-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
213 lines (180 loc) · 6.08 KB
/
scripts.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(async () => {
const container = document.getElementById('param-table-body')
const apiUrl = new URL('https://datosabiertos.bogota.gov.co/api/3/action/datastore_search?')
const apiResourceCodes = 'https://datosabiertos.bogota.gov.co/api/3/action/datastore_search?resource_id=_table_metadata'
const urlField = document.getElementById('url-field')
const copyBtn = document.getElementById('copy-button')
const resourceIdDoc = {key: 'resource_id', type: 'string', defaultValue: '', description: 'Código del recurso que se desea consultar'}
const docs = [
{key: 'distinct',
type: 'boolean',
defaultValue: 'true',
description: 'Obtener sólo registros distintos',
example: 'true'},
{key: 'fields',
type: 'string',
defaultValue: '',
description: 'Especificar los campos a obtener',
example: ''},
{key: 'filters',
type: 'string',
defaultValue: '',
description: 'Condiciones de búsqueda',
example: '{"key1": "a", "key2": "b"}'},
{key: 'include_total',
type: 'boolean',
defaultValue: 'true',
description: 'Obtener el número de registros',
example: 'true'},
{key: 'language',
type: 'string',
defaultValue: 'english',
description: 'Especificar el lengaje de la consulta',
example: 'english'},
{key: 'limit',
type: 'integer',
defaultValue: '100',
description: 'Número máximo de registros que se desea obtener',
example: '100'},
{key: 'offset',
type: 'integer',
defaultValue: '',
description: '<<Offset this number of rows>>',
example: ''},
{key: 'plain',
type: 'boolean',
defaultValue: 'true',
description: 'Especificar si se desea hacer la consulta en texto plano',
example: 'true'},
{key: 'q',
type: 'string',
defaultValue: '',
description: 'Buscar el texto dado en todas las filas o especificar pares de clave y valor para buscar en campos específicos',
example: 'text | {"key": "value"}'},
{key: 'records_format',
type: 'string',
defaultValue: 'objects',
description: 'Especificar el formato en que se desea obtener los datos',
example: 'objects | lists | csv | tsv'},
{key: 'sort',
type: 'string',
defaultValue: '',
description: 'comma separated field names with ordering',
example: 'field, field2 desc'},
]
const resourceList = [
{label: 'infectados Covid19', name: 'b64ba3c4-9e41-41b8-b3fd-2da21d627558'}
]
const createTableRow = ({key, type, defaultValue, description, example}, isResource) => {
// V A L I D A T E
key = typeof key === "string" ? key : ''
type = typeof type === "string" ? type : ''
defaultValue = typeof defaultValue === "string" ? defaultValue : ''
description = typeof description === "string" ? description : ''
example = typeof example === "string" ? example : ''
// D E C L A R E
let row, opt, checkField, keyField, valueField, typeField, defaultField, descriptionField
// C H E C K
checkField = document.createElement('input')
checkField.type = 'checkbox'
// K E Y
keyField = document.createElement('td')
keyField.innerText = key
// V A L U E
if (isResource) {
valueField = createSourceSelect(resourceList)
valueField.value = valueField[0].value
} else {
valueField = document.createElement('input')
valueField.type = 'text'
valueField.value = defaultValue
valueField.placeholder = example
}
// T Y P E
typeField = document.createElement('td')
typeField.innerText = type
// D E F A U L T
defaultField = document.createElement('td')
defaultField.innerText = defaultValue
// D E S C R I P T I O N
descriptionField = document.createElement('td')
descriptionField.innerText = description
if (isResource) {
checkField.checked = true
checkField.disabled = true
checkField.addEventListener('change', handleResourceChange)
valueField.addEventListener('change', handleResourceChange)
} else {
checkField.addEventListener('change', handleInputsChange)
valueField.addEventListener('change', handleInputsChange)
}
row = document.createElement('tr')
row.dataset.key = key
row.appendChild(document.createElement('td').appendChild(checkField).parentElement)
row.appendChild(keyField)
row.appendChild(document.createElement('td').appendChild(valueField).parentElement)
row.appendChild(typeField)
row.appendChild(defaultField)
row.appendChild(descriptionField)
return row
}
const createSourceSelect = optionList => {
const select = document.createElement('select')
if (!optionList ) {
select.disabled = true
return select
}
let opt
for (const iterator of optionList) {
opt = document.createElement('option')
opt.value = iterator.name
opt.innerText = iterator.label
select.appendChild(opt)
}
return select
}
// error here
const getResourceCodes = async () => {
const reqHead = new Headers()
const reqBody = { method: 'GET', headers: reqHead, mode: 'cors', cache: 'default' }
const res = await fetch(apiResourceCodes, reqBody)
const data = await res.json()
console.log(data)
}
const fillTableBody = () => {
let row = createTableRow(resourceIdDoc, true)
container.appendChild(row)
for(const param of docs) {
row = createTableRow(param)
container.appendChild(row)
}
}
const handleCopyBtnClick = e => {
urlField.select()
document.execCommand('copy')
}
const handleResourceChange = e => {
let row = e.target.parentElement.parentElement
let key = row.dataset.key
let value = row.querySelector('select').value.trim() || 'none'
apiUrl.searchParams.set(key, value)
urlField.value = apiUrl.toString()
}
const handleInputsChange = e => {
let row = e.target.parentElement.parentElement
let key = row.dataset.key
let checked = row.querySelector('input[type=checkbox]').checked
let value = row.querySelector('input[type=text]').value.trim()
if (checked && value) {
apiUrl.searchParams.set(key, value)
} else {
apiUrl.searchParams.delete(key)
}
urlField.value = apiUrl.toString()
}
// A U T O E X E C U T E
fillTableBody()
apiUrl.searchParams.append('resource_id', document.querySelector('select').value)
urlField.value = apiUrl.toString()
copyBtn.addEventListener('click', handleCopyBtnClick)
})()