"/* global $ */\nconst M = require( '../mobile.startup/moduleLoaderSingleton' ),\n\tutil = require( '../mobile.startup/util' ),\n\teditorLoadingOverlay = require( './editorLoadingOverlay' ),\n\tOverlayManager = require( '../mobile.startup/OverlayManager' ),\n\t// #ca-edit, .mw-editsection are standard MediaWiki elements\n\t// .edit-link can be added to links anywhere to trigger the editor (e.g. MobileFrontend\n\t// user page creation CTA, edit-full-page overflow menu item)\n\t// Links in content are handled separately to allow reloading the content (T324686)\n\t$editTab = $( '#ca-edit, #ca-editsource, #ca-viewsource, #ca-ve-edit, #ca-ve-create, #ca-createsource' ),\n\thasTwoEditIcons = $editTab.length > 1,\n\tEDITSECTION_SELECTOR = '.mw-editsection a, .edit-link',\n\tuser = mw.user,\n\tCtaDrawer = require( '../mobile.startup/CtaDrawer' ),\n\tveConfig = mw.config.get( 'wgVisualEditorConfig' ),\n\teditorPath = /^\\/editor\\/(\\d+|T-\\d+|all)$/;\n\nlet editorOverride = null;\n\n/**\n * Event handler for edit link clicks. Will prevent default link\n * behaviour and will not allow propagation\n *\n * @method\n * @ignore\n * @param {HTMLElement} elem\n * @param {jQuery.Event} ev\n * @param {Router} router\n */\nfunction onEditLinkClick( elem, ev, router ) {\n\tlet section;\n\tif ( $( EDITSECTION_SELECTOR ).length === 0 ) {\n\t\t// If section edit links are not available, the only edit link\n\t\t// should allow editing the whole page (T232170)\n\t\tsection = 'all';\n\t} else {\n\t\tsection = mw.util.getParamValue( 'section', elem.href ) || 'all';\n\t}\n\t// Don't do anything for section edit links for different pages (transcluded)\n\tif ( mw.config.get( 'wgPageName' ) !== mw.util.getParamValue( 'title', elem.href ) ) {\n\t\treturn;\n\t}\n\tif ( hasTwoEditIcons ) {\n\t\tif ( elem.id === 'ca-ve-edit' || elem.id === 'ca-ve-create' ) {\n\t\t\t// \"Edit\" tab loads the visual editor\n\t\t\teditorOverride = 'VisualEditor';\n\t\t} else if ( elem.id === 'ca-editsource' || elem.id === 'ca-createsource' ) {\n\t\t\t// \"Edit source\" tab loads the source editor\n\t\t\teditorOverride = 'SourceEditor';\n\t\t} else {\n\t\t\t// Any other edit links (e.g. for sections) load the preferred editor\n\t\t}\n\t}\n\trouter.navigate( '#/editor/' + section );\n\t// DO NOT USE stopPropagation or you'll break click tracking in WikimediaEvents\n\t// You DO NOT NEED to\n\t// prevent folding section when clicking Edit by stopping propagation\n\t// as this is a concern of the Toggler class and taken care of by inspecting\n\t// !ev.target.href (see Toggler.js)\n\t// avoid navigating to ?action=edit\n\tev.preventDefault();\n}\n\n/**\n * Retrieve the user's preferred editor setting. If none is set, return the default\n * editor for this wiki.\n *\n * @method\n * @ignore\n * @return {string} Either 'VisualEditor' or 'SourceEditor'\n */\nfunction getPreferredEditor() {\n\tif ( editorOverride ) {\n\t\t// Temporary override, set via the URL for this request\n\t\t// or by clicking the chosen mode when both tabs are shown\n\t\treturn editorOverride;\n\t}\n\tconst preferredEditor = mw.user.options.get( 'mobile-editor' ) || mw.storage.get( 'preferredEditor' );\n\tif ( preferredEditor ) {\n\t\treturn preferredEditor;\n\t}\n\tswitch ( mw.config.get( 'wgMFDefaultEditor' ) ) {\n\t\tcase 'source':\n\t\t\treturn 'SourceEditor';\n\t\tcase 'visual':\n\t\t\treturn 'VisualEditor';\n\t\tcase 'preference':\n\t\t\t// First check if the user has actually used the desktop editor.\n\t\t\t// This is done hackily by checking if they have the preference\n\t\t\t// set to suppress the welcome dialog or user education popups. (T261423)\n\t\t\tif ( mw.user.options.get( 'visualeditor-hidebetawelcome' ) || mw.user.options.get( 'visualeditor-hideusered' ) ) {\n\t\t\t\treturn mw.user.options.get( 'visualeditor-editor' ) === 'visualeditor' ? 'VisualEditor' : 'SourceEditor';\n\t\t\t} else {\n\t\t\t\t// When there is no desktop preference, we use MFFallbackEditor.\n\t\t\t\treturn mw.config.get( 'wgMFFallbackEditor' ) === 'visual' ? 'VisualEditor' : 'SourceEditor';\n\t\t\t}\n\t}\n\t// In the event of misconfiguration, fall back to source\n\treturn 'SourceEditor';\n}\n\n/**\n * Initialize the edit button so that it launches the editor interface when clicked.\n *\n * @method\n * @ignore\n * @param {Page} page The page to edit.\n * @param {Skin} skin\n * @param {module:mobile.startup/PageHTMLParser} currentPageHTMLParser\n * @param {Router} router\n */\nfunction setupEditor( page, skin, currentPageHTMLParser, router ) {\n\tconst\n\t\toverlayManager = OverlayManager.getSingleton(),\n\t\tisNewPage = page.id === 0;\n\n\t$editTab.add( '.edit-link' ).on( 'click.mfeditlink', function ( ev ) {\n\t\tonEditLinkClick( this, ev, overlayManager.router );\n\t} );\n\tmw.hook( 'wikipage.content' ).add( function ( $content ) {\n\t\t// make sure that any .edit-link links in here don't get double-handled\n\t\t$content.find( EDITSECTION_SELECTOR ).off( 'click.mfeditlink' ).on( 'click.mfeditlink', function ( ev ) {\n\t\t\tonEditLinkClick( this, ev, overlayManager.router );\n\t\t} );\n\t} );\n\n\toverlayManager.add( editorPath, function ( sectionId ) {\n\t\tconst\n\t\t\tscrollTop = window.pageYOffset,\n\t\t\t$contentText = $( '#mw-content-text' ),\n\t\t\turl = new URL( location.href ),\n\t\t\teditorOptions = {\n\t\t\t\toverlayManager: overlayManager,\n\t\t\t\tcurrentPageHTMLParser: currentPageHTMLParser,\n\t\t\t\tfakeScroll: 0,\n\t\t\t\tapi: new mw.Api(),\n\t\t\t\tlicenseMsg: skin.getLicenseMsg(),\n\t\t\t\ttitle: page.title,\n\t\t\t\ttitleObj: page.titleObj,\n\t\t\t\tisAnon: user.isAnon(),\n\t\t\t\tisNewPage: isNewPage,\n\t\t\t\toldId: mw.util.getParamValue( 'oldid' ),\n\t\t\t\tcontentLang: $contentText.attr( 'lang' ),\n\t\t\t\tcontentDir: $contentText.attr( 'dir' ),\n\t\t\t\t// Arrange preload content if we're on a page with those URL parameters\n\t\t\t\tpreload: url.searchParams.get( 'preload' ),\n\t\t\t\tpreloadparams: mw.util.getArrayParam( 'preloadparams', url.searchParams ),\n\t\t\t\teditintro: url.searchParams.get( 'editintro' )\n\t\t\t},\n\t\t\tvisualAbortPromise = $.Deferred();\n\n\t\tconst animationDelayDeferred = util.Deferred();\n\n\t\tlet abortableDataPromise, overlayPromise,\n\t\t\tinitMechanism = mw.util.getParamValue( 'redlink' ) ? 'new' : 'click';\n\n\t\tif ( sectionId !== 'all' ) {\n\t\t\teditorOptions.sectionId = page.isWikiText() ? sectionId : undefined;\n\t\t}\n\n\t\tfunction showLoading() {\n\t\t\tlet $sectionTop, fakeScroll, enableVisualSectionEditing;\n\n\t\t\t$( document.body ).addClass( 've-loading' );\n\n\t\t\tconst $page = $( '#mw-mf-page-center' );\n\t\t\tconst $content = $( '#content' );\n\t\t\tif ( sectionId === '0' || sectionId === 'all' ) {\n\t\t\t\t$sectionTop = $( '#bodyContent' );\n\t\t\t} else {\n\t\t\t\t$sectionTop = $(\n\t\t\t\t\t// ends with section=N\n\t\t\t\t\t'a[href$=\"section=' + sectionId + '\"],' +\n\t\t\t\t\t// contains section=N&...\n\t\t\t\t\t'a[href*=\"section=' + sectionId + '&\"]'\n\t\t\t\t).closest( '.mw-heading, h1, h2, h3, h4, h5, h6' );\n\t\t\t\t// When loading on action=edit URLs, there is no page content\n\t\t\t\tif ( !$sectionTop.length ) {\n\t\t\t\t\t$sectionTop = $( '#bodyContent' );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Pretend that we didn't just scroll the page to the top.\n\t\t\t$page.prop( 'scrollTop', scrollTop );\n\t\t\t// Then, pretend that we're scrolling to the position of the clicked heading.\n\t\t\tfakeScroll = $sectionTop[0].getBoundingClientRect().top;\n\t\t\t// Adjust for height of the toolbar.\n\t\t\tfakeScroll -= 48;\n\t\t\tif ( shouldLoadVisualEditor() ) {\n\t\t\t\tenableVisualSectionEditing = veConfig.enableVisualSectionEditing === true ||\n\t\t\t\t\t// === ve.init.mw.MobileArticleTarget.static.trackingName\n\t\t\t\t\tveConfig.enableVisualSectionEditing === 'mobile';\n\t\t\t\tif ( sectionId === '0' || sectionId === 'all' || enableVisualSectionEditing ) {\n\t\t\t\t\t// Adjust for surface padding. Only needed if we're at the beginning of the doc.\n\t\t\t\t\tfakeScroll -= 16;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif ( sectionId === '0' || sectionId === 'all' ) {\n\t\t\t\t\tfakeScroll -= 16;\n\t\t\t\t}\n\t\t\t}\n\t\t\t$content.css( {\n\t\t\t\t// Use transform instead of scroll for smoother animation (via CSS transitions).\n\t\t\t\ttransform: 'translate( 0, ' + -fakeScroll + 'px )',\n\t\t\t\t// If the clicked heading is near the end of the page, we might need to insert\n\t\t\t\t// some extra space to allow us to scroll \"beyond the end\" of the page.\n\t\t\t\t'padding-bottom': '+=' + fakeScroll,\n\t\t\t\t'margin-bottom': '-=' + fakeScroll\n\t\t\t} );\n\t\t\teditorOptions.fakeScroll = fakeScroll;\n\t\t\tsetTimeout( animationDelayDeferred.resolve, 500 );\n\t\t}\n\n\t\tfunction clearLoading() {\n\t\t\tif ( abortableDataPromise && abortableDataPromise.abort ) {\n\t\t\t\tabortableDataPromise.abort();\n\t\t\t}\n\n\t\t\t$( '#content' ).css( {\n\t\t\t\ttransform: '',\n\t\t\t\t'padding-bottom': '',\n\t\t\t\t'margin-bottom': ''\n\t\t\t} );\n\n\t\t\t$( document.body ).removeClass( 've-loading' );\n\t\t}\n\n\t\tfunction loadBasicEditor() {\n\t\t\t// Note that this option was used when logging a wikitext init later\n\t\t\tinitMechanism = 'tooslow';\n\n\t\t\t// This restarts the loading (whether it was aborted when loading the code or the data)\n\t\t\tvisualAbortPromise.reject();\n\t\t\tif ( abortableDataPromise && abortableDataPromise.abort ) {\n\t\t\t\tabortableDataPromise.abort();\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Log init event to edit schema.\n\t\t * Need to log this from outside the Overlay object because that module\n\t\t * won't have loaded yet.\n\t\t *\n\t\t * @private\n\t\t * @ignore\n\t\t * @param {string} editor name e.g. wikitext or visualeditor\n\t\t * @method\n\t\t */\n\t\tfunction logInit( editor ) {\n\t\t\tmw.track( 'editAttemptStep', {\n\t\t\t\taction: 'init',\n\t\t\t\ttype: 'section',\n\t\t\t\tmechanism: initMechanism,\n\t\t\t\tintegration: 'page',\n\t\t\t\t/* eslint-disable camelcase */\n\t\t\t\teditor_interface: editor\n\t\t\t\t/* eslint-enable camelcase */\n\t\t\t} );\n\t\t}\n\n\t\t/**\n\t\t * Check whether VisualEditor should be loaded\n\t\t *\n\t\t * @private\n\t\t * @ignore\n\t\t * @method\n\t\t * @return {bool}\n\t\t */\n\t\tfunction shouldLoadVisualEditor() {\n\t\t\tconst preferredEditor = getPreferredEditor();\n\n\t\t\treturn page.isVESourceAvailable() || (\n\t\t\t\tpage.isVEVisualAvailable() &&\n\t\t\t\t// If the user prefers visual mode or the user has no preference and\n\t\t\t\t// the visual mode is the default editor for this wiki\n\t\t\t\tpreferredEditor === 'VisualEditor'\n\t\t\t);\n\t\t}\n\n\t\t/**\n\t\t * Load source editor\n\t\t *\n\t\t * @ignore\n\t\t * @method\n\t\t * @return {jQuery.Promise} Promise resolved with the editor overlay\n\t\t * @fires mobileFrontend.editorOpening\n\t\t */\n\t\tfunction loadSourceEditor() {\n\t\t\tlogInit( 'wikitext' );\n\t\t\t// Inform other interested code that we're loading the editor\n\t\t\t/**\n\t\t\t * Internal for use in GrowthExperiments only.\n\t\t\t *\n\t\t\t * @event ~'mobileFrontend.editorOpening'\n\t\t\t * @memberof Hooks\n\t\t\t */\n\t\t\tmw.hook( 'mobileFrontend.editorOpening' ).fire();\n\n\t\t\treturn mw.loader.using( 'mobile.editor.overlay' ).then( function () {\n\t\t\t\tconst SourceEditorOverlay = M.require( 'mobile.editor.overlay/SourceEditorOverlay' );\n\t\t\t\treturn new SourceEditorOverlay( editorOptions );\n\t\t\t} );\n\t\t}\n\n\t\t/**\n\t\t * Load visual editor. If it fails to load for any reason, load the source editor instead.\n\t\t *\n\t\t * @private\n\t\t * @ignore\n\t\t * @method\n\t\t * @return {jQuery.Promise} Promise resolved with the editor overlay\n\t\t */\n\t\tfunction loadVisualEditorMaybe() {\n\t\t\tlogInit( 'visualeditor' );\n\t\t\t// Inform other interested code that we're loading the editor\n\t\t\t/**\n\t\t\t * Internal for use in GrowthExperiments only.\n\t\t\t *\n\t\t\t * @event ~'mobileFrontend.editorOpening'\n\t\t\t * @memberof Hooks\n\t\t\t */\n\t\t\tmw.hook( 'mobileFrontend.editorOpening' ).fire();\n\n\t\t\teditorOptions.mode = mw.config.get( 'wgMFEnableVEWikitextEditor' ) && getPreferredEditor() === 'SourceEditor' ?\n\t\t\t\t'source' :\n\t\t\t\t'visual';\n\t\t\teditorOptions.dataPromise = mw.loader.using( 'ext.visualEditor.targetLoader' ).then( function () {\n\t\t\t\tabortableDataPromise = mw.libs.ve.targetLoader.requestPageData(\n\t\t\t\t\teditorOptions.mode,\n\t\t\t\t\teditorOptions.titleObj.getPrefixedDb(),\n\t\t\t\t\t{\n\t\t\t\t\t\tsessionStore: true,\n\t\t\t\t\t\tsection: editorOptions.sectionId === undefined ?\n\t\t\t\t\t\t\tnull : editorOptions.sectionId,\n\t\t\t\t\t\toldId: editorOptions.oldId || undefined,\n\t\t\t\t\t\tpreload: editorOptions.preload,\n\t\t\t\t\t\tpreloadparams: editorOptions.preloadparams,\n\t\t\t\t\t\teditintro: editorOptions.editintro,\n\t\t\t\t\t\t// Should be ve.init.mw.MobileArticleTarget.static.trackingName,\n\t\t\t\t\t\t// but the class hasn't loaded yet.\n\t\t\t\t\t\ttargetName: 'mobile'\n\t\t\t\t\t} );\n\t\t\t\treturn abortableDataPromise;\n\t\t\t} );\n\n\t\t\tconst visualLoadingPromise = mw.loader.using( 'ext.visualEditor.targetLoader' )\n\t\t\t\t.then( function () {\n\t\t\t\t\t// Load 'mobile.editor.overlay' separately, so that if we fall back to basic\n\t\t\t\t\t// editor, we can display it without waiting for the visual code\n\t\t\t\t\treturn mw.loader.using( 'mobile.editor.overlay' ).then( function () {\n\t\t\t\t\t\tmw.libs.ve.targetLoader.addPlugin( 'ext.visualEditor.mobileArticleTarget' );\n\t\t\t\t\t\tif ( mw.config.get( 'wgMFEnableVEWikitextEditor' ) ) {\n\t\t\t\t\t\t\t// Target loader only loads wikitext editor if the desktop\n\t\t\t\t\t\t\t// preference is set.\n\t\t\t\t\t\t\t// TODO: Have a cleaner API for this instead of duplicating\n\t\t\t\t\t\t\t// the module name here.\n\t\t\t\t\t\t\tmw.libs.ve.targetLoader.addPlugin( 'ext.visualEditor.mwwikitext' );\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn mw.libs.ve.targetLoader.loadModules( editorOptions.mode );\n\t\t\t\t\t} );\n\t\t\t\t} );\n\n\t\t\t// Continue when loading is completed or aborted\n\t\t\tconst visualPromise = $.Deferred();\n\t\t\tvisualLoadingPromise.then( visualPromise.resolve, visualPromise.reject );\n\t\t\tvisualAbortPromise.then( visualPromise.reject, visualPromise.reject );\n\n\t\t\treturn visualPromise\n\t\t\t\t.then( function () {\n\t\t\t\t\tconst VisualEditorOverlay = M.require( 'mobile.editor.overlay/VisualEditorOverlay' ),\n\t\t\t\t\t\tSourceEditorOverlay = M.require( 'mobile.editor.overlay/SourceEditorOverlay' );\n\t\t\t\t\teditorOptions.SourceEditorOverlay = SourceEditorOverlay;\n\t\t\t\t\treturn new VisualEditorOverlay( editorOptions );\n\t\t\t\t}, function () {\n\t\t\t\t\treturn loadSourceEditor();\n\t\t\t\t} );\n\t\t}\n\n\t\t// showLoading() has to run after the overlay has opened, which disables page scrolling.\n\t\t// clearLoading() has to run after the loading overlay is hidden in any way\n\t\t// (either when loading is aborted, or when the editor overlay is shown instead).\n\t\tconst loadingOverlay = editorLoadingOverlay(\n\t\t\tshowLoading, clearLoading, shouldLoadVisualEditor() ? loadBasicEditor : null\n\t\t);\n\n\t\tif ( shouldLoadVisualEditor() ) {\n\t\t\toverlayPromise = loadVisualEditorMaybe();\n\t\t} else {\n\t\t\toverlayPromise = loadSourceEditor();\n\t\t}\n\n\t\t// Wait for the scroll animation to finish before we show the editor overlay\n\t\tutil.Promise.all( [ overlayPromise, animationDelayDeferred ] ).then( function ( overlay ) {\n\t\t\t// Wait for the data to load before we show the editor overlay\n\t\t\toverlay.getLoadingPromise().catch( function ( error ) {\n\t\t\t\tif ( visualAbortPromise.state() === 'rejected' ) {\n\t\t\t\t\treturn loadSourceEditor().then( function ( sourceOverlay ) {\n\t\t\t\t\t\toverlay = sourceOverlay;\n\t\t\t\t\t\treturn overlay.getLoadingPromise();\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\treturn $.Deferred().reject( error ).promise();\n\t\t\t} ).then( function () {\n\t\t\t\t// Make sure the user did not close the loading overlay while we were waiting\n\t\t\t\tconst overlayData = overlayManager.stack[0];\n\t\t\t\tif ( !overlayData || overlayData.overlay !== loadingOverlay ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\t// Show the editor!\n\t\t\t\toverlayManager.replaceCurrent( overlay );\n\t\t\t}, function ( error, apiResponse ) {\n\t\t\t\t// Could not load the editor.\n\t\t\t\toverlayManager.router.back();\n\t\t\t\tif ( error.show ) {\n\t\t\t\t\t// Probably a blockMessageDrawer returned because the user is blocked.\n\t\t\t\t\tdocument.body.appendChild( error.$el[ 0 ] );\n\t\t\t\t\terror.show();\n\t\t\t\t} else if ( apiResponse ) {\n\t\t\t\t\tmw.notify( editorOptions.api.getErrorMessage( apiResponse ) );\n\t\t\t\t} else {\n\t\t\t\t\tmw.notify( mw.msg( 'mobile-frontend-editor-error-loading' ) );\n\t\t\t\t}\n\t\t\t} );\n\t\t} );\n\n\t\t// Reset the temporary override for the next load\n\t\teditorOverride = null;\n\n\t\treturn loadingOverlay;\n\t} );\n\n\t$( '#ca-edit a, a#ca-edit, #ca-editsource a, a#ca-editsource' ).prop( 'href', function ( i, href ) {\n\t\tconst editUrl = new URL( href, location.href );\n\t\t// By default the editor opens section 0 (lead section), rather than the whole article.\n\t\t// This might be changed in the future (T210659).\n\t\teditUrl.searchParams.set( 'section', '0' );\n\t\treturn editUrl.toString();\n\t} );\n\n\t// We use wgAction instead of getParamValue('action') as the former can be\n\t// overridden by hooks to stop the editor loading automatically.\n\tif ( !router.getPath() && ( mw.util.getParamValue( 'veaction' ) || mw.config.get( 'wgAction' ) === 'edit' ) ) {\n\t\tif ( mw.util.getParamValue( 'veaction' ) === 'edit' ) {\n\t\t\teditorOverride = 'VisualEditor';\n\t\t} else if ( mw.util.getParamValue( 'veaction' ) === 'editsource' ) {\n\t\t\teditorOverride = 'SourceEditor';\n\t\t}\n\t\t// else: action=edit, for which we allow the default to take effect\n\t\tconst fragment = '#/editor/' + ( mw.util.getParamValue( 'section' ) || ( mw.config.get( 'wgAction' ) === 'edit' ? 'all' : '0' ) );\n\t\t// eslint-disable-next-line no-restricted-properties\n\t\tif ( window.history && history.pushState ) {\n\t\t\t// We're reformatting the action=edit URL into a view URL and\n\t\t\t// replacing it into the history, and then will fall through to\n\t\t\t// router.navigate which will move us to the editing URL for the\n\t\t\t// mobile site. We do this because the editor overlay deeply\n\t\t\t// expects to have been opened on top of an actual page, and e.g.\n\t\t\t// closing the editor via the X will produce unexpected behavior\n\t\t\t// otherwise.\n\t\t\tconst url = new URL( location.href );\n\t\t\turl.searchParams.delete( 'action' );\n\t\t\turl.searchParams.delete( 'veaction' );\n\t\t\turl.searchParams.delete( 'section' );\n\t\t\thistory.replaceState( null, document.title, url );\n\t\t}\n\t\tutil.docReady( function () {\n\t\t\trouter.navigate( fragment );\n\t\t} );\n\t}\n}\n\n/**\n * Hide any section id icons in the page. This will not hide the edit icon in the page action\n * menu.\n *\n * @method\n * @ignore\n * @param {module:mobile.startup/PageHTMLParser} currentPageHTMLParser\n */\nfunction hideSectionEditIcons( currentPageHTMLParser ) {\n\tcurrentPageHTMLParser.$el.find( '.mw-editsection' ).hide();\n}\n\n/**\n * Show a drawer with log in / sign up buttons.\n *\n * @method\n * @ignore\n * @param {Router} router\n */\nfunction bindEditLinksLoginDrawer( router ) {\n\tlet drawer;\n\tfunction showLoginDrawer() {\n\t\tif ( !drawer ) {\n\t\t\tdrawer = new CtaDrawer( {\n\t\t\t\tcontent: mw.msg( 'mobile-frontend-editor-disabled-anon' ),\n\t\t\t\tsignupQueryParams: {\n\t\t\t\t\twarning: 'mobile-frontend-watchlist-signup-action'\n\t\t\t\t}\n\t\t\t} );\n\t\t\tdocument.body.appendChild( drawer.$el[ 0 ] );\n\t\t}\n\t\tdrawer.show();\n\t}\n\t$editTab.on( 'click', function ( ev ) {\n\t\tshowLoginDrawer();\n\t\tev.preventDefault();\n\t} );\n\tmw.hook( 'wikipage.content' ).add( function ( $content ) {\n\t\t$content.find( EDITSECTION_SELECTOR ).on( 'click', function ( ev ) {\n\t\t\tshowLoginDrawer();\n\t\t\tev.preventDefault();\n\t\t} );\n\t} );\n\trouter.addRoute( editorPath, function () {\n\t\tshowLoginDrawer();\n\t} );\n\trouter.checkRoute();\n}\n\n/**\n * Setup the editor if the user can edit the page otherwise show a sorry toast.\n *\n * @method\n * @ignore\n * @param {Page} currentPage\n * @param {module:mobile.startup/PageHTMLParser} currentPageHTMLParser\n * @param {Skin} skin\n * @param {Router} router\n */\nfunction init( currentPage, currentPageHTMLParser, skin, router ) {\n\tlet editErrorMessage, editRestrictions;\n\t// see: https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#Page-specific\n\tconst isReadOnly = mw.config.get( 'wgMinervaReadOnly' );\n\tconst isEditable = !isReadOnly && mw.config.get( 'wgIsProbablyEditable' );\n\n\tif ( isEditable ) {\n\t\t// Edit button updated in setupEditor.\n\t\tsetupEditor( currentPage, skin, currentPageHTMLParser, router );\n\t} else {\n\t\thideSectionEditIcons( currentPageHTMLParser );\n\t\teditRestrictions = mw.config.get( 'wgRestrictionEdit' );\n\t\tif ( mw.user.isAnon() && Array.isArray( editRestrictions ) && !editRestrictions.length ) {\n\t\t\tbindEditLinksLoginDrawer( router );\n\t\t} else {\n\t\t\tconst $link = $( '<a>' ).attr( 'href', mw.util.getUrl( mw.config.get( 'wgPageName' ), { action: 'edit' } ) );\n\t\t\teditErrorMessage = isReadOnly ? mw.msg( 'apierror-readonly' ) : mw.message( 'mobile-frontend-editor-disabled', $link ).parseDom();\n\t\t\tbindEditLinksSorryToast( editErrorMessage, router );\n\t\t}\n\t}\n}\n\n/**\n * Wire up events that ensure we\n * show a toast message with sincere condolences when user navigates to\n * #/editor or clicks on an edit button\n *\n * @method\n * @ignore\n * @param {string} msg Message for sorry message\n * @param {Router} router\n */\nfunction bindEditLinksSorryToast( msg, router ) {\n\t$editTab.on( 'click', function ( ev ) {\n\t\tmw.notify( msg );\n\t\tev.preventDefault();\n\t} );\n\tmw.hook( 'wikipage.content' ).add( function ( $content ) {\n\t\t$content.find( EDITSECTION_SELECTOR ).on( 'click', function ( ev ) {\n\t\t\tmw.notify( msg );\n\t\t\tev.preventDefault();\n\t\t} );\n\t} );\n\trouter.addRoute( editorPath, function () {\n\t\tmw.notify( msg );\n\t} );\n\trouter.checkRoute();\n}\n\nmodule.exports = function ( currentPage, currentPageHTMLParser, skin ) {\n\tconst router = __non_webpack_require__( 'mediawiki.router' );\n\n\tif ( currentPage.inNamespace( 'file' ) && currentPage.id === 0 ) {\n\t\t// Is a new file page (enable upload image only) T60311\n\t\tbindEditLinksSorryToast( mw.msg( 'mobile-frontend-editor-uploadenable' ), router );\n\t} else {\n\t\t// Edit button is currently hidden. A call to init() will update it as needed.\n\t\tinit( currentPage, currentPageHTMLParser, skin, router );\n\t}\n};\n",
"const\n\tfakeToolbar = require( './fakeToolbar' ),\n\tIconButton = require( '../mobile.startup/IconButton' ),\n\tOverlay = require( '../mobile.startup/Overlay' );\n\n/* global $ */\n\n/**\n * Like loadingOverlay(), but with a fake editor toolbar instead of the spinner.\n *\n * @private\n * @param {Function} afterShow Callback which runs after overlay is shown\n * @param {Function} afterHide Callback which runs after overlay is hidden\n * @param {Function} [loadBasicEditor] Callback for the \"Use basic editor\" button\n * @return {module:mobile.startup/Overlay}\n */\nmodule.exports = function editorLoadingOverlay( afterShow, afterHide, loadBasicEditor ) {\n\tlet timeout;\n\tconst\n\t\t$fakeToolbar = fakeToolbar(),\n\t\t$loadBasicWrapper = $( '<div>' ).addClass( 've-loadbasiceditor' ),\n\t\tloadBasicButton = new IconButton( {\n\t\t\tlabel: mw.msg( 'mobile-frontend-editor-loadbasiceditor' ),\n\t\t\taction: 'progressive',\n\t\t\tweight: 'normal',\n\t\t\tsize: 'medium',\n\t\t\tisIconOnly: false,\n\t\t\ticon: null\n\t\t} ),\n\t\toverlay = new Overlay( {\n\t\t\tclassName: 'overlay overlay-loading',\n\t\t\tnoHeader: true,\n\t\t\tisBorderBox: false,\n\t\t\tonBeforeExit: function ( exit ) {\n\t\t\t\texit();\n\t\t\t\tafterHide();\n\t\t\t\tif ( timeout ) {\n\t\t\t\t\tclearTimeout( timeout );\n\t\t\t\t}\n\t\t\t}\n\t\t} ),\n\t\tlogFeatureUse = function ( feature, action ) {\n\t\t\tmw.track( 'visualEditorFeatureUse', {\n\t\t\t\tfeature: feature,\n\t\t\t\taction: action,\n\t\t\t\t// eslint-disable-next-line camelcase\n\t\t\t\teditor_interface: 'visualeditor'\n\t\t\t} );\n\t\t};\n\n\toverlay.show = function () {\n\t\tOverlay.prototype.show.call( this );\n\t\tafterShow();\n\t};\n\n\toverlay.$el.find( '.overlay-content' ).append( $fakeToolbar );\n\n\tif ( loadBasicEditor ) {\n\t\toverlay.$el.find( '.overlay-content' ).append(\n\t\t\t$loadBasicWrapper.append(\n\t\t\t\t$( '<p>' ).text( mw.msg( 'mobile-frontend-editor-loadingtooslow' ) ),\n\t\t\t\tloadBasicButton.$el\n\t\t\t)\n\t\t);\n\n\t\ttimeout = setTimeout( function () {\n\t\t\t$loadBasicWrapper.addClass( 've-loadbasiceditor-shown' );\n\t\t\tlogFeatureUse( 'mobileVisualFallback', 'context-show' );\n\t\t}, 3000 );\n\n\t\tloadBasicButton.$el.on( 'click', function () {\n\t\t\t$loadBasicWrapper.removeClass( 've-loadbasiceditor-shown' );\n\t\t\tlogFeatureUse( 'mobileVisualFallback', 'fallback-confirm' );\n\t\t\tloadBasicEditor();\n\t\t} );\n\t}\n\n\t// Animate the toolbar sliding into place.\n\t$fakeToolbar.addClass( 'toolbar-hidden' );\n\tsetTimeout( function () {\n\t\t$fakeToolbar.addClass( 'toolbar-shown' );\n\t\tsetTimeout( function () {\n\t\t\t$fakeToolbar.addClass( 'toolbar-shown-done' );\n\t\t}, 250 );\n\t} );\n\n\treturn overlay;\n};\n",
"const lazyImageLoader = require( '../mobile.startup/lazyImages/lazyImageLoader' );\n\n/**\n * @namespace Hooks\n */\n\n/**\n * Initialise lazy loading images to supplement the HTML changes inside the\n * MobileFormatter.\n *\n * @param {jQuery} $container\n * @private\n */\nfunction init( $container ) {\n\n\t// T360781 - since this is invoked via hook, the value of $container is not guaranteed.\n\t// If undefined, return early since no further work can be done on $container.\n\tif ( !( $container[ 0 ] instanceof HTMLElement ) ) {\n\t\treturn;\n\t}\n\n\tconst imagePlaceholders = lazyImageLoader.queryPlaceholders( $container[ 0 ] );\n\n\t// Regardless of whether or not lazy load is turned on\n\t// We need to load in all images before print\n\twindow.addEventListener( 'beforeprint', function () {\n\t\tlazyImageLoader.loadImages( imagePlaceholders );\n\t} );\n\n\tif ( !mw.config.get( 'wgMFLazyLoadImages' ) ) {\n\t\treturn;\n\t}\n\n\t// eslint-disable-next-line compat/compat\n\tconst observer = new IntersectionObserver(\n\t\t( entries ) => {\n\t\t\tentries.forEach( ( entry ) => {\n\t\t\t\tconst placeholder = entry.target;\n\t\t\t\t// If intersecting load image and stop observing it to free up resources.\n\t\t\t\tif ( entry.isIntersecting ) {\n\t\t\t\t\tlazyImageLoader.loadImage( placeholder );\n\t\t\t\t\tobserver.unobserve( placeholder );\n\t\t\t\t}\n\t\t\t} );\n\t\t},\n\t\t// See https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API\n\t\t{\n\t\t\t// Setup the area for observing.\n\t\t\t// By default the root is the viewport.\n\t\t\t// We want the detection area to be as tall as 150% of the viewport height,\n\t\t\t// allowing elements to be detected before they reach the viewport.\n\t\t\t// This is achieved with a 50% bottom margin.\n\t\t\trootMargin: '0px 0px 50% 0px',\n\t\t\t// The default is 0 (meaning as soon as even one pixel is visible,\n\t\t\t// the callback will be run), however we explicitly set this so that\n\t\t\t// it is clear we have made this choice in case we want to revisit later.\n\t\t\tthreshold: 0\n\t\t}\n\t);\n\n\t// observe all the placeholders\n\timagePlaceholders.forEach( ( placeholder ) => {\n\t\tobserver.observe( placeholder );\n\t} );\n}\n\n/**\n * Make modifications after an image was lazy loaded, stable for use.\n *\n * @event ~'mobileFrontend.loadLazyImages'\n * @memberof Hooks\n * @param {jQuery} $container\n */\nmw.hook( 'mobileFrontend.loadLazyImages' ).add( function ( $container ) {\n\tconst imagePlaceholders = lazyImageLoader.queryPlaceholders( $container[ 0 ] );\n\tlazyImageLoader.loadImages( imagePlaceholders );\n} );\n\nmodule.exports = function () {\n\tmw.hook( 'wikipage.content' ).add( init );\n};\n",
"// FIXME: make this an object with a constructor to facilitate testing\n// (see https://bugzilla.wikimedia.org/show_bug.cgi?id=44264)\n/**\n * mobileFrontend namespace\n * @private\n */\nlet url;\n\nconst\n\ttoggling = require( './toggling' ),\n\tFONT_SIZE_KEY = 'mf-font-size',\n\tSECTION_COLLAPSING_TOGGLE = 'mf-expand-sections',\n\tstorage = mw.storage,\n\tapi = new mw.Api(),\n\tlazyLoadedImages = require( './lazyLoadedImages' ),\n\teditor = require( './editor' ),\n\tcurrentPage = require( '../mobile.startup/currentPage' )(),\n\tcurrentPageHTMLParser = require( '../mobile.startup/currentPageHTMLParser' )(),\n\tmfUtil = require( '../mobile.startup/util' ),\n\t$window = mfUtil.getWindow(),\n\tSkin = require( '../mobile.startup/Skin' ),\n\teventBus = require( '../mobile.startup/eventBusSingleton' );\n\nconst skin = Skin.getSingleton();\n\n/**\n * Given 2 functions, it returns a function that will run both with it's\n * context and parameters and return the results combined\n *\n * @private\n * @param {Function} fn1\n * @param {Function} fn2\n * @return {Function} which returns void\n */\nfunction apply2( fn1, fn2 ) {\n\treturn function () {\n\t\tfn1.apply( this, arguments );\n\t\tfn2.apply( this, arguments );\n\t};\n}\n\n/**\n * The `window`'s resize event debounced at 100 ms.\n * The `resize:throttled` event is the `window`'s\n * resize event throttled to 200 ms.\n *\n * @event resize\n * @memberof window\n */\n\n/**\n * The `window`'s scroll event debounced at 100 ms.\n * The `scroll:throttled` event is the `window`'s\n * scroll event throttled to 200 ms.\n *\n * @event scroll\n * @memberof window\n */\n\n$window\n\t.on( 'resize', apply2(\n\t\tmw.util.debounce( function () {\n\t\t\teventBus.emit( 'resize' );\n\t\t}, 100 ),\n\t\tmw.util.throttle( function () {\n\t\t\teventBus.emit( 'resize:throttled' );\n\t\t}, 200 )\n\t) )\n\t.on( 'scroll', apply2(\n\t\tmw.util.debounce( function () {\n\t\t\teventBus.emit( 'scroll' );\n\t\t}, 100 ),\n\t\tmw.util.throttle( function () {\n\t\t\teventBus.emit( 'scroll:throttled' );\n\t\t}, 200 )\n\t) );\n\n// Hide URL flags used to pass state through reloads\n// venotify is normally handled in ve.init.mw.DesktopArticleTarget.init.js\n// but that's not loaded on mobile\n// eslint-disable-next-line no-restricted-properties\nif ( window.history && history.pushState ) {\n\t// eslint-disable-next-line no-restricted-properties\n\turl = new URL( window.location.href );\n\tif ( url.searchParams.has( 'venotify' ) || url.searchParams.has( 'mfnotify' ) ) {\n\t\turl.searchParams.delete( 'venotify' );\n\t\turl.searchParams.delete( 'mfnotify' );\n\t\t// eslint-disable-next-line no-restricted-properties\n\t\twindow.history.replaceState( null, document.title, url.toString() );\n\t}\n}\n\n// Recruit volunteers through the console\n// (note console.log may not be a function so check via apply)\n/* eslint-disable no-console */\nif ( window.console && window.console.log && window.console.log.apply &&\n\t\tmw.config.get( 'wgMFEnableJSConsoleRecruitment' ) ) {\n\tconsole.log( mw.msg( 'mobile-frontend-console-recruit' ) );\n}\n/* eslint-enable no-console */\n\n// Setup editor, if supported for the current page view\nif ( mw.config.get( 'wgMFIsSupportedEditRequest' ) ) {\n\teditor( currentPage, currentPageHTMLParser, skin );\n}\n\nfunction migrateXLargeToLarge() {\n\tif ( document.documentElement.classList.contains( 'mf-font-size-clientpref-xlarge' ) ) {\n\t\tif ( mw.user.isAnon() ) {\n\t\t\tmw.user.clientPrefs.set( FONT_SIZE_KEY, 'large' );\n\t\t} else {\n\t\t\tapi.saveOption( FONT_SIZE_KEY, 'large' );\n\t\t}\n\t}\n}\n\nfunction migrateLegacyExpandAllSectionsToggle() {\n\tconst currentValue = mw.storage.get( 'expandSections' );\n\tif ( currentValue ) {\n\t\tif ( mw.user.isAnon() ) {\n\t\t\tmw.user.clientPrefs.set( SECTION_COLLAPSING_TOGGLE, '1' );\n\t\t} else {\n\t\t\tapi.saveOption( SECTION_COLLAPSING_TOGGLE, '1' );\n\t\t}\n\t\tstorage.remove( 'expandSections' );\n\t}\n}\n\nmigrateXLargeToLarge();\nmigrateLegacyExpandAllSectionsToggle();\ntoggling();\nlazyLoadedImages();\n",
"module.exports = function () {\n\tconst\n\t\tcurrentPage = require( '../mobile.startup/currentPage' )(),\n\t\tToggler = require( '../mobile.startup/Toggler' ),\n\t\tsectionCollapsing = require( '../mobile.startup/sectionCollapsing' ),\n\t\teventBus = require( '../mobile.startup/eventBusSingleton' );\n\n\t/**\n\t * Initialises toggling code.\n\t *\n\t * @method\n\t * @param {jQuery.Object} $container to enable toggling on\n\t * @param {string} prefix a prefix to use for the id.\n\t * @param {Page} page The current page\n\t * @ignore\n\t */\n\tfunction init( $container, prefix, page ) {\n\t\tconst isParsoidEnabled = !!document.querySelector( '.mw-parser-output[data-mw-parsoid-version]' );\n\t\tif ( isParsoidEnabled ) {\n\t\t\tsectionCollapsing.init( $container[0] );\n\t\t} else {\n\t\t\t// Only handle headings in content processed by MakeSectionsTransform.\n\t\t\t// Remove event handler added by MakeSectionsTransform::interimTogglingSupport().\n\t\t\t$container.find( '.section-heading' ).removeAttr( 'onclick' );\n\t\t\t// Cleanup global as it is no longer needed. We check if it's undefined because\n\t\t\t// there is no guarantee this won't be run on other skins e.g. Vector or cached HTML.\n\t\t\tif ( window.mfTempOpenSection !== undefined ) {\n\t\t\t\tdelete window.mfTempOpenSection;\n\t\t\t}\n\t\t\t// eslint-disable-next-line no-new\n\t\t\tnew Toggler( {\n\t\t\t\t$container: $container,\n\t\t\t\tprefix: prefix,\n\t\t\t\tpage: page,\n\t\t\t\teventBus: eventBus\n\t\t\t} );\n\t\t}\n\t}\n\n\tif (\n\t\t// Avoid this running on Watchlist.\n\t\t!currentPage.inNamespace( 'special' ) &&\n\t\t(\n\t\t\tmw.config.get( 'wgAction' ) === 'view' ||\n\t\t\tmw.config.get( 'wgAction' ) === 'edit'\n\t\t)\n\t) {\n\t\tmw.hook( 'wikipage.content' ).add( function ( $container ) {\n\t\t\tlet $contentContainer = $container.find( '.mw-parser-output' );\n\t\t\t// If there was no mw-parser-output wrapper, just use the parent.\n\t\t\tif ( $contentContainer.length === 0 ) {\n\t\t\t\t$contentContainer = $container;\n\t\t\t}\n\t\t\tinit( $contentContainer, 'content-', currentPage );\n\t\t} );\n\t}\n};\n",
"const\n\tutil = require( './util' ),\n\tescapeSelector = util.escapeSelector,\n\tarrowOptions = {\n\t\ticon: 'expand',\n\t\tisSmall: true,\n\t\tadditionalClassNames: 'indicator'\n\t},\n\tIcon = require( './Icon' );\nconst isCollapsedByDefault = require( './isCollapsedByDefault.js' );\n\n/**\n * @typedef {Object} ToggledEvent\n * @prop {boolean} expanded True if section is opened, false if closed.\n * @prop {Page} page\n * @prop {jQuery.Object} $heading\n * @memberof module:mobile.startup\n * @ignore\n */\n\n/**\n * A class for enabling toggling\n *\n * Toggling can be disabled on a sepcific heading by adding the\n * collapsible-heading-disabled class.\n *\n * @class Toggler\n * @param {Object} options\n * @param {OO.EventEmitter} options.eventBus Object used to emit section-toggled events.\n * @param {jQuery.Object} options.$container to apply toggling to\n * @param {string} options.prefix a prefix to use for the id.\n * @param {Page} options.page to allow storage of session for future visits\n */\nfunction Toggler( options ) {\n\tthis.eventBus = options.eventBus;\n\tthis.$container = options.$container;\n\tthis.prefix = options.prefix;\n\tthis.page = options.page;\n\tthis._enable();\n}\n\n/**\n * Using the settings module looks at what sections were previously expanded on\n * existing page.\n *\n * @param {Page} page\n * @return {Object} representing open sections\n * @ignore\n */\nfunction getExpandedSections( page ) {\n\tconst expandedSections = mw.storage.session.getObject( 'expandedSections' ) || {};\n\texpandedSections[page.title] = expandedSections[page.title] || {};\n\treturn expandedSections;\n}\n\n/**\n * Save expandedSections to sessionStorage\n *\n * @param {Object} expandedSections\n * @ignore\n */\nfunction saveExpandedSections( expandedSections ) {\n\tmw.storage.session.setObject(\n\t\t'expandedSections', expandedSections\n\t);\n}\n\n/**\n * Given an expanded heading, store it to sessionStorage.\n * If the heading is collapsed, remove it from sessionStorage.\n *\n * @param {jQuery.Object} $heading - A heading belonging to a section\n * @param {Page} page\n * @ignore\n */\nfunction storeSectionToggleState( $heading, page ) {\n\tconst headline = $heading.find( '.mw-headline' ).attr( 'id' ),\n\t\texpandedSections = getExpandedSections( page );\n\n\tif ( headline && expandedSections[page.title] ) {\n\t\tconst isSectionOpen = $heading.hasClass( 'open-block' );\n\t\tif ( isSectionOpen ) {\n\t\t\texpandedSections[page.title][headline] = true;\n\t\t} else {\n\t\t\tdelete expandedSections[page.title][headline];\n\t\t}\n\n\t\tsaveExpandedSections( expandedSections );\n\t}\n}\n\n/**\n * Expand sections that were previously expanded before leaving this page.\n *\n * @param {Toggler} toggler\n * @param {jQuery.Object} $container\n * @param {Page} page\n * @ignore\n */\nfunction expandStoredSections( toggler, $container, page ) {\n\tconst expandedSections = getExpandedSections( page ),\n\t\t$headlines = $container.find( '.section-heading span' );\n\n\t$headlines.each( function () {\n\t\tconst $headline = $container.find( this );\n\t\tconst $sectionHeading = $headline.parents( '.section-heading' );\n\t\t// toggle only if the section is not already expanded\n\t\tif (\n\t\t\texpandedSections[page.title][$headline.attr( 'id' )] &&\n\t\t\t!$sectionHeading.hasClass( 'open-block' )\n\t\t) {\n\t\t\ttoggler.toggle( $sectionHeading, true );\n\t\t}\n\t} );\n}\n\n/**\n * Check if sections should be collapsed by default\n *\n * @return {boolean}\n */\nToggler.prototype.isCollapsedByDefault = function () {\n\tif ( this._isCollapsedByDefault === undefined ) {\n\t\t// Thess classes override site settings and user preferences. For example:\n\t\t// * ...-collapsed used on talk pages by DiscussionTools. (T321618, T322628)\n\t\t// * ...-expanded used in previews (T336572)\n\t\tconst $override = this.$container.closest( '.collapsible-headings-collapsed, .collapsible-headings-expanded' );\n\t\tif ( $override.length ) {\n\t\t\tthis._isCollapsedByDefault = $override.hasClass( 'collapsible-headings-collapsed' );\n\t\t} else {\n\t\t\t// Check site config\n\t\t\tthis._isCollapsedByDefault = isCollapsedByDefault();\n\t\t}\n\t}\n\treturn this._isCollapsedByDefault;\n};\n\n/**\n * Given a heading, toggle it and any of its children\n *\n * @memberof Toggler\n * @instance\n * @param {jQuery.Object} $heading A heading belonging to a section\n * @param {boolean} fromSaved Section is being toggled from a saved state\n * @return {boolean}\n */\nToggler.prototype.toggle = function ( $heading, fromSaved ) {\n\tif ( !fromSaved && $heading.hasClass( 'collapsible-heading-disabled' ) ) {\n\t\treturn false;\n\t}\n\n\tconst self = this,\n\t\twasExpanded = $heading.is( '.open-block' );\n\n\t$heading.toggleClass( 'open-block' );\n\n\tarrowOptions.rotation = wasExpanded ? 0 : 180;\n\tconst newIndicator = new Icon( arrowOptions );\n\tconst $indicatorElement = $heading.data( 'indicator' );\n\tif ( $indicatorElement ) {\n\t\t$indicatorElement.replaceWith( newIndicator.$el );\n\t\t$heading.data( 'indicator', newIndicator.$el );\n\t}\n\n\tconst $headingLabel = $heading.find( '.mw-headline' );\n\t$headingLabel.attr( 'aria-expanded', !wasExpanded );\n\n\tconst $content = $heading.next();\n\tif ( $content.hasClass( 'open-block' ) ) {\n\t\t$content.removeClass( 'open-block' );\n\t\t// jquery doesn't allow custom values for the hidden attribute it seems.\n\t\t$content.get( 0 ).setAttribute( 'hidden', 'until-found' );\n\t} else {\n\t\t$content.addClass( 'open-block' );\n\t\t$content.removeAttr( 'hidden' );\n\t}\n\n\t/* T239418 We consider this event as a low-priority one and emit it asynchronously.\n\tThis ensures that any logic associated with section toggling is async and not contributing\n\tdirectly to a slow click/press event handler.\n\n\tCurrently costly reflow-inducing viewport size computation is being done for lazy-loaded\n\timages by the main listener to this event. */\n\tmw.requestIdleCallback( () => {\n\t\t/**\n\t\t * Global event emitted after a section has been toggled\n\t\t *\n\t\t * @event ~section-toggled\n\t\t * @type {ToggledEvent}\n\t\t * @memberof module:mobile.startup~Toggler\n\t\t * @ignore\n\t\t */\n\n\t\tself.eventBus.emit( 'section-toggled', {\n\t\t\texpanded: wasExpanded,\n\t\t\t$heading\n\t\t} );\n\t\t/**\n\t\t * Internal for use inside ExternalGuidance.\n\t\t *\n\t\t * @event ~'mobileFrontend.section-toggled'\n\t\t * @memberof Hooks\n\t\t */\n\t\tmw.hook( 'mobileFrontend.section-toggled' ).fire( {\n\t\t\texpanded: wasExpanded,\n\t\t\t$heading\n\t\t} );\n\t} );\n\n\tif ( this.isCollapsedByDefault() ) {\n\t\tstoreSectionToggleState( $heading, this.page );\n\t}\n\treturn true;\n};\n\n/**\n * Enables toggling via enter and space keys\n *\n * @param {Toggler} toggler instance.\n * @param {jQuery.Object} $heading\n * @ignore\n */\nfunction enableKeyboardActions( toggler, $heading ) {\n\t$heading.on( 'keypress', ( ev ) => {\n\t\tif ( ev.which === 13 || ev.which === 32 ) {\n\t\t\t// Only handle keypresses on the \"Enter\" or \"Space\" keys\n\t\t\ttoggler.toggle( $heading );\n\t\t}\n\t} ).find( 'a' ).on( 'keypress mouseup', ( ev ) => ev.stopPropagation() );\n}\n\n/**\n * Reveals an element and its parent section as identified by it's id\n *\n * @memberof Toggler\n * @instance\n * @param {string} id An element ID within the $container\n * @return {boolean} Target ID was found\n */\nToggler.prototype.reveal = function ( id ) {\n\tlet $target;\n\t// jQuery will throw for hashes containing certain characters which can break toggling\n\ttry {\n\t\t$target = this.$container.find( '#' + escapeSelector( id ) );\n\t} catch ( e ) {}\n\tif ( !$target || !$target.length ) {\n\t\treturn false;\n\t}\n\n\tlet $heading = $target.parents( '.collapsible-heading' );\n\t// The heading is not a section heading, check if in a content block!\n\tif ( !$heading.length ) {\n\t\t$heading = $target.parents( '.collapsible-block' ).prev( '.collapsible-heading' );\n\t}\n\tif ( $heading.length && !$heading.hasClass( 'open-block' ) ) {\n\t\tthis.toggle( $heading );\n\t}\n\tif ( $heading.length ) {\n\t\t// scroll again after opening section (opening section makes the page longer)\n\t\twindow.scrollTo( 0, $target.offset().top );\n\t}\n\treturn true;\n};\n\n/**\n * Enables section toggling in a given container.\n *\n * @memberof Toggler\n * @instance\n * @private\n */\nToggler.prototype._enable = function () {\n\tconst self = this;\n\n\t// FIXME This should use .find() instead of .children(), some extensions like Wikibase\n\t// want to toggle other headlines than direct descendants of $container. (T95889)\n\tthis.$container.children( '.section-heading' ).each( function ( i ) {\n\t\tconst $heading = self.$container.find( this ),\n\t\t\t$headingLabel = $heading.find( '.mw-headline' ),\n\t\t\t$indicator = $heading.find( '.indicator' ),\n\t\t\tid = self.prefix + 'collapsible-block-' + i;\n\t\t// Be sure there is a `section` wrapping the section content.\n\t\t// Otherwise, collapsible sections for this page is not enabled.\n\t\tif ( $heading.next().is( 'section' ) ) {\n\t\t\tconst $content = $heading.next( 'section' );\n\t\t\t$heading\n\t\t\t\t.addClass( 'collapsible-heading ' )\n\t\t\t\t.data( 'section-number', i )\n\t\t\t\t.on( 'click', ( ev ) => {\n\t\t\t\t\t// don't toggle, if the click target was a link\n\t\t\t\t\t// (a link in a section heading)\n\t\t\t\t\t// See T117880\n\t\t\t\t\tconst clickedLink = ev.target.closest( 'a' );\n\t\t\t\t\tif ( !clickedLink || !clickedLink.href ) {\n\t\t\t\t\t\t// prevent taps/clicks on edit button after toggling (T58209)\n\t\t\t\t\t\tev.preventDefault();\n\t\t\t\t\t\tself.toggle( $heading );\n\t\t\t\t\t}\n\t\t\t\t} );\n\t\t\t$headingLabel\n\t\t\t\t.attr( {\n\t\t\t\t\ttabindex: 0,\n\t\t\t\t\trole: 'button',\n\t\t\t\t\t'aria-controls': id,\n\t\t\t\t\t'aria-expanded': 'false'\n\t\t\t\t} );\n\n\t\t\tarrowOptions.rotation = !self.isCollapsedByDefault() ? 180 : 0;\n\t\t\tconst indicator = new Icon( arrowOptions );\n\t\t\tif ( $indicator.length ) {\n\t\t\t\t// replace the existing indicator\n\t\t\t\t$indicator.replaceWith( indicator.$el );\n\t\t\t} else {\n\t\t\t\tindicator.prependTo( $heading );\n\t\t\t}\n\t\t\t$heading.data( 'indicator', indicator.$el );\n\t\t\t$content\n\t\t\t\t.addClass( 'collapsible-block' )\n\t\t\t\t.eq( 0 )\n\t\t\t\t.attr( {\n\t\t\t\t\t// We need to give each content block a unique id as that's\n\t\t\t\t\t// the only way we can tell screen readers what element we're\n\t\t\t\t\t// referring to via `aria-controls`.\n\t\t\t\t\tid\n\t\t\t\t} )\n\t\t\t\t.on( 'beforematch', () => self.toggle( $heading ) )\n\t\t\t\t.addClass( 'collapsible-block-js' )\n\t\t\t\t.get( 0 ).setAttribute( 'hidden', 'until-found' );\n\n\t\t\tenableKeyboardActions( self, $heading );\n\n\t\t\tif ( !self.isCollapsedByDefault() ) {\n\t\t\t\t// Expand sections by default on wide screen devices\n\t\t\t\t// or if the expand sections setting is set.\n\t\t\t\t// The wide screen logic for determining whether to collapse sections initially\n\t\t\t\t// should be kept in sync with mobileoptions#initLocalStorageElements().\n\t\t\t\tself.toggle( $heading );\n\t\t\t}\n\t\t}\n\t} );\n\n\t/**\n\t * Checks the existing hash and toggles open any section that contains the fragment.\n\t *\n\t * @method\n\t */\n\tfunction checkHash() {\n\t\t// eslint-disable-next-line no-restricted-properties\n\t\tlet hash = window.location.hash;\n\t\tif ( hash.indexOf( '#' ) === 0 ) {\n\t\t\thash = hash.slice( 1 );\n\t\t\t// Per https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-element\n\t\t\t// we try the raw fragment first, then the percent-decoded fragment.\n\t\t\tif ( !self.reveal( hash ) ) {\n\t\t\t\tconst decodedHash = mw.util.percentDecodeFragment( hash );\n\t\t\t\tif ( decodedHash ) {\n\t\t\t\t\tself.reveal( decodedHash );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Checks the value of wgInternalRedirectTargetUrl and sets the hash if present.\n\t * checkHash() will reveal the collapsed section that contains it afterwards.\n\t *\n\t * @method\n\t */\n\tfunction checkInternalRedirectAndHash() {\n\t\tconst internalRedirect = mw.config.get( 'wgInternalRedirectTargetUrl' ),\n\t\t\tinternalRedirectHash = internalRedirect ? internalRedirect.split( '#' )[1] : false;\n\n\t\tif ( internalRedirectHash ) {\n\t\t\t// eslint-disable-next-line no-restricted-properties\n\t\t\twindow.location.hash = internalRedirectHash;\n\t\t}\n\t}\n\n\tcheckInternalRedirectAndHash();\n\tcheckHash();\n\n\tutil.getWindow().on( 'hashchange', () => checkHash() );\n\n\tif ( this.isCollapsedByDefault() && this.page ) {\n\t\texpandStoredSections( this, this.$container, this.page );\n\t}\n};\n\nToggler._getExpandedSections = getExpandedSections;\nToggler._expandStoredSections = expandStoredSections;\n\nmodule.exports = Toggler;\n",
"const browser = require( './Browser' ).getSingleton();\n\n/**\n * Checks if sections should be collapsed by default.\n * @ignore\n *\n * @return {boolean}\n */\nfunction isCollapsedByDefault() {\n\treturn mw.config.get( 'wgMFCollapseSectionsByDefault' ) && !browser.isWideScreen() &&\n\t\t// Section collapsing can be disabled in MobilePreferences\n\t\t!document.documentElement.classList.contains(\n\t\t\t'mf-expand-sections-clientpref-1'\n\t\t);\n}\n\nmodule.exports = isCollapsedByDefault;\n",
"const isCollapsedByDefault = require( './isCollapsedByDefault' );\n\n/**\n * Sets attributes on collapsible elements based on collapsed state\n *\n * @method\n * @param {HTMLElement} container div element containing collapsible heading\n * @param {HTMLElement} headingText span element containing heading text\n * @param {HTMLElement} icon span element for icon\n * @param {boolean} isCollapsed collapsed state to set\n * @ignore\n */\nfunction setCollapsedState( content, headingText, icon, isCollapsed ) {\n\t// show the content if hidden, hide if shown (until found so find in page works)\n\tcontent.hidden = isCollapsed ? 'until-found' : false;\n\n\t// update the dropdown state based on the content visibility\n\tif ( isCollapsed ) {\n\t\theadingText.setAttribute( 'aria-expanded', 'true' );\n\t\ticon.classList.add( 'mf-icon-expand' );\n\t\ticon.classList.remove( 'mf-icon-collapse' );\n\t} else {\n\t\theadingText.setAttribute( 'aria-expanded', 'false' );\n\t\ticon.classList.add( 'mf-icon-collapse' );\n\t\ticon.classList.remove( 'mf-icon-expand' );\n\t}\n}\n\n/**\n * Toggles collapsible state for a heading\n *\n * @method\n * @param {HTMLElement} container div element containing collapsible heading\n * @param {HTMLElement} headingText span element containing heading text\n * @param {HTMLElement} icon span element for icon\n * @ignore\n */\nfunction toggle( content, headingText, icon ) {\n\tconst currentlyHidden = content.hidden;\n\tsetCollapsedState( content, headingText, icon, !currentlyHidden );\n}\n\n/**\n * Initialises collapsing code.\n *\n * @method\n * @param {HTMLElement} container to enable collapsing on\n * @ignore\n */\nfunction init( container ) {\n\tconst isCollapsed = isCollapsedByDefault();\n\tconst headingWrappers = Array.from( container.querySelectorAll( '.mw-parser-output > section > .mw-heading' ) );\n\theadingWrappers.forEach( ( wrapper ) => {\n\t\twrapper.classList.add( 'mf-collapsible-heading' );\n\t\tconst heading = wrapper.firstElementChild;\n\n\t\t// Add class to collapsible content\n\t\t// Used in CSS before hidden attribute is added\n\t\tconst content = wrapper.nextElementSibling;\n\t\tcontent.classList.add( 'mf-collapsible-content' );\n\n\t\t// Update the heading text to account for semantics of collapsing sections\n\t\tconst headingText = document.createElement( 'span' );\n\t\theadingText.textContent = heading.textContent;\n\t\theadingText.setAttribute( 'tabindex', '0' );\n\t\theadingText.setAttribute( 'role', 'button' );\n\t\theadingText.setAttribute( 'aria-controls', content.id );\n\n\t\t// Create the dropdown arrow\n\t\tconst icon = document.createElement( 'span' );\n\t\ticon.classList.add( 'mf-icon', 'mf-icon--small', 'mf-collapsible-icon' );\n\t\ticon.setAttribute( 'aria-hidden', true );\n\n\t\tsetCollapsedState( content, headingText, icon, isCollapsed );\n\n\t\t// Replace contents of the heading element\n\t\theading.innerHTML = '';\n\t\theading.append( icon );\n\t\theading.append( headingText );\n\n\t\t// Register the click handlers\n\t\theading.addEventListener( 'click', () => toggle( content, headingText, icon ) );\n\t} );\n}\n\nmodule.exports = { init };\n"