Skip to content

Commit 6322900

Browse files
committed
Move some helpers into util.js
1 parent 5f4b40f commit 6322900

File tree

4 files changed

+136
-130
lines changed

4 files changed

+136
-130
lines changed

InteractiveHtmlBom/generate_interactive_bom.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ def get_file_content(file_name):
518518
html = html.replace('///SPLITJS///', get_file_content('split.js'))
519519
html = html.replace('///CONFIG///', config_js)
520520
html = html.replace('///PCBDATA///', pcbdata_js)
521+
html = html.replace('///UTILJS///', get_file_content('util.js'))
521522
html = html.replace('///RENDERJS///', get_file_content('render.js'))
522523
html = html.replace('///IBOMJS///', get_file_content('ibom.js'))
523524
with open(bom_file_name, "wt") as bom:

InteractiveHtmlBom/ibom.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
///PCBDATA///
2222
///////////////////////////////////////////////
2323

24+
///////////////////////////////////////////////
25+
///UTILJS///
26+
///////////////////////////////////////////////
27+
2428
///////////////////////////////////////////////
2529
///RENDERJS///
2630
///////////////////////////////////////////////

InteractiveHtmlBom/ibom.js

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* DOM manipulation and misc code */
22

3-
var storagePrefix = 'KiCad_HTML_BOM__' + pcbdata.metadata.title + '__' +
4-
pcbdata.metadata.revision + '__';
53
var bomsplit;
64
var canvassplit;
75
var canvaslayout = "default";
@@ -15,40 +13,8 @@ var highlightedModules = [];
1513
var checkboxes = [];
1614
var bomCheckboxes = "";
1715
var highlightpin1 = false;
18-
var storage;
1916
var lastClicked;
2017

21-
function initStorage(key) {
22-
try {
23-
window.localStorage.getItem("blank");
24-
storage = window.localStorage;
25-
} catch (e) {
26-
// localStorage not available
27-
}
28-
if (!storage) {
29-
try {
30-
window.sessionStorage.getItem("blank");
31-
storage = window.sessionStorage;
32-
} catch (e) {
33-
// sessionStorage also not available
34-
}
35-
}
36-
}
37-
38-
function readStorage(key) {
39-
if (storage) {
40-
return storage.getItem(storagePrefix + '#' + key);
41-
} else {
42-
return null;
43-
}
44-
}
45-
46-
function writeStorage(key, value) {
47-
if (storage) {
48-
storage.setItem(storagePrefix + '#' + key, value);
49-
}
50-
}
51-
5218
function dbg(html) {
5319
dbgdiv.innerHTML = html;
5420
}
@@ -274,23 +240,6 @@ function createColumnHeader(name, cls, comparator) {
274240
return th;
275241
}
276242

277-
function fancyDblClickHandler(el, onsingle, ondouble) {
278-
return function() {
279-
if (el.getAttribute("data-dblclick") == null) {
280-
el.setAttribute("data-dblclick", 1);
281-
setTimeout(function() {
282-
if (el.getAttribute("data-dblclick") == 1) {
283-
onsingle();
284-
}
285-
el.removeAttribute("data-dblclick");
286-
}, 200);
287-
} else {
288-
el.removeAttribute("data-dblclick");
289-
ondouble();
290-
}
291-
}
292-
}
293-
294243
function populateBomHeader() {
295244
while (bomhead.firstChild) {
296245
bomhead.removeChild(bomhead.firstChild);
@@ -441,14 +390,6 @@ function populateBomBody() {
441390
}
442391
}
443392

444-
function smoothScrollToRow(rowid) {
445-
document.getElementById(rowid).scrollIntoView({
446-
behavior: "smooth",
447-
block: "center",
448-
inline: "nearest"
449-
});
450-
}
451-
452393
function highlightPreviousRow() {
453394
if (!currentHighlightedRowId) {
454395
highlightHandlers[highlightHandlers.length - 1].handler();
@@ -515,55 +456,6 @@ function updateRefLookup(input) {
515456
populateBomTable();
516457
}
517458

518-
function copyToClipboard() {
519-
var text = '';
520-
for (var node of bomhead.childNodes[0].childNodes) {
521-
if (node.firstChild) {
522-
text = text + node.firstChild.nodeValue;
523-
}
524-
if (node != bomhead.childNodes[0].lastChild) {
525-
text += '\t';
526-
}
527-
}
528-
text += '\n';
529-
for (var row of bombody.childNodes) {
530-
for (var cell of row.childNodes) {
531-
for (var node of cell.childNodes) {
532-
if (node.nodeName == "INPUT") {
533-
if (node.checked) {
534-
text = text + '✓';
535-
}
536-
} else if (node.nodeName == "MARK") {
537-
text = text + node.firstChild.nodeValue;
538-
} else {
539-
text = text + node.nodeValue;
540-
}
541-
}
542-
if (cell != row.lastChild) {
543-
text += '\t';
544-
}
545-
}
546-
text += '\n';
547-
}
548-
var textArea = document.createElement("textarea");
549-
textArea.classList.add('clipboard-temp');
550-
textArea.value = text;
551-
552-
document.body.appendChild(textArea);
553-
textArea.focus();
554-
textArea.select();
555-
556-
try {
557-
if (document.execCommand('copy')) {
558-
console.log('Bom copied to clipboard.');
559-
}
560-
} catch (err) {
561-
console.log('Can not copy to clipboard.');
562-
}
563-
564-
document.body.removeChild(textArea);
565-
}
566-
567459
function silkscreenVisible(visible) {
568460
if (visible) {
569461
allcanvas.front.silk.style.display = "";
@@ -691,12 +583,6 @@ function changeBomLayout(layout) {
691583
changeCanvasLayout(canvaslayout);
692584
}
693585

694-
function focusInputField(input) {
695-
input.scrollIntoView(false);
696-
input.focus();
697-
input.select();
698-
}
699-
700586
function focusFilterField() {
701587
focusInputField(document.getElementById("filter"));
702588
}
@@ -732,22 +618,6 @@ function checkBomCheckbox(bomrowid, checkboxname) {
732618
checkbox.onchange();
733619
}
734620

735-
736-
function removeGutterNode(node) {
737-
for (var i = 0; i < node.childNodes.length; i++) {
738-
if (node.childNodes[i].classList &&
739-
node.childNodes[i].classList.contains("gutter")) {
740-
node.removeChild(node.childNodes[i]);
741-
break;
742-
}
743-
}
744-
}
745-
746-
function cleanGutters() {
747-
removeGutterNode(document.getElementById("bot"));
748-
removeGutterNode(document.getElementById("canvasdiv"));
749-
}
750-
751621
function setBomCheckboxes(value) {
752622
bomCheckboxes = value;
753623
writeStorage("bomCheckboxes", value);

InteractiveHtmlBom/util.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* Utility functions */
2+
3+
var storagePrefix = 'KiCad_HTML_BOM__' + pcbdata.metadata.title + '__' +
4+
pcbdata.metadata.revision + '__';
5+
var storage;
6+
7+
function initStorage(key) {
8+
try {
9+
window.localStorage.getItem("blank");
10+
storage = window.localStorage;
11+
} catch (e) {
12+
// localStorage not available
13+
}
14+
if (!storage) {
15+
try {
16+
window.sessionStorage.getItem("blank");
17+
storage = window.sessionStorage;
18+
} catch (e) {
19+
// sessionStorage also not available
20+
}
21+
}
22+
}
23+
24+
function readStorage(key) {
25+
if (storage) {
26+
return storage.getItem(storagePrefix + '#' + key);
27+
} else {
28+
return null;
29+
}
30+
}
31+
32+
function writeStorage(key, value) {
33+
if (storage) {
34+
storage.setItem(storagePrefix + '#' + key, value);
35+
}
36+
}
37+
38+
function fancyDblClickHandler(el, onsingle, ondouble) {
39+
return function() {
40+
if (el.getAttribute("data-dblclick") == null) {
41+
el.setAttribute("data-dblclick", 1);
42+
setTimeout(function() {
43+
if (el.getAttribute("data-dblclick") == 1) {
44+
onsingle();
45+
}
46+
el.removeAttribute("data-dblclick");
47+
}, 200);
48+
} else {
49+
el.removeAttribute("data-dblclick");
50+
ondouble();
51+
}
52+
}
53+
}
54+
55+
function smoothScrollToRow(rowid) {
56+
document.getElementById(rowid).scrollIntoView({
57+
behavior: "smooth",
58+
block: "center",
59+
inline: "nearest"
60+
});
61+
}
62+
63+
function focusInputField(input) {
64+
input.scrollIntoView(false);
65+
input.focus();
66+
input.select();
67+
}
68+
69+
function copyToClipboard() {
70+
var text = '';
71+
for (var node of bomhead.childNodes[0].childNodes) {
72+
if (node.firstChild) {
73+
text = text + node.firstChild.nodeValue;
74+
}
75+
if (node != bomhead.childNodes[0].lastChild) {
76+
text += '\t';
77+
}
78+
}
79+
text += '\n';
80+
for (var row of bombody.childNodes) {
81+
for (var cell of row.childNodes) {
82+
for (var node of cell.childNodes) {
83+
if (node.nodeName == "INPUT") {
84+
if (node.checked) {
85+
text = text + '✓';
86+
}
87+
} else if (node.nodeName == "MARK") {
88+
text = text + node.firstChild.nodeValue;
89+
} else {
90+
text = text + node.nodeValue;
91+
}
92+
}
93+
if (cell != row.lastChild) {
94+
text += '\t';
95+
}
96+
}
97+
text += '\n';
98+
}
99+
var textArea = document.createElement("textarea");
100+
textArea.classList.add('clipboard-temp');
101+
textArea.value = text;
102+
103+
document.body.appendChild(textArea);
104+
textArea.focus();
105+
textArea.select();
106+
107+
try {
108+
if (document.execCommand('copy')) {
109+
console.log('Bom copied to clipboard.');
110+
}
111+
} catch (err) {
112+
console.log('Can not copy to clipboard.');
113+
}
114+
115+
document.body.removeChild(textArea);
116+
}
117+
118+
function removeGutterNode(node) {
119+
for (var i = 0; i < node.childNodes.length; i++) {
120+
if (node.childNodes[i].classList &&
121+
node.childNodes[i].classList.contains("gutter")) {
122+
node.removeChild(node.childNodes[i]);
123+
break;
124+
}
125+
}
126+
}
127+
128+
function cleanGutters() {
129+
removeGutterNode(document.getElementById("bot"));
130+
removeGutterNode(document.getElementById("canvasdiv"));
131+
}

0 commit comments

Comments
 (0)