-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbuttons.js
102 lines (96 loc) · 3.49 KB
/
buttons.js
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
/*
This file is part of Alpertron Calculators.
Copyright 2023 Dario Alejandro Alpern
Alpertron Calculators is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Alpertron Calculators is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
*/
/* global currentInputBox */
/* global getFuncNames */
/* global get */
/* global getParens */
function buttonClick(event)
{
let input = currentInputBox;
input.focus();
let start = input.selectionStart;
let chars = event.target.getAttribute("totalchars");
if (chars === "\u23CE")
{
chars = "\n";
}
input.value = input.value.substring(0, start) +
chars +
input.value.substring(input.selectionEnd);
// Place the caret at the end of the appended text.
let offset = chars.indexOf("(") + 1;
if (offset == 0)
{
offset = chars.length;
}
input.selectionStart = start + offset;
input.selectionEnd = input.selectionStart;
}
function generateFuncButtons(optionCategory, funcButtons)
{
let button;
let catIndex;
let funcbtns = get(funcButtons);
let catnbr = get(optionCategory).selectedIndex;
let funcname = (getParens() + getFuncNames()[+catnbr]).split(",");
// Append all buttons to document fragment instead of funcbtns
// and finally append the fragment to funcbtns to minimize redraws.
let fragment = document.createDocumentFragment();
for (catIndex = 0; catIndex < funcname.length/2; catIndex++)
{
if (funcButtons == "wzdfuncbtns" && catIndex == 2)
{ // Do not show Enter button on wizard.
continue;
}
button = document.createElement("button");
button.setAttribute("type", "button"); // Indicate this is a button, not submit.
button.setAttribute("title", funcname[catIndex*2]); // Text of tooltip.
let btnName = funcname[catIndex*2 + 1];
let nbrArguments = parseInt(btnName.slice(-1), 10);
if (nbrArguments >= 1 && nbrArguments <= 9)
{
let text = btnName.slice(0, -1) + "(";
// Set text of button.
button.innerHTML = text;
// Text to be displayed when the button is pressed.
button.setAttribute("totalchars", text + ",".repeat(nbrArguments-1) + ")");
}
else
{
// Set text of button.
button.innerHTML = btnName;
// Text to be displayed when the button is pressed.
button.setAttribute("totalchars", btnName);
}
button.classList.add("funcbtn");
button.onclick = buttonClick;
fragment.appendChild(button);
}
funcbtns.innerHTML = "";
funcbtns.appendChild(fragment);
}
function completeFuncButtons(funcButtons)
{
let button;
let catIndex;
let funcname = (getParens() + getFuncNames()[0]).split(",");
let funcbtns = get(funcButtons);
for (catIndex = 0; catIndex < funcname.length/2; catIndex++)
{
button = funcbtns.children[+catIndex];
button.setAttribute("title", funcname[catIndex*2]); // Text of tooltip.
button.onclick = buttonClick;
}
}