Debian Codegraph @

extensions/Math/modules/ext.math.popup.js

raw symbols SCIP
1 ( function () {
2 'use strict';
3 const previewType = 'math';
4 const selector = '.mwe-math-element[href*="qid"], .mwe-math-element[data-qid] img';
5 const api = new mw.Rest();
6 const fetch = function ( qid ) {
7 return api.get( '/math/v0/popup/html/' + qid, {}, {
8 Accept: 'application/json; charset=utf-8',
9 'Accept-Language': mw.config.language
10 } );
11 };
12 const getQidStr = function ( parent ) {
13 if ( parent.getAttribute( 'href' ) ) {
14 const href = parent.getAttribute( 'href' );
15 const match = href.match( /qid=(Q\d+)/ );
16 if ( match ) {
17 return match[ 1 ];
18 }
19 }
20 return null;
21 };
22 const fetchPreviewForTitle = function ( title, el ) {
23 const deferred = $.Deferred();
24 const parent = el.closest( '.mwe-math-element' );
25 let qidstr = getQidStr( parent );
26 if ( parent.dataset.qid ) {
27 qidstr = parent.dataset.qid;
28 }
29 if ( !qidstr || ( qidstr.match( /Q\d+/g ) === null ) ) {
30 return deferred.reject();
31 }
32 qidstr = qidstr.slice( 1 );
33 fetch( qidstr ).then( ( body ) => {
34 const model = {
35 title: body.title,
36 url: body.canonicalurl,
37 languageCode: body.pagelanguagehtmlcode,
38 languageDirection: body.pagelanguagedir,
39 extract: body.extract,
40 type: previewType,
41 thumbnail: undefined,
42 pageId: body.pageId
43 };
44 deferred.resolve( model );
45 } );
46 return deferred.promise();
47 };
48 // popups require offsetHeight and offsetWidth attributes
49 [].forEach.call(
50 document.querySelectorAll( selector ),
51 ( node ) => {
52 // temporary hack to enable popup T380079
53 node.href = node.baseURI;
54 if ( typeof node.offsetWidth === 'undefined' ) {
55 node.offsetWidth = node.getBoundingClientRect().width || 1;
56 }
57 if ( typeof node.offsetHeight === 'undefined' ) {
58 node.offsetHeight = node.getBoundingClientRect().height || 1;
59 }
60 }
61 );
62
63 const mathDisabledByUser = mw.user.isNamed() && mw.user.options.get( 'math-popups' ) !== '1';
64 const mathAppliesToThisPage = document.querySelectorAll( selector ).length > 0;
65
66 module.exports = !mathAppliesToThisPage || mathDisabledByUser ? null : {
67 type: previewType,
68 selector,
69 gateway: {
70 fetch,
71 fetchPreviewForTitle
72 }
73 };
74 }() );