-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilicon-hex-table.html
executable file
·637 lines (574 loc) · 20.4 KB
/
silicon-hex-table.html
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<link rel="import" href="../polymer/polymer.html">
<!--
An efficient hex table for all your online hexadecimal needs.
Specify an `offset` into `bytes`, and the table will automatically populate
the offset, hexadecimal, and ASCII tables with what it finds
(or '--' if it goes beyond the length of `bytes`).
Example:
<silicon-hex-table offset="0x0" bytes="[[uint8array]]"></silicon-hex-table>
Please **NOTE** the `bytes` must be a `Uint8Array` for now;
in the future, a base64 encoded string will also be accepted.
If you data-bind to `offset` and modify it, the table automatically adjusts itself,
and displays the values (if any) after that offset.
Negative offsets are accepted, but that's crazy and means nothing,
so don't rely on that behavior.
If you want a smaller or larger table, change the `rows` property to the
desired value, and the table will automatically adjust itself.
More rows means more updates, so be careful.
Unless you specify `no-select` or set `noSelect = true`,
dragging/selecting across the hex table will select that portion of bytes.
To programmatically initiate this, call `selectRange(start, count)`,
where start is the `offset` to start selecting at, and `count`
is the amount.
### Styling
The table is themeable via the following CSS custom properties.
Custom property | Description | Default
:---------------|:------------|:-------|
`--highlight-color`| Mouse highlight color | `#f0f3a9`
`--selected-color`| Selected bytes highlight color | `#f0f0a0`
`--ascii-color` | Color of the ASCII table characters | `blue`
`--hex-color` | Color of the hexadecimal bytes |`#333`
`--header-color` | Color of the table header | `#ccc`
`--background-color` | Background color of the table | `#fafafa`
`--offset-color` | Offset column color | `#bd8d00`
`--font-size` | Font size | `16px`
@demo
-->
<dom-module id="silicon-hex-table">
<template>
<style>
:host {
display: block;
--highlight-color: #f0f3a9;
--selected-color: #f0f0a0;
--ascii-color: blue;
--hex-color: #333;
--column-padding: 0px;
--inner-column-padding: 0px;
--header-color: #ccc;
--background-color: #fafafa;
--offset-color: #bd8d00;
--font-size: 16px;
font-size: var(--font-size);
font-family: 'Inconsolata', monospace;
}
div {
display: inline-block;
background: var(--header-color);
}
table {
display: inline-block;
background: var(--background-color);
color: var(--hex-color);
border: 0px;
border-collapse: collapse;
border-spacing: 0px 0px;
padding: 0px;
}
td,
th {
border: 0px;
margin: 0px;
padding-right: var(--column-padding);
font-weight: normal;
}
.header {
background: var(--header-color);
}
.asciiheader span {
color: var(--ascii-color);
}
.selected {
background: var(--selected-color);
}
.highlighted {
background-color: var(--highlight-color);
}
.offsets {
color: var(--offset-color);
}
::selection {
background: #fff;
color: var(--text-color);
opacity: .25;
}
::-moz-selection {
background: #fff;
color: var(--text-color);
opacity: .25;
}
</style>
<div>
<table id="offsetTable">
<tr class="header">
<th>
<span>87654321</span>
</th>
</tr>
<template is="dom-repeat" items="[[_table]]" as="row">
<tr>
<td class="offsets">
<span>[[_getOffset(offset, index)]]</span>
</td>
</tr>
</template>
</table>
<table id="hexTable">
<tr class="header">
<th>
<template is="dom-repeat" items="[[_header]]">
<span class="hexheader">[[item]]</span>
</template>
</th>
</tr>
<template is="dom-repeat" items="[[_table]]" as="row">
<tr>
<td class="bytes">
<template is="dom-repeat" items="[[_get(row)]]" as="col">
<span id$="[[col.id]]" class$="[[_computeSelected(_selectedSet, col.id)]]">[[_getHex(_bytes, offset, col)]]</span>
</template>
</td>
</tr>
</template>
</table>
<table id="asciiTable">
<tr class="header">
<th>
<!-- because http://stackoverflow.com/questions/31846667/dom-repeat-with-span-whitespace-solution -->
<template is="dom-repeat" items="[[_asciiheader]]"><span>[[item]]</span></template>
</th>
</tr>
<template is="dom-repeat" items="[[_table]]" as="row">
<tr>
<td class="asciiheader">
<template is="dom-repeat" items="[[_get(row)]]" as="col"><span id$="[[col.hid]]" class$="[[_computeSelected(_selectedSet, col.id)]]">[[_getAscii(_bytes, offset, col)]]</span></template>
</td>
</tr>
</template>
</table>
</div>
</template>
<script>
(function() {
Polymer({
is: 'silicon-hex-table',
properties: {
/**
* The bytes to display in the table.
* **NOTE**: must be a `Uint8Array`.
*/
bytes: {
type: Array
},
/**
* The currently highlighted row and column in the table.
* If you want to track the highlighted byte, listen to this event.
*/
highlight: {
type: Object,
notify: true,
value: function() {
return {
row: 0,
col: 0,
offset: 0,
count: 0 // TODO: add count feature
}
}
},
/**
* The offset into `bytes`.
* If you modify this value, the hex table recomputes the various tables accordingly.
*/
offset: {
type: Number,
value: 0x0
},
/**
* The number of columns to display.
* This is `readOnly` for now.
*/
columns: {
type: Number,
readOnly: true,
value: 16
},
/**
* The number of rows to display, zero-indexed.
* If you modify this value, the table is recalculated (this is relatively expensive, don't do this so much).
*/
rows: {
type: Number,
value: 19
},
/**
* The selected offsets. Bind to this to watch changes.
* @type {{begin: Number, end: Number, count: Number}}
*/
selected: {
type: Array,
notify: true,
value: function() {
return {};
}
},
/**
* Do not perform selection via tracking drag gestures
* (you can still manually select with `selectRange`).
*/
noSelect: {
type: Boolean,
value: false
},
/**
* The set of selected nodes.
* @type {Set}
*/
_selectedSet: {
type: Object,
value: function() {
return new Set();
}
},
/**
* The computed property for the bytes to display.
* Used instead of `bytes` in case we allow base64 encoded strings in addition to Uint8Array in a future release.
*/
_bytes: {
computed: '_computeBytes(bytes)'
},
/**
* Emacs `hexl-mode` style ascii row header display. In the future we'll let this be customizable.
*
*/
_asciiheader: {
type: Array,
value: function() {
return ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
];
}
},
/**
* Emacs `hexl-mode` style hex row header display. In the future we'll let this be customizable.
*
*/
_header: {
type: Array,
value: function() {
return ['00', '11', '22', '33', '44', '55', '66', '77', '88',
'99', 'aa', 'bb', 'cc', 'dd', 'ee', 'ff'
];
}
},
/**
* The table we compute when the rows change.
*
*/
_table: {
type: Array,
computed: '_computeTable(rows)',
observer: '_tableChanged'
}
},
listeners: {
'hexTable.track': '_trackSoHard',
'hexTable.tap': '_tap'
},
/**
* Selects a range of bytes (and thereby highlights them).
* Wipes the current selection, and sets `selected` to the new range.
*
* @param {Number} begin The offset to begin the selection
* @param {Number} count The number of bytes to select
*/
selectRange: function(begin, count) {
var set = new Set();
for (var i = 0; i < count; i++) {
var id = this._offset2id(begin + i);
set.add(id);
}
this._selectedSet = set;
this.selected = {
begin: begin,
end: begin + count,
count: count
};
},
/**
* Deselects the selected bytes (removes highlighting).
*/
deselect: function() {
this._selectedSet = new Set();
this.selected = {}; // or null?
this._selectStart = undefined;
this._selectCount = undefined;
},
/**
* Toggles the offset table's display state.
*/
toggleOffsetTable: function() {
var hidden = this.$.offsetTable.style.display === 'none';
this.$.offsetTable.style.display = hidden ? 'inline-block' : 'none';
},
/**
* Toggles the hex table's display state.
*/
toggleHexTable: function() {
var hidden = this.$.hexTable.style.display === 'none';
this.$.hexTable.style.display = hidden ? 'inline-block' : 'none';
},
/**
* Toggles the ascii table's display state.
*/
toggleAsciiTable: function() {
var hidden = this.$.asciiTable.style.display === 'none';
this.$.asciiTable.style.display = hidden ? 'inline-block' : 'none';
},
/**
* Computed property for setting the selected class state of the hexadecimal
* and ascii bytes.
* @param {Set} selectedSet The set of selected ids
* @param {String} id The id to check if it's in the selected ids set
*/
_computeSelected: function(selectedSet, id) {
return selectedSet.has(id) ? 'selected' : '';
},
/**
* Selects the target node if it's a valid node, has an id, and sets the appropriate
* internal values `_selectStart` and `_selectCount` for use in `selectRange()`.
* **TODO**: implement backwards dragging.
*
* @param {Node} target The target node to add or not.
*/
_selectTarget: function(target) {
if (target && target.id) {
if (this._selectStart === undefined) {
this._selectStart = this._id2offset(target.id);
} else {
var end = this._id2offset(target.id);
if (this._selectStart > end) { // TODO: if backwards
var tmp = this._selectStart;
this._selectStart = end;
end = tmp;
this._backwards = true;
}
this._selectCount = end - this._selectStart + 1; // because need to include last byte
}
}
},
_tap: function(e) {
if (!this.noSelect) {
this.deselect();
}
},
/**
* The gesture tracker for Polymer `track` events. Tracks _real_ hard.
*/
_trackSoHard: function(e) {
if (!this.noSelect) {
switch (e.detail.state) {
case 'start':
this.deselect();
var target = e.detail.hover();
this._selectTarget(target);
break;
case 'end':
var target = e.detail.hover();
this._selectTarget(target);
this.selectRange(this._selectStart, this._selectCount);
break;
case 'track':
var target = e.detail.hover();
this._selectTarget(target);
break;
default:
break;
}
}
},
// Element Behavior
/**
* The `highlight-changed` event is fired whenever `highlighted` is changed.
*
* {{row: Number, col: Number, offset: Number}}
* @event highlight-changed
*/
/**
* The `selected-changed` event is fired whenever `selected` is
* changed via dragging or through `selectRange()`, etc..
*
* {{begin: Number, end: Number, count: Number}}
* @event selected-changed
*/
/**
* Computed function for `_bytes`; currently just checks if `bytes` attribute is a Uint8Array, and if not, educates the user and returns an empty `Uint8Array`.
* @param {Uint8Array} bytes The bytes to view
* @return {Uint8Array} The actual internal representation we use
*/
_computeBytes: function(bytes) {
if (bytes instanceof Uint8Array) {
return bytes;
} else {
console.log('Error: <silicon-hex-table> requires Uint8Array of bytes');
return new Uint8Array(0);
}
// phuture:
// return StringView.base64ToBytes(bytes);
},
/**
* A computed function for displaying the offset column values. Pads the bytes with zeros. Recomputes on `offset` change.
* @param {Uint8Array} offset The current offset that's been set
* @param {Uint8Array} index The offset row we're displaying
* @return {string} The pretty-printed hexadecimal offset for the row of the bytes we're viewing
*/
_getOffset: function(offset, index) {
// if (offset < 0) return ' ';
var offsetstr = (offset + (index * this.columns)).toString(16);
var len = offsetstr.length;
var pad = '';
for (var i = 0; i < (8 - len); i++) {
pad += '0';
}
return pad + offsetstr;
},
_id2raster: function(id) {
var split = id.slice(1).split('c');
var r = parseInt(split[0]);
var c = parseInt(split[1]);
return {
r: r,
c: c
};
},
/**
* Takes a target's id in the byte table and returns its hex value.
* @param {String} id The id of the target
* @return {Number} The offset of the byte
*/
_id2offset: function(id) {
var raster = this._id2raster(id);
return this._raster2index(this.offset, raster.r, raster.c);
},
_offset2id: function(offset) {
var base = offset - this.offset;
var r = Math.floor(base / this.columns);
var c = base % this.columns;
return 'r' + r + 'c' + c;
},
/*
* Takes a rasterized x (column) and y (row) coordinate for a byte in the hex table, and the current offset, and converts it into an index into the byte array.
* @param {number} offset The current offset into the byte array
* @param {number} r The row of the byte in question
* @param {number} c The column of the byte in question
* @return {number} The offset into the byte array
*/
_raster2index: function(offset, r, c) {
var i = c + (r * this.columns);
return i + offset;
},
/*
* Takes a raster object stored in `_table` which has an x (column) and y (row) coordinate for a byte in the hex table, and the current offset, and returns its ASCII code.
* @param {Uint8Array} bytes The `_bytes` we're looking at
* @param {number} offset The current offset into the byte array
* @param {object} raster The raster object from the `_table` computed property with row and column index
* @return {string} The ascii value of the byte in the table
*/
_getAscii: function(bytes, offset, raster) {
var i = this._raster2index(offset, raster.r, raster.c);
var code = bytes[i];
return code >= 0x20 && code <= 0x7e ? String.fromCharCode(code) : '.';
},
/*
* Takes a raster object stored in `_table` which has an x (column) and y (row) coordinate for a byte in the hex table, and the current offset, and returns its hexadecimal string representation for display.
* @param {Uint8Array} bytes The `_bytes` we're looking at
* @param {number} offset The current offset into the byte array
* @param {object} raster The raster object from the `_table` computed property with row and column index
* @return {string} The hexadecimal string value of the byte in the table, padded with a 0 if necessary
*/
_getHex: function(bytes, offset, raster) {
var i = this._raster2index(offset, raster.r, raster.c);
if (i >= bytes.length || i < 0) {
// if our index is bad, we return the distinguished '--'
return '--';
}
var code = bytes[i];
return code < 16 ? '0' + code.toString(16) : code.toString(16);
},
// Haxors
_get: function(row) {
return row;
},
/**
* Computes the hex table, given a row size. Updates on `rows` change.
* @params {number} size The size of the table (number of rows).
* @return {array} An 2-D array of objects containing row, column, id, and ids for use in computing various components of the table efficiently.
*/
_computeTable: function(size) {
//console.log('computing table with size: ', size);
var rows = [];
for (var r = 0; r < size; r++) {
var cols = []
for (var c = 0; c < 16; c++) {
cols.push({
id: ('r' + r + 'c' + c),
hid: ('asciir' + r + 'c' + c),
r: r,
c: c
})
}
rows.push(cols);
}
return rows;
},
/**
* Highlights the corresponding ASCII character in the ascii table when the mouse hovers over its sister byte in the hexadecimal table.
* @params {number} highlight The coordinates to highlight
* @params {e} e The `mouseenter`/`mouseleave` event.
*/
_highlightAscii: function(highlight, e) {
var id = e.target.id;
var split = id.slice(1).split('c');
var r = parseInt(split[0]);
var c = parseInt(split[1]);
var ascii = '#asciir';
var hex = '#r';
var selector = ascii + r + 'c' + c;
var ascii = this.$$(selector);
if (highlight) {
Polymer.dom(ascii).classList.add('highlighted');
e.target.classList.add('highlighted');
this.set('highlight', {
row: r,
col: c,
offset: this.offset + (c + (r * this.rows))
});
} else {
Polymer.dom(ascii).classList.remove('highlighted');
e.target.classList.remove('highlighted');
}
},
_initHighlighting: function() {
// some timing issue here
this.async(function() {
var bytes = Polymer.dom(this.$.hexTable).querySelectorAll('.bytes span');
bytes.forEach(function(byte) {
byte.addEventListener('mouseenter', (function(e) {
this._highlightAscii(true, e)
}).bind(this));
byte.addEventListener('mouseleave', (function(e) {
this._highlightAscii(false, e)
}).bind(this));
}, this);
}, 100);
},
_tableChanged: function(table) {
if (table) {
this._initHighlighting();
}
},
attached: function() {}
});
})();
</script>
</dom-module>