mediawiki-extensions-Math/modules/ext.math.popup.js
AndreG-P 95c5757ee5 Enable popups for annotated math formulae
Enable popup extension for annotated math expressions. The popup shows a short summary of the formula and is generated via the Popup API introduced with I65fcbf25ac5818f6c649daf494c719921247e8f5. It is a follow up of Iefe98c1f0422dbf034e385b1a41a859d030a2cf4.

Bug: T208758
Change-Id: I8c45b7e441083c240bbf2e0217ec219df7dcc351
2022-12-06 06:07:22 +00:00

52 lines
1.4 KiB
JavaScript

( function () {
'use strict';
var previewType = 'math';
var api = new mw.Rest();
var isValidId = function ( qid ) {
return qid.match( /Q\d+/g ) === null;
};
var fetch = function ( qid ) {
return api.get( '/math/v0/popup/html/' + qid, {}, {
Accept: 'application/json; charset=utf-8',
'Accept-Language': mw.config.language
} );
};
var fetchPreviewForTitle = function ( title, el ) {
var deferred = $.Deferred();
var qidstr = el.parentNode.parentNode.dataset.qid;
if ( isValidId( qidstr ) ) {
return deferred.reject();
}
qidstr = qidstr.slice( 1 );
fetch( qidstr ).then( function ( body ) {
var model = {
title: body.title,
url: body.canonicalurl,
languageCode: body.pagelanguagehtmlcode,
languageDirection: body.pagelanguagedir,
extract: body.extract,
type: previewType,
thumbnail: undefined,
pageId: body.pageId
};
deferred.resolve( model );
} );
return deferred.promise();
};
// popups require title attributes
document.querySelectorAll( '.mwe-math-element[data-qid] img' )
.forEach( function ( node ) {
if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) {
node.dataset.title = 'math-unique-identifier';
}
} );
module.exports = {
type: previewType,
selector: '.mwe-math-element[data-qid] img',
gateway: {
fetch: fetch,
fetchPreviewForTitle: fetchPreviewForTitle
}
};
}() );