-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpx-reporter.js
140 lines (140 loc) · 3.94 KB
/
cpx-reporter.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
import { ReporterEvent } from "./reporter.js";
export class CPXReporter extends HTMLElement {
static get tag() { return 'cpx-reporter'; }
get debug() {
return this._debug;
}
set debug(val) {
if (this._debug === val)
return;
this._debug = val;
}
get beat() { return this._beat; }
set beat(val) {
if (this._beat === val)
return;
this._beat = val;
if (this.scope === 'global') {
top.addEventListener(this.beat, this.report);
}
else {
this.addEventListener(this.beat, this.report);
}
this.setAttribute('beat', this._beat);
}
get scope() { return this._scope; }
set scope(val) {
if (this._scope === val)
return;
this._scope = val;
this.setAttribute('scope', this._scope);
}
get event() { return this._event; }
set event(val) {
if (this._event === val)
return;
this._event = val;
this.setAttribute('event', this._event);
this.evt = 'eddl-' + this._event.toLowerCase().split(' ').join('-');
}
get evt() { return this._evt; }
set evt(val) {
if (this._evt === val)
return;
this._evt = val;
this.setAttribute('evt', this._evt);
}
get option() { return this._option; }
set option(val) {
if (this._option === val)
return;
this._option = val;
this.setAttribute('option', this._option);
}
get data() { return this._data; }
set data(val) {
if (this._data === val)
return;
this._data = val;
}
constructor() {
super();
this._debug = null;
this._scope = 'global';
this._data = {};
this.tasks = new Set(['beat', 'event', 'data']);
this._ready = false;
this._emit = 'cpx-report';
this.report = this.report.bind(this);
}
get ready() { return this._ready; }
set ready(val) {
if (this._ready === !!val)
return;
this._ready = !!val;
if (this._ready) {
this.report({});
}
}
get emit() { return this._emit; }
set emit(val) {
if (this._emit === val)
return;
this._emit = val;
}
connectedCallback() {
const dataEle = this.querySelector(':scope > script[type="data"]');
if (dataEle) {
this.data = JSON.parse(dataEle.textContent);
}
const attrs = new Set();
for (let i = 0; i < this.attributes.length; i++) {
attrs.add(this.attributes[i]);
}
this.tasks.forEach(r => {
if (!attrs.has(r)) {
this.tasks.delete(r);
}
});
}
static get observedAttributes() {
return [
"scope",
"data",
"emit",
"event",
"beat",
"debug",
"option"
];
}
attributeChangedCallback(name, oldVal, newVal) {
this[name] = newVal;
if (this.tasks.has(name)) {
this.tasks.delete(name);
}
if (this.getAttribute('auto') === '' && this.tasks.size === 0) {
this.ready = true;
}
}
report(e) {
if (this.debug !== null) {
if (this.debug !== 'verbose') {
console.log('DEBUG ON');
}
else {
console.log('VERBOSE DEBUG ON');
}
}
const detail = e['detail'];
if (this.option) {
this.data = Object.assign(this.data ?? {}, detail[this.option] ?? {});
}
else {
this.data = Object.assign(this.data ?? {}, detail ?? {});
}
this.dispatchEvent(new ReporterEvent(this.event, this.data, this.emit));
this.dispatchEvent(new CustomEvent(this.evt, { bubbles: true, composed: true }));
}
}
window.customElements.define(CPXReporter.tag, CPXReporter);