Debian Codegraph @

extensions/Cite/modules/ext.cite.referencePreviews/index.js

raw symbols SCIP
1 const { initReferencePreviewsInstrumentation, LOGGING_SCHEMA } = require( './referencePreviewsInstrumentation.js' );
2 const createReferenceGateway = require( './createReferenceGateway.js' );
3 const createReferencePreview = require( './createReferencePreview.js' );
4 const TYPE_REFERENCE = 'reference';
5
6 /**
7 * Given the global state of the application, creates a function that gets
8 * whether or not the user should have Reference Previews enabled.
9 *
10 * @param {mw.user} user The `mw.user` singleton instance
11 * @param {Function} isPreviewTypeEnabled check whether preview has been disabled or enabled.
12 * @param {mw.Map} config
13 *
14 * @return {boolean|null} Null when there is no way the popup type can be enabled at run-time.
15 * @memberof module:ext.cite.referencePreviews
16 */
17 function isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ) {
18 if ( !config.get( 'wgCiteReferencePreviewsActive' ) ) {
19 return null;
20 }
21
22 if ( user.isAnon() ) {
23 return isPreviewTypeEnabled( TYPE_REFERENCE );
24 }
25
26 return true;
27 }
28
29 const referencePreviewsState = isReferencePreviewsEnabled(
30 mw.user,
31 mw.popups.isEnabled,
32 mw.config
33 );
34
35 // For tracking baseline stats in the Cite extension https://phabricator.wikimedia.org/T353798
36 // FIXME: This might be obsolete when the code moves to the Cite extension and the tracking there
37 // can check that state differently.
38 mw.config.set( 'wgCiteReferencePreviewsVisible', !!referencePreviewsState );
39
40 mw.trackSubscribe( 'Popups.SettingChange', ( data ) => {
41 if ( data.previewType === TYPE_REFERENCE ) {
42 mw.track( LOGGING_SCHEMA, data );
43 }
44 } );
45
46 /**
47 * Create the relevant config to register the preview type in the Popups extension.
48 *
49 * @see mw.popups.register()
50 * @memberof module:ext.cite.referencePreviews
51 */
52 function createReferencePreviewsType() {
53 return {
54 type: TYPE_REFERENCE,
55 selector: '#mw-content-text .reference a[ href*="#" ]',
56 delay: 150,
57 gateway: createReferenceGateway(),
58 renderFn: createReferencePreview,
59 init: () => {
60 initReferencePreviewsInstrumentation();
61 }
62 };
63 }
64
65 module.exports = referencePreviewsState !== null ? createReferencePreviewsType() : null;
66
67 // Expose private methods for QUnit tests
68 if ( typeof QUnit !== 'undefined' ) {
69 module.exports = { private: {
70 createReferenceGateway: require( './createReferenceGateway.js' ),
71 createReferencePreview: require( './createReferencePreview.js' ),
72 isReferencePreviewsEnabled
73 } };
74 }