-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqudt.js
More file actions
217 lines (184 loc) · 5.45 KB
/
Copy pathqudt.js
File metadata and controls
217 lines (184 loc) · 5.45 KB
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
"use strict";
var _alphabetSearch;
function showErr(err) {
let spinner = document.getElementById("spinner");
spinner.style.display = "none";
console.log(err);
alert(err.message);
}
function setMessage(message) {
const spinner = document.getElementById("spinner");
const messageDiv = document.getElementById("message");
if (!message) {
spinner.style.display = "none";
messageDiv.style.display = "none";
} else {
spinner.style.display = "block";
messageDiv.style.display = "block";
messageDiv.innerHTML = message;
}
}
function bin(data) {
var letter;
var bins = {};
for (var i = 0, ien = data.length; i < ien; i++) {
const link = data[i];
const text = link.match(/<a[^>]*>([^<]+)<\/a>/)[1];
letter = text.charAt(0).toUpperCase();
if (bins[letter]) {
bins[letter]++;
}
else {
bins[letter] = 1;
}
}
return bins;
}
function parseUnitOwlText(owlText) {
setMessage("Parsing data...");
const units = [];
let unit = {
Unit: "",
Label: "",
UCUM: "",
Description: "",
UnitType: ""
};
const allLines = owlText.split(/\r\n|\n/);
allLines.forEach((line) => {
if (line.substring(0, 5) === "unit:") {
const id = line.substring(5);
const href = '"https://qudt.org/vocab/unit/' + id + '.html"';
const a = '<a href=' + href + '>' + id + '</a>';
unit = {
Unit: a,
Label: "",
UCUM: "",
Description: "",
UnitType: "",
LabelLang: ""
};
units.push(unit);
} else if (line.substring(0, 36) === " qudt:hasQuantityKind quantitykind:") {
const unitType = line.substring(36, line.length - 2);
if (!unit.UnitType) {
unit.UnitType = unitType;
}
else {
unit.UnitType += ", " + unitType;
}
} else if (line.substring(0, 27) === " qudt:plainTextDescription" && !unit.Description) {
unit.Description = line.substring(29, line.length - 3);
} else if (line.substring(0, 15) === " qudt:ucumCode" && !unit.UCUM) {
let ucum = line.substring(17);
ucum = ucum.substring(0, ucum.indexOf('"'));
unit.UCUM = ucum;
} else if (line.substring(0, 12) === " rdfs:label") {
// Prefer the en-us version of the label
let label = line.substring(14);
const at = label.indexOf('"@')
// The language is the part between the @ symbol and the semi-colon
const lang = label.substring(at + 2, label.length - 1).trim();
label = label.substring(0, at);
if (!unit.Label || lang === "en-us") {
unit.Label = label;
unit.LabelLang = lang;
}
else if (lang === "en" && unit.LabelLang !== "en-us") {
unit.Label = label;
unit.LabelLang = lang;
}
}
});
return units;
}
function renderTable(units) {
setMessage("Rendering table...");
const table = document.getElementById("unitTable");
table.style.display = "block";
var dataTable = $("#unitTable").DataTable({
data: units,
columns: [{
data: "Unit"
},
{
data: "Label"
},
{
data: "UCUM"
},
{
data: "Description"
},
{
data: "UnitType"
}
],
order: [
[0, "asc"]
],
paging: false
});
// Initialize filter by letter
var alphabet = $('<div class="alphabet"/>').append('Filter: ');
var columnData = dataTable.column(0).data();
var bins = bin(columnData);
$('<span class="clear active"/>')
.data('letter', '')
.data('match-count', columnData.length)
.html('All')
.appendTo(alphabet);
for (var i = 0; i < 26; i++) {
var letter = String.fromCharCode(65 + i);
$('<span/>')
.data('letter', letter)
.data('match-count', bins[letter] || 0)
.addClass(!bins[letter] ? 'empty' : '')
.html(letter)
.appendTo(alphabet);
}
alphabet.insertBefore(dataTable.table().container());
alphabet.on('click', 'span', function () {
alphabet.find('.active').removeClass('active');
$(this).addClass('active');
_alphabetSearch = $(this).data('letter');
dataTable.draw();
});
var info = $('<div class="alphabetInfo"></div>')
.appendTo(alphabet);
alphabet
.on('mouseenter', 'span', function () {
info
.css({
opacity: 1,
left: $(this).position().left,
width: $(this).width()
})
.html($(this).data('match-count'))
})
.on('mouseleave', 'span', function () {
info.css('opacity', 0);
});
setMessage("");
}
function fetchUnits(qudtUnitListUri) {
setMessage("Fetching data...");
fetch(qudtUnitListUri)
.then((response) => response.text())
.then((owlText) => parseUnitOwlText(owlText))
.then((units) => renderTable(units))
.catch((err) => showErr(err));
}
$.fn.dataTable.ext.search.push(function (settings, searchData) {
if (!_alphabetSearch) { // No search term - all results shown
return true;
}
if (searchData[0].charAt(0) === _alphabetSearch) {
return true;
}
return false;
});
window.onload = function () {
const qudtUnitListUri = "https://qudt.org/vocab/unit/"; // not CORS friendly
fetchUnits(qudtUnitListUri);
};