Skip to content

Commit 06efe05

Browse files
megothArne Hassel
authored andcommitted
Migrated matrix module to TypeScript
1 parent 7f88fda commit 06efe05

4 files changed

Lines changed: 78 additions & 70 deletions

File tree

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ import { create } from './create/index'
4747
import icons from './iconBase'
4848
// @ts-ignore
4949
import log from './log'
50-
// @ts-ignore
51-
import matrix from './matrix'
50+
import { matrix } from './matrix/index'
5251
// @ts-ignore
5352
import media from './media-capture'
5453
// @ts-ignore

src/matrix/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {
2+
matrixForQuery
3+
} from './matrix'
4+
5+
export const matrix = {
6+
matrixForQuery
7+
}

src/matrix.js renamed to src/matrix/matrix.ts

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* global $rdf */
21
// Build a 2D matrix of values
32
//
43
// dom AKA document
@@ -10,8 +9,8 @@
109
//
1110
// Options:
1211
// cellFunction(td, x, y, value) fill the TD element of a single cell
13-
// xDecreasing set true for x axis to be in decreasiong order.
14-
// yDecreasing set true for y axis to be in decreasiong order.
12+
// xDecreasing set true for x axis to be in decreasing order.
13+
// yDecreasing set true for y axis to be in decreasing order.
1514
// set_x array of X values to be define initial rows (order irrelevant)
1615
// set_y array of Y values to be define initial columns
1716
//
@@ -20,38 +19,31 @@
2019
// Extra rows and columns are inserted as needed to hold new data points
2120
// matrix.refresh() will re-run the query and adjust the display
2221

23-
var UI = {
24-
icons: require('./iconBase'),
25-
log: require('./log'),
26-
ns: require('./ns'),
27-
pad: require('./'),
28-
rdf: require('rdflib'),
29-
store: require('./store'),
30-
widgets: require('./widgets')
31-
}
32-
33-
const utils = require('./utils')
34-
const kb = UI.store
35-
36-
module.exports.matrixForQuery = function (
37-
dom,
38-
query,
39-
vx,
40-
vy,
41-
vvalue,
42-
options,
43-
whenDone
22+
import utils from '../utils'
23+
import kb from '../store'
24+
import * as $rdf from 'rdflib'
25+
import { MatrixOptions } from './types'
26+
27+
export function matrixForQuery (
28+
dom: HTMLDocument,
29+
query: $rdf.Query,
30+
vx: $rdf.Variable,
31+
vy: $rdf.Variable,
32+
vvalue: $rdf.Variable,
33+
options: MatrixOptions,
34+
whenDone: Function
4435
) {
45-
var matrix = dom.createElement('table')
46-
var header = dom.createElement('tr')
47-
var corner = header.appendChild(dom.createElement('td'))
36+
// @@ TODO Remove need to cast to any
37+
const matrix: any = dom.createElement('table')
38+
const header = dom.createElement('tr')
39+
const corner = header.appendChild(dom.createElement('td'))
4840
corner.setAttribute('class', 'MatrixCorner')
4941
matrix.appendChild(header) // just one for now
5042
matrix.lastHeader = header // Element before data
51-
var columns = [] // Vector
52-
var rows = [] // Associative array
43+
let columns: any[] = [] // Vector
44+
const rows: any[] = [] // Associative array
5345

54-
var setCell = function (cell, x, y, value) {
46+
const setCell = function (cell, x, y, value) {
5547
while (cell.firstChild) {
5648
// Empty any previous
5749
cell.removeChild(cell.firstChild)
@@ -68,11 +60,12 @@ module.exports.matrixForQuery = function (
6860
delete cell.old
6961
}
7062

71-
var rowFor = function (y1) {
72-
var y = y1.toNT()
63+
const rowFor = function (y1) {
64+
const y = y1.toNT()
7365
if (rows[y]) return rows[y]
74-
var tr = dom.createElement('tr')
75-
var header = tr.appendChild(dom.createElement('td'))
66+
// @@ TODO Remove need for casting to any
67+
const tr: any = dom.createElement('tr')
68+
const header = tr.appendChild(dom.createElement('td'))
7669
header.setAttribute('style', 'padding: 0.3em;')
7770
header.textContent = utils.label(y1) // first approximation
7871
if (y1.termType === 'NamedNode') {
@@ -84,7 +77,7 @@ module.exports.matrixForQuery = function (
8477
if (ok) header.textContent = utils.label(y1)
8578
})
8679
}
87-
for (var i = 0; i < columns.length; i++) {
80+
for (let i = 0; i < columns.length; i++) {
8881
setCell(
8982
tr.appendChild(dom.createElement('td')),
9083
$rdf.fromNT(columns[i]),
@@ -94,7 +87,7 @@ module.exports.matrixForQuery = function (
9487
}
9588
tr.dataValueNT = y
9689
rows[y] = tr
97-
for (var ele = matrix.lastHeader.nextSibling; ele; ele = ele.nextSibling) {
90+
for (let ele = matrix.lastHeader.nextSibling; ele; ele = ele.nextSibling) {
9891
// skip header
9992
if (
10093
(y > ele.dataValueNT && options && options.yDecreasing) ||
@@ -106,11 +99,11 @@ module.exports.matrixForQuery = function (
10699
return matrix.appendChild(tr) // return the tr
107100
}
108101

109-
var columnNumberFor = function (x1) {
110-
var xNT = x1.toNT() // xNT is a NT string
111-
var col = null
102+
const columnNumberFor = function (x1): number {
103+
const xNT: any = x1.toNT() // xNT is a NT string
104+
let col: any = null
112105
// These are data columns (not headings)
113-
for (var i = 0; i < columns.length; i++) {
106+
for (let i = 0; i < columns.length; i++) {
114107
if (columns[i] === xNT) {
115108
return i
116109
}
@@ -134,10 +127,10 @@ module.exports.matrixForQuery = function (
134127
}
135128

136129
// col is the number of the new column, starting from 0
137-
for (var row = matrix.firstChild; row; row = row.nextSibling) {
130+
for (let row = matrix.firstChild; row; row = row.nextSibling) {
138131
// For every row header or not
139-
var y = row.dataValueNT
140-
var td = dom.createElement('td') // Add a new cell
132+
const y = row.dataValueNT
133+
const td = dom.createElement('td') // Add a new cell
141134
td.style.textAlign = 'center'
142135
if (row === matrix.firstChild) {
143136
td.textContent = utils.label(x1)
@@ -147,8 +140,8 @@ module.exports.matrixForQuery = function (
147140
if (col === columns.length - 1) {
148141
row.appendChild(td)
149142
} else {
150-
var t = row.firstChild
151-
for (var j = 0; j < col + 1; j++) {
143+
let t = row.firstChild
144+
for (let j = 0; j < col + 1; j++) {
152145
// Skip header col too
153146
t = t.nextSibling
154147
}
@@ -158,28 +151,28 @@ module.exports.matrixForQuery = function (
158151
return col
159152
}
160153

161-
var markOldCells = function () {
162-
for (var i = 1; i < matrix.children.length; i++) {
163-
var row = matrix.children[i]
164-
for (var j = 1; j < row.children.length; j++) {
154+
const markOldCells = function () {
155+
for (let i = 1; i < matrix.children.length; i++) {
156+
const row = matrix.children[i]
157+
for (let j = 1; j < row.children.length; j++) {
165158
row.children[j].old = true
166159
}
167160
}
168161
}
169162

170-
var clearOldCells = function () {
171-
var row, cell
172-
var colsUsed = []
173-
var rowsUsed = []
163+
const clearOldCells = function () {
164+
let row, cell
165+
const colsUsed: any[] = []
166+
const rowsUsed: any[] = []
174167

175168
if (options.set_y) {
176169
// Knows y values create rows
177-
for (var k = 0; k < options.set_y.length; k++) {
170+
for (let k = 0; k < options.set_y.length; k++) {
178171
rowsUsed[options.set_y[k]] = true
179172
}
180173
}
181174
if (options.set_x) {
182-
for (k = 0; k < options.set_x.length; k++) {
175+
for (let k = 0; k < options.set_x.length; k++) {
183176
colsUsed[columnNumberFor(options.set_x[k]) + 1] = true
184177
}
185178
}
@@ -189,8 +182,8 @@ module.exports.matrixForQuery = function (
189182
for (let j = 1; j < row.children.length; j++) {
190183
cell = row.children[j]
191184
if (cell.old) {
192-
var y = $rdf.fromNT(row.dataValueNT)
193-
var x = $rdf.fromNT(columns[j - 1])
185+
const y = $rdf.fromNT(row.dataValueNT)
186+
const x = $rdf.fromNT(columns[j - 1])
194187
setCell(cell, x, y, null)
195188
} else {
196189
rowsUsed[row.dataValueNT] = true
@@ -205,7 +198,7 @@ module.exports.matrixForQuery = function (
205198
delete rows[row.dataValueNT]
206199
matrix.removeChild(row)
207200
} else {
208-
for (var j = row.children.length - 1; j > 0; j--) {
201+
for (let j = row.children.length - 1; j > 0; j--) {
209202
// backwards
210203
const cell = row.children[j]
211204
if (!colsUsed[j]) {
@@ -214,7 +207,7 @@ module.exports.matrixForQuery = function (
214207
}
215208
}
216209
}
217-
var newcolumns = []
210+
const newcolumns: any[] = []
218211
for (let j = 0; j < columns.length; j++) {
219212
if (colsUsed[j + 1]) {
220213
newcolumns.push(columns[j])
@@ -228,24 +221,24 @@ module.exports.matrixForQuery = function (
228221
kb.query(query, addCellFromBindings, undefined, clearOldCells)
229222
}
230223

231-
var addCellFromBindings = function (bindings) {
232-
var x = bindings[vx]
233-
var y = bindings[vy]
234-
var value = bindings[vvalue]
235-
var row = rowFor(y)
236-
var colNo = columnNumberFor(x)
237-
var cell = row.children[colNo + 1] // number of Y axis headings
224+
const addCellFromBindings = function (bindings) {
225+
const x = bindings[vx.toString()]
226+
const y = bindings[vy.toString()]
227+
const value = bindings[(vvalue.toString())]
228+
const row = rowFor(y)
229+
const colNo = columnNumberFor(x)
230+
const cell = row.children[colNo + 1] // number of Y axis headings
238231
setCell(cell, x, y, value)
239232
}
240233

241234
if (options.set_y) {
242235
// Knows y values create rows
243-
for (var k = 0; k < options.set_y.length; k++) {
236+
for (let k = 0; k < options.set_y.length; k++) {
244237
rowFor(options.set_y[k])
245238
}
246239
}
247240
if (options.set_x) {
248-
for (k = 0; k < options.set_x.length; k++) {
241+
for (let k = 0; k < options.set_x.length; k++) {
249242
columnNumberFor(options.set_x[k])
250243
}
251244
}

src/matrix/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type MatrixOptions = {
2+
cellFunction?: (td, x, y, value) => string
3+
xDecreasing?: boolean
4+
yDecreasing?: boolean
5+
// eslint-disable-next-line camelcase
6+
set_x: any[]
7+
// eslint-disable-next-line camelcase
8+
set_y: any[]
9+
}

0 commit comments

Comments
 (0)