-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoiniciativas-widget.js
163 lines (142 loc) · 5.67 KB
/
geoiniciativas-widget.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
var $GEO = {tab: []};
let checkIfURL = function(str){
var expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
if (str.match(regex)) {
return true;
} else {
return false;
}
}
let getSpreadsheetTab = function(spreadsheetID, tabID){
const spreadsheetURL = `https://spreadsheets.google.com/feeds/list/${spreadsheetID}/${tabID}/public/values?alt=json`;
return fetch(spreadsheetURL).then(response => {
responseCopy = response.clone()
responseCopy.json().then(data => $GEO['tab'][tabID] = data );
return response.json()
});
}
let downloadTab = function(spreadsheetID, tabID){
getSpreadsheetTab(spreadsheetID, tabID).then(data => {
let cleanData = getGsheetsJSON(data);
let csv = JSONtoCSV(cleanData);
let bodyEl = document.getElementsByTagName("body")[0];
const aNode= document.createElement('a');
aNode.style = "display:none";
bodyEl.appendChild(aNode)
aNode.setAttribute("href","data:text/csv,"+ encodeURIComponent(csv) );
aNode.setAttribute("download", `${data.feed.title.$t}.csv`);
aNode.click();
});
}
let getGsheetsJSON = function(data){
return data.feed.entry.map(elem => {
let obj = {};
for (var prop in elem) {
const parts = prop.split("gsx$");
if(parts.length > 1){
obj[parts[1]] = elem[prop]['$t'];
}
}
return obj;
});
};
let JSONtoCSV = function(json){
const items = json
const replacer = (key, value) => value === null ? '' : value
const header = Object.keys(json[0])
const csv = [
header.join(','),
...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
].join('\r\n')
return csv;
}
let getSpreedsheetMetadata = function(spreadsheetID){
const spreadsheetURL = `https://spreadsheets.google.com/feeds/worksheets/${spreadsheetID}/public/full?alt=json`;
return fetch(spreadsheetURL).then(response => {
responseCopy = response.clone()
responseCopy.json().then(data => $GEO['metadata'] = data );
return response.json()
});
}
let getSheetByName = function(spreadsheetID, tabName){
return getSpreedsheetMetadata(spreadsheetID).then(data => {
const numTabs = data.feed.openSearch$totalResults.$t;
return new Promise(function(resolve, reject) {
recursiveSearch(spreadsheetID, tabName, numTabs, resolve, reject);
});
})
}
let recursiveSearch = function(spreadsheetID, tabName, numTabs, resolve, reject, tabIDCursor = 1){
if (tabIDCursor <= numTabs) {
return getSpreadsheetTab(spreadsheetID, tabIDCursor).then(data => {
if(data.feed.title.$t == tabName){
return resolve({cells: getGsheetsJSON(data), tabID: tabIDCursor})
}else{
return recursiveSearch(spreadsheetID, tabName, numTabs, resolve, reject, tabIDCursor+1);
}
})
}
else {
reject(`Tab name: ${tabName} doesn't exists, check: https://docs.google.com/spreadsheets/d/${spreadsheetID}/edit#gid=0`);
}
}
let getQueryParam = function (str, param) {
var rx = new RegExp("[?&]" + param + "=([^&]+).*$");
var returnVal = str.match(rx);
return returnVal === null ? "" : returnVal[1];
}
let findTabGID = function(tabIndex){
const result = $GEO.metadata.feed.entry[tabIndex].link.filter(url => getQueryParam(url.href, 'gid') != '' );
if(result.length > 0){
return getQueryParam(result[0].href, "gid")
}
console.error(`gid not found for ${tabIndex}`)
}
const renderGeoIniciativasWidget = function(){
document.querySelectorAll('.geoiniciativas-widget').forEach(elem => {
getSheetByName(elem.dataset.sheetid, elem.dataset.tab).then(data => {
const tabGID = findTabGID(data.tabID-1);
const cells = data.cells;
const keys = Object.keys(cells[0]);
let tableHTML = `<table class="giw"><thead><tr>`;
keys.forEach(key => tableHTML += `<th>${key.charAt(0).toUpperCase() + key.slice(1)}</th>`);
tableHTML += `</tr></thead><tbody>`;
cells.forEach(row => {
tableHTML += '<tr>';
for (const [key, value] of Object.entries(row)) {
if(checkIfURL(value)){
tableHTML += `<td><a href="${value}">URL</a></td>`
}else{
tableHTML += `<td>${value}</td>`
}
}
tableHTML += '</tr>';
})
tableHTML += `
</tbody></table>
<p class="geoiniciativas-links">
<a href="#" onclick="downloadTab('${elem.dataset.sheetid}', ${data.tabID}); return false;">
Descargar CSV
</a>
|
<a href="https://docs.google.com/spreadsheets/d/${elem.dataset.sheetid}/edit#gid=${tabGID}">
Abrir hoja de cálculo
</a>
|
<a href="https://github.com/Geo-Developers/geoiniciativas-widget#c%C3%B3mo-usarlo">
Embeber esta tabla
</a>
|
<a href="https://github.com/Geo-Developers/geoiniciativas-widget#contribuciones">
Reportar / Colaborar
</a>
</p>
`;
elem.innerHTML = tableHTML
}).catch(err => {
console.error(err)
})
});
}
renderGeoIniciativasWidget();