2 * Configuration of Dialog module for wikiEditor
4 ( function ( $, mw, OO ) {
6 var hasOwn = Object.prototype.hasOwnProperty;
8 $.wikiEditor.modules.dialogs.config = {
10 replaceIcons: function ( $textarea ) {
12 .wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'xlink' } )
13 .wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'ilink' } )
14 .wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'file' } )
15 .wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'reference' } )
16 .wikiEditor( 'removeFromToolbar', { section: 'advanced', group: 'insert', tool: 'table' } )
17 .wikiEditor( 'addToToolbar', {
22 labelMsg: 'wikieditor-toolbar-tool-link',
24 icon: 'insert-link.png',
32 labelMsg: 'wikieditor-toolbar-tool-file',
34 icon: 'insert-file.png',
42 labelMsg: 'wikieditor-toolbar-tool-reference',
43 filters: [ 'body.ns-subject' ],
45 icon: 'insert-reference.png',
49 module: 'insert-reference'
54 .wikiEditor( 'addToToolbar', {
59 labelMsg: 'wikieditor-toolbar-tool-table',
61 icon: 'insert-table.png',
65 module: 'insert-table'
70 .wikiEditor( 'addToToolbar', {
76 labelMsg: 'wikieditor-toolbar-tool-replace',
78 icon: 'search-replace.png',
79 offset: [ -70, -214 ],
82 module: 'search-and-replace'
91 getDefaultConfig: function () {
94 titleMsg: 'wikieditor-toolbar-tool-link-title',
95 id: 'wikieditor-toolbar-link-dialog',
96 htmlTemplate: 'dialogInsertLink.html',
99 var api = new mw.Api();
101 function isExternalLink( s ) {
102 // The following things are considered to be external links:
103 // * Starts with a URL protocol
104 // * Starts with www.
105 // All of these are potentially valid titles, and the latter two categories match about 6300
106 // titles in enwiki's ns0. Out of 6.9M titles, that's 0.09%
107 /* eslint-disable no-caller */
108 if ( typeof arguments.callee.regex === 'undefined' ) {
110 arguments.callee.regex =
111 new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + '|www\\.)', 'i' );
113 return s.match( arguments.callee.regex );
114 /* eslint-enable no-caller */
117 // Updates the status indicator above the target link
118 function updateWidget( status ) {
119 $( '#wikieditor-toolbar-link-int-target-status' ).children().hide();
120 $( '#wikieditor-toolbar-link-int-target' ).parent()
122 'status-invalid status-external status-notexists status-exists status-loading'
125 $( '#wikieditor-toolbar-link-int-target-status-' + status ).show();
126 $( '#wikieditor-toolbar-link-int-target' ).parent().addClass( 'status-' + status );
128 if ( status === 'invalid' ) {
129 $( '.ui-dialog:visible .ui-dialog-buttonpane button:first' )
130 .prop( 'disabled', true )
131 .addClass( 'disabled' );
133 $( '.ui-dialog:visible .ui-dialog-buttonpane button:first' )
134 .prop( 'disabled', false )
135 .removeClass( 'disabled' );
139 // Updates the UI to show if the page title being inputted by the user exists or not
140 // accepts parameter internal for bypassing external link detection
141 function updateExistence( internal ) {
142 // Abort previous request
143 var request = $( '#wikieditor-toolbar-link-int-target-status' ).data( 'request' ),
144 target = $( '#wikieditor-toolbar-link-int-target' ).val(),
145 cache = $( '#wikieditor-toolbar-link-int-target-status' ).data( 'existencecache' );
146 // ensure the internal parameter is a boolean
147 if ( internal !== true ) {
153 if ( hasOwn.call( cache, target ) ) {
154 updateWidget( cache[ target ] );
157 if ( target.replace( /^\s+$/, '' ) === '' ) {
158 // Hide the widget when the textbox is empty
159 updateWidget( false );
162 // If the forced internal parameter was not true, check if the target is an external link
163 if ( !internal && isExternalLink( target ) ) {
164 updateWidget( 'external' );
167 if ( target.indexOf( '|' ) !== -1 ) {
168 // Title contains | , which means it's invalid
169 // but confuses the API. Show invalid and bypass API
170 updateWidget( 'invalid' );
173 // Show loading spinner while waiting for the API to respond
174 updateWidget( 'loading' );
175 // Call the API to check page status, saving the request object so it can be aborted if
177 // This used to request a page that would show whether or not the target exists, but we can
178 // also check whether it has the disambiguation property and still get existence information.
179 // If the Disambiguator extension is not installed then such a property won't be set.
180 $( '#wikieditor-toolbar-link-int-target-status' ).data(
187 ppprop: 'disambiguation'
188 } ).done( function ( data ) {
190 if ( !data.query || !data.query.pages ) {
191 // This happens in some weird cases like interwiki links
194 page = data.query.pages[ 0 ];
196 if ( page.missing ) {
197 status = 'notexists';
198 } else if ( page.invalid ) {
200 } else if ( page.pageprops ) {
204 // Cache the status of the link target if the force internal
205 // parameter was not passed
207 cache[ target ] = status;
209 updateWidget( status );
213 $( '#wikieditor-toolbar-link-type-int, #wikieditor-toolbar-link-type-ext' ).click( function () {
215 if ( $( '#wikieditor-toolbar-link-type-ext' ).prop( 'checked' ) ) {
216 // Abort previous request
217 request = $( '#wikieditor-toolbar-link-int-target-status' ).data( 'request' );
221 updateWidget( 'external' );
223 if ( $( '#wikieditor-toolbar-link-type-int' ).prop( 'checked' ) ) {
224 updateExistence( true );
227 // Set labels of tabs based on rel values
228 $( this ).find( '[rel]' ).each( function () {
229 $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
231 // Set tabindexes on form fields
232 $.wikiEditor.modules.dialogs.fn.setTabindexes( $( this ).find( 'input' ).not( '[tabindex]' ) );
233 // Setup the tooltips in the textboxes
234 $( '#wikieditor-toolbar-link-int-target' )
235 .data( 'tooltip', mw.msg( 'wikieditor-toolbar-tool-link-int-target-tooltip' ) );
236 $( '#wikieditor-toolbar-link-int-text' )
237 .data( 'tooltip', mw.msg( 'wikieditor-toolbar-tool-link-int-text-tooltip' ) );
238 $( '#wikieditor-toolbar-link-int-target, #wikieditor-toolbar-link-int-text' )
240 if ( $( this ).val() === '' ) {
242 .addClass( 'wikieditor-toolbar-dialog-hint' )
243 .val( $( this ).data( 'tooltip' ) )
244 .data( 'tooltip-mode', true );
247 .on( 'focus', function () {
248 if ( $( this ).val() === $( this ).data( 'tooltip' ) ) {
251 .removeClass( 'wikieditor-toolbar-dialog-hint' )
252 .data( 'tooltip-mode', false );
255 .on( 'change', function () {
256 if ( $( this ).val() !== $( this ).data( 'tooltip' ) ) {
258 .removeClass( 'wikieditor-toolbar-dialog-hint' )
259 .data( 'tooltip-mode', false );
262 .on( 'blur', function () {
263 if ( $( this ).val() === '' ) {
265 .addClass( 'wikieditor-toolbar-dialog-hint' )
266 .val( $( this ).data( 'tooltip' ) )
267 .data( 'tooltip-mode', true );
271 // Automatically copy the value of the internal link page title field to the link text field unless the
272 // user has changed the link text field - this is a convenience thing since most link texts are going to
273 // be the the same as the page title - Also change the internal/external radio button accordingly
274 $( '#wikieditor-toolbar-link-int-target' ).on( 'change keydown paste cut', function () {
275 // $( this ).val() is the old value, before the keypress - Defer this until $( this ).val() has
277 setTimeout( function () {
278 if ( isExternalLink( $( '#wikieditor-toolbar-link-int-target' ).val() ) ) {
279 $( '#wikieditor-toolbar-link-type-ext' ).prop( 'checked', true );
280 updateWidget( 'external' );
282 $( '#wikieditor-toolbar-link-type-int' ).prop( 'checked', true );
285 if ( $( '#wikieditor-toolbar-link-int-text' ).data( 'untouched' ) ) {
286 // eslint-disable-next-line eqeqeq
287 if ( $( '#wikieditor-toolbar-link-int-target' ).val() ==
288 $( '#wikieditor-toolbar-link-int-target' ).data( 'tooltip' )
290 $( '#wikieditor-toolbar-link-int-text' )
291 .addClass( 'wikieditor-toolbar-dialog-hint' )
292 .val( $( '#wikieditor-toolbar-link-int-text' ).data( 'tooltip' ) )
295 $( '#wikieditor-toolbar-link-int-text' )
296 .val( $( '#wikieditor-toolbar-link-int-target' ).val() )
302 $( '#wikieditor-toolbar-link-int-text' ).on( 'change keydown paste cut', function () {
303 var oldVal = $( this ).val(),
305 setTimeout( function () {
306 if ( $( that ).val() !== oldVal ) {
307 $( that ).data( 'untouched', false );
311 // Add images to the page existence widget, which will be shown mutually exclusively to communicate if
312 // the page exists, does not exist or the title is invalid (like if it contains a | character)
313 $( '#wikieditor-toolbar-link-int-target-status' )
314 .append( $( '<div>' )
315 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-exists' )
316 .text( mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-exists' ) )
318 .append( $( '<div>' )
319 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-notexists' )
320 .text( mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-notexists' ) )
322 .append( $( '<div>' )
323 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-invalid' )
324 .text( mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-invalid' ) )
326 .append( $( '<div>' )
327 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-external' )
328 .text( mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-external' ) )
330 .append( $( '<div>' )
331 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-loading' )
332 .attr( 'title', mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-loading' ) )
334 .append( $( '<div>' )
335 .attr( 'id', 'wikieditor-toolbar-link-int-target-status-disambig' )
336 .text( mw.msg( 'wikieditor-toolbar-tool-link-int-target-status-disambig' ) )
338 .data( 'existencecache', {} )
341 $( '#wikieditor-toolbar-link-int-target' )
342 .on( 'keyup paste cut', function () {
344 // Cancel the running timer if applicable
345 if ( typeof $( this ).data( 'timerID' ) !== 'undefined' ) {
346 clearTimeout( $( this ).data( 'timerID' ) );
348 // Delay fetch for a while
349 // FIXME: Make 120 configurable elsewhere
350 timerID = setTimeout( updateExistence, 120 );
351 $( this ).data( 'timerID', timerID );
353 .on( 'change', function () {
354 // Cancel the running timer if applicable
355 if ( typeof $( this ).data( 'timerID' ) !== 'undefined' ) {
356 clearTimeout( $( this ).data( 'timerID' ) );
363 $( '#wikieditor-toolbar-link-int-target' ).data( 'suggcache', {} ).suggestions( {
367 title = $( this ).val();
369 if ( isExternalLink( title ) || title.indexOf( '|' ) !== -1 || title === '' ) {
370 $( this ).suggestions( 'suggestions', [] );
374 cache = $( this ).data( 'suggcache' );
375 if ( hasOwn.call( cache, title ) ) {
376 $( this ).suggestions( 'suggestions', cache[ title ] );
382 action: 'opensearch',
387 .done( function ( data ) {
388 cache[ title ] = data[ 1 ];
389 $( that ).suggestions( 'suggestions', data[ 1 ] );
391 $( this ).data( 'request', request );
393 cancel: function () {
394 var request = $( this ).data( 'request' );
403 dialogClass: 'wikiEditor-toolbar-dialog',
405 'wikieditor-toolbar-tool-link-insert': function () {
406 var match, buttons, escTarget, escText,
409 whitespace = $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace' ),
410 target = $( '#wikieditor-toolbar-link-int-target' ).val(),
411 text = $( '#wikieditor-toolbar-link-int-text' ).val();
413 function escapeInternalText( s ) {
414 return s.replace( /(\]{2,})/g, '<nowiki>$1</nowiki>' );
416 function escapeExternalTarget( s ) {
417 return s.replace( / /g, '%20' )
418 .replace( /\[/g, '%5B' )
419 .replace( /\]/g, '%5D' );
421 function escapeExternalText( s ) {
422 return s.replace( /(\]+)/g, '<nowiki>$1</nowiki>' );
424 // check if the tooltips were passed as target or text
425 if ( $( '#wikieditor-toolbar-link-int-target' ).data( 'tooltip-mode' ) ) {
428 if ( $( '#wikieditor-toolbar-link-int-text' ).data( 'tooltip-mode' ) ) {
431 if ( target === '' ) {
432 // eslint-disable-next-line no-alert
433 alert( mw.msg( 'wikieditor-toolbar-tool-link-empty' ) );
436 if ( $.trim( text ) === '' ) {
437 // [[Foo| ]] creates an invisible link
438 // Instead, generate [[Foo|]]
441 if ( $( '#wikieditor-toolbar-link-type-int' ).is( ':checked' ) ) {
442 // FIXME: Exactly how fragile is this?
443 if ( $( '#wikieditor-toolbar-link-int-target-status-invalid' ).is( ':visible' ) ) {
444 // Refuse to add links to invalid titles
445 // eslint-disable-next-line no-alert
446 alert( mw.msg( 'wikieditor-toolbar-tool-link-int-invalid' ) );
450 if ( target === text || !text.length ) {
451 insertText = '[[' + target + ']]';
453 insertText = '[[' + target + '|' + escapeInternalText( text ) + ']]';
456 target = $.trim( target );
457 // Prepend http:// if there is no protocol
458 if ( !target.match( /^[a-z]+:\/\/./ ) ) {
459 target = 'http://' + target;
462 // Detect if this is really an internal link in disguise
463 match = target.match( $( this ).data( 'articlePathRegex' ) );
464 if ( match && !$( this ).data( 'ignoreLooksInternal' ) ) {
466 buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-int' ) ] =
468 $( '#wikieditor-toolbar-link-int-target' ).val( match[ 1 ] ).change();
469 $( this ).dialog( 'close' );
471 buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-ext' ) ] =
473 $( that ).data( 'ignoreLooksInternal', true );
474 $( that ).closest( '.ui-dialog' ).find( 'button:first' ).click();
475 $( that ).data( 'ignoreLooksInternal', false );
476 $( this ).dialog( 'close' );
478 $.wikiEditor.modules.dialogs.quickDialog(
479 mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal', match[ 1 ] ),
485 escTarget = escapeExternalTarget( target );
486 escText = escapeExternalText( text );
488 if ( escTarget === escText ) {
489 insertText = escTarget;
490 } else if ( text === '' ) {
491 insertText = '[' + escTarget + ']';
493 insertText = '[' + escTarget + ' ' + escText + ']';
496 // Preserve whitespace in selection when replacing
498 insertText = whitespace[ 0 ] + insertText + whitespace[ 1 ];
500 $( this ).dialog( 'close' );
501 $.wikiEditor.modules.toolbar.fn.doAction( $( this ).data( 'context' ), {
509 $( '#wikieditor-toolbar-link-int-target, #wikieditor-toolbar-link-int-text' ).val( '' );
510 $( '#wikieditor-toolbar-link-type-int, #wikieditor-toolbar-link-type-ext' )
511 .prop( 'checked', false );
513 'wikieditor-toolbar-tool-link-cancel': function () {
514 $( this ).dialog( 'close' );
518 var target, text, type, matches, context, selection,
519 // Obtain the server name without the protocol. wgServer may be protocol-relative
520 serverName = mw.config.get( 'wgServer' ).replace( /^(https?:)?\/\//, '' );
521 // Cache the articlepath regex
522 $( this ).data( 'articlePathRegex', new RegExp(
523 '^https?://' + mw.RegExp.escape( serverName + mw.config.get( 'wgArticlePath' ) )
524 .replace( /\\\$1/g, '(.*)' ) + '$'
526 // Pre-fill the text fields based on the current selection
527 context = $( this ).data( 'context' );
528 selection = context.$textarea.textSelection( 'getSelection' );
529 $( '#wikieditor-toolbar-link-int-target' ).focus();
530 // Trigger the change event, so the link status indicator is up to date
531 $( '#wikieditor-toolbar-link-int-target' ).change();
532 $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ '', '' ] );
533 if ( selection !== '' ) {
534 if ( ( matches = selection.match( /^(\s*)\[\[([^\]\|]+)(\|([^\]\|]*))?\]\](\s*)$/ ) ) ) {
535 // [[foo|bar]] or [[foo]]
536 target = matches[ 2 ];
537 text = ( matches[ 4 ] ? matches[ 4 ] : matches[ 2 ] );
539 // Preserve whitespace when replacing
540 $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ matches[ 1 ], matches[ 5 ] ] );
541 } else if ( ( matches = selection.match( /^(\s*)\[([^\] ]+)( ([^\]]+))?\](\s*)$/ ) ) ) {
542 // [http://www.example.com foo] or [http://www.example.com]
543 target = matches[ 2 ];
544 text = ( matches[ 4 ] || '' );
546 // Preserve whitespace when replacing
547 $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ matches[ 1 ], matches[ 5 ] ] );
549 // Trim any leading and trailing whitespace from the selection,
550 // but preserve it when replacing
551 target = text = $.trim( selection );
552 if ( target.length < selection.length ) {
553 $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [
554 selection.substr( 0, selection.indexOf( target.charAt( 0 ) ) ),
556 selection.lastIndexOf( target.charAt( target.length - 1 ) ) + 1
562 // Change the value by calling val() doesn't trigger the change event, so let's do that
564 if ( typeof text !== 'undefined' ) {
565 $( '#wikieditor-toolbar-link-int-text' ).val( text ).change();
567 if ( typeof target !== 'undefined' ) {
568 $( '#wikieditor-toolbar-link-int-target' ).val( target ).change();
570 if ( typeof type !== 'undefined' ) {
571 $( '#wikieditor-toolbar-link-' + type ).prop( 'checked', true );
574 $( '#wikieditor-toolbar-link-int-text' ).data( 'untouched',
575 $( '#wikieditor-toolbar-link-int-text' ).val() ===
576 $( '#wikieditor-toolbar-link-int-target' ).val() ||
577 $( '#wikieditor-toolbar-link-int-text' ).hasClass( 'wikieditor-toolbar-dialog-hint' )
579 $( '#wikieditor-toolbar-link-int-target' ).suggestions();
581 // don't overwrite user's text
582 if ( selection !== '' ) {
583 $( '#wikieditor-toolbar-link-int-text' ).data( 'untouched', false );
586 $( '#wikieditor-toolbar-link-int-text, #wikiedit-toolbar-link-int-target' )
588 if ( $( this ).val() === '' ) {
589 $( this ).parent().find( 'label' ).show();
593 if ( !$( this ).data( 'dialogkeypressset' ) ) {
594 $( this ).data( 'dialogkeypressset', true );
595 // Execute the action associated with the first button
596 // when the user presses Enter
597 $( this ).closest( '.ui-dialog' ).keypress( function ( e ) {
599 if ( ( e.keyCode || e.which ) === 13 ) {
600 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
606 // Make tabbing to a button and pressing
607 // Enter do what people expect
608 $( this ).closest( '.ui-dialog' ).find( 'button' ).focus( function () {
609 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
615 'insert-reference': {
616 titleMsg: 'wikieditor-toolbar-tool-reference-title',
617 id: 'wikieditor-toolbar-reference-dialog',
618 htmlTemplate: 'dialogInsertReference.html',
620 // Insert translated strings into labels
621 $( this ).find( '[rel]' ).each( function () {
622 $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
627 dialogClass: 'wikiEditor-toolbar-dialog',
630 'wikieditor-toolbar-tool-reference-insert': function () {
631 var insertText = $( '#wikieditor-toolbar-reference-text' ).val(),
632 whitespace = $( '#wikieditor-toolbar-reference-dialog' ).data( 'whitespace' ),
633 attributes = $( '#wikieditor-toolbar-reference-dialog' ).data( 'attributes' );
635 $( this ).dialog( 'close' );
636 $.wikiEditor.modules.toolbar.fn.doAction(
637 $( this ).data( 'context' ),
641 pre: whitespace[ 0 ] + '<ref' + attributes + '>',
643 post: '</ref>' + whitespace[ 1 ]
648 // Restore form state
649 $( '#wikieditor-toolbar-reference-text' ).val( '' );
651 'wikieditor-toolbar-tool-reference-cancel': function () {
652 $( this ).dialog( 'close' );
656 // Pre-fill the text fields based on the current selection
658 context = $( this ).data( 'context' ),
659 selection = context.$textarea.textSelection( 'getSelection' );
661 $( '#wikieditor-toolbar-reference-text' ).focus();
662 $( '#wikieditor-toolbar-reference-dialog' )
663 .data( 'whitespace', [ '', '' ] )
664 .data( 'attributes', '' );
665 if ( selection !== '' ) {
666 if ( ( matches = selection.match( /^(\s*)<ref([^\>]*)>([^<]*)<\/ref\>(\s*)$/ ) ) ) {
668 // Preserve whitespace when replacing
669 $( '#wikieditor-toolbar-reference-dialog' )
670 .data( 'whitespace', [ matches[ 1 ], matches[ 4 ] ] );
671 $( '#wikieditor-toolbar-reference-dialog' ).data( 'attributes', matches[ 2 ] );
675 $( '#wikieditor-toolbar-reference-text' ).val( text );
677 if ( !( $( this ).data( 'dialogkeypressset' ) ) ) {
678 $( this ).data( 'dialogkeypressset', true );
679 // Execute the action associated with the first button
680 // when the user presses Enter
681 $( this ).closest( '.ui-dialog' ).keypress( function ( e ) {
683 if ( ( e.keyCode || e.which ) === 13 ) {
684 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
689 // Make tabbing to a button and pressing
690 // Enter do what people expect
691 $( this ).closest( '.ui-dialog' ).find( 'button' ).focus( function () {
692 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
699 titleMsg: 'wikieditor-toolbar-tool-file-title',
700 id: 'wikieditor-toolbar-file-dialog',
701 htmlTemplate: 'dialogInsertFile.html',
703 var magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' ),
704 defaultMsg = mw.msg( 'wikieditor-toolbar-file-default' );
706 .find( '[data-i18n-magic]' )
708 return magicWordsI18N[ $( this ).attr( 'data-i18n-magic' ) ];
710 .removeAttr( 'data-i18n-magic' )
712 .find( '#wikieditor-toolbar-file-size' )
713 .attr( 'placeholder', defaultMsg )
714 // The message may be long in some languages
715 .attr( 'size', defaultMsg.length )
719 return mw.msg( $( this ).attr( 'rel' ) );
726 dialogClass: 'wikiEditor-toolbar-dialog',
729 'wikieditor-toolbar-tool-file-insert': function () {
730 var fileName, caption, fileFloat, fileFormat, fileSize, fileTitle,
733 magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' );
734 fileName = $( '#wikieditor-toolbar-file-target' ).val();
735 caption = $( '#wikieditor-toolbar-file-caption' ).val();
736 fileFloat = $( '#wikieditor-toolbar-file-float' ).val();
737 fileFormat = $( '#wikieditor-toolbar-file-format' ).val();
738 fileSize = $( '#wikieditor-toolbar-file-size' ).val();
739 // Append px to end to size if not already contains it
740 if ( fileSize !== '' && !hasPxRgx.test( fileSize ) ) {
743 if ( fileName !== '' ) {
744 fileTitle = new mw.Title( fileName );
745 // Append file namespace prefix to filename if not already contains it
746 if ( fileTitle.getNamespaceId() !== 6 ) {
747 fileTitle = new mw.Title( fileName, 6 );
749 fileName = fileTitle.toText();
751 options = [ fileSize, fileFormat, fileFloat ];
752 // Filter empty values
753 options = $.grep( options, function ( val ) {
754 return val.length && val !== 'default';
756 if ( caption.length ) {
757 options.push( caption );
759 fileUse = options.length === 0 ? fileName : ( fileName + '|' + options.join( '|' ) );
760 $( this ).dialog( 'close' );
761 $.wikiEditor.modules.toolbar.fn.doAction(
762 $( this ).data( 'context' ),
775 // Restore form state
776 $( [ '#wikieditor-toolbar-file-target',
777 '#wikieditor-toolbar-file-caption',
778 '#wikieditor-toolbar-file-size' ].join( ',' )
780 $( '#wikieditor-toolbar-file-float' ).val( 'default' );
781 $( '#wikieditor-toolbar-file-format' ).val( magicWordsI18N.img_thumbnail );
783 'wikieditor-toolbar-tool-file-cancel': function () {
784 $( this ).dialog( 'close' );
786 'wikieditor-toolbar-tool-file-upload': function () {
787 var windowManager = new OO.ui.WindowManager(),
788 uploadDialog = new mw.Upload.Dialog( {
789 bookletClass: mw.ForeignStructuredUpload.BookletLayout
792 $( this ).dialog( 'close' );
793 $( 'body' ).append( windowManager.$element );
794 windowManager.addWindows( [ uploadDialog ] );
795 windowManager.openWindow( uploadDialog );
797 uploadDialog.uploadBooklet.on( 'fileSaved', function ( imageInfo ) {
798 uploadDialog.close();
799 windowManager.$element.remove();
801 $.wikiEditor.modules.dialogs.api.openDialog( this, 'insert-file' );
802 $( '#wikieditor-toolbar-file-target' ).val( imageInfo.canonicaltitle );
807 $( '#wikieditor-toolbar-file-target' ).focus();
808 if ( !( $( this ).data( 'dialogkeypressset' ) ) ) {
809 $( this ).data( 'dialogkeypressset', true );
810 // Execute the action associated with the first button
811 // when the user presses Enter
812 $( this ).closest( '.ui-dialog' ).keypress( function ( e ) {
814 if ( e.which === 13 ) {
815 button = $( this ).data( 'dialogaction' ) ||
816 $( this ).find( 'button:first' );
822 // Make tabbing to a button and pressing
823 // Enter do what people expect
824 $( this ).closest( '.ui-dialog' ).find( 'button' ).focus( function () {
825 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
832 titleMsg: 'wikieditor-toolbar-tool-table-title',
833 id: 'wikieditor-toolbar-table-dialog',
834 htmlTemplate: 'dialogInsertTable.html',
836 $( this ).find( '[rel]' ).each( function () {
837 $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
839 // Set tabindexes on form fields
840 $.wikiEditor.modules.dialogs.fn.setTabindexes( $( this ).find( 'input' ).not( '[tabindex]' ) );
842 $( '#wikieditor-toolbar-table-dimensions-rows' ).val( 3 );
843 $( '#wikieditor-toolbar-table-dimensions-columns' ).val( 3 );
844 $( '#wikieditor-toolbar-table-wikitable' ).click( function () {
845 $( '.wikieditor-toolbar-table-preview' ).toggleClass( 'wikitable' );
848 // Hack for sortable preview: dynamically adding
849 // sortable class doesn't work, so we use a clone
850 $( '#wikieditor-toolbar-table-preview' )
852 .attr( 'id', 'wikieditor-toolbar-table-preview2' )
853 .addClass( 'sortable' )
854 .insertAfter( $( '#wikieditor-toolbar-table-preview' ) )
857 mw.loader.using( 'jquery.tablesorter', function () {
858 $( '#wikieditor-toolbar-table-preview2' ).tablesorter();
861 $( '#wikieditor-toolbar-table-sortable' ).click( function () {
862 // Swap the currently shown one clone with the other one
863 $( '#wikieditor-toolbar-table-preview' )
865 .attr( 'id', 'wikieditor-toolbar-table-preview3' );
866 $( '#wikieditor-toolbar-table-preview2' )
867 .attr( 'id', 'wikieditor-toolbar-table-preview' )
869 $( '#wikieditor-toolbar-table-preview3' ).attr( 'id', 'wikieditor-toolbar-table-preview2' );
872 $( '#wikieditor-toolbar-table-dimensions-header' ).click( function () {
873 // Instead of show/hiding, switch the HTML around
874 // We do this because the sortable tables script styles the first row,
877 headerHTML = $( '.wikieditor-toolbar-table-preview-header' ).html(),
878 hiddenHTML = $( '.wikieditor-toolbar-table-preview-hidden' ).html();
879 $( '.wikieditor-toolbar-table-preview-header' ).html( hiddenHTML );
880 $( '.wikieditor-toolbar-table-preview-hidden' ).html( headerHTML );
881 $sortable = $( '#wikieditor-toolbar-table-preview, #wikieditor-toolbar-table-preview2' )
882 .filter( '.sortable' );
883 mw.loader.using( 'jquery.tablesorter', function () {
884 $sortable.tablesorter();
890 dialogClass: 'wikiEditor-toolbar-dialog',
893 'wikieditor-toolbar-tool-table-insert': function () {
894 var headerText, normalText, table, r, c,
895 isHeader, delim, classes, classStr,
896 rowsVal = $( '#wikieditor-toolbar-table-dimensions-rows' ).val(),
897 colsVal = $( '#wikieditor-toolbar-table-dimensions-columns' ).val(),
898 rows = parseInt( rowsVal, 10 ),
899 cols = parseInt( colsVal, 10 ),
900 header = $( '#wikieditor-toolbar-table-dimensions-header' ).prop( 'checked' ) ? 1 : 0;
901 if ( isNaN( rows ) || isNaN( cols ) || String( rows ) !== rowsVal || String( cols ) !== colsVal || rowsVal < 0 || colsVal < 0 ) {
902 // eslint-disable-next-line no-alert
903 alert( mw.msg( 'wikieditor-toolbar-tool-table-invalidnumber' ) );
906 if ( rows + header === 0 || cols === 0 ) {
907 // eslint-disable-next-line no-alert
908 alert( mw.msg( 'wikieditor-toolbar-tool-table-zero' ) );
911 if ( ( rows * cols ) > 1000 ) {
912 // 1000 is in the English message. The parameter replacement is kept for BC.
913 // eslint-disable-next-line no-alert
914 alert( mw.msg( 'wikieditor-toolbar-tool-table-toomany', 1000 ) );
917 headerText = mw.msg( 'wikieditor-toolbar-tool-table-example-header' );
918 normalText = mw.msg( 'wikieditor-toolbar-tool-table-example' );
920 for ( r = 0; r < rows + header; r++ ) {
922 for ( c = 0; c < cols; c++ ) {
923 isHeader = ( header && r === 0 );
924 delim = isHeader ? '!' : '|';
928 table += delim + ' ' + ( isHeader ? headerText : normalText ) + ' ';
930 // Replace trailing space by newline
931 // table[table.length - 1] is read-only
932 table = table.substr( 0, table.length - 1 ) + '\n';
935 if ( $( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
936 classes.push( 'wikitable' );
938 if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
939 classes.push( 'sortable' );
941 classStr = classes.length > 0 ? ' class="' + classes.join( ' ' ) + '"' : '';
942 $( this ).dialog( 'close' );
943 $.wikiEditor.modules.toolbar.fn.doAction(
944 $( this ).data( 'context' ),
948 pre: '{|' + classStr + '\n',
957 // Restore form state
958 $( '#wikieditor-toolbar-table-dimensions-rows' ).val( 3 );
959 $( '#wikieditor-toolbar-table-dimensions-columns' ).val( 3 );
960 // Simulate clicks instead of setting values, so the according
961 // actions are performed
962 if ( !$( '#wikieditor-toolbar-table-dimensions-header' ).is( ':checked' ) ) {
963 $( '#wikieditor-toolbar-table-dimensions-header' ).click();
965 if ( !$( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
966 $( '#wikieditor-toolbar-table-wikitable' ).click();
968 if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
969 $( '#wikieditor-toolbar-table-sortable' ).click();
972 'wikieditor-toolbar-tool-table-cancel': function () {
973 $( this ).dialog( 'close' );
977 $( '#wikieditor-toolbar-table-dimensions-rows' ).focus();
978 if ( !( $( this ).data( 'dialogkeypressset' ) ) ) {
979 $( this ).data( 'dialogkeypressset', true );
980 // Execute the action associated with the first button
981 // when the user presses Enter
982 $( this ).closest( '.ui-dialog' ).keypress( function ( e ) {
984 if ( ( e.keyCode || e.which ) === 13 ) {
985 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
991 // Make tabbing to a button and pressing
992 // Enter do what people expect
993 $( this ).closest( '.ui-dialog' ).find( 'button' ).focus( function () {
994 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
1000 'search-and-replace': {
1002 // Left-to-right languages
1004 msie: [ [ '>=', 11 ] ] // Known to work on 11.
1006 // Right-to-left languages
1008 msie: [ [ '>=', 11 ] ] // Works on 11 but dialog positioning is cruddy.
1011 titleMsg: 'wikieditor-toolbar-tool-replace-title',
1012 id: 'wikieditor-toolbar-replace-dialog',
1013 htmlTemplate: 'dialogReplace.html',
1015 $( this ).find( '[rel]' ).each( function () {
1016 $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
1018 // Set tabindexes on form fields
1019 $.wikiEditor.modules.dialogs.fn.setTabindexes( $( this ).find( 'input' ).not( '[tabindex]' ) );
1021 // TODO: Find a cleaner way to share this function
1022 $( this ).data( 'replaceCallback', function ( mode ) {
1023 var offset, textRemainder, regex, index, i,
1024 searchStr, replaceStr, flags, matchCase, isRegex,
1025 $textarea, text, match,
1026 matchedText, replace, newEnd,
1030 $( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
1032 // Search string cannot be empty
1033 searchStr = $( '#wikieditor-toolbar-replace-search' ).val();
1034 if ( searchStr === '' ) {
1035 $( '#wikieditor-toolbar-replace-emptysearch' ).show();
1039 // Replace string can be empty
1040 replaceStr = $( '#wikieditor-toolbar-replace-replace' ).val();
1042 // Prepare the regular expression flags
1044 matchCase = $( '#wikieditor-toolbar-replace-case' ).is( ':checked' );
1048 isRegex = $( '#wikieditor-toolbar-replace-regex' ).is( ':checked' );
1050 searchStr = mw.RegExp.escape( searchStr );
1052 if ( mode === 'replaceAll' ) {
1057 regex = new RegExp( searchStr, flags );
1059 $( '#wikieditor-toolbar-replace-invalidregex' )
1060 .text( mw.msg( 'wikieditor-toolbar-tool-replace-invalidregex',
1066 $textarea = $( this ).data( 'context' ).$textarea;
1067 text = $textarea.textSelection( 'getContents' );
1069 if ( mode !== 'replaceAll' ) {
1070 if ( mode === 'replace' ) {
1071 offset = $( this ).data( 'matchIndex' );
1073 offset = $( this ).data( 'offset' );
1075 textRemainder = text.substr( offset );
1076 match = textRemainder.match( regex );
1079 // Search hit BOTTOM, continuing at TOP
1080 // TODO: Add a "Wrap around" option.
1082 textRemainder = text;
1083 match = textRemainder.match( regex );
1087 $( '#wikieditor-toolbar-replace-nomatch' ).show();
1088 } else if ( mode === 'replaceAll' ) {
1089 // Instead of using repetitive .match() calls, we use one .match() call with /g
1090 // and indexOf() followed by substr() to find the offsets. This is actually
1091 // faster because our indexOf+substr loop is faster than a match loop, and the
1092 // /g match is so ridiculously fast that it's negligible.
1093 // FIXME: Repetitively calling encapsulateSelection() is probably the best strategy
1094 // in Firefox/Webkit, but in IE replacing the entire content once is better.
1095 for ( i = 0; i < match.length; i++ ) {
1096 index = textRemainder.indexOf( match[ i ] );
1097 if ( index === -1 ) {
1098 // This shouldn't happen
1101 matchedText = textRemainder.substr( index, match[ i ].length );
1102 textRemainder = textRemainder.substr( index + match[ i ].length );
1104 start = index + offset;
1105 end = start + match[ i ].length;
1106 // Make regex placeholder substitution ($1) work
1107 replace = isRegex ? matchedText.replace( regex, replaceStr ) : replaceStr;
1108 newEnd = start + replace.length;
1110 .textSelection( 'setSelection', { start: start, end: end } )
1111 .textSelection( 'encapsulateSelection', {
1114 .textSelection( 'setSelection', { start: start, end: newEnd } );
1117 $( '#wikieditor-toolbar-replace-success' )
1118 .text( mw.msg( 'wikieditor-toolbar-tool-replace-success', match.length ) )
1120 $( this ).data( 'offset', 0 );
1123 if ( mode === 'replace' ) {
1126 // If backreferences (like $1) are used, the actual actual replacement string will be different
1127 actualReplacement = match[ 0 ].replace( regex, replaceStr );
1129 actualReplacement = replaceStr;
1133 // Do the replacement
1134 $textarea.textSelection( 'encapsulateSelection', {
1135 peri: actualReplacement,
1137 // Reload the text after replacement
1138 text = $textarea.textSelection( 'getContents' );
1141 // Find the next instance
1142 offset = offset + match[ 0 ].length + actualReplacement.length;
1143 textRemainder = text.substr( offset );
1144 match = textRemainder.match( regex );
1147 start = offset + match.index;
1148 end = start + match[ 0 ].length;
1150 // If no new string was found, try searching from the beginning.
1151 // TODO: Add a "Wrap around" option.
1152 textRemainder = text;
1153 match = textRemainder.match( regex );
1155 start = match.index;
1156 end = start + match[ 0 ].length;
1164 start = offset + match.index;
1165 end = start + match[ 0 ].length;
1168 $( this ).data( 'matchIndex', start );
1170 $textarea.textSelection( 'setSelection', {
1173 $textarea.textSelection( 'scrollToCaretPosition' );
1174 $( this ).data( 'offset', end );
1175 $textarea[ 0 ].focus();
1181 dialogClass: 'wikiEditor-toolbar-dialog',
1184 'wikieditor-toolbar-tool-replace-button-findnext': function ( e ) {
1185 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
1186 $( this ).data( 'replaceCallback' ).call( this, 'find' );
1188 'wikieditor-toolbar-tool-replace-button-replace': function ( e ) {
1189 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
1190 $( this ).data( 'replaceCallback' ).call( this, 'replace' );
1192 'wikieditor-toolbar-tool-replace-button-replaceall': function ( e ) {
1193 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', e.target );
1194 $( this ).data( 'replaceCallback' ).call( this, 'replaceAll' );
1196 'wikieditor-toolbar-tool-replace-close': function () {
1197 $( this ).dialog( 'close' );
1201 var dialog, context, textbox,
1203 $( this ).data( 'offset', 0 );
1204 $( this ).data( 'matchIndex', 0 );
1206 $( '#wikieditor-toolbar-replace-search' ).focus();
1207 $( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
1208 if ( !( $( this ).data( 'onetimeonlystuff' ) ) ) {
1209 $( this ).data( 'onetimeonlystuff', true );
1210 // Execute the action associated with the first button
1211 // when the user presses Enter
1212 $( this ).closest( '.ui-dialog' ).keypress( function ( e ) {
1214 if ( ( e.keyCode || e.which ) === 13 ) {
1215 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
1220 // Make tabbing to a button and pressing
1221 // Enter do what people expect
1222 $( this ).closest( '.ui-dialog' ).find( 'button' ).focus( function () {
1223 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', this );
1226 dialog = $( this ).closest( '.ui-dialog' );
1228 context = $( this ).data( 'context' );
1229 textbox = context.$textarea;
1232 .on( 'keypress.srdialog', function ( e ) {
1234 if ( e.which === 13 ) {
1236 button = dialog.data( 'dialogaction' ) || dialog.find( 'button:first' );
1239 } else if ( e.which === 27 ) {
1241 $( that ).dialog( 'close' );
1245 close: function () {
1246 var context = $( this ).data( 'context' ),
1247 textbox = context.$textarea;
1248 $( textbox ).off( 'keypress.srdialog' );
1249 $( this ).closest( '.ui-dialog' ).data( 'dialogaction', false );
1258 }( jQuery, mediaWiki, OO ) );