Module:Calculator widget
Creates an interactive 4-function calculator widget (See right). Requires the calculator gadget to work.
Usage
{{#invoke:Calculator widget|getWidget}}
Optional parameter fallback to determine what to do for users without javascript (or calculator gadget enabled). Default "all" to show full thing, "keypad" to show only keypad and "none" to make calculator invisible
local p = {}
-- Make a calculator widget using {{calculator}}
local function getDisplay()
-- Normal calculators don't display "Infinity" when dividing by 0.
return 'iffinite(ifzero(displayY,x,y),ifzero(displayY,x,y),nan)'
end
local function pressDecimal()
return {
{'decimal', 'ifequal(decimal,0,1,decimal)'},
{ 'y', 'ifzero(equalFlag,y,0)' },
{ 'x', 'ifzero(displayY,x,0)' },
{ 'equalFlag', 0 },
{ 'displayY', '0' }
}
end
-- This is sketch because x should happen before decimal.
function pressNumber(n)
return { {'x', 'ifequal(decimal,0,not(displayY)*x*10+(ifpositive(not(displayY)*x,1,-1))*' .. n .. ','
.. 'x+(ifpositive(x,1,-1))*(' .. n .. '/pow(10,decimal)))'},
{'decimal','ifzero(decimal,0,decimal+1)'},
{ 'y', 'ifzero(equalFlag,y,0)' },
{ 'op', 'ifzero(equalFlag,op,0)' },
{ 'equalFlag', 0 },
{ 'displayY', '0' },
}
end
function getClear()
return {
{ 'x', '0' },
{ 'y', '0' },
{ 'op', '0' },
{ 'decimal', '0' },
{ 'percentFlag', '0' },
{ 'equalFlag', '0' },
{ 'displayY', '0' },
}
end
-- Compute a function
-- 0: add 1: subtract 2: multiply 4: divide 5: square root.
function compute(op)
local res = {
{ 'x', 'ifzero(equalFlag,ifequal(percentFlag, 1, x*y/100, x),ifgreater(op,1,1,0))' },
{ 'y', 'switch(op,0,x+y,1,y-x,2,x*y,3,y/x,4,y)' },
--{ 'x', '0' },
{ 'decimal', '0' },
{ 'displayY', '1' },
{ 'percentFlag', 0 },
{ 'op', tostring(op) },
{ 'equalFlag', 0 }
}
return res
end
-- Equal key is a bit weird
-- If you press equal a second time, it should repeat operation "10 + 5 = = = =" gives 30
-- If you press equal and then a number, we start a new calc "10 + 5 = 2 + 1" gives 3
-- If you press an operation, you use the current result as the first number in operation "10 + 5 = + 2" gives 17
function computeEqual()
local res = {
{ 'x', 'ifequal(percentFlag, 1, x*y/100, x)' },
{ 'y', 'switch(op,0,x+y,1,y-x,2,x*y,3,y/x,4,sqrt(y))' },
--{ 'x', '0' },
{ 'decimal', '0' },
{ 'displayY', '1' },
{ 'percentFlag', 0 },
{ 'equalFlag', 1 }
}
return res
end
-- only arg is "fallback". Set to "all" (Default) to show everything when no-js
-- set to "keypad" to show only keypad to non-js users. set to "none" to show nothing.
function p.getWidget( frame )
local buttons = {
{ "MC", {{"mem", "0"}}, "Memory clear" },
{ "MR", {{"x", "mem"},{"displayY", 0}}, "Memory recall" },
{ "M−", {{"mem", "ifzero(displayY,mem-x,mem-y)"}}, "Subtract from memory" },
{ "M+", {{"mem", "ifzero(displayY,mem+x,mem+y)"}}, "Add to memory" },
{ "C", getClear(), "Clear" },
-- Its a bit unclear what the expected behaviour of pressing ± immediately after equal.
{ "±", {{'x', '0-x'}}, "Change sign" },
{ "%", {{'percentFlag', '1'}} },
-- A bit hacky, sqrt is the only unary operator. We want to make
-- sure it does it again if you press 25 √ = =
{ "√", {{'x','sqrt(ifzero(equalFlag,x,y))'},{'op','4'},{'equalFlag','1'},{'y','x'}} },
{ "7", pressNumber(7) },
{ "8", pressNumber(8) },
{ "9", pressNumber(9) },
{ "÷", compute(3) },
{ "4", pressNumber(4) },
{ "5", pressNumber(5) },
{ "6", pressNumber(6) },
{ "×", compute(2), "Multiply" },
{ "1", pressNumber(1) },
{ "2", pressNumber(2) },
{ "3", pressNumber(3) },
{ "−", compute(1), "Subtract" },
{ "0", pressNumber(0) },
{ ".", pressDecimal(), "Decimal point" },
{ "=", computeEqual() },
{ "+", compute(0), "Add" },
}
local calc = '<div class="calculatorwidget calculator-container" style="display:grid;grid-template-columns:repeat(4, 1fr);grid-gap:5px;min-width:256px;width:20ch;border:thin solid gray;padding: 5px;">'
if frame.args['fallback'] == 'none' then
-- if no js, hide.
calc = '<div style="display:none;" class="calculatorgadget-enabled">' .. calc
end
-- On Calculator page, we want to show the layout of buttons on print, but
-- not the answer field.
calc = calc .. '<div style="grid-column:1/5;'
if frame.args['fallback'] == 'keypad' then
-- for non-js users only show keypad but not answer widget.
calc = calc .. 'display:none" class="calculatorgadget-enabled'
end
calc = calc .. '">{{Calculator codex text|default=0|id=ans|formula=' .. getDisplay() .. '|style=text-align:right;font-weight:bold;|readonly=1|NaN-text=Error|aria-label=Result}}</div>'
for i, v in ipairs(buttons) do
local calcType, calcWeight
if v[1] == 'C' then
calcType = 'destructive'
calcWeight = 'normal'
elseif v[1] == '=' then
calcType = 'progressive'
calcWeight = 'primary'
else
calcType = 'default'
calcWeight = 'normal'
end
-- maybe make "=" be progressive instead of default. Perhaps weight = primary?
local forVar = ''
local formula = ''
for i2, v2 in ipairs( v[2] ) do
forVar = forVar .. ';' .. v2[1]
formula = formula .. ';' .. v2[2]
end
-- Should the alt text also be a title attribute to make a tooltip?
local alt = v[3] ~= nil and '|alt=' .. v[3] or ''
calc = calc .. '{{calculator button|type='.. calcType ..'|weight='.. calcWeight .. alt .. '|for=' .. string.sub(forVar,2) .. '|formula=' .. string.sub(formula,2) .. '|contents='.. v[1] .. '}}'
end
calc = calc .. '{{calculator|type=hidden|id=x|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=y|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=mem|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=op|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=decimal|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=displayY|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=percentFlag|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=equalFlag|default=0}}'
calc = calc .. '<templatestyles src="Module:Calculator widget/style.css"/>'
calc = calc .. '</div>'
if frame.args['fallback'] == 'none' then
-- if no js, hide.
calc = calc .. '</div>'
end
return frame:preprocess(calc)
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.