mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-27 17:01:07 +00:00
f382be8562
npm: * eslint-config-wikimedia: 0.25.1 → 0.26.0 * grunt-banana-checker: 0.11.0 → 0.11.1 * wdio-mediawiki: 2.1.0 → 2.5.0 * get-func-name: 2.0.0 → 2.0.2 * https://github.com/advisories/GHSA-4q6p-r6v2-jvc5 * postcss: 8.4.30 → 8.4.35 * https://github.com/advisories/GHSA-7fh5-64p2-3v2j Change-Id: I5b01fa01c11a57a180b50bf2f8b3275e69d75f1c
59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
( function () {
|
|
'use strict';
|
|
const previewType = 'math';
|
|
const api = new mw.Rest();
|
|
const isValidId = function ( qid ) {
|
|
return qid.match( /Q\d+/g ) === null;
|
|
};
|
|
const fetch = function ( qid ) {
|
|
return api.get( '/math/v0/popup/html/' + qid, {}, {
|
|
Accept: 'application/json; charset=utf-8',
|
|
'Accept-Language': mw.config.language
|
|
} );
|
|
};
|
|
const fetchPreviewForTitle = function ( title, el ) {
|
|
const deferred = $.Deferred();
|
|
let qidstr = el.parentNode.parentNode.dataset.qid;
|
|
if ( isValidId( qidstr ) ) {
|
|
return deferred.reject();
|
|
}
|
|
qidstr = qidstr.slice( 1 );
|
|
fetch( qidstr ).then( function ( body ) {
|
|
const 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
|
|
[].forEach.call(
|
|
document.querySelectorAll( '.mwe-math-element[data-qid] img' ),
|
|
function ( node ) {
|
|
if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) {
|
|
node.dataset.title = 'math-unique-identifier';
|
|
}
|
|
}
|
|
);
|
|
|
|
const mathDisabledByUser = mw.user.isNamed() && mw.user.options.get( 'math-popups' ) !== '1';
|
|
const selector = '.mwe-math-element[data-qid] img';
|
|
const mathAppliesToThisPage = document.querySelectorAll( selector ).length > 0;
|
|
|
|
module.exports = !mathAppliesToThisPage || mathDisabledByUser ? null : {
|
|
type: previewType,
|
|
selector,
|
|
gateway: {
|
|
fetch,
|
|
fetchPreviewForTitle
|
|
}
|
|
};
|
|
}() );
|