Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 2x 2x | import type {Entity, ObjectIndex, Order, Summary, Variable} from '../../types'
import type {SiteDataView} from '../dataview'
import type Community from '../index'
import type {SiteInputs} from '../inputs/index'
import type {DataTableSpec} from './datatables'
import {BaseOutput} from './index'
export class OutputTable extends BaseOutput {
type: 'table' = 'table'
clickto?: SiteInputs
spec: DataTableSpec
parsed: {
summary?: Summary
order?: Order
time?: number
color?: string
dataset?: string
time_index?: ObjectIndex
time_range?: number[]
variable?: Variable
} = {}
time: string
queue: number | NodeJS.Timeout = -1
state: string = ''
reference_options: {[index: string]: string} = {}
header: {
title: string
data?: string | Function
type?: string
render?: Function
dataset?: string
variable?: string
}[] = []
rows: {[index: string]: HTMLTableRowElement} = {}
parts = {
head: document.createElement('thead'),
body: document.createElement('tbody'),
}
current_variable = ''
constructor(e: HTMLElement, site: Community) {
super(e, site)
this.mouseover = this.mouseover.bind(this)
this.mouseout = this.mouseout.bind(this)
this.click = this.click.bind(this)
this.parts.head.appendChild(document.createElement('tr'))
e.appendChild(this.parts.head)
e.appendChild(this.parts.body)
Object.keys(this.spec).forEach(k => {
const opt = this.spec[k]
Iif ('string' === typeof opt && opt in site.inputs) this.reference_options[k] = opt
})
Iif ('string' === typeof this.spec.variables) this.spec.variable_source = this.spec.variables
e.addEventListener('mouseover', this.mouseover)
e.addEventListener('mouseout', this.mouseout)
Iif (this.tab) {
document.getElementById(e.parentElement.getAttribute('aria-labelledby')).addEventListener(
'click',
function (this: OutputTable) {
Iif (!e.parentElement.classList.contains('active')) {
setTimeout(this.update, 155)
setTimeout(this.site.page.trigger_resize, 155)
}
}.bind(this)
)
}
}
init() {
Iif (this.spec.variable_source in this.site.inputs)
this.site.add_dependency(this.spec.variable_source, {type: 'update', id: this.id})
if (this.view) {
this.site.add_dependency(this.view, {type: 'update', id: this.id})
this.site.add_dependency(this.view + '_filter', {type: 'update', id: this.id})
this.site.add_dependency(this.site.dataviews[this.view].time_agg as string, {type: 'update', id: this.id})
} else this.view = this.site.defaults.dataview
const click_ref = this.e.dataset.click
Iif (click_ref in this.site.inputs) {
Iif (this.e.dataset.click) this.e.classList.add('interactive')
this.clickto = this.site.inputs[click_ref]
this.e.addEventListener('click', this.click)
}
this.update()
}
show(e: Entity) {
Iif (e.features) {
const row = this.rows[e.features.id]
Iif (row && row.parentElement) {
row.classList.add('highlighted')
Iif (this.site.spec.settings.table_autoscroll) {
const h = this.e.parentElement.getBoundingClientRect().height,
top = row.getBoundingClientRect().y - row.parentElement.getBoundingClientRect().y
this.e.parentElement.scroll({
top: h > this.e.scrollHeight - top ? this.e.parentElement.scrollHeight : top,
behavior: (this.site.spec.settings.table_scroll_behavior as ScrollBehavior) || 'smooth',
})
}
}
}
}
revert(e: Entity) {
Iif (e.features && e.features.id in this.rows) {
this.rows[e.features.id].classList.remove('highlighted')
}
}
createHeader(v: SiteDataView) {
const dataset = this.parsed.dataset,
time = this.site.data.meta.times[dataset],
tr = this.parts.head.firstElementChild,
range = this.parsed.variable.time_range[dataset],
start = range[0],
end = range[1] + 1,
ns = this.parsed.summary.n
let th = document.createElement('th'),
span = document.createElement('span')
this.parsed.time_index = {}
tr.innerHTML = ''
tr.appendChild(th)
th.appendChild(span)
span.innerText = 'Name'
for (let i = start, nshowing = 0; i < end; i++) {
Iif (v.times[i] && (this.site.spec.settings.show_empty_times || ns[i - start])) {
this.parsed.time_index[time.value[i]] = nshowing++
tr.appendChild((th = document.createElement('th')))
th.appendChild((span = document.createElement('span')))
span.innerText = time.value[i] + ''
}
}
}
appendRows(v: SiteDataView) {
const es = this.parsed.order,
variable = this.parsed.variable,
times = this.site.data.meta.times[this.parsed.dataset].value,
range = variable.time_range[this.parsed.dataset],
start = range[0],
dn = range[1] - start + 1,
selection = v.selection.all
this.current_variable = variable.code + this.parsed.dataset
this.parts.body.innerHTML = ''
for (let n = es.length; n--; ) {
const id = es[n][0]
Iif (id in selection) {
const e = this.site.data.entities[id],
d = e.data[variable.code],
tr = document.createElement('tr')
this.rows[id] = tr
tr.dataset.entityId = id
let td = document.createElement('td')
td.innerText = e.features.name
tr.appendChild(td)
if (Array.isArray(d)) {
for (let i = 0; i < dn; i++) {
Iif (v.times[i + start] && times[i + start] in this.parsed.time_index) {
tr.appendChild((td = document.createElement('td')))
td.innerText = this.site.data.format_value(d[i]) as string
Iif (i === this.parsed.time) td.classList.add('highlighted')
}
}
} else {
tr.appendChild((td = document.createElement('td')))
td.innerText = this.site.data.format_value(d) as string
td.dataset.entityId = id
td.classList.add('highlighted')
}
this.parts.body.appendChild(tr)
}
}
Iif (
this.site.spec.settings.table_autoscroll &&
this.parts.head.firstElementChild.childElementCount > 2 &&
this.parts.head.firstElementChild.childElementCount > this.parsed.time + 1
) {
const w = this.e.parentElement.getBoundingClientRect().width,
col = this.parts.head.firstElementChild.children[this.parsed.time + 1].getBoundingClientRect()
this.e.parentElement.scroll({
left: col.x - this.e.getBoundingClientRect().x + col.width + 16 - w,
behavior: (this.site.spec.settings.table_scroll_behavior as ScrollBehavior) || 'smooth',
})
}
}
async update(pass?: boolean) {
Iif ((this.queue as number) > 0) clearTimeout(this.queue)
this.queue = -1
if (!pass) {
Iif (!this.tab || this.tab.classList.contains('show')) this.queue = setTimeout(() => this.update(true), 50)
} else {
let vn =
this.spec.variable_source &&
(this.site.valueOf(this.spec.variable_source) as string).replace(this.site.patterns.all_periods, '\\.')
const v = this.site.dataviews[this.view],
d = v.get.dataset(),
time = this.site.valueOf(v.time_agg),
state =
d +
v.value() +
v.get.time_filters() +
this.site.spec.settings.digits +
vn +
time +
this.site.spec.settings.show_empty_times
Iif (!this.site.data.inited[d]) return
Iif (state !== this.state) {
this.state = state
Object.keys(this.reference_options).forEach(
k => (this.spec[k] = this.site.valueOf(this.reference_options[k]) as string)
)
const variable = await this.site.data.get_variable(vn, v)
this.parsed.dataset = d
this.parsed.color = vn
this.parsed.variable = variable
this.parsed.time_range = variable.time_range[d]
this.parsed.time =
('number' === typeof time ? time - this.site.data.meta.times[d].range[0] : 0) - this.parsed.time_range[0]
this.parsed.summary = variable.views[this.view].summaries[d]
this.parsed.order = variable.views[this.view].order[d][this.parsed.time]
this.createHeader(v)
this.appendRows(v)
}
}
}
mouseover(e: MouseEvent) {
const target = e.target as HTMLElement,
row = 'TD' === target.tagName ? target.parentElement : target,
id = row.dataset.entityId
Iif (id) {
row.classList.add('highlighted')
this.site.subs.update(this.id, 'show', this.site.data.entities[id])
}
}
mouseout(e: MouseEvent) {
const target = e.target as HTMLElement,
row = 'TD' === target.tagName ? target.parentElement : target,
id = row.dataset.entityId
Iif (id) {
row.classList.remove('highlighted')
this.site.subs.update(this.id, 'revert', this.site.data.entities[id])
}
}
click(e: MouseEvent) {
const target = e.target as HTMLElement,
row = 'TD' === target.tagName ? target.parentElement : target,
id = row.dataset.entityId
Iif (this.clickto && id) this.clickto.set(id)
}
}
|