2011-02-24 01:45:55 +00:00
|
|
|
/**
|
|
|
|
* Configuration of Dialog module for wikiEditor
|
|
|
|
*/
|
2018-11-12 22:28:40 +00:00
|
|
|
( function () {
|
2011-02-24 01:45:55 +00:00
|
|
|
|
2021-09-01 07:17:53 +00:00
|
|
|
var toolbarModule = require( './jquery.wikiEditor.toolbar.js' ),
|
|
|
|
InsertLinkTitleInputField = require( './insertlink/TitleInputField.js' ),
|
2021-09-13 03:17:05 +00:00
|
|
|
LinkTextField = require( './insertlink/LinkTextField.js' ),
|
2021-10-11 05:31:20 +00:00
|
|
|
LinkTypeField = require( './insertlink/LinkTypeField.js' ),
|
2021-09-01 07:17:53 +00:00
|
|
|
insertLinkTitleInputField = new InsertLinkTitleInputField(),
|
2021-09-13 03:17:05 +00:00
|
|
|
insertLinkLinkTextField = new LinkTextField(),
|
2021-10-11 05:31:20 +00:00
|
|
|
insertLinkLinkTypeField = new LinkTypeField(),
|
2019-07-20 08:09:04 +00:00
|
|
|
configData = require( './data.json' );
|
2014-05-05 13:51:10 +00:00
|
|
|
|
2021-02-10 18:29:40 +00:00
|
|
|
function triggerButtonClick( element ) {
|
2021-06-15 14:45:13 +00:00
|
|
|
// The dialog action should always be a DOMElement.
|
|
|
|
var dialogAction = $( element ).data( 'dialogaction' );
|
|
|
|
var $button = dialogAction ? $( dialogAction ) : $( element ).find( 'button' ).first();
|
2021-02-10 18:29:40 +00:00
|
|
|
// Since we're reading from data attribute, make sure we got an element before clicking.
|
|
|
|
// Note when closing a dialog this can be false leading to TypeError: $button.trigger is not a function
|
|
|
|
// (T261529)
|
|
|
|
if ( $button ) {
|
|
|
|
$button.trigger( 'click' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 08:09:04 +00:00
|
|
|
module.exports = {
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
replaceIcons: function ( $textarea ) {
|
|
|
|
$textarea
|
|
|
|
.wikiEditor( 'addToToolbar', {
|
|
|
|
section: 'main',
|
|
|
|
group: 'insert',
|
|
|
|
tools: {
|
|
|
|
link: {
|
|
|
|
labelMsg: 'wikieditor-toolbar-tool-link',
|
|
|
|
type: 'button',
|
2018-03-29 23:20:24 +00:00
|
|
|
oouiIcon: 'link',
|
2016-11-20 17:13:39 +00:00
|
|
|
action: {
|
|
|
|
type: 'dialog',
|
|
|
|
module: 'insert-link'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
file: {
|
|
|
|
labelMsg: 'wikieditor-toolbar-tool-file',
|
|
|
|
type: 'button',
|
2018-03-29 23:20:24 +00:00
|
|
|
oouiIcon: 'image',
|
2016-11-20 17:13:39 +00:00
|
|
|
action: {
|
|
|
|
type: 'dialog',
|
|
|
|
module: 'insert-file'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
reference: {
|
|
|
|
labelMsg: 'wikieditor-toolbar-tool-reference',
|
|
|
|
filters: [ 'body.ns-subject' ],
|
|
|
|
type: 'button',
|
2018-08-21 22:52:29 +00:00
|
|
|
oouiIcon: 'reference',
|
2016-11-20 17:13:39 +00:00
|
|
|
action: {
|
2018-11-16 20:49:31 +00:00
|
|
|
type: 'encapsulate',
|
|
|
|
options: {
|
|
|
|
pre: '<ref>',
|
|
|
|
post: '</ref>'
|
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
} )
|
|
|
|
.wikiEditor( 'addToToolbar', {
|
|
|
|
section: 'advanced',
|
|
|
|
group: 'insert',
|
|
|
|
tools: {
|
|
|
|
table: {
|
|
|
|
labelMsg: 'wikieditor-toolbar-tool-table',
|
|
|
|
type: 'button',
|
2018-03-29 23:20:24 +00:00
|
|
|
oouiIcon: 'table',
|
2016-11-20 17:13:39 +00:00
|
|
|
action: {
|
|
|
|
type: 'dialog',
|
|
|
|
module: 'insert-table'
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
} )
|
|
|
|
.wikiEditor( 'addToToolbar', {
|
|
|
|
section: 'advanced',
|
|
|
|
groups: {
|
|
|
|
search: {
|
|
|
|
tools: {
|
|
|
|
replace: {
|
|
|
|
labelMsg: 'wikieditor-toolbar-tool-replace',
|
|
|
|
type: 'button',
|
2018-08-24 16:41:17 +00:00
|
|
|
oouiIcon: 'articleSearch',
|
2016-11-20 17:13:39 +00:00
|
|
|
action: {
|
|
|
|
type: 'dialog',
|
|
|
|
module: 'search-and-replace'
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
} );
|
|
|
|
},
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
getDefaultConfig: function () {
|
|
|
|
return { dialogs: {
|
|
|
|
'insert-link': {
|
|
|
|
titleMsg: 'wikieditor-toolbar-tool-link-title',
|
|
|
|
id: 'wikieditor-toolbar-link-dialog',
|
|
|
|
htmlTemplate: 'dialogInsertLink.html',
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
init: function () {
|
2021-09-01 07:17:53 +00:00
|
|
|
$( '.wikieditor-toolbar-link-target' ).replaceWith( insertLinkTitleInputField.$element );
|
2021-09-13 03:17:05 +00:00
|
|
|
$( '.wikieditor-toolbar-link-text' ).replaceWith( insertLinkLinkTextField.$element );
|
2021-10-11 05:31:20 +00:00
|
|
|
$( '.wikieditor-toolbar-link-type' ).replaceWith( insertLinkLinkTypeField.$element );
|
2015-01-07 18:25:47 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Automatically copy the value of the internal link page title field to the link text field unless the
|
|
|
|
// user has changed the link text field - this is a convenience thing since most link texts are going to
|
2018-08-25 12:55:44 +00:00
|
|
|
// be the same as the page title - Also change the internal/external radio button accordingly
|
2021-09-01 07:17:53 +00:00
|
|
|
insertLinkTitleInputField.connect( this, {
|
|
|
|
change: function ( val ) {
|
2021-10-11 05:31:20 +00:00
|
|
|
insertLinkLinkTypeField.setIsExternal( insertLinkTitleInputField.getField().isExternalLink( val ) );
|
2021-09-13 03:17:05 +00:00
|
|
|
insertLinkLinkTextField.setValueIfUntouched( val );
|
2021-09-01 07:17:53 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
|
|
|
$( '.ui-dialog:visible .ui-dialog-buttonpane button' )
|
|
|
|
.first()
|
|
|
|
.prop( 'disabled', false )
|
|
|
|
.removeClass( 'disabled' );
|
|
|
|
},
|
|
|
|
invalid: function () {
|
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
|
|
|
$( '.ui-dialog:visible .ui-dialog-buttonpane button' )
|
|
|
|
.first()
|
|
|
|
.prop( 'disabled', true )
|
|
|
|
.addClass( 'disabled' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
// Tell the title input field when the internal/external radio changes.
|
2021-10-11 05:31:20 +00:00
|
|
|
insertLinkLinkTypeField.connect( this, {
|
|
|
|
change: function ( isExternal ) {
|
|
|
|
var urlMode = isExternal ?
|
|
|
|
insertLinkTitleInputField.urlModes.external :
|
|
|
|
insertLinkTitleInputField.urlModes.internal;
|
|
|
|
insertLinkTitleInputField.setUrlMode( urlMode );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
} );
|
2016-11-20 17:13:39 +00:00
|
|
|
},
|
|
|
|
dialog: {
|
|
|
|
width: 500,
|
|
|
|
dialogClass: 'wikiEditor-toolbar-dialog',
|
|
|
|
buttons: {
|
|
|
|
'wikieditor-toolbar-tool-link-insert': function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var that = this;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
function escapeInternalText( s ) {
|
|
|
|
return s.replace( /(\]{2,})/g, '<nowiki>$1</nowiki>' );
|
|
|
|
}
|
|
|
|
function escapeExternalTarget( s ) {
|
|
|
|
return s.replace( / /g, '%20' )
|
|
|
|
.replace( /\[/g, '%5B' )
|
|
|
|
.replace( /\]/g, '%5D' );
|
|
|
|
}
|
|
|
|
function escapeExternalText( s ) {
|
|
|
|
return s.replace( /(\]+)/g, '<nowiki>$1</nowiki>' );
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
|
2021-09-01 07:17:53 +00:00
|
|
|
var target = insertLinkTitleInputField.getField().getValue();
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( target === '' ) {
|
2017-05-04 21:36:58 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2016-11-20 17:13:39 +00:00
|
|
|
alert( mw.msg( 'wikieditor-toolbar-tool-link-empty' ) );
|
2012-07-03 05:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
|
2021-09-13 03:17:05 +00:00
|
|
|
var text = insertLinkLinkTextField.getField().getValue();
|
2018-11-27 17:36:31 +00:00
|
|
|
if ( text.trim() === '' ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
// [[Foo| ]] creates an invisible link
|
|
|
|
// Instead, generate [[Foo|]]
|
|
|
|
text = '';
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var insertText = '';
|
2021-10-11 05:31:20 +00:00
|
|
|
if ( insertLinkLinkTypeField.isInternal() ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
// FIXME: Exactly how fragile is this?
|
2019-10-07 15:52:10 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( $( '#wikieditor-toolbar-link-int-target-status-invalid' ).is( ':visible' ) ) {
|
|
|
|
// Refuse to add links to invalid titles
|
2017-05-04 21:36:58 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2016-11-20 17:13:39 +00:00
|
|
|
alert( mw.msg( 'wikieditor-toolbar-tool-link-int-invalid' ) );
|
|
|
|
return;
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( target === text || !text.length ) {
|
2021-09-21 17:07:13 +00:00
|
|
|
insertText = '[[' + target + ']]';
|
2016-11-20 17:13:39 +00:00
|
|
|
} else {
|
2021-09-21 17:07:13 +00:00
|
|
|
insertText = '[[' + target + '|' + escapeInternalText( text ) + ']]';
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
2015-11-15 14:30:18 +00:00
|
|
|
} else {
|
2018-11-27 17:36:31 +00:00
|
|
|
target = target.trim();
|
2016-11-20 17:13:39 +00:00
|
|
|
// Prepend http:// if there is no protocol
|
|
|
|
if ( !target.match( /^[a-z]+:\/\/./ ) ) {
|
|
|
|
target = 'http://' + target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if this is really an internal link in disguise
|
2021-06-15 14:45:13 +00:00
|
|
|
var match = target.match( $( this ).data( 'articlePathRegex' ) );
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( match && !$( this ).data( 'ignoreLooksInternal' ) ) {
|
2021-06-15 14:45:13 +00:00
|
|
|
var buttons = {};
|
2016-11-20 17:13:39 +00:00
|
|
|
buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-int' ) ] =
|
|
|
|
function () {
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-link-int-target' ).val( match[ 1 ] ).trigger( 'change' );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).dialog( 'close' );
|
|
|
|
};
|
|
|
|
buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-ext' ) ] =
|
|
|
|
function () {
|
|
|
|
$( that ).data( 'ignoreLooksInternal', true );
|
2019-10-07 15:52:10 +00:00
|
|
|
$( that ).closest( '.ui-dialog' ).find( 'button' ).first().trigger( 'click' );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( that ).data( 'ignoreLooksInternal', false );
|
|
|
|
$( this ).dialog( 'close' );
|
|
|
|
};
|
|
|
|
$.wikiEditor.modules.dialogs.quickDialog(
|
|
|
|
mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal', match[ 1 ] ),
|
|
|
|
{ buttons: buttons }
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var escTarget = escapeExternalTarget( target );
|
|
|
|
var escText = escapeExternalText( text );
|
2016-11-20 17:13:39 +00:00
|
|
|
|
|
|
|
if ( escTarget === escText ) {
|
|
|
|
insertText = escTarget;
|
|
|
|
} else if ( text === '' ) {
|
|
|
|
insertText = '[' + escTarget + ']';
|
|
|
|
} else {
|
|
|
|
insertText = '[' + escTarget + ' ' + escText + ']';
|
|
|
|
}
|
2015-11-15 14:30:18 +00:00
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
|
|
|
|
var whitespace = $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace' );
|
2016-11-20 17:13:39 +00:00
|
|
|
// Preserve whitespace in selection when replacing
|
|
|
|
if ( whitespace ) {
|
|
|
|
insertText = whitespace[ 0 ] + insertText + whitespace[ 1 ];
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).dialog( 'close' );
|
2019-07-20 08:09:04 +00:00
|
|
|
toolbarModule.fn.doAction( $( this ).data( 'context' ), {
|
2016-11-20 17:13:39 +00:00
|
|
|
type: 'replace',
|
|
|
|
options: {
|
|
|
|
pre: insertText
|
|
|
|
}
|
|
|
|
}, $( this ) );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Blank form
|
2021-09-01 07:17:53 +00:00
|
|
|
insertLinkTitleInputField.getField().setValue( '' );
|
2021-09-13 03:17:05 +00:00
|
|
|
insertLinkLinkTextField.getField().setValue( '' );
|
2021-10-11 05:31:20 +00:00
|
|
|
insertLinkLinkTypeField.getField().selectItem( null );
|
2016-11-20 17:13:39 +00:00
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-link-cancel': function () {
|
|
|
|
$( this ).dialog( 'close' );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
},
|
2016-11-20 17:13:39 +00:00
|
|
|
open: function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
// Obtain the server name without the protocol. wgServer may be protocol-relative
|
|
|
|
var serverName = mw.config.get( 'wgServer' ).replace( /^(https?:)?\/\//, '' );
|
2016-11-20 17:13:39 +00:00
|
|
|
// Cache the articlepath regex
|
|
|
|
$( this ).data( 'articlePathRegex', new RegExp(
|
2019-09-09 17:32:48 +00:00
|
|
|
'^https?://' + mw.util.escapeRegExp( serverName + mw.config.get( 'wgArticlePath' ) )
|
2016-11-20 17:13:39 +00:00
|
|
|
.replace( /\\\$1/g, '(.*)' ) + '$'
|
|
|
|
) );
|
|
|
|
// Pre-fill the text fields based on the current selection
|
2021-06-15 14:45:13 +00:00
|
|
|
var context = $( this ).data( 'context' );
|
|
|
|
var selection = context.$textarea.textSelection( 'getSelection' );
|
2021-09-01 07:17:53 +00:00
|
|
|
|
|
|
|
insertLinkTitleInputField.getField().focus();
|
2016-11-20 17:13:39 +00:00
|
|
|
// Trigger the change event, so the link status indicator is up to date
|
2021-09-01 07:17:53 +00:00
|
|
|
insertLinkTitleInputField.getField().$input.trigger( 'change' );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ '', '' ] );
|
|
|
|
if ( selection !== '' ) {
|
2021-10-11 05:31:20 +00:00
|
|
|
var matches, target, text, isExternal;
|
2021-09-21 17:07:13 +00:00
|
|
|
if ( ( matches = selection.match( /^(\s*)\[\[([^\]|]+)(\|([^\]|]*))?\]\](\s*)$/ ) ) ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
// [[foo|bar]] or [[foo]]
|
|
|
|
target = matches[ 2 ];
|
|
|
|
text = ( matches[ 4 ] ? matches[ 4 ] : matches[ 2 ] );
|
2021-10-11 05:31:20 +00:00
|
|
|
isExternal = false;
|
2016-11-20 17:13:39 +00:00
|
|
|
// Preserve whitespace when replacing
|
|
|
|
$( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ matches[ 1 ], matches[ 5 ] ] );
|
|
|
|
} else if ( ( matches = selection.match( /^(\s*)\[([^\] ]+)( ([^\]]+))?\](\s*)$/ ) ) ) {
|
|
|
|
// [http://www.example.com foo] or [http://www.example.com]
|
|
|
|
target = matches[ 2 ];
|
|
|
|
text = ( matches[ 4 ] || '' );
|
2021-10-11 05:31:20 +00:00
|
|
|
isExternal = true;
|
2016-11-20 17:13:39 +00:00
|
|
|
// Preserve whitespace when replacing
|
|
|
|
$( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ matches[ 1 ], matches[ 5 ] ] );
|
|
|
|
} else {
|
|
|
|
// Trim any leading and trailing whitespace from the selection,
|
|
|
|
// but preserve it when replacing
|
2018-11-27 17:36:31 +00:00
|
|
|
target = text = selection.trim();
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( target.length < selection.length ) {
|
|
|
|
$( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [
|
|
|
|
selection.substr( 0, selection.indexOf( target.charAt( 0 ) ) ),
|
|
|
|
selection.substr(
|
|
|
|
selection.lastIndexOf( target.charAt( target.length - 1 ) ) + 1
|
|
|
|
) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2021-09-13 03:17:05 +00:00
|
|
|
// Change the values of the title and text fields to the parts extracted from the selection.
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( typeof target !== 'undefined' ) {
|
2021-09-01 07:17:53 +00:00
|
|
|
insertLinkTitleInputField.getField().setValue( target );
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
2021-09-13 03:17:05 +00:00
|
|
|
if ( typeof text !== 'undefined' ) {
|
|
|
|
insertLinkLinkTextField.getField().setValue( text );
|
|
|
|
}
|
2021-10-11 05:31:20 +00:00
|
|
|
if ( typeof isExternal !== 'undefined' ) {
|
|
|
|
insertLinkLinkTypeField.setIsExternal( isExternal );
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2021-09-13 03:17:05 +00:00
|
|
|
// Don't overwrite user's selection.
|
|
|
|
insertLinkLinkTextField.setTouched( selection !== '' );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !$( this ).data( 'dialogkeypressset' ) ) {
|
|
|
|
$( this ).data( 'dialogkeypressset', true );
|
|
|
|
// Execute the action associated with the first button
|
|
|
|
// when the user presses Enter
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).on( 'keypress', function ( e ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( ( e.keyCode || e.which ) === 13 ) {
|
2021-02-10 18:29:40 +00:00
|
|
|
triggerButtonClick( this );
|
2016-11-20 17:13:39 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Make tabbing to a button and pressing
|
|
|
|
// Enter do what people expect
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).find( 'button' ).on( 'focus', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
|
|
|
|
} );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
|
|
|
},
|
2016-11-20 17:13:39 +00:00
|
|
|
'insert-file': {
|
|
|
|
titleMsg: 'wikieditor-toolbar-tool-file-title',
|
|
|
|
id: 'wikieditor-toolbar-file-dialog',
|
|
|
|
htmlTemplate: 'dialogInsertFile.html',
|
|
|
|
init: function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var magicWordsI18N = configData.magicWords;
|
2020-10-18 07:23:19 +00:00
|
|
|
|
2017-10-31 17:35:21 +00:00
|
|
|
$( this ).find( '[data-i18n-magic]' )
|
|
|
|
.text( function () {
|
2021-01-22 20:36:31 +00:00
|
|
|
return magicWordsI18N[ $( this ).attr( 'data-i18n-magic' ) ][ 0 ];
|
2017-10-31 17:35:21 +00:00
|
|
|
} )
|
|
|
|
.removeAttr( 'data-i18n-magic' );
|
2021-06-15 14:45:13 +00:00
|
|
|
|
|
|
|
var defaultMsg = mw.msg( 'wikieditor-toolbar-file-default' );
|
2017-10-31 17:35:21 +00:00
|
|
|
$( this ).find( '#wikieditor-toolbar-file-size' )
|
|
|
|
.attr( 'placeholder', defaultMsg )
|
|
|
|
// The message may be long in some languages
|
|
|
|
.attr( 'size', defaultMsg.length );
|
|
|
|
$( this ).find( '[rel]' )
|
|
|
|
.text( function () {
|
2019-10-29 22:16:13 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
2017-10-31 17:35:21 +00:00
|
|
|
return mw.msg( $( this ).attr( 'rel' ) );
|
|
|
|
} )
|
|
|
|
.removeAttr( 'rel' );
|
2020-02-09 09:52:46 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var altHelpText = mw.msg( 'wikieditor-toolbar-file-alt-help' );
|
|
|
|
var altHelpLabel = mw.msg( 'wikieditor-toolbar-file-alt-help-label' );
|
2020-10-18 07:23:19 +00:00
|
|
|
// Expandable help message for 'alt text' field
|
|
|
|
$( this ).find( '.wikieditor-toolbar-file-alt-help' ).text( altHelpLabel );
|
|
|
|
$( '.wikieditor-toolbar-file-alt-help' ).on( 'click', function () {
|
|
|
|
$( this ).text( function ( i, text ) {
|
|
|
|
return text === altHelpLabel ? altHelpText : altHelpLabel;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2020-02-09 09:52:46 +00:00
|
|
|
// Preload modules of file upload dialog.
|
|
|
|
mw.loader.load( [
|
|
|
|
'mediawiki.ForeignStructuredUpload.BookletLayout',
|
|
|
|
'mediawiki.Upload.Dialog',
|
|
|
|
'oojs-ui-windows'
|
|
|
|
] );
|
2016-11-20 17:13:39 +00:00
|
|
|
},
|
|
|
|
dialog: {
|
|
|
|
resizable: false,
|
|
|
|
dialogClass: 'wikiEditor-toolbar-dialog',
|
|
|
|
width: 590,
|
|
|
|
buttons: {
|
|
|
|
'wikieditor-toolbar-tool-file-insert': function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var hasPxRgx = /.+px$/,
|
2019-07-20 08:09:04 +00:00
|
|
|
magicWordsI18N = configData.magicWords;
|
2021-06-15 14:45:13 +00:00
|
|
|
var fileName = $( '#wikieditor-toolbar-file-target' ).val();
|
|
|
|
var caption = $( '#wikieditor-toolbar-file-caption' ).val();
|
|
|
|
var fileAlt = $( '#wikieditor-toolbar-file-alt' ).val();
|
|
|
|
var fileFloat = $( '#wikieditor-toolbar-file-float' ).val();
|
|
|
|
var fileFormat = $( '#wikieditor-toolbar-file-format' ).val();
|
|
|
|
var fileSize = $( '#wikieditor-toolbar-file-size' ).val();
|
|
|
|
var whitespace = $( '#wikieditor-toolbar-file-dialog' ).data( 'whitespace' );
|
2016-11-20 17:13:39 +00:00
|
|
|
// Append px to end to size if not already contains it
|
|
|
|
if ( fileSize !== '' && !hasPxRgx.test( fileSize ) ) {
|
|
|
|
fileSize += 'px';
|
2012-05-27 19:14:34 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( fileName !== '' ) {
|
2021-06-15 14:45:13 +00:00
|
|
|
var fileTitle = mw.Title.newFromText( fileName );
|
2016-11-20 17:13:39 +00:00
|
|
|
// Append file namespace prefix to filename if not already contains it
|
2021-04-27 22:56:10 +00:00
|
|
|
if ( fileTitle && fileTitle.getNamespaceId() !== 6 ) {
|
|
|
|
fileTitle = mw.Title.makeTitle( 6, fileName );
|
|
|
|
}
|
|
|
|
if ( fileTitle ) {
|
|
|
|
fileName = fileTitle.toText();
|
2012-05-27 19:14:34 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var options = [ fileSize, fileFormat, fileFloat ];
|
2016-11-20 17:13:39 +00:00
|
|
|
// Filter empty values
|
2018-11-27 17:36:31 +00:00
|
|
|
options = options.filter( function ( val ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
return val.length && val !== 'default';
|
2015-10-20 13:48:31 +00:00
|
|
|
} );
|
2020-10-18 07:23:19 +00:00
|
|
|
if ( fileAlt.length ) {
|
2021-01-22 20:36:31 +00:00
|
|
|
options.push( magicWordsI18N.img_alt[ 0 ].replace( '$1', fileAlt ) );
|
2020-10-18 07:23:19 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( caption.length ) {
|
|
|
|
options.push( caption );
|
|
|
|
}
|
2020-10-18 07:23:19 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var fileUse = options.length === 0 ? fileName : ( fileName + '|' + options.join( '|' ) );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).dialog( 'close' );
|
2019-07-20 08:09:04 +00:00
|
|
|
toolbarModule.fn.doAction(
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).data( 'context' ),
|
|
|
|
{
|
|
|
|
type: 'replace',
|
|
|
|
options: {
|
2014-12-02 14:23:18 +00:00
|
|
|
pre: whitespace[ 0 ] + '[[',
|
2016-11-20 17:13:39 +00:00
|
|
|
peri: fileUse,
|
2014-12-02 14:23:18 +00:00
|
|
|
post: ']]' + whitespace[ 1 ],
|
2016-11-20 17:13:39 +00:00
|
|
|
ownline: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$( this )
|
|
|
|
);
|
2015-10-20 13:48:31 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Restore form state
|
|
|
|
$( [ '#wikieditor-toolbar-file-target',
|
|
|
|
'#wikieditor-toolbar-file-caption',
|
2020-10-18 07:23:19 +00:00
|
|
|
'#wikieditor-toolbar-file-alt',
|
2016-11-20 17:13:39 +00:00
|
|
|
'#wikieditor-toolbar-file-size' ].join( ',' )
|
|
|
|
).val( '' );
|
|
|
|
$( '#wikieditor-toolbar-file-float' ).val( 'default' );
|
2021-01-22 20:36:31 +00:00
|
|
|
$( '#wikieditor-toolbar-file-format' ).val( magicWordsI18N.img_thumbnail[ 0 ] );
|
2016-11-20 17:13:39 +00:00
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-file-cancel': function () {
|
|
|
|
$( this ).dialog( 'close' );
|
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-file-upload': function () {
|
|
|
|
$( this ).dialog( 'close' );
|
2019-03-16 19:53:29 +00:00
|
|
|
mw.loader.using( [
|
|
|
|
'mediawiki.ForeignStructuredUpload.BookletLayout',
|
|
|
|
'mediawiki.Upload.Dialog',
|
|
|
|
'oojs-ui-windows'
|
|
|
|
] ).then( function () {
|
|
|
|
var windowManager = new OO.ui.WindowManager(),
|
|
|
|
uploadDialog = new mw.Upload.Dialog( {
|
|
|
|
bookletClass: mw.ForeignStructuredUpload.BookletLayout
|
|
|
|
} );
|
2015-10-20 13:48:31 +00:00
|
|
|
|
2019-03-16 19:53:29 +00:00
|
|
|
windowManager.$element.appendTo( document.body );
|
|
|
|
windowManager.addWindows( [ uploadDialog ] );
|
|
|
|
windowManager.openWindow( uploadDialog );
|
2012-05-27 19:14:34 +00:00
|
|
|
|
2019-03-16 19:53:29 +00:00
|
|
|
uploadDialog.uploadBooklet.on( 'fileSaved', function ( imageInfo ) {
|
|
|
|
uploadDialog.close();
|
|
|
|
windowManager.$element.remove();
|
|
|
|
|
|
|
|
$.wikiEditor.modules.dialogs.api.openDialog( this, 'insert-file' );
|
|
|
|
$( '#wikieditor-toolbar-file-target' ).val( imageInfo.canonicaltitle );
|
|
|
|
} );
|
2016-11-20 17:13:39 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
open: function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var magicWordsI18N = configData.magicWords,
|
2014-12-02 14:23:18 +00:00
|
|
|
fileData = {
|
|
|
|
pre: '',
|
|
|
|
post: '',
|
|
|
|
fileName: '',
|
|
|
|
caption: '',
|
2020-10-18 07:23:19 +00:00
|
|
|
fileAlt: '',
|
2014-12-02 14:23:18 +00:00
|
|
|
fileSize: '',
|
|
|
|
fileFloat: 'default',
|
2021-01-22 20:36:31 +00:00
|
|
|
fileFormat: magicWordsI18N.img_thumbnail[ 0 ]
|
2014-12-02 14:23:18 +00:00
|
|
|
};
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var parseFileSyntax = function ( wikitext ) {
|
|
|
|
var escapedPipe = '\u0001';
|
2014-12-02 14:23:18 +00:00
|
|
|
if ( wikitext.indexOf( escapedPipe ) !== -1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var match = /^(\s*)\[\[(.*)\]\](\s*)$/.exec( wikitext );
|
2014-12-02 14:23:18 +00:00
|
|
|
if ( !match ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var result = {};
|
2014-12-02 14:23:18 +00:00
|
|
|
result.pre = match[ 1 ];
|
|
|
|
result.post = match[ 3 ];
|
|
|
|
// Escape pipes inside links and templates,
|
|
|
|
// then split the parameters at the remaining pipes
|
2021-06-15 14:45:13 +00:00
|
|
|
var params = match[ 2 ].replace( /\[\[[^[\]]*\]\]|\{\{[^{}]\}\}/g, function ( link ) {
|
2014-12-02 14:23:18 +00:00
|
|
|
return link.replace( /\|/g, escapedPipe );
|
|
|
|
} ).split( '|' );
|
2021-06-15 14:45:13 +00:00
|
|
|
var file = mw.Title.newFromText( params[ 0 ] );
|
2021-04-27 22:56:10 +00:00
|
|
|
if ( !file || file.getNamespaceId() !== 6 ) {
|
2014-12-02 14:23:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
result.fileName = file.getMainText();
|
2021-06-15 14:45:13 +00:00
|
|
|
for ( var i = 1; i < params.length; i++ ) {
|
|
|
|
var paramOrig = params[ i ];
|
|
|
|
var param = paramOrig.toLowerCase();
|
2021-01-22 20:36:31 +00:00
|
|
|
if ( magicWordsI18N.img_right.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFloat = magicWordsI18N.img_right[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_left.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFloat = magicWordsI18N.img_left[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_none.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFloat = magicWordsI18N.img_none[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_center.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFloat = magicWordsI18N.img_center[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_thumbnail.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFormat = magicWordsI18N.img_thumbnail[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_framed.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFormat = magicWordsI18N.img_framed[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_frameless.indexOf( param ) !== -1 ) {
|
|
|
|
result.fileFormat = magicWordsI18N.img_frameless[ 0 ];
|
|
|
|
} else if ( magicWordsI18N.img_alt.indexOf( param.split( '=', 2 )[ 0 ] + '=$1' ) !== -1 ) {
|
2021-01-22 20:17:18 +00:00
|
|
|
result.fileAlt = paramOrig.split( '=', 2 )[ 1 ];
|
2014-12-02 14:23:18 +00:00
|
|
|
} else if ( /.+px$/.test( param ) ) {
|
|
|
|
result.fileSize = param.replace( /px$/, '' );
|
|
|
|
} else if ( param === '' ) {
|
|
|
|
continue;
|
|
|
|
} else if ( i === params.length - 1 ) { // Last param -> caption
|
2021-01-22 20:17:18 +00:00
|
|
|
result.caption = paramOrig.replace( new RegExp( mw.util.escapeRegExp( escapedPipe ), 'g' ), '|' );
|
2014-12-02 14:23:18 +00:00
|
|
|
} else { // Unknown param
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !result.fileFormat ) {
|
|
|
|
result.fileFormat = 'default';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Retrieve the current selection
|
2021-06-15 14:45:13 +00:00
|
|
|
var context = $( this ).data( 'context' );
|
|
|
|
var selection = context.$textarea.textSelection( 'getSelection' );
|
2014-12-02 14:23:18 +00:00
|
|
|
|
|
|
|
// Pre-fill the text fields based on the current selection
|
|
|
|
if ( selection !== '' ) {
|
|
|
|
fileData = $.extend( fileData, parseFileSyntax( selection ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the form fields
|
|
|
|
$( '#wikieditor-toolbar-file-dialog' )
|
|
|
|
.data( 'whitespace', [ fileData.pre, fileData.post ] );
|
|
|
|
$( '#wikieditor-toolbar-file-target' ).val( fileData.fileName );
|
|
|
|
$( '#wikieditor-toolbar-file-caption' ).val( fileData.caption );
|
2020-10-18 07:23:19 +00:00
|
|
|
$( '#wikieditor-toolbar-file-alt' ).val( fileData.fileAlt );
|
2014-12-02 14:23:18 +00:00
|
|
|
$( '#wikieditor-toolbar-file-float' ).val( fileData.fileFloat );
|
|
|
|
$( '#wikieditor-toolbar-file-format' ).val( fileData.fileFormat );
|
|
|
|
$( '#wikieditor-toolbar-file-size' ).val( fileData.fileSize );
|
|
|
|
|
|
|
|
// Set focus
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-file-target' ).trigger( 'focus' );
|
2014-12-02 14:23:18 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !( $( this ).data( 'dialogkeypressset' ) ) ) {
|
|
|
|
$( this ).data( 'dialogkeypressset', true );
|
|
|
|
// Execute the action associated with the first button
|
|
|
|
// when the user presses Enter
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).on( 'keypress', function ( e ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( e.which === 13 ) {
|
2021-02-10 18:29:40 +00:00
|
|
|
triggerButtonClick( this );
|
2016-11-20 17:13:39 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Make tabbing to a button and pressing
|
|
|
|
// Enter do what people expect
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).find( 'button' ).on( 'focus', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
|
|
|
|
} );
|
|
|
|
}
|
2012-05-27 19:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
},
|
|
|
|
'insert-table': {
|
|
|
|
titleMsg: 'wikieditor-toolbar-tool-table-title',
|
|
|
|
id: 'wikieditor-toolbar-table-dialog',
|
|
|
|
htmlTemplate: 'dialogInsertTable.html',
|
|
|
|
init: function () {
|
|
|
|
$( this ).find( '[rel]' ).each( function () {
|
2019-10-29 22:16:13 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
$( '#wikieditor-toolbar-table-dimensions-rows' ).val( 3 );
|
|
|
|
$( '#wikieditor-toolbar-table-dimensions-columns' ).val( 3 );
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-wikitable' ).on( 'click', function () {
|
2019-10-07 15:52:10 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
2016-11-20 17:13:39 +00:00
|
|
|
$( '.wikieditor-toolbar-table-preview' ).toggleClass( 'wikitable' );
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Hack for sortable preview: dynamically adding
|
|
|
|
// sortable class doesn't work, so we use a clone
|
|
|
|
$( '#wikieditor-toolbar-table-preview' )
|
|
|
|
.clone()
|
|
|
|
.attr( 'id', 'wikieditor-toolbar-table-preview2' )
|
|
|
|
.addClass( 'sortable' )
|
|
|
|
.insertAfter( $( '#wikieditor-toolbar-table-preview' ) )
|
|
|
|
.hide();
|
2011-10-22 17:21:03 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
mw.loader.using( 'jquery.tablesorter', function () {
|
|
|
|
$( '#wikieditor-toolbar-table-preview2' ).tablesorter();
|
|
|
|
} );
|
2011-10-22 17:21:03 +00:00
|
|
|
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-sortable' ).on( 'click', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
// Swap the currently shown one clone with the other one
|
|
|
|
$( '#wikieditor-toolbar-table-preview' )
|
|
|
|
.hide()
|
|
|
|
.attr( 'id', 'wikieditor-toolbar-table-preview3' );
|
|
|
|
$( '#wikieditor-toolbar-table-preview2' )
|
|
|
|
.attr( 'id', 'wikieditor-toolbar-table-preview' )
|
|
|
|
.show();
|
|
|
|
$( '#wikieditor-toolbar-table-preview3' ).attr( 'id', 'wikieditor-toolbar-table-preview2' );
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-dimensions-header' ).on( 'click', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
// Instead of show/hiding, switch the HTML around
|
|
|
|
// We do this because the sortable tables script styles the first row,
|
|
|
|
// visible or not
|
2021-06-15 14:45:13 +00:00
|
|
|
var headerHTML = $( '.wikieditor-toolbar-table-preview-header' ).html(),
|
2016-11-20 17:13:39 +00:00
|
|
|
hiddenHTML = $( '.wikieditor-toolbar-table-preview-hidden' ).html();
|
|
|
|
$( '.wikieditor-toolbar-table-preview-header' ).html( hiddenHTML );
|
|
|
|
$( '.wikieditor-toolbar-table-preview-hidden' ).html( headerHTML );
|
2021-06-15 14:45:13 +00:00
|
|
|
var $sortable = $( '#wikieditor-toolbar-table-preview, #wikieditor-toolbar-table-preview2' )
|
2017-04-20 08:44:27 +00:00
|
|
|
.filter( '.sortable' );
|
|
|
|
mw.loader.using( 'jquery.tablesorter', function () {
|
|
|
|
$sortable.tablesorter();
|
|
|
|
} );
|
2016-11-20 17:13:39 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
dialog: {
|
|
|
|
resizable: false,
|
|
|
|
dialogClass: 'wikiEditor-toolbar-dialog',
|
|
|
|
width: 590,
|
|
|
|
buttons: {
|
|
|
|
'wikieditor-toolbar-tool-table-insert': function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var rowsVal = $( '#wikieditor-toolbar-table-dimensions-rows' ).val(),
|
2016-11-20 17:13:39 +00:00
|
|
|
colsVal = $( '#wikieditor-toolbar-table-dimensions-columns' ).val(),
|
|
|
|
rows = parseInt( rowsVal, 10 ),
|
|
|
|
cols = parseInt( colsVal, 10 ),
|
|
|
|
header = $( '#wikieditor-toolbar-table-dimensions-header' ).prop( 'checked' ) ? 1 : 0;
|
|
|
|
if ( isNaN( rows ) || isNaN( cols ) || String( rows ) !== rowsVal || String( cols ) !== colsVal || rowsVal < 0 || colsVal < 0 ) {
|
2017-05-04 21:36:58 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2016-11-20 17:13:39 +00:00
|
|
|
alert( mw.msg( 'wikieditor-toolbar-tool-table-invalidnumber' ) );
|
|
|
|
return;
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( rows + header === 0 || cols === 0 ) {
|
2017-05-04 21:36:58 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2016-11-20 17:13:39 +00:00
|
|
|
alert( mw.msg( 'wikieditor-toolbar-tool-table-zero' ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( ( rows * cols ) > 1000 ) {
|
2017-05-04 21:36:58 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2020-03-26 06:54:05 +00:00
|
|
|
alert( mw.msg( 'wikieditor-toolbar-tool-table-toomany', mw.language.convertNumber( 1000 ) ) );
|
2016-11-20 17:13:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var captionText = mw.msg( 'wikieditor-toolbar-tool-table-example-caption' );
|
|
|
|
var headerText = mw.msg( 'wikieditor-toolbar-tool-table-example-header' );
|
|
|
|
var normalText = mw.msg( 'wikieditor-toolbar-tool-table-example' );
|
|
|
|
var table = '';
|
2020-05-11 09:42:01 +00:00
|
|
|
table += '|+ ' + captionText + '\n';
|
2021-06-15 14:45:13 +00:00
|
|
|
for ( var r = 0; r < rows + header; r++ ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
table += '|-\n';
|
2021-06-15 14:45:13 +00:00
|
|
|
for ( var c = 0; c < cols; c++ ) {
|
|
|
|
var isHeader = ( header && r === 0 );
|
|
|
|
var delim = isHeader ? '!' : '|';
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( c > 0 ) {
|
|
|
|
delim += delim;
|
|
|
|
}
|
|
|
|
table += delim + ' ' + ( isHeader ? headerText : normalText ) + ' ';
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
// Replace trailing space by newline
|
|
|
|
// table[table.length - 1] is read-only
|
|
|
|
table = table.substr( 0, table.length - 1 ) + '\n';
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var classes = [];
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( $( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
|
|
|
|
classes.push( 'wikitable' );
|
|
|
|
}
|
|
|
|
if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
|
|
|
|
classes.push( 'sortable' );
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var classStr = classes.length > 0 ? ' class="' + classes.join( ' ' ) + '"' : '';
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).dialog( 'close' );
|
2019-07-20 08:09:04 +00:00
|
|
|
toolbarModule.fn.doAction(
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).data( 'context' ),
|
|
|
|
{
|
|
|
|
type: 'replace',
|
|
|
|
options: {
|
|
|
|
pre: '{|' + classStr + '\n',
|
|
|
|
peri: table,
|
|
|
|
post: '|}',
|
|
|
|
ownline: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$( this )
|
|
|
|
);
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Restore form state
|
|
|
|
$( '#wikieditor-toolbar-table-dimensions-rows' ).val( 3 );
|
|
|
|
$( '#wikieditor-toolbar-table-dimensions-columns' ).val( 3 );
|
|
|
|
// Simulate clicks instead of setting values, so the according
|
|
|
|
// actions are performed
|
|
|
|
if ( !$( '#wikieditor-toolbar-table-dimensions-header' ).is( ':checked' ) ) {
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-dimensions-header' ).trigger( 'click' );
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
|
|
|
if ( !$( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-wikitable' ).trigger( 'click' );
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
|
|
|
if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-sortable' ).trigger( 'click' );
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-table-cancel': function () {
|
|
|
|
$( this ).dialog( 'close' );
|
2015-11-15 14:30:18 +00:00
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
},
|
2016-11-20 17:13:39 +00:00
|
|
|
open: function () {
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-table-dimensions-rows' ).trigger( 'focus' );
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !( $( this ).data( 'dialogkeypressset' ) ) ) {
|
|
|
|
$( this ).data( 'dialogkeypressset', true );
|
|
|
|
// Execute the action associated with the first button
|
|
|
|
// when the user presses Enter
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).on( 'keypress', function ( e ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( ( e.keyCode || e.which ) === 13 ) {
|
2021-02-10 18:29:40 +00:00
|
|
|
triggerButtonClick( this );
|
2016-11-20 17:13:39 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Make tabbing to a button and pressing
|
|
|
|
// Enter do what people expect
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).find( 'button' ).on( 'focus', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
|
|
|
|
} );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
},
|
2016-11-20 17:13:39 +00:00
|
|
|
'search-and-replace': {
|
|
|
|
titleMsg: 'wikieditor-toolbar-tool-replace-title',
|
|
|
|
id: 'wikieditor-toolbar-replace-dialog',
|
|
|
|
htmlTemplate: 'dialogReplace.html',
|
|
|
|
init: function () {
|
|
|
|
$( this ).find( '[rel]' ).each( function () {
|
2019-10-29 22:16:13 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
|
|
|
|
} );
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// TODO: Find a cleaner way to share this function
|
|
|
|
$( this ).data( 'replaceCallback', function ( mode ) {
|
|
|
|
$( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Search string cannot be empty
|
2021-06-15 14:45:13 +00:00
|
|
|
var searchStr = $( '#wikieditor-toolbar-replace-search' ).val();
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( searchStr === '' ) {
|
|
|
|
$( '#wikieditor-toolbar-replace-emptysearch' ).show();
|
|
|
|
return;
|
|
|
|
}
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Replace string can be empty
|
2021-06-15 14:45:13 +00:00
|
|
|
var replaceStr = $( '#wikieditor-toolbar-replace-replace' ).val();
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Prepare the regular expression flags
|
2021-06-15 14:45:13 +00:00
|
|
|
var flags = 'm';
|
|
|
|
var matchCase = $( '#wikieditor-toolbar-replace-case' ).is( ':checked' );
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !matchCase ) {
|
|
|
|
flags += 'i';
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var isRegex = $( '#wikieditor-toolbar-replace-regex' ).is( ':checked' );
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !isRegex ) {
|
2019-09-09 17:32:48 +00:00
|
|
|
searchStr = mw.util.escapeRegExp( searchStr );
|
2016-11-20 17:13:39 +00:00
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var matchWord = $( '#wikieditor-toolbar-replace-word' ).is( ':checked' );
|
2018-11-27 14:57:59 +00:00
|
|
|
if ( matchWord ) {
|
|
|
|
searchStr = '\\b(?:' + searchStr + ')\\b';
|
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( mode === 'replaceAll' ) {
|
|
|
|
flags += 'g';
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var regex;
|
2016-11-20 17:13:39 +00:00
|
|
|
try {
|
|
|
|
regex = new RegExp( searchStr, flags );
|
|
|
|
} catch ( e ) {
|
|
|
|
$( '#wikieditor-toolbar-replace-invalidregex' )
|
|
|
|
.text( mw.msg( 'wikieditor-toolbar-tool-replace-invalidregex',
|
|
|
|
e.message ) )
|
|
|
|
.show();
|
|
|
|
return;
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var $textarea = $( this ).data( 'context' ).$textarea;
|
|
|
|
var text = $textarea.textSelection( 'getContents' );
|
|
|
|
var match = false;
|
|
|
|
var offset, textRemainder;
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( mode !== 'replaceAll' ) {
|
|
|
|
if ( mode === 'replace' ) {
|
|
|
|
offset = $( this ).data( 'matchIndex' );
|
|
|
|
} else {
|
|
|
|
offset = $( this ).data( 'offset' );
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
textRemainder = text.substr( offset );
|
|
|
|
match = textRemainder.match( regex );
|
|
|
|
}
|
|
|
|
if ( !match ) {
|
|
|
|
// Search hit BOTTOM, continuing at TOP
|
|
|
|
// TODO: Add a "Wrap around" option.
|
|
|
|
offset = 0;
|
|
|
|
textRemainder = text;
|
|
|
|
match = textRemainder.match( regex );
|
|
|
|
}
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( !match ) {
|
|
|
|
$( '#wikieditor-toolbar-replace-nomatch' ).show();
|
|
|
|
} else if ( mode === 'replaceAll' ) {
|
2018-11-17 20:28:41 +00:00
|
|
|
$textarea.textSelection( 'setContents', text.replace( regex, replaceStr ) );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( '#wikieditor-toolbar-replace-success' )
|
2020-03-26 06:54:05 +00:00
|
|
|
.text( mw.msg( 'wikieditor-toolbar-tool-replace-success', mw.language.convertNumber( match.length ) ) )
|
2016-11-20 17:13:39 +00:00
|
|
|
.show();
|
|
|
|
$( this ).data( 'offset', 0 );
|
|
|
|
} else {
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var start, end;
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( mode === 'replace' ) {
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var actualReplacement;
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( isRegex ) {
|
|
|
|
// If backreferences (like $1) are used, the actual actual replacement string will be different
|
|
|
|
actualReplacement = match[ 0 ].replace( regex, replaceStr );
|
|
|
|
} else {
|
|
|
|
actualReplacement = replaceStr;
|
|
|
|
}
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( match ) {
|
|
|
|
// Do the replacement
|
|
|
|
$textarea.textSelection( 'encapsulateSelection', {
|
2015-11-15 14:30:18 +00:00
|
|
|
peri: actualReplacement,
|
2018-11-17 13:54:55 +00:00
|
|
|
replace: true,
|
|
|
|
selectionStart: offset + match.index,
|
|
|
|
selectionEnd: offset + match.index + match[ 0 ].length,
|
|
|
|
selectPeri: true
|
|
|
|
} );
|
2016-11-20 17:13:39 +00:00
|
|
|
// Reload the text after replacement
|
|
|
|
text = $textarea.textSelection( 'getContents' );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
// Find the next instance
|
2018-11-17 13:54:55 +00:00
|
|
|
offset = offset + match.index + actualReplacement.length;
|
2016-11-20 17:13:39 +00:00
|
|
|
textRemainder = text.substr( offset );
|
2012-07-03 05:52:27 +00:00
|
|
|
match = textRemainder.match( regex );
|
2016-11-20 17:13:39 +00:00
|
|
|
|
2014-04-30 10:59:01 +00:00
|
|
|
if ( match ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
start = offset + match.index;
|
2015-11-15 14:30:18 +00:00
|
|
|
end = start + match[ 0 ].length;
|
2012-07-03 05:52:27 +00:00
|
|
|
} else {
|
2016-11-20 17:13:39 +00:00
|
|
|
// If no new string was found, try searching from the beginning.
|
|
|
|
// TODO: Add a "Wrap around" option.
|
|
|
|
textRemainder = text;
|
|
|
|
match = textRemainder.match( regex );
|
|
|
|
if ( match ) {
|
|
|
|
start = match.index;
|
|
|
|
end = start + match[ 0 ].length;
|
|
|
|
} else {
|
|
|
|
// Give up
|
|
|
|
start = 0;
|
|
|
|
end = 0;
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
} else {
|
|
|
|
start = offset + match.index;
|
|
|
|
end = start + match[ 0 ].length;
|
2011-09-13 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).data( 'matchIndex', start );
|
2011-09-13 08:53:12 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
$textarea.textSelection( 'setSelection', {
|
2015-11-15 14:30:18 +00:00
|
|
|
start: start,
|
|
|
|
end: end } );
|
2016-11-20 17:13:39 +00:00
|
|
|
$textarea.textSelection( 'scrollToCaretPosition' );
|
|
|
|
$( this ).data( 'offset', end );
|
|
|
|
$textarea[ 0 ].focus();
|
|
|
|
}
|
|
|
|
} );
|
2012-07-03 05:52:27 +00:00
|
|
|
},
|
2016-11-20 17:13:39 +00:00
|
|
|
dialog: {
|
|
|
|
width: 500,
|
|
|
|
dialogClass: 'wikiEditor-toolbar-dialog',
|
|
|
|
modal: false,
|
|
|
|
buttons: {
|
|
|
|
'wikieditor-toolbar-tool-replace-button-findnext': function ( e ) {
|
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
|
|
|
|
$( this ).data( 'replaceCallback' ).call( this, 'find' );
|
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-replace-button-replace': function ( e ) {
|
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
|
|
|
|
$( this ).data( 'replaceCallback' ).call( this, 'replace' );
|
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-replace-button-replaceall': function ( e ) {
|
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
|
|
|
|
$( this ).data( 'replaceCallback' ).call( this, 'replaceAll' );
|
|
|
|
},
|
|
|
|
'wikieditor-toolbar-tool-replace-close': function () {
|
|
|
|
$( this ).dialog( 'close' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
open: function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var that = this;
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).data( 'offset', 0 );
|
|
|
|
$( this ).data( 'matchIndex', 0 );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2019-01-08 23:32:56 +00:00
|
|
|
$( '#wikieditor-toolbar-replace-search' ).trigger( 'focus' );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
|
|
|
|
if ( !( $( this ).data( 'onetimeonlystuff' ) ) ) {
|
|
|
|
$( this ).data( 'onetimeonlystuff', true );
|
|
|
|
// Execute the action associated with the first button
|
|
|
|
// when the user presses Enter
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).on( 'keypress', function ( e ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( ( e.keyCode || e.which ) === 13 ) {
|
2021-02-10 18:29:40 +00:00
|
|
|
triggerButtonClick( this );
|
2016-11-20 17:13:39 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
// Make tabbing to a button and pressing
|
|
|
|
// Enter do what people expect
|
2019-01-08 23:32:56 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).find( 'button' ).on( 'focus', function () {
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
|
|
|
|
} );
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var $dialog = $( this ).closest( '.ui-dialog' );
|
2016-11-20 17:13:39 +00:00
|
|
|
that = this;
|
2021-06-15 14:45:13 +00:00
|
|
|
var context = $( this ).data( 'context' );
|
|
|
|
var $textbox = context.$textarea;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2019-10-29 22:16:13 +00:00
|
|
|
$textbox
|
2016-11-22 02:22:13 +00:00
|
|
|
.on( 'keypress.srdialog', function ( e ) {
|
2016-11-20 17:13:39 +00:00
|
|
|
if ( e.which === 13 ) {
|
|
|
|
// Enter
|
2021-02-10 18:29:40 +00:00
|
|
|
triggerButtonClick( $dialog );
|
2016-11-20 17:13:39 +00:00
|
|
|
e.preventDefault();
|
|
|
|
} else if ( e.which === 27 ) {
|
|
|
|
// Escape
|
|
|
|
$( that ).dialog( 'close' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
close: function () {
|
|
|
|
var context = $( this ).data( 'context' ),
|
2019-10-29 22:16:13 +00:00
|
|
|
$textbox = context.$textarea;
|
|
|
|
$textbox.off( 'keypress.srdialog' );
|
2016-11-20 17:13:39 +00:00
|
|
|
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', false );
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
}
|
2011-02-24 01:45:55 +00:00
|
|
|
}
|
2016-11-20 17:13:39 +00:00
|
|
|
} };
|
|
|
|
}
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2016-11-20 17:13:39 +00:00
|
|
|
};
|
2011-02-24 01:45:55 +00:00
|
|
|
|
2018-11-12 22:28:40 +00:00
|
|
|
}() );
|