Skip to content

Commit

Permalink
Refactor contextual menu in a different module.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Mar 31, 2024
1 parent c2ebbf4 commit 0c2dc0c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
11 changes: 2 additions & 9 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { ASN1 } from './asn1.js';
import { oids } from './oids.js';
import { bindContextMenu } from './context.js';

const
lineLength = 80,
Expand Down Expand Up @@ -191,15 +192,7 @@ export class ASN1DOM extends ASN1 {
this.className = 'hex';
}
};
// handler to copy the complete hex dump into the clipboard
node.onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
contextMenu.style.left = event.pageX + "px";
contextMenu.style.top = event.pageY + "px";
contextMenu.style.visibility = 'visible';
document.getElementById('contextmenu').node = this;
event.stopPropagation();
};
bindContextMenu(node);
if (root == node) {
let lineStart = this.posStart() & 0xF;
if (lineStart != 0) {
Expand Down
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
<link rel="icon" type="image/svg+xml" sizes="192x192" href="favicon.svg">
</head>
<body>
<div id="contextmenu" onmouseleave="this.style.visibility = 'hidden';"><button id="btnCopyHex">Copy as HEX</button><br><button id="btnCopyString">Copy as String</button><br><button id="btnCopyPretty">Copy as Pretty</button></div>
<div id="contextmenu" onmouseleave="this.style.visibility = 'hidden';">
<button id="btnCopyHex">Copy as HEX</button><br>
<button id="btnCopyString">Copy as String</button><br>
<button id="btnCopyPretty">Copy as Pretty</button>
</div>
<header>
<div class="title">
<h1>ASN.1 JavaScript decoder</h1>
Expand Down
43 changes: 0 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Defs } from './defs.js';
import { tags } from './tags.js';

const
ASN1 = require('./asn1'),
maxLength = 10240,
reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/,
tree = id('tree'),
Expand Down Expand Up @@ -235,45 +234,3 @@ selectTag.onchange = function (ev) {
let tag = ev.target.selectedOptions[0].value;
window.location.href = 'https://rawcdn.githack.com/lapo-luchini/asn1js/' + tag + '/index.html';
};

// register context menu function
document.getElementById('btnCopyHex').onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
let node = contextMenu.node;
const pos = parseInt(node.getAttribute('pos'));
const end = parseInt(node.getAttribute('end'));
const hex = node.asn1.buf2hex(window.derBuffer.subarray(pos, end));
navigator.clipboard.writeText(hex);
contextMenu.style.visibility = 'hidden';
event.stopPropagation();
};

document.getElementById('btnCopyString').onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
let node = contextMenu.node;
const pos = parseInt(node.getAttribute('pos'));
const end = parseInt(node.getAttribute('end'));
let result = ASN1.decode(window.derBuffer.subarray(pos, end));
let type = result.typeName();
switch (type) {
case 'SET':
case 'SEQUENCE':
alert('Selected value is not a String!');
break;
default:
navigator.clipboard.writeText(result.content());
}
contextMenu.style.visibility = 'hidden';
event.stopPropagation();
};

document.getElementById('btnCopyPretty').onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
let node = contextMenu.node;
const pos = parseInt(node.getAttribute('pos'));
const end = parseInt(node.getAttribute('end'));
let result = ASN1.decode(window.derBuffer.subarray(pos, end));
navigator.clipboard.writeText(result.toPrettyString());
contextMenu.style.visibility = 'hidden';
event.stopPropagation();
};

0 comments on commit 0c2dc0c

Please sign in to comment.