Skip to content

Commit

Permalink
fix: safe defaults for json fields, reportName, field.format=ago, def…
Browse files Browse the repository at this point in the history
…aultSort
  • Loading branch information
popstas committed Jun 28, 2021
1 parent a65de65 commit 162841d
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export default {
openedPanels: [],
sort: {},
hideTable: false,
reportName: '',
};
},
Expand Down Expand Up @@ -517,6 +518,8 @@ export default {
pageTitle() {
let title = [];
if (this.reportName) title.push(this.reportName);
const reportName = this.$store.getters.shortReportUrl(this.itemsJsonUrl);
title.push(reportName);
Expand Down Expand Up @@ -731,6 +734,26 @@ export default {
return this.$store.getters.getValidateFunc(str);
},
ago(ms) {
// seconds to ms
if (new Date(ms).getFullYear() < 1971) {
ms = ms * 1000;
}
// console.log('ms: ', ms);
// console.log('new Date(ms): ', new Date(ms));
const delta = (Date.now() - ms) / 1000;
const mins = Math.floor(delta % 60);
const hours = Math.floor(delta % 86400 / 3600);
const days = Math.floor(delta / 86400);
const parts = [];
if (days) parts.push(`${days}d`);
if (hours && days < 2) parts.push(`${hours}h`);
if (mins && !days) parts.push(`${mins}m`);
return parts.join(' ');
},
// обёртка над шаблоном колонки
getColumnValue(row, colName) {
// достает значение colName из row, со вложенностью
Expand All @@ -742,14 +765,23 @@ export default {
// шаблоны полей задаются здесь
if (field.href && row.href) {
valueText = `<a href="${row.href}" target="_blank">${valueText}</a>`;
}
if (field.type === 'integer' && valueText) {
valueText = new Intl.NumberFormat().format(valueText);
}
if (field.type === 'timestamp' && valueText) {
const offset = new Date().getTimezoneOffset() * 60000;
valueText = new Date(valueText * 1000 - offset).toISOString();
valueText = valueText.replace('T', ' ').replace(/\..*/, '')
if (field.format == 'ago') {
valueText = this.ago(valueText);
}
else {
const offset = new Date().getTimezoneOffset() * 60000;
valueText = new Date(valueText * 1000 - offset).toISOString();
valueText = valueText.replace('T', ' ').replace(/\..*/, '')
}
}
if (field.type === 'email' && valueText) {
Expand Down Expand Up @@ -875,11 +907,12 @@ export default {
const itemsJson = await this.$axios.$get(itemsJsonUrl);
// console.log('itemsJson.items: ', itemsJson.items);
// console.log('itemsJson.fields: ', itemsJson.fields);
this.$store.commit("columnPresets", itemsJson.columns);
this.$store.commit("filterPresets", itemsJson.filters);
this.$store.commit("tests", itemsJson.fields);
this.$store.commit("scanOptions", itemsJson.scan);
this.$store.dispatch("items", itemsJson.items);
this.$store.commit("columnPresets", itemsJson.columns || {});
this.$store.commit("filterPresets", itemsJson.filters || []);
this.$store.commit("tests", itemsJson.fields || []);
this.$store.commit("scanOptions", itemsJson.scan || {});
this.$store.dispatch("items", itemsJson.items || []);
this.reportName = itemsJson.name || '';
// open details when single row
if (this.itemsLength === 1) {
Expand All @@ -895,6 +928,10 @@ export default {
this.$route.query["q"] = this.defaultFilter.q;
}
if (itemsJson.defaultSort && !this.$route.query["sort"]) {
this.sort = itemsJson.defaultSort;
}
// q
this.$store.dispatch("q", this.$route.query["q"]);
Expand Down

0 comments on commit 162841d

Please sign in to comment.