2017-07-12 15:12:40 +00:00
|
|
|
( function ( M, $ ) {
|
|
|
|
|
|
|
|
var
|
|
|
|
router = require( 'mediawiki.router' ),
|
|
|
|
overlayManager = M.require( 'skins.minerva.scripts/overlayManager' ),
|
|
|
|
loader = M.require( 'mobile.startup/rlModuleLoader' ),
|
|
|
|
skin = M.require( 'skins.minerva.scripts/skin' ),
|
2018-04-06 19:52:07 +00:00
|
|
|
currentPage = M.getCurrentPage(),
|
2018-09-21 01:38:10 +00:00
|
|
|
// #ca-edit, .mw-editsection are standard MediaWiki elements
|
|
|
|
// .edit-link comes from MobileFrontend user page creation CTA
|
|
|
|
$allEditLinks = $( '#ca-edit a, .mw-editsection a, .edit-link' ),
|
2017-07-12 15:12:40 +00:00
|
|
|
user = M.require( 'mobile.startup/user' ),
|
|
|
|
popup = M.require( 'mobile.startup/toast' ),
|
2018-11-02 17:35:37 +00:00
|
|
|
CtaDrawer = M.require( 'mobile.startup/CtaDrawer' ),
|
2017-07-12 15:12:40 +00:00
|
|
|
// FIXME: Disable on IE < 10 for time being
|
|
|
|
blacklisted = /MSIE \d\./.test( navigator.userAgent ),
|
2017-08-22 14:12:32 +00:00
|
|
|
contentModel = mw.config.get( 'wgPageContentModel' ),
|
2017-07-12 15:12:40 +00:00
|
|
|
isEditingSupported = router.isSupported() && !blacklisted,
|
|
|
|
// FIXME: Use currentPage.getId()
|
|
|
|
isNewPage = currentPage.options.id === 0,
|
|
|
|
isNewFile = currentPage.inNamespace( 'file' ) && isNewPage,
|
|
|
|
veConfig = mw.config.get( 'wgVisualEditorConfig' ),
|
|
|
|
// FIXME: Should we consider default site options and user prefs?
|
2018-11-02 22:44:55 +00:00
|
|
|
isVisualEditorEnabled = veConfig,
|
|
|
|
editorPath = /^\/editor\/(\d+|all)$/;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-08-24 23:32:06 +00:00
|
|
|
/**
|
|
|
|
* Event handler for edit link clicks. Will prevent default link
|
|
|
|
* behaviour and will not allow propagation
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function onEditLinkClick() {
|
2018-09-21 01:38:10 +00:00
|
|
|
var section = ( new mw.Uri( this.href ) ).query.section || 'all';
|
|
|
|
router.navigate( '#/editor/' + section );
|
2018-08-24 23:32:06 +00:00
|
|
|
// prevent folding section when clicking Edit by stopping propagation
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the user's preferred editor setting. If none is set, return the default
|
|
|
|
* editor for this wiki.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @return {string} Either 'VisualEditor' or 'SourceEditor'
|
|
|
|
*/
|
|
|
|
function getPreferredEditor() {
|
|
|
|
var preferredEditor = mw.storage.get( 'preferredEditor' );
|
|
|
|
if ( !preferredEditor ) {
|
|
|
|
// For now, we are going to ignore which editor is set as the default for the
|
|
|
|
// wiki and always default to the source editor. Once we decide to honor the
|
|
|
|
// default editor setting for the wiki, we'll want to use:
|
2018-09-13 15:33:20 +00:00
|
|
|
// visualEditorDefault = veConfig && veConfig.defaultUserOptions &&
|
|
|
|
// veConfig.defaultUserOptions.enable;
|
2017-07-12 15:12:40 +00:00
|
|
|
// return visualEditorDefault ? 'VisualEditor' : 'SourceEditor';
|
|
|
|
return 'SourceEditor';
|
|
|
|
}
|
2018-08-31 22:48:34 +00:00
|
|
|
return preferredEditor;
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the edit button so that it launches the editor interface when clicked.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @param {Page} page The page to edit.
|
|
|
|
*/
|
|
|
|
function setupEditor( page ) {
|
2018-09-21 01:38:10 +00:00
|
|
|
var uri, fragment, editorOverride, section,
|
2018-05-19 20:10:39 +00:00
|
|
|
isNewPage = page.options.id === 0,
|
2017-07-31 19:16:30 +00:00
|
|
|
leadSection = page.getLeadSectionElement();
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-09-21 01:38:10 +00:00
|
|
|
$allEditLinks.on( 'click', onEditLinkClick );
|
2018-11-02 22:44:55 +00:00
|
|
|
overlayManager.add( editorPath, function ( sectionId ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
var
|
|
|
|
$content = $( '#mw-content-text' ),
|
|
|
|
preferredEditor = getPreferredEditor(),
|
|
|
|
editorOptions = {
|
|
|
|
overlayManager: overlayManager,
|
|
|
|
api: new mw.Api(),
|
|
|
|
licenseMsg: skin.getLicenseMsg(),
|
|
|
|
title: page.title,
|
|
|
|
isAnon: user.isAnon(),
|
|
|
|
isNewPage: isNewPage,
|
|
|
|
isNewEditor: user.getEditCount() === 0,
|
|
|
|
oldId: mw.util.getParamValue( 'oldid' ),
|
|
|
|
contentLang: $content.attr( 'lang' ),
|
|
|
|
contentDir: $content.attr( 'dir' ),
|
|
|
|
sessionId: mw.user.generateRandomSessionId()
|
|
|
|
},
|
|
|
|
visualEditorNamespaces = veConfig && veConfig.namespaces,
|
|
|
|
initMechanism = mw.util.getParamValue( 'redlink' ) ? 'new' : 'click';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log init event to edit schema.
|
|
|
|
* Need to log this from outside the Overlay object because that module
|
|
|
|
* won't have loaded yet.
|
|
|
|
* @private
|
|
|
|
* @ignore
|
|
|
|
* @param {string} editor name e.g. wikitext or visualeditor
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
function logInit( editor ) {
|
2017-09-05 21:19:05 +00:00
|
|
|
// If MobileFrontend is not available this will not be possible so
|
|
|
|
// check first.
|
2018-08-22 00:26:46 +00:00
|
|
|
mw.loader.using( 'mobile.loggingSchemas.edit' ).then( function () {
|
2017-09-05 21:19:05 +00:00
|
|
|
mw.track( 'mf.schemaEdit', {
|
|
|
|
action: 'init',
|
|
|
|
type: 'section',
|
|
|
|
mechanism: initMechanism,
|
|
|
|
editor: editor,
|
|
|
|
editingSessionId: editorOptions.sessionId
|
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
} );
|
|
|
|
}
|
2017-09-05 21:19:05 +00:00
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Load source editor
|
|
|
|
* @private
|
|
|
|
* @ignore
|
|
|
|
* @method
|
2018-08-22 00:26:46 +00:00
|
|
|
* @returns {JQuery.Promise}
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
function loadSourceEditor() {
|
|
|
|
logInit( 'wikitext' );
|
|
|
|
|
2018-08-22 00:26:46 +00:00
|
|
|
return loader.loadModule( 'mobile.editor.overlay' ).then( function () {
|
2017-07-12 15:12:40 +00:00
|
|
|
var EditorOverlay = M.require( 'mobile.editor.overlay/EditorOverlay' );
|
2018-08-22 00:26:46 +00:00
|
|
|
return new EditorOverlay( editorOptions );
|
2017-07-12 15:12:40 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:55:30 +00:00
|
|
|
if ( sectionId !== 'all' ) {
|
|
|
|
editorOptions.sectionId = page.isWikiText() ? +sectionId : null;
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
// Check whether VisualEditor should be loaded
|
|
|
|
if ( isVisualEditorEnabled &&
|
|
|
|
|
|
|
|
// Only for pages with a wikitext content model
|
|
|
|
page.isWikiText() &&
|
|
|
|
|
|
|
|
// Only in enabled namespaces
|
|
|
|
$.inArray( mw.config.get( 'wgNamespaceNumber' ), visualEditorNamespaces ) > -1 &&
|
|
|
|
|
|
|
|
// Not on pages which are outputs of the Page Translation feature
|
|
|
|
mw.config.get( 'wgTranslatePageTranslation' ) !== 'translation' &&
|
|
|
|
|
2018-05-19 20:10:39 +00:00
|
|
|
(
|
|
|
|
// If the user prefers the VisualEditor or the user has no preference and
|
|
|
|
// the VisualEditor is the default editor for this wiki
|
|
|
|
preferredEditor === 'VisualEditor' ||
|
|
|
|
// We've loaded it via the URL for this request
|
|
|
|
editorOverride === 'VisualEditor'
|
|
|
|
) &&
|
|
|
|
|
|
|
|
editorOverride !== 'SourceEditor'
|
2017-07-12 15:12:40 +00:00
|
|
|
) {
|
|
|
|
logInit( 'visualeditor' );
|
2018-08-22 00:26:46 +00:00
|
|
|
return loader.loadModule( 'mobile.editor.ve' ).then( function () {
|
2017-07-12 15:12:40 +00:00
|
|
|
var VisualEditorOverlay = M.require( 'mobile.editor.ve/VisualEditorOverlay' );
|
2018-08-22 00:26:46 +00:00
|
|
|
return new VisualEditorOverlay( editorOptions );
|
|
|
|
}, loadSourceEditor );
|
2017-07-12 15:12:40 +00:00
|
|
|
} else {
|
2018-08-22 00:26:46 +00:00
|
|
|
return loadSourceEditor();
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2018-09-21 01:38:10 +00:00
|
|
|
// By default the editor opens section 0 (lead section). If lead section is empty, and
|
|
|
|
// there are sections on the page, open editor with section 1 instead.
|
|
|
|
// (Be careful not to do this when leadSection is null, as this means MobileFormatter
|
|
|
|
// has not been run and thus we could not identify the lead.)
|
|
|
|
section = 0;
|
|
|
|
if ( leadSection && !leadSection.text() && !isNewPage && page.getSections().length !== 0 ) {
|
|
|
|
section = 1;
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2018-09-21 01:38:10 +00:00
|
|
|
$( '#ca-edit a' ).prop( 'href', function ( i, href ) {
|
|
|
|
var uri = new mw.Uri( href );
|
|
|
|
uri.query.section = section;
|
|
|
|
return uri.toString();
|
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-06-22 19:09:32 +00:00
|
|
|
if ( !router.getPath() && ( mw.util.getParamValue( 'veaction' ) || mw.util.getParamValue( 'action' ) === 'edit' ) ) {
|
2018-05-19 20:10:39 +00:00
|
|
|
if ( mw.util.getParamValue( 'veaction' ) === 'edit' ) {
|
|
|
|
editorOverride = 'VisualEditor';
|
|
|
|
} else if ( mw.util.getParamValue( 'veaction' ) === 'editsource' ) {
|
|
|
|
editorOverride = 'SourceEditor';
|
|
|
|
}
|
|
|
|
// else: action=edit, for which we allow the default to take effect
|
2018-06-11 15:55:30 +00:00
|
|
|
fragment = '#/editor/' + ( mw.util.getParamValue( 'section' ) || ( mw.util.getParamValue( 'action' ) === 'edit' && 'all' ) || '0' );
|
2018-05-19 20:10:39 +00:00
|
|
|
if ( window.history && history.pushState ) {
|
|
|
|
uri = mw.Uri();
|
|
|
|
delete uri.query.action;
|
|
|
|
delete uri.query.veaction;
|
2018-06-11 15:55:30 +00:00
|
|
|
delete uri.query.section;
|
2018-05-19 20:10:39 +00:00
|
|
|
// Note: replaceState rather than pushState, because we're
|
|
|
|
// just reformatting the URL to the equivalent-meaning for the
|
|
|
|
// mobile site.
|
|
|
|
history.replaceState( null, document.title, uri.toString() + fragment );
|
|
|
|
} else {
|
|
|
|
router.navigate( fragment );
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:15:43 +00:00
|
|
|
/**
|
|
|
|
* Hide any section id icons in the page. This will not hide the edit icon in the page action
|
|
|
|
* menu.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function hideSectionEditIcons() {
|
2018-09-21 01:38:10 +00:00
|
|
|
currentPage.$( '.mw-editsection' ).hide();
|
2018-08-23 20:15:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 22:44:55 +00:00
|
|
|
/**
|
|
|
|
* Show a drawer with log in / sign up buttons.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function showLoginDrawer() {
|
|
|
|
var drawer = new CtaDrawer( {
|
|
|
|
content: mw.msg( 'mobile-frontend-editor-disabled-anon' ),
|
|
|
|
signupQueryParams: {
|
|
|
|
warning: 'mobile-frontend-watchlist-signup-action'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
$allEditLinks.on( 'click', function ( ev ) {
|
|
|
|
drawer.show();
|
|
|
|
ev.preventDefault();
|
|
|
|
return drawer;
|
|
|
|
} );
|
|
|
|
router.route( editorPath, function () {
|
|
|
|
drawer.show();
|
|
|
|
} );
|
|
|
|
router.checkRoute();
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Setup the editor if the user can edit the page otherwise show a sorry toast.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function init() {
|
2018-11-02 22:44:55 +00:00
|
|
|
var isReadOnly, isEditable, editErrorMessage, editRestrictions;
|
2018-10-05 23:31:06 +00:00
|
|
|
// see: https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#Page-specific
|
|
|
|
isReadOnly = mw.config.get( 'wgMinervaReadOnly' );
|
|
|
|
isEditable = !isReadOnly && mw.config.get( 'wgIsProbablyEditable' );
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
if ( isEditable ) {
|
2018-04-06 19:52:07 +00:00
|
|
|
// Edit button updated in setupEditor.
|
2017-07-12 15:12:40 +00:00
|
|
|
setupEditor( currentPage );
|
|
|
|
} else {
|
2018-08-23 20:15:43 +00:00
|
|
|
hideSectionEditIcons();
|
2018-11-02 17:35:37 +00:00
|
|
|
editRestrictions = mw.config.get( 'wgRestrictionEdit' );
|
|
|
|
if ( mw.user.isAnon() && Array.isArray( editRestrictions ) && editRestrictions.indexOf( '*' ) !== -1 ) {
|
2018-11-02 22:44:55 +00:00
|
|
|
showLoginDrawer();
|
2018-11-02 17:35:37 +00:00
|
|
|
} else {
|
|
|
|
editErrorMessage = isReadOnly ? mw.msg( 'apierror-readonly' ) : mw.msg( 'mobile-frontend-editor-disabled' );
|
|
|
|
showSorryToast( editErrorMessage );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a toast message with sincere condolences.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @param {string} msg Message for sorry message
|
|
|
|
*/
|
|
|
|
function showSorryToast( msg ) {
|
2018-09-21 01:38:10 +00:00
|
|
|
$allEditLinks.on( 'click', function ( ev ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
popup.show( msg );
|
|
|
|
ev.preventDefault();
|
|
|
|
} );
|
2018-11-02 22:44:55 +00:00
|
|
|
router.route( editorPath, function () {
|
|
|
|
popup.show( msg );
|
|
|
|
} );
|
|
|
|
router.checkRoute();
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 14:12:32 +00:00
|
|
|
if ( contentModel !== 'wikitext' ) {
|
|
|
|
// Only load the wikitext editor on wikitext. Otherwise we'll rely on the fallback behaviour
|
|
|
|
// (You can test this on MediaWiki:Common.css) ?action=edit url (T173800)
|
|
|
|
return;
|
2018-08-31 22:48:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:40:51 +00:00
|
|
|
if ( mw.util.getParamValue( 'undo' ) ) {
|
|
|
|
// Our fancy editor doesn't support undo, but we can rely on the fallback.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-31 22:48:34 +00:00
|
|
|
if ( !isEditingSupported ) {
|
2018-09-21 00:20:37 +00:00
|
|
|
// Browser doesn't support mobile editor (or is blacklisted), use the fallback editor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isNewFile ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
// Is a new file page (enable upload image only) Bug 58311
|
|
|
|
showSorryToast( mw.msg( 'mobile-frontend-editor-uploadenable' ) );
|
|
|
|
} else {
|
2018-10-05 23:31:06 +00:00
|
|
|
// Edit button is currently hidden. A call to init() will update it as needed.
|
|
|
|
init();
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
}( mw.mobileFrontend, jQuery ) );
|