2016-10-25 20:44:26 +00:00
|
|
|
/*!
|
2017-06-10 11:48:09 +00:00
|
|
|
* VisualEditor MediaWiki CollabTarget init.
|
2016-10-25 20:44:26 +00:00
|
|
|
*
|
2019-01-01 13:24:23 +00:00
|
|
|
* @copyright 2011-2019 VisualEditor Team and others; see AUTHORS.txt
|
2016-10-25 20:44:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2019-02-20 20:23:43 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2019-01-08 17:00:09 +00:00
|
|
|
|
2016-10-25 20:44:26 +00:00
|
|
|
( function () {
|
2017-05-20 19:22:44 +00:00
|
|
|
var target,
|
2018-09-16 18:53:24 +00:00
|
|
|
$specialTab = $( '#ca-nstab-special' ),
|
|
|
|
$padTab = $( '#ca-pad' ),
|
2016-10-25 20:44:26 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' ),
|
2017-05-20 19:22:44 +00:00
|
|
|
pageName = mw.config.get( 'collabPadPageName' ) || '',
|
|
|
|
pageTitle = mw.Title.newFromText( pageName ),
|
2018-05-22 21:39:36 +00:00
|
|
|
modules = [ OO.ui.isMobile() ? 'ext.visualEditor.collabTarget.mobile' : 'ext.visualEditor.collabTarget.desktop' ]
|
2016-10-25 20:44:26 +00:00
|
|
|
// Add modules from $wgVisualEditorPluginModules
|
|
|
|
.concat( conf.pluginModules.filter( mw.loader.getState ) ),
|
2018-08-25 22:18:02 +00:00
|
|
|
modulePromise = mw.loader.using( modules ),
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar = OO.ui.infuse( $( '.ve-init-mw-collabTarget-loading' ) ),
|
|
|
|
documentNameInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameInput' ) ),
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameButton' ) ),
|
2018-06-23 18:29:33 +00:00
|
|
|
importInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importInput' ), {
|
|
|
|
showImages: mw.config.get( 'wgVisualEditor' ).usePageImages,
|
|
|
|
showDescriptions: mw.config.get( 'wgVisualEditor' ).usePageDescriptions
|
|
|
|
} ),
|
|
|
|
importButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importButton' ) ),
|
|
|
|
// Infuse the form last to avoid recursive infusion with no config
|
|
|
|
form = OO.ui.infuse( $( '.ve-init-mw-collabTarget-form' ) );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( !VisualEditorSupportCheck() ) {
|
|
|
|
// VE not supported - say something?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:48:09 +00:00
|
|
|
function setTitle( title ) {
|
|
|
|
$( '#firstHeading' ).text( title );
|
|
|
|
document.title = title;
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:20:25 +00:00
|
|
|
function showPage( title, importTitle ) {
|
2018-09-16 18:53:24 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
|
|
|
|
|
2017-06-10 11:48:09 +00:00
|
|
|
setTitle( mw.msg( 'collabpad-doctitle', title.getPrefixedText() ) );
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2018-09-16 18:53:24 +00:00
|
|
|
mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
|
|
|
|
mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
|
|
|
|
if ( !$padTab.length ) {
|
|
|
|
$padTab = $( '<li>' ).attr( 'id', 'ca-pad' ).addClass( 'selected' ).append(
|
|
|
|
$( '<span>' ).append(
|
|
|
|
$( '<a>' ).attr( 'href', '' ).text( title.getPrefixedText() )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$padTab.insertAfter( $specialTab.removeClass( 'selected' ) );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( true );
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( false );
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2018-08-25 22:18:02 +00:00
|
|
|
modulePromise.done( function () {
|
|
|
|
var dummySurface, surfaceModel,
|
|
|
|
progressDeferred = $.Deferred();
|
|
|
|
|
2018-05-21 14:20:25 +00:00
|
|
|
target = ve.init.mw.targetFactory.create( 'collab', title, conf.rebaserUrl, { importTitle: importTitle } );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
$( 'body' ).addClass( 've-activated ve-active' );
|
|
|
|
|
2018-09-17 14:24:27 +00:00
|
|
|
$( '#content' ).prepend( target.$element );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
target.transformPage();
|
|
|
|
$( '#firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
|
|
|
|
2018-08-25 22:18:02 +00:00
|
|
|
// Add a dummy surface while the doc is loading
|
|
|
|
dummySurface = target.addSurface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
|
2019-02-08 19:18:22 +00:00
|
|
|
dummySurface.setReadOnly( true );
|
2018-08-25 22:18:02 +00:00
|
|
|
|
|
|
|
// TODO: Create the correct model surface type (ve.ui.Surface#createModel)
|
|
|
|
surfaceModel = new ve.dm.Surface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
|
|
|
|
surfaceModel.createSynchronizer(
|
|
|
|
title.toString(),
|
|
|
|
{
|
|
|
|
server: conf.rebaserUrl,
|
|
|
|
// TODO: server could communicate with MW (via oauth?) to know the
|
|
|
|
// current-user's name. Disable changing name if logged in?
|
|
|
|
// Communicate an I-am-a-valid-user flag to other clients?
|
|
|
|
defaultName: mw.user.isAnon() ? mw.user.getName() : undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
dummySurface.createProgress( progressDeferred.promise(), ve.msg( 'visualeditor-rebase-client-connecting' ), true );
|
|
|
|
|
|
|
|
surfaceModel.synchronizer.once( 'initDoc', function () {
|
2018-09-17 14:24:27 +00:00
|
|
|
var initPromise;
|
2018-08-25 22:18:02 +00:00
|
|
|
|
|
|
|
progressDeferred.resolve();
|
2018-09-17 14:24:27 +00:00
|
|
|
// Resolving the progress bar doesn't close the window in this cycle,
|
|
|
|
// so wait until we call clearSurfaces which destroys the window manager.
|
|
|
|
setTimeout( function () {
|
|
|
|
target.clearSurfaces();
|
|
|
|
// Don't add the surface until the history has been applied
|
|
|
|
target.addSurface( surfaceModel );
|
|
|
|
target.once( 'surfaceReady', function () {
|
|
|
|
initPromise.then( function () {
|
|
|
|
surfaceModel.selectFirstContentOffset();
|
|
|
|
} );
|
2018-09-14 23:19:54 +00:00
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
|
2018-09-17 14:24:27 +00:00
|
|
|
if ( target.importTitle && !surfaceModel.getDocument().getCompleteHistoryLength() ) {
|
|
|
|
initPromise = mw.libs.ve.targetLoader.requestParsoidData( target.importTitle.toString(), { targetName: 'collabpad' } ).then( function ( response ) {
|
|
|
|
var doc, dmDoc, fragment,
|
|
|
|
data = response.visualeditor;
|
|
|
|
|
|
|
|
if ( data && data.content ) {
|
|
|
|
doc = target.constructor.static.parseDocument( data.content );
|
|
|
|
dmDoc = target.constructor.static.createModelFromDom( doc );
|
|
|
|
fragment = surfaceModel.getLinearFragment( new ve.Range( 0, 2 ) );
|
|
|
|
fragment.insertDocument( dmDoc );
|
|
|
|
|
|
|
|
target.etag = data.etag;
|
|
|
|
target.baseTimeStamp = data.basetimestamp;
|
|
|
|
target.startTimeStamp = data.starttimestamp;
|
|
|
|
target.revid = data.oldid;
|
|
|
|
|
|
|
|
// Store the document metadata as a hidden meta item
|
|
|
|
fragment.collapseToEnd().insertContent( [
|
|
|
|
{
|
|
|
|
type: 'alienMeta',
|
|
|
|
attributes: {
|
|
|
|
importedDocument: {
|
|
|
|
title: target.importTitle.toString(),
|
|
|
|
etag: target.etag,
|
|
|
|
baseTimeStamp: target.baseTimeStamp,
|
|
|
|
startTimeStamp: target.startTimeStamp,
|
|
|
|
revid: target.revid
|
|
|
|
}
|
2018-08-25 22:18:02 +00:00
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
},
|
|
|
|
{ type: '/alienMeta' }
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
// Import failed
|
|
|
|
return $.Deferred().reject( 'No content for ' + target.importTitle ).promise();
|
|
|
|
}
|
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
} else {
|
2018-09-17 14:24:27 +00:00
|
|
|
// No import, or history already exists
|
|
|
|
initPromise = $.Deferred().resolve().promise();
|
|
|
|
|
|
|
|
// Look for import metadata in document
|
|
|
|
surfaceModel = target.getSurface().getModel();
|
|
|
|
surfaceModel.getMetaList().getItemsInGroup( 'misc' ).some( function ( item ) {
|
|
|
|
var importedDocument = item.getAttribute( 'importedDocument' );
|
|
|
|
if ( importedDocument ) {
|
|
|
|
target.importTitle = mw.Title.newFromText( importedDocument.title );
|
|
|
|
target.etag = importedDocument.etag;
|
|
|
|
target.baseTimeStamp = importedDocument.baseTimeStamp;
|
|
|
|
target.startTimeStamp = importedDocument.startTimeStamp;
|
|
|
|
target.revid = importedDocument.revid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
initPromise.fail( function ( err ) {
|
|
|
|
setTimeout( function () {
|
|
|
|
throw new Error( err );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
initPromise.always( function () {
|
|
|
|
progressDeferred.resolve();
|
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
} ).always( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( false );
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( false );
|
2018-05-22 21:39:36 +00:00
|
|
|
} ).fail( function ( err ) {
|
|
|
|
mw.log.error( err );
|
2017-05-20 19:22:44 +00:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
showForm();
|
2016-10-25 20:44:26 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:25:26 +00:00
|
|
|
function showForm( pushState ) {
|
2018-09-16 18:53:24 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad' );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2019-03-04 13:25:26 +00:00
|
|
|
if ( pushState ) {
|
|
|
|
history.pushState( { tag: 'collabTarget' }, mw.msg( 'collabpad' ), specialTitle.getUrl() );
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
if ( target ) {
|
|
|
|
$( '#firstHeading' ).removeClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
|
|
|
target.restorePage();
|
|
|
|
target.destroy();
|
|
|
|
|
|
|
|
$( 'body' ).removeClass( 've-activated ve-active' );
|
|
|
|
}
|
|
|
|
|
2018-09-16 18:53:24 +00:00
|
|
|
setTitle( mw.msg( 'collabpad' ) );
|
|
|
|
mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
|
|
|
|
mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
|
|
|
|
if ( $padTab ) {
|
|
|
|
$padTab.detach();
|
|
|
|
}
|
|
|
|
$specialTab.addClass( 'selected' );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( false );
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( true );
|
2017-05-20 19:22:44 +00:00
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2018-05-21 14:20:25 +00:00
|
|
|
function loadTitle( title, importTitle ) {
|
2018-05-18 09:43:29 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
|
|
|
|
if ( history.pushState ) {
|
|
|
|
// TODO: Handle popstate
|
2018-05-21 14:20:25 +00:00
|
|
|
history.pushState( { tag: 'collabTarget', title: title.toString() }, title.getMain(), specialTitle.getUrl() );
|
|
|
|
showPage( title, importTitle );
|
2018-05-18 09:43:29 +00:00
|
|
|
} else {
|
|
|
|
location.href = specialTitle.getUrl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomTitle() {
|
|
|
|
return Math.random().toString( 36 ).slice( 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
function onNameChange() {
|
2017-09-14 12:42:36 +00:00
|
|
|
documentNameInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( false );
|
2017-09-14 12:42:36 +00:00
|
|
|
}, function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( true );
|
2017-09-14 12:42:36 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-05-18 09:43:29 +00:00
|
|
|
function loadFromName() {
|
2017-05-20 19:22:44 +00:00
|
|
|
documentNameInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
var title = mw.Title.newFromText(
|
|
|
|
documentNameInput.getValue().trim() || getRandomTitle()
|
|
|
|
);
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( title ) {
|
2018-05-18 09:43:29 +00:00
|
|
|
loadTitle( title );
|
2016-10-25 20:44:26 +00:00
|
|
|
} else {
|
|
|
|
documentNameInput.focus();
|
|
|
|
}
|
2017-05-20 19:22:44 +00:00
|
|
|
} );
|
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
documentNameInput.setValidation( function ( value ) {
|
2018-01-08 15:32:15 +00:00
|
|
|
// Empty input will create a random document name, otherwise must be valid
|
|
|
|
return value === '' || !!mw.Title.newFromText( value );
|
2017-05-20 19:22:44 +00:00
|
|
|
} );
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( false );
|
|
|
|
|
|
|
|
documentNameInput.on( 'change', onNameChange );
|
|
|
|
documentNameInput.on( 'enter', loadFromName );
|
|
|
|
documentNameButton.on( 'click', loadFromName );
|
|
|
|
onNameChange();
|
|
|
|
|
|
|
|
function onImportChange() {
|
2018-07-14 09:20:11 +00:00
|
|
|
importInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
importButton.setDisabled( false );
|
|
|
|
}, function () {
|
|
|
|
importButton.setDisabled( true );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
function importTitle() {
|
|
|
|
importInput.getValidity().then( function () {
|
|
|
|
var title = mw.Title.newFromText( importInput.getValue().trim() );
|
|
|
|
|
|
|
|
if ( title ) {
|
2018-05-21 14:20:25 +00:00
|
|
|
loadTitle( mw.Title.newFromText( getRandomTitle() ), title );
|
2018-05-18 09:43:29 +00:00
|
|
|
} else {
|
|
|
|
documentNameInput.focus();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2018-05-18 09:43:29 +00:00
|
|
|
importInput.setValidation( function ( value ) {
|
|
|
|
// TODO: Check page exists?
|
|
|
|
return !!mw.Title.newFromText( value );
|
|
|
|
} );
|
2018-07-14 09:20:11 +00:00
|
|
|
importInput.on( 'change', onImportChange );
|
2018-05-18 09:43:29 +00:00
|
|
|
importInput.on( 'enter', importTitle );
|
|
|
|
importButton.on( 'click', importTitle );
|
|
|
|
onImportChange();
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( pageTitle ) {
|
|
|
|
showPage( pageTitle );
|
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2019-02-28 19:20:14 +00:00
|
|
|
$specialTab.on( 'click', function ( e ) {
|
2019-03-04 13:25:26 +00:00
|
|
|
showForm( true );
|
2019-02-28 19:20:14 +00:00
|
|
|
e.preventDefault();
|
|
|
|
} );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
// Tag current state
|
|
|
|
if ( history.replaceState ) {
|
|
|
|
history.replaceState( { tag: 'collabTarget', title: pageName }, document.title, location.href );
|
|
|
|
}
|
|
|
|
window.addEventListener( 'popstate', function ( e ) {
|
|
|
|
if ( e.state && e.state.tag === 'collabTarget' ) {
|
|
|
|
if ( e.state.title ) {
|
|
|
|
showPage( mw.Title.newFromText( e.state.title ) );
|
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2016-10-25 20:44:26 +00:00
|
|
|
}() );
|