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
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team and others; see AUTHORS.txt
|
2016-10-25 20:44:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function () {
|
2017-05-20 19:22:44 +00:00
|
|
|
var target,
|
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 ),
|
2016-10-25 20:44:26 +00:00
|
|
|
modules = [ 'ext.visualEditor.collabTarget' ]
|
|
|
|
// Add modules from $wgVisualEditorPluginModules
|
|
|
|
.concat( conf.pluginModules.filter( mw.loader.getState ) ),
|
2017-05-20 19:22:44 +00:00
|
|
|
loadingPromise = mw.loader.using( modules ),
|
|
|
|
progressBar = OO.ui.infuse( $( '.ve-init-mw-collabTarget-loading' ) ),
|
|
|
|
documentNameField = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameField' ) ),
|
|
|
|
documentNameInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameInput' ) ),
|
|
|
|
submitButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameButton' ) );
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:44:26 +00:00
|
|
|
function showPage( title ) {
|
2017-06-10 11:48:09 +00:00
|
|
|
setTitle( mw.msg( 'collabpad-doctitle', title.getPrefixedText() ) );
|
2017-05-20 19:22:44 +00:00
|
|
|
|
|
|
|
progressBar.toggle( true );
|
|
|
|
documentNameField.toggle( false );
|
|
|
|
|
|
|
|
loadingPromise.done( function () {
|
2017-05-20 21:26:42 +00:00
|
|
|
target = ve.init.mw.targetFactory.create( 'collab', title, conf.rebaserUrl );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
$( 'body' ).addClass( 've-activated ve-active' );
|
|
|
|
|
|
|
|
$( '#content' ).append( target.$element );
|
|
|
|
|
|
|
|
target.transformPage();
|
|
|
|
$( '#firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
|
|
|
|
|
|
|
target.documentReady( ve.createDocumentFromHtml( '' ) );
|
2017-05-20 19:22:44 +00:00
|
|
|
} ).always( function () {
|
|
|
|
documentNameField.toggle( false );
|
|
|
|
progressBar.toggle( false );
|
|
|
|
} ).fail( function () {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
showForm();
|
2016-10-25 20:44:26 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
function showForm() {
|
2017-06-10 11:48:09 +00:00
|
|
|
setTitle( mw.msg( 'collabpad' ) );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
progressBar.toggle( false );
|
|
|
|
documentNameField.toggle( true );
|
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2017-09-14 12:42:36 +00:00
|
|
|
function onChange() {
|
|
|
|
documentNameInput.getValidity().then( function () {
|
|
|
|
submitButton.setDisabled( false );
|
|
|
|
}, function () {
|
|
|
|
submitButton.setDisabled( true );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
function onSubmit() {
|
|
|
|
documentNameInput.getValidity().then( function () {
|
2018-01-08 15:32:15 +00:00
|
|
|
var specialTitle,
|
|
|
|
title = mw.Title.newFromText(
|
|
|
|
documentNameInput.getValue().trim() || Math.random().toString( 36 ).slice( 2 )
|
|
|
|
);
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( title ) {
|
2018-01-08 15:32:15 +00:00
|
|
|
specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
|
2016-10-25 20:44:26 +00:00
|
|
|
if ( history.pushState ) {
|
|
|
|
// TODO: Handle popstate
|
2017-05-20 19:22:44 +00:00
|
|
|
history.pushState( { tag: 'collabTarget', title: title.toString() }, title.getMain(), specialTitle.getUrl() );
|
2016-10-25 20:44:26 +00:00
|
|
|
showPage( title );
|
|
|
|
} else {
|
2017-05-20 19:22:44 +00:00
|
|
|
location.href = specialTitle.getUrl();
|
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
|
|
|
} );
|
|
|
|
submitButton.setDisabled( false );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2017-09-14 12:42:36 +00:00
|
|
|
documentNameInput.on( 'change', onChange );
|
2017-05-20 19:22:44 +00:00
|
|
|
documentNameInput.on( 'enter', onSubmit );
|
|
|
|
submitButton.on( 'click', onSubmit );
|
2017-09-14 12:42:36 +00:00
|
|
|
onChange();
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( pageTitle ) {
|
|
|
|
showPage( pageTitle );
|
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
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
|
|
|
}() );
|