@@ -1,43 +1,3 @@ | |||||
// utilities | |||||
// generate downloadable csv | |||||
const download = (options={}) => { | |||||
options.filename = options.filename || 'file' | |||||
if (options.type == 'csv') { | |||||
let headers = Object.keys(options.data[0]) | |||||
let body = options.data.map(row => headers.map(key => row[key]).join(',')).join('\n') | |||||
options.data = '\ufeff' + headers.join(',') + '\n' + body | |||||
options.type = 'text/csv' | |||||
} else if (options.type == 'json') { | |||||
} | |||||
let blob = new Blob([options.data], {type: options.type}) | |||||
let url = URL.createObjectURL(blob) | |||||
if (options.timestamp) | |||||
options.filename = options.filename + (new Date()).toLocaleTimeString(undefined, { | |||||
year: "numeric", | |||||
month: "2-digit", | |||||
day: "2-digit", | |||||
hour: "2-digit", | |||||
minute: "2-digit", | |||||
second: "2-digit", | |||||
hour12: false | |||||
}) | |||||
let anchor = document.createElement('a') | |||||
anchor.href = url | |||||
anchor.target = '_blank' | |||||
anchor.download = `${options.filename}.csv` | |||||
anchor.click() | |||||
URL.revokeObjectURL(url) | |||||
anchor.remove() | |||||
} | |||||
// authentication model | // authentication model | ||||
const _make_auth = api_root => { | const _make_auth = api_root => { | ||||
@@ -208,6 +208,44 @@ export default (options={}) => { | |||||
}) | }) | ||||
return _promise | return _promise | ||||
}, | |||||
export(options={}) { | |||||
options.filename = options.filename || _configs.endpoint | |||||
let _data = this.data() | |||||
if (options.type == 'csv') { | |||||
let headers = _configs.selects && _configs.selects.map(select => select.alias || select.label) || Object.keys(_data[0]) | |||||
let body = _data.map(row => headers.map(key => row[key]).join(',')).join('\n') | |||||
_data = '\ufeff' + headers.join(',') + '\n' + body | |||||
options.type = 'text/csv' | |||||
} | |||||
let blob = new Blob([_data], {type: options.type}) | |||||
let url = URL.createObjectURL(blob) | |||||
if (options.timestamp) | |||||
options.filename = options.filename + (new Date()).toLocaleTimeString(undefined, { | |||||
year: "numeric", | |||||
month: "2-digit", | |||||
day: "2-digit", | |||||
hour: "2-digit", | |||||
minute: "2-digit", | |||||
second: "2-digit", | |||||
hour12: false | |||||
}) | |||||
let anchor = document.createElement('a') | |||||
anchor.href = url | |||||
anchor.target = '_blank' | |||||
anchor.download = `${options.filename}.csv` | |||||
anchor.click() | |||||
URL.revokeObjectURL(url) | |||||
anchor.remove() | |||||
} | } | ||||
} | } | ||||