Jump to content

MediaWiki talk:Gadget-calculator.js

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Make it previewable

[edit]

Currently it doesn't work in live preview, reply tool, etc. This should fix it (code).

Line 176: Line 176:
// Evaluate the value of an AST at runtime. // Evaluate the value of an AST at runtime.
var evaluate = function( ast ) { var evaluate = function( ast, parserOutput ) {
if ( ast instanceof Numb ) { if ( ast instanceof Numb ) {
return ast.value; return ast.value;
} }
if ( ast instanceof Ident ) { if ( ast instanceof Ident ) {
var elm = document.getElementById( 'calculator-field-' + ast.value ); var elm = parserOutput.querySelector( '#calculator-field-' + ast.value );
if ( elm.tagName === 'INPUT' ) { if ( elm.tagName === 'INPUT' ) {
if ( elm.type === 'radio' || elm.type === 'checkbox' ) { if ( elm.type === 'radio' || elm.type === 'checkbox' ) {
Line 192: Line 192:
} }
if ( ast instanceof Operator ) { if ( ast instanceof Operator ) {
evaledArgs = ast.args.map( evaluate ); evaledArgs = ast.args.map( function (arg) {
return evaluate( arg, parserOutput );
});
if ( mathFuncs.indexOf(ast.op) !== -1 ) { if ( mathFuncs.indexOf(ast.op) !== -1 ) {
return Math[ast.op].apply( Math, evaledArgs ); return Math[ast.op].apply( Math, evaledArgs );
Line 276: Line 278:
// Start code that does setup and HTML interaction. // Start code that does setup and HTML interaction.
var setup = function() { var setup = function( $content ) {
var elms = Array.from( document.getElementsByClassName( 'calculator-field' ) ); var elms = Array.from( $content.find( '.calculator-field' ) );
if (elms.length > 200) { if (elms.length > 200) {
console.log( "Too many calculator widgets on page" ); console.log( "Too many calculator widgets on page" );
Line 346: Line 348:
console.log( "Change happened!" ); console.log( "Change happened!" );
inProgressRefresh = {}; inProgressRefresh = {};
// Fall back on body
var parserOutput = e.target.closest( '.mw-parser-output, body' );
var itemId = e.target.id.replace( /^calculator-field-/, '' ); var itemId = e.target.id.replace( /^calculator-field-/, '' );
inProgressRefresh[itemId] = true; inProgressRefresh[itemId] = true;
for ( var i in backlinks[itemId] ) { for ( var i in backlinks[itemId] ) {
refresh( backlinks[itemId][i] ); refresh( backlinks[itemId][i], parserOutput );
} }
if ( e.target.type === 'radio' ) { if ( e.target.type === 'radio' ) {
// when a radio element gets checked, others get unchecked. // when a radio element gets checked, others get unchecked.
var otherElms = document.getElementsByName( e.target.name ); var otherElms = parserOutput.querySelectorAll( '[name="' + e.target.name + '"]' );
for ( var l = 0; l < otherElms.length; l++ ) { for ( var l = 0; l < otherElms.length; l++ ) {
if ( otherElms[l].id === e.target.id ) { if ( otherElms[l].id === e.target.id ) {
Line 361: Line 365:
if ( backlinks[oElmId] ) { if ( backlinks[oElmId] ) {
for ( var k in backlinks[oElmId] ) { for ( var k in backlinks[oElmId] ) {
refresh( backlinks[oElmId][k] ); refresh( backlinks[oElmId][k], parserOutput );
} }
} }
Line 369: Line 373:
} }
var refresh = function (itemId) { var refresh = function (itemId, parserOutput) {
if ( !itemList[itemId] || itemList[itemId] instanceof Null ) { if ( !itemList[itemId] || itemList[itemId] instanceof Null ) {
// This should not happen. // This should not happen.
Line 380: Line 384:
} }
inProgressRefresh[itemId] = true; inProgressRefresh[itemId] = true;
var res = evaluate( itemList[itemId] ); var res = evaluate( itemList[itemId], parserOutput );
var elm = document.getElementById( "calculator-field-" + itemId ); var elm = parserOutput.querySelector( "#calculator-field-" + itemId );
if ( elm.tagName === 'INPUT' ) { if ( elm.tagName === 'INPUT' ) {
elm.value = res; elm.value = res;
Line 391: Line 395:
} }
for ( var i in backlinks[itemId] ) { for ( var i in backlinks[itemId] ) {
refresh( backlinks[itemId][i] ); refresh( backlinks[itemId][i], parserOutput );
} }
} }
$( setup ); mw.hook( 'wikipage.content' ).add( setup );
} )(); } )();

Nardog (talk) 01:59, 14 September 2024 (UTC) updated 11:34, 14 September 2024 (UTC)[reply]

cc: @Bawolff. – SD0001 (talk) 07:18, 14 September 2024 (UTC)[reply]
Should these sort of changes be made "upstream" during testing? — xaosflux Talk 08:15, 14 September 2024 (UTC)[reply]
I'm unsure that would work properly if the wikipage.content hook was fired multiple times during one page view. Bawolff (talk) 08:51, 14 September 2024 (UTC)[reply]
@Bawolff: For what reason do you suspect it might not? And if it doesn't then it should be fixed. Not being able to preview is unacceptable IMO. I tested my patch and it works in live preview, realtime preview, and reply tool. Nardog (talk) 10:25, 14 September 2024 (UTC)[reply]
Ah, it doesn't work if both live and realtime preview are used, presumably because elements with the same IDs exist. That could be mitigated by scoping queries to the $content fired by the hook and keeping references to the associated elements, requiring a wrapper element, or including a serial number in the IDs that increases on each hook firing. Nardog (talk) 10:34, 14 September 2024 (UTC)[reply]
Another technique is to attach a --bound class to the elements that have been processed, and skip them on further runs, c.f. mw:MediaWiki:Gadget-site-tpl-copy.js. – SD0001 (talk) 11:01, 14 September 2024 (UTC)[reply]
I'm not sure if that would help because IDs are looked up not only when elements are processed but also on interaction.
Come to think of it, probably the simplest solution is to scope the query to the nearest .mw-parser-output. I'll test it and update the patch. Nardog (talk) 11:12, 14 September 2024 (UTC)[reply]
I think the best way to do this would be to have the gadget keep references to the actual elements, instead of just their IDs. Bawolff (talk) 19:34, 15 September 2024 (UTC)[reply]
@Izno: What is "the answer" here? Nardog (talk) 09:42, 15 September 2024 (UTC)[reply]
My concern from above didn't really get answered - right now this is a test, and I'd expect that improvements are going to get synced in from the external site, clobbering local improvements. Not a showstopper, just something to consider. — xaosflux Talk 13:31, 15 September 2024 (UTC)[reply]
@Nardog You said I'll test it and update the patch. without posting a further update and only now do I see you did actually work on it. You're free to reactivate if you think it's ready again. That was what I based that off.
I do also agree with Xaosflux though. Izno (talk) 16:04, 15 September 2024 (UTC)[reply]

Can this be resynced with https://mdwiki.org/wiki/MediaWiki:Gadget-calculator.js ? The new version should address User:Nardog's concerns. Bawolff (talk) 06:40, 26 September 2024 (UTC)[reply]

NaN

[edit]

@Bawolff Another issue is that NaN shows up in result cells when one or more inputs are blank. Can we make the result blank in such cases? – SD0001 (talk) 07:54, 26 September 2024 (UTC)[reply]

This can be worked around by setting a default value for all the cells of 0 (or whatever is appropriate), so they don't start off as blank. Alternatively, the calculator template can take a NaN-text parameter to adjust the text used for not-a-number (The #if in the template code won't let you set it to empty string, although the js should work if you manually made the html. I guess using something like &#32; should work with the current template). Bawolff (talk) 09:33, 26 September 2024 (UTC)[reply]

inconsistent sizing on firefox

[edit]

Can this be resynced with https://mdwiki.org/wiki/MediaWiki:Gadget-calculator.js to fix a bug with inconsistent sizing of number boxes on firefox between different skins. Bawolff (talk) 03:29, 30 September 2024 (UTC)[reply]

user:Xaosflux completed this. Bawolff (talk) 20:29, 9 October 2024 (UTC)[reply]

Live preview

[edit]

It seems like being a "category gadget" breaks live preview, because during live preview, MediaWiki won't recognize the page as being in the right category. I'm not sure if there is any solution to that. Bawolff (talk) 05:08, 10 October 2024 (UTC)[reply]

Looks like this bug is already reported at phab:T376875. — xaosflux Talk 19:44, 10 October 2024 (UTC)[reply]

Please update to new version

[edit]

Based on feedback (Mostly from User:Uwappa) there is a new version of this gadget that adds some requested features and fixes some bugs (An issue where sometimes fields are refreshed in the wrong order). Please update this page to the new version from https://mdwiki.org/wiki/MediaWiki:Gadget-calculator.js . Thanks Bawolff (talk) 06:48, 30 October 2024 (UTC)[reply]

Done for easy debugging:
  • display normally hidden checkboxes WHTR against NICE ranges 0.4 (LT GE), 0.5 (GE), 0.6 (GE)
  • display normally hidden radio buttons, which NICE health level warning to show.
To see bug:
- OK height 180 waist 89, one radio button checked
- NOK height 178, waist 88, no radio button checked
It looks like
  • OK radio for NICE level 0.5-0.6 switched off as WHtR drops below .05
  • NOK radio button for NICE level 0.40-0.5 does not switch on as WHtR drops below 0.5. No radio button selected from 4.
  • OK radio button for NICE level 0.40-0.50 does switch on when WHtR drops from 0.49 to 0.48
See fields in action at: Template:Body_roundness_index/sandbox
Good luck with debugging. Uwappa (talk) 07:15, 30 October 2024 (UTC)[reply]
@Uwappa: If you want to test the new version of the gadget, i was using https://mdwiki.org/wiki/User:Bawolff/sandbox2 as a test page (I didn't copy over the css since that is not really needed to test this). I believe the new version fixes the issue you mentioned. Bawolff (talk) 07:28, 30 October 2024 (UTC)[reply]
No, thanks, If you say it's a fixed bug than I just trust that, won't bother to redo your work.
Thank you! Uwappa (talk) 07:33, 30 October 2024 (UTC)[reply]
 Done * Pppery * it has begun... 16:18, 9 November 2024 (UTC)[reply]
What does 'done' mean?
I can't recreate the bug any more, so the fix is live?
So body roundness calculator version 4 can go live soon as well, escape the sandbox??? Uwappa (talk) 17:43, 9 November 2024 (UTC)[reply]
It means the script is updated, so the bug should no longer be present. If you encounter any more bugs, let me know. Bawolff (talk) 11:59, 11 November 2024 (UTC)[reply]
This is probably a Calculator bug as well:
- OK default height 180, waist 81, WHtR 0.45
- kind of OK height down to 79-> waist down to 80, WHtR down to 0.44692737430167595
(preferred: WHtR remains 4.5, just waist updated)
- NOK: height back up to 180->, WHtR 0.4444444444444444 (kind of OK), but waist not updated?
What I tried to do
- if new height, new waist, same WHtR
- if new waist, new WHtR, height unchanged
- if new WHtR, new waist, height unchanged Uwappa (talk) 15:03, 11 November 2024 (UTC)[reply]
I think its just refusing to update waist because it detects a loop is happening since waist and whtr both mutually depend on each other. Bawolff (talk) 18:20, 11 November 2024 (UTC)[reply]
Yes, I've been on that train of thought.
But why the f*** is there a difference between:
  • height from 179 to 180
  • height from 180 to 179
Uwappa (talk) 19:25, 11 November 2024 (UTC)[reply]

request to resync script

[edit]

Sorry, I realize I asked for an update not that long ago. There was recently some discussion about potentially using this gadget to make step-by-step interactive math proofs (e.g. User:Dimitris131/sandbox, User:Bawolff/math steps). It turns out that is possible with the current version of the gadget, however the resulting HTML doesn't follow accessibility best practices. Anyways, the new version from mdwiki supports making buttons with the proper HTML that should be accessible (Along with some unrelated minor fixes). Any chance we could get this gadget resynced with https://mdwiki.org/wiki/MediaWiki:Gadget-calculator.js ? Bawolff (talk) 18:35, 12 November 2024 (UTC)[reply]

 Done * Pppery * it has begun... 01:41, 13 November 2024 (UTC)[reply]

Request to resync

[edit]

Please resync this with https://mdwiki.org/wiki/MediaWiki:Gadget-calculator.js . New version has some small bug fixes and accessibility improvements. Thanks Bawolff (talk) 18:13, 20 November 2024 (UTC)[reply]

 Done * Pppery * it has begun... 04:46, 21 November 2024 (UTC)[reply]