-
Notifications
You must be signed in to change notification settings - Fork 0
/
DashboardApp.ts
171 lines (144 loc) · 4.7 KB
/
DashboardApp.ts
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
import Tonic from '@operatortc/tonic';
import {TonicIcon} from './TonicIcon.js'
import TonicLoader from '@operatortc/components/loader/'
import { DataSource, DataSet, BaseDataSet, initDataSets} from './datasource.js';
import { InputFilter, DaterangeFilter, NavTabs, TabItem, AutocompleteFilter } from './filter-input.js';
import { VegaChart } from './vega-tonic.js'
import {DependableComponent, Query} from "./dom.js"
import { DateTime } from "luxon";
import { TaskDialog} from "./ui.js"
function initApp() {
if (window['BASE_URL'])
DependableComponent._base_url = window['BASE_URL'];
initDataSets();
Tonic.add(TonicIcon);
Tonic.add(TonicLoader.TonicLoader, 'tonic-loader');
Tonic.add(AutocompleteFilter);
Tonic.add(InputFilter);
Tonic.add(DaterangeFilter);
Tonic.add(VegaChart);
Tonic.add(DashboardApp);
initDataSets();
const app = <DashboardApp> <unknown>document.getElementsByTagName('dashboard-app')[0];
//console.log('---------------- init ----------------')
}
class DashboardApp extends DependableComponent {
query:Query;
submitListener:any;
popstateListener:any;
constructor() {
super();
this.query = Query.init();
this.addEventListener('change', this.change);
const form = this.querySelector('form');
this.submitListener = (e) => {
this.submit(e);
}
this.popstateListener = (e) => {
window.setTimeout(() => {
this.popstate(e);
}, 0);
}
form.addEventListener('submit', this.submitListener);
window.addEventListener('popstate', this.popstateListener);
this.setState(this.query);
const loaded = (event?) => {
for (const chart of document.querySelectorAll('vega-chart')) {
(chart as unknown as VegaChart).loadcharts();
}
this.loadContent(this.query.url);
};
if (document.readyState === 'loading') { // Loading hasn't finished yet
document.addEventListener('DOMContentLoaded', loaded);
} else { // `DOMContentLoaded` has already fired
loaded();
}
}
update_state_listeners() {
if (this.query.change_count) {
for (const ele of this.querySelectorAll(`[data-state="*"]`) as any as DataSet[]) {
ele.stateChanged('project', this.query.state_changes);
}
}
this.query.reset_changes();
}
change(e) {
if (!e.target.state){
return;
}
this.query.set(e.target.id, e.target.value)
this.debug('changed', e.target.id, e.target.value);
this.update_state_listeners();
}
popstate(e){
this.debug('popstate', e);
if (e.state) {
const url = new URL(location.href)
this.query.url = url;
this.query.state = e.state;
this.setState(e.state);
this.loadContent(url);
}
}
setState(state=null) {
//this.update_state_listeners();
for (const ele of this.querySelectorAll('.filter') as any as InputFilter[]) {
this.debug('setState',ele,this.query);
ele.setState(this.query);
}
}
submit(e?){
if (e) {
e.preventDefault();
e.stopPropagation();
}
for (var child of this.querySelectorAll('.filter') as any as InputFilter[]) {
if (child['modifyState']) {
child.modifyState(this.query);
}
}
const state_changes = this.query.reset_changes();
const invalidated:Set<BaseDataSet> = new Set();
for (const k in state_changes) {
for (const ele of this.querySelectorAll(`[data-state~="${k}"], [data-state="*"]`) as any as DataSet[]) {
ele.stateChanged(k, state_changes);
invalidated.add(ele);
}
}
this.debug('submit', this.query);
history.pushState(this.query.state, window.document.title, this.query.url);
for (const ele of invalidated) {
this.debug('rerender', ele);
ele.reRender();
}
this.loadContent(this.query.url);
return false;
}
async loadContent(url:URL) {
this.debug('loadContent', url);
// for (const ele of document.querySelectorAll('data-set')) {
// const ds = ele as unknown as DataSet;
// ds.reRender();
// }
var reportUrl = new URL(url);
reportUrl.pathname = url.pathname.replace('dashboard/project-metric', 'cycletime/')
this.debug('reportUrl', reportUrl.href);
const response = await fetch(reportUrl.href);
if (response.status === 200) {
const tmpl = document.createElement('template');
const text = await response.text();
tmpl.innerHTML=text;
const newBody = tmpl.content.querySelector('.metrics-report');
const oldBody = document.querySelector('#cycle');
oldBody.replaceChildren(newBody);
}
}
render() {
return this.html`
${this.elements}
`;
}
}
initApp();
export {DashboardApp, InputFilter, VegaChart, DateTime, TonicIcon};
export default DashboardApp;