2022-07-08 05:13:08 +00:00
|
|
|
( function () {
|
|
|
|
'use strict';
|
2024-02-13 00:31:24 +00:00
|
|
|
const previewType = 'math';
|
2024-11-29 01:34:57 +00:00
|
|
|
const selector = '.mwe-math-element[href*="qid"], .mwe-math-element[data-qid] img';
|
2024-02-13 00:31:24 +00:00
|
|
|
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-11-29 01:34:57 +00:00
|
|
|
const getQidStr = function ( parent ) {
|
|
|
|
if ( parent.getAttribute( 'href' ) ) {
|
|
|
|
const href = parent.getAttribute( 'href' );
|
|
|
|
const match = href.match( /qid=(Q\d+)/ );
|
|
|
|
if ( match ) {
|
|
|
|
return match[ 1 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2024-02-13 00:31:24 +00:00
|
|
|
const fetchPreviewForTitle = function ( title, el ) {
|
|
|
|
const deferred = $.Deferred();
|
2024-11-29 01:34:57 +00:00
|
|
|
const parent = el.closest( '.mwe-math-element' );
|
|
|
|
let qidstr = getQidStr( parent );
|
|
|
|
if ( parent.dataset.qid ) {
|
|
|
|
qidstr = parent.dataset.qid;
|
|
|
|
}
|
|
|
|
if ( !qidstr || isValidId( qidstr ) ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
return deferred.reject();
|
|
|
|
}
|
|
|
|
qidstr = qidstr.slice( 1 );
|
2024-06-07 14:24:08 +00:00
|
|
|
fetch( qidstr ).then( ( 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(
|
2024-11-29 01:34:57 +00:00
|
|
|
document.querySelectorAll( selector ),
|
2024-06-07 14:24:08 +00:00
|
|
|
( node ) => {
|
2024-11-29 01:34:57 +00:00
|
|
|
const qidstr = getQidStr( node );
|
|
|
|
if ( qidstr && isValidId( qidstr ) ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
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 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
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|