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 ) {
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 = .( '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( )
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() {
var elms = Array.from( document.getElementsByClassName( 'calculator-field' ) ); var elms = Array.from( .( '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] );
} }
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 = .( 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] );
} }
} }
Line 369: Line 373:
} }
var refresh = function (itemId) { var refresh = function (itemId) {
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] );
var elm = document.getElementById( "calculator-field-" + itemId ); var elm = .( "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] );
} }
} }
$( setup ); ( 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]