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]