-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.module.html
More file actions
143 lines (138 loc) · 4.47 KB
/
Copy pathtest.module.html
File metadata and controls
143 lines (138 loc) · 4.47 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
<html>
<head>
<title> test </title>
<style>
input[type=button] {
width: 100px
}
</style>
<script type="module">
import ByteConverter from './dist/byte-converter.min.js'
function calc (val) {
const value = val || window.val.value
const from = window.converter.value(value, window.selFrom.value)
if (window.selTo.value !== 'auto') {
window.result.value = window.converter.convert(from, window.converter.dataFormats[window.selTo.value]).formatted
} else {
const opt = { type: window.autoBase(), unit: window.autoUnit() }
const val = window.converter.autoScale(from, opt)
window.result.value = val.formatted()
}
}
function change () {
if (window.selTo.value !== 'auto') {
const from = window.selFrom.value
const to = window.selTo.value
window.selTo.value = from
window.selFrom.value = to
calc()
}
}
function ready () {
console.log('ready called')
/* global ByteConverter */
/* eslint new-cap: ["error", { "newIsCap": false }] */
window.converter = ByteConverter
window.result = document.getElementById('result')
window.selFrom = document.getElementById('selectFrom')
window.selTo = document.getElementById('selectTo')
window.val = document.getElementById('val')
window.autoOptionTable = document.getElementById('autotableoption')
window.autoBase = () => document.querySelector('input[name=autobase]:checked').value
window.autoUnit = () => document.querySelector('input[name=autounit]:checked').value
function recurseListAndCreateElement (destination, isDescendent, prependAuto) {
const sorted = window.converter.unitsList.sort(function (a, b) { return window.converter.compareFormat(a, b, isDescendent) })
if (prependAuto) {
const opt = document.createElement('option')
opt.value = 'auto'
opt.innerText = '(auto)'
destination.appendChild(opt)
}
for (const { unit, name } of sorted) {
const opt = document.createElement('option')
opt.value = unit
opt.innerText = '(' + unit + ') ' + name
destination.appendChild(opt)
}
}
recurseListAndCreateElement(window.selFrom)
recurseListAndCreateElement(window.selTo, true, true)
window.val.onkeypress = evnt => {
try {
const curVal = window.val.value + evnt.key
const num = Number(curVal)
if (curVal !== '' && !isNaN(num)) {
calc(curVal)
} else {
evnt.preventDefault()
}
} catch (e) {
console.warn(e)
evnt.preventDefault()
}
}
window.selTo.onchange = evnt => {
if (window.selTo.value === 'auto') {
window.autoOptionTable.style.display = 'inherit'
} else {
window.autoOptionTable.style.display = 'none'
}
calc()
}
window.selFrom.onchange = evnt => {
calc()
}
}
window.onload = function () { ready() }
</script>
</head>
<body>
<table>
<tr>
<td>from:</td>
<td><input type="text" id="val"></input></td>
<td><select id="selectFrom"></select></td>
<td><input type="button" onclick="change()" value="switch"></input></td>
</tr>
<tr>
<td>to:</td>
<td><input type="text" disabled id="result"></input></td>
<td><select id="selectTo"></select></td>
<td><input type="button" onclick="calc()" value="calc"></input></td>
</tr>
</table>
<table id="autotableoption">
<tr>
<td colspan="4">Option for autoScale:</td>
</tr>
<tr>
<td colspan="2">Unit:</td>
<td colspan="2">Type:</td>
</tr>
<tr>
<td>preferSameUnit</td>
<td><input type="radio" name="autounit" value ="same" checked /></td>
<td>preferSameType</td>
<td><input type="radio" name="autobase" value ="same" checked /></td>
</tr>
<tr>
<td>preferOppositeUnit</td>
<td><input type="radio" name="autounit" value="opposite"/></td>
<td>preferOppositeType</td>
<td><input type="radio" name="autobase" value="opposite"/></td>
</tr>
<tr>
<td>preferBit</td>
<td><input type="radio" name="autounit" value="bit"/></td>
<td>preferDecimal</td>
<td><input type="radio" name="autobase" value="decimal"/></td>
</tr>
<tr>
<td>preferByte</td>
<td><input type="radio" name="autounit" value="byte"/></td>
<td>preferBinary</td>
<td><input type="radio" name="autobase" value="binary"/></td>
</tr>
</table>
</body>
</html>