mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-27 17:00:37 +00:00
Load Modules support initialization
Rather than obscurely loading instrumentation code as a side effect of loading UI code, run it explicitly inside the index.js initalization code. Instrumentation is moved to its own file and Popups modules now support an init function. Change-Id: I9d2643ec8fb4e1dedc7ab9534b2965272f12335f
This commit is contained in:
parent
12e823ddbf
commit
d4376bf203
BIN
resources/dist/index.js
vendored
BIN
resources/dist/index.js
vendored
Binary file not shown.
BIN
resources/dist/index.js.map.json
vendored
BIN
resources/dist/index.js.map.json
vendored
Binary file not shown.
|
@ -29,6 +29,7 @@ import { previewTypes, getPreviewType,
|
|||
import isReferencePreviewsEnabled from './isReferencePreviewsEnabled';
|
||||
import setUserConfigFlags from './setUserConfigFlags';
|
||||
import { registerGatewayForPreviewType, getGatewayForPreviewType } from './gateway';
|
||||
import { initReferencePreviewsInstrumentation } from './instrumentation/referencePreviews';
|
||||
|
||||
const EXCLUDED_LINK_SELECTORS = [
|
||||
'.extiw',
|
||||
|
@ -236,7 +237,10 @@ function handleDOMEventIfEligible( handler ) {
|
|||
type: previewTypes.TYPE_REFERENCE,
|
||||
selector: '#mw-content-text .reference a[ href*="#" ]',
|
||||
gateway: referenceGateway,
|
||||
renderFn: createReferencePreview
|
||||
renderFn: createReferencePreview,
|
||||
init: () => {
|
||||
initReferencePreviewsInstrumentation();
|
||||
}
|
||||
} );
|
||||
}
|
||||
if ( !isAnythingEligible() ) {
|
||||
|
|
21
src/instrumentation/referencePreviews.js
Normal file
21
src/instrumentation/referencePreviews.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
let isTracking = false;
|
||||
|
||||
export const LOGGING_SCHEMA = 'event.ReferencePreviewsPopups';
|
||||
|
||||
/**
|
||||
* Run once the preview is initialized.
|
||||
*/
|
||||
export function initReferencePreviewsInstrumentation() {
|
||||
if ( mw.config.get( 'wgPopupsReferencePreviews' ) &&
|
||||
navigator.sendBeacon &&
|
||||
mw.config.get( 'wgIsArticle' ) &&
|
||||
!isTracking
|
||||
) {
|
||||
isTracking = true;
|
||||
mw.track( LOGGING_SCHEMA, { action: 'pageview' } );
|
||||
}
|
||||
}
|
||||
|
||||
export function isTrackingEnabled() {
|
||||
return isTracking;
|
||||
}
|
|
@ -57,7 +57,7 @@ export default function createMwPopups( store, registerModel, registerPreviewUI,
|
|||
* @param {PopupModule} module
|
||||
*/
|
||||
register: function ( module ) {
|
||||
const { type, selector, gateway, renderFn, subTypes } = module;
|
||||
const { type, selector, gateway, renderFn, subTypes, init } = module;
|
||||
if ( !type || !selector || !gateway ) {
|
||||
throw new Error(
|
||||
`Registration of Popups custom preview type "${type}" failed: You must specify a type, a selector, and a gateway.`
|
||||
|
@ -71,6 +71,10 @@ export default function createMwPopups( store, registerModel, registerPreviewUI,
|
|||
registerPreviewUI( subTypePreview.type, subTypePreview.renderFn );
|
||||
} );
|
||||
}
|
||||
// Run initialization function if provided.
|
||||
if ( typeof init === 'function' ) {
|
||||
init();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* @module referencePreview
|
||||
*/
|
||||
|
||||
import { isTrackingEnabled, LOGGING_SCHEMA } from '../../../instrumentation/referencePreviews';
|
||||
import { renderPopup } from '../popup/popup';
|
||||
import { createNodeFromTemplate, escapeHTML } from '../templateUtil';
|
||||
|
||||
|
@ -22,19 +22,6 @@ const templateHTML = `
|
|||
</footer>
|
||||
</div>`;
|
||||
|
||||
const LOGGING_SCHEMA = 'event.ReferencePreviewsPopups';
|
||||
let isTracking = false;
|
||||
$( () => {
|
||||
if ( mw.config.get( 'wgPopupsReferencePreviews' ) &&
|
||||
navigator.sendBeacon &&
|
||||
mw.config.get( 'wgIsArticle' ) &&
|
||||
!isTracking
|
||||
) {
|
||||
isTracking = true;
|
||||
mw.track( LOGGING_SCHEMA, { action: 'pageview' } );
|
||||
}
|
||||
} );
|
||||
|
||||
/**
|
||||
* @param {ext.popups.ReferencePreviewModel} model
|
||||
* @return {JQuery}
|
||||
|
@ -108,7 +95,7 @@ export function renderReferencePreview(
|
|||
$el.find( '.mwe-popups-container' ).addClass( 'footer-empty' );
|
||||
}
|
||||
|
||||
if ( isTracking ) {
|
||||
if ( isTrackingEnabled() ) {
|
||||
$el.find( '.mw-parser-output' ).on( 'click', 'a', () => {
|
||||
mw.track( LOGGING_SCHEMA, {
|
||||
action: 'clickedReferencePreviewsContentLink'
|
||||
|
@ -121,7 +108,7 @@ export function renderReferencePreview(
|
|||
// We are dealing with floating point numbers here when the page is zoomed!
|
||||
scrolledToBottom = element.scrollTop >= element.scrollHeight - element.clientHeight - 1;
|
||||
|
||||
if ( isTracking ) {
|
||||
if ( isTrackingEnabled() ) {
|
||||
if ( !element.isOpenRecorded ) {
|
||||
mw.track( LOGGING_SCHEMA, {
|
||||
action: 'poppedOpen',
|
||||
|
|
Loading…
Reference in a new issue