2022-07-08 05:13:08 +00:00
|
|
|
( function () {
|
|
|
|
'use strict';
|
2024-02-13 00:31:24 +00:00
|
|
|
const previewType = 'math';
|
|
|
|
const api = new mw.Rest();
|
|
|
|
const isValidId = function ( qid ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
return qid.match( /Q\d+/g ) === null;
|
|
|
|
};
|
2024-02-13 00:31:24 +00:00
|
|
|
const fetch = function ( qid ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
return api.get( '/math/v0/popup/html/' + qid, {}, {
|
|
|
|
Accept: 'application/json; charset=utf-8',
|
|
|
|
'Accept-Language': mw.config.language
|
|
|
|
} );
|
|
|
|
};
|
2024-02-13 00:31:24 +00:00
|
|
|
const fetchPreviewForTitle = function ( title, el ) {
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
let qidstr = el.parentNode.parentNode.dataset.qid;
|
2022-07-08 05:13:08 +00:00
|
|
|
if ( isValidId( qidstr ) ) {
|
|
|
|
return deferred.reject();
|
|
|
|
}
|
|
|
|
qidstr = qidstr.slice( 1 );
|
|
|
|
fetch( qidstr ).then( function ( body ) {
|
2024-02-13 00:31:24 +00:00
|
|
|
const model = {
|
2022-07-08 05:13:08 +00:00
|
|
|
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
|
2023-03-25 21:49:04 +00:00
|
|
|
[].forEach.call(
|
|
|
|
document.querySelectorAll( '.mwe-math-element[data-qid] img' ),
|
|
|
|
function ( node ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) {
|
|
|
|
node.dataset.title = 'math-unique-identifier';
|
|
|
|
}
|
2023-03-25 21:49:04 +00:00
|
|
|
}
|
|
|
|
);
|
2023-10-17 23:28:57 +00:00
|
|
|
|
|
|
|
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 : {
|
2022-07-08 05:13:08 +00:00
|
|
|
type: previewType,
|
2023-10-17 23:28:57 +00:00
|
|
|
selector,
|
2022-07-08 05:13:08 +00:00
|
|
|
gateway: {
|
2023-10-17 23:28:57 +00:00
|
|
|
fetch,
|
|
|
|
fetchPreviewForTitle
|
2022-07-08 05:13:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|