]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/WikiEditor/modules/jquery.wikiEditor.dialogs.config.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / WikiEditor / modules / jquery.wikiEditor.dialogs.config.js
1 /**
2  * Configuration of Dialog module for wikiEditor
3  */
4 ( function ( $, mw, OO ) {
5
6         var hasOwn = Object.prototype.hasOwnProperty;
7
8         $.wikiEditor.modules.dialogs.config = {
9
10                 replaceIcons: function ( $textarea ) {
11                         $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', {
18                                         section: 'main',
19                                         group: 'insert',
20                                         tools: {
21                                                 link: {
22                                                         labelMsg: 'wikieditor-toolbar-tool-link',
23                                                         type: 'button',
24                                                         icon: 'insert-link.png',
25                                                         offset: [ 2, -1654 ],
26                                                         action: {
27                                                                 type: 'dialog',
28                                                                 module: 'insert-link'
29                                                         }
30                                                 },
31                                                 file: {
32                                                         labelMsg: 'wikieditor-toolbar-tool-file',
33                                                         type: 'button',
34                                                         icon: 'insert-file.png',
35                                                         offset: [ 2, -1438 ],
36                                                         action: {
37                                                                 type: 'dialog',
38                                                                 module: 'insert-file'
39                                                         }
40                                                 },
41                                                 reference: {
42                                                         labelMsg: 'wikieditor-toolbar-tool-reference',
43                                                         filters: [ 'body.ns-subject' ],
44                                                         type: 'button',
45                                                         icon: 'insert-reference.png',
46                                                         offset: [ 2, -1798 ],
47                                                         action: {
48                                                                 type: 'dialog',
49                                                                 module: 'insert-reference'
50                                                         }
51                                                 }
52                                         }
53                                 } )
54                                 .wikiEditor( 'addToToolbar', {
55                                         section: 'advanced',
56                                         group: 'insert',
57                                         tools: {
58                                                 table: {
59                                                         labelMsg: 'wikieditor-toolbar-tool-table',
60                                                         type: 'button',
61                                                         icon: 'insert-table.png',
62                                                         offset: [ 2, -1942 ],
63                                                         action: {
64                                                                 type: 'dialog',
65                                                                 module: 'insert-table'
66                                                         }
67                                                 }
68                                         }
69                                 } )
70                                 .wikiEditor( 'addToToolbar', {
71                                         section: 'advanced',
72                                         groups: {
73                                                 search: {
74                                                         tools: {
75                                                                 replace: {
76                                                                         labelMsg: 'wikieditor-toolbar-tool-replace',
77                                                                         type: 'button',
78                                                                         icon: 'search-replace.png',
79                                                                         offset: [ -70, -214 ],
80                                                                         action: {
81                                                                                 type: 'dialog',
82                                                                                 module: 'search-and-replace'
83                                                                         }
84                                                                 }
85                                                         }
86                                                 }
87                                         }
88                                 } );
89                 },
90
91                 getDefaultConfig: function () {
92                         return { dialogs: {
93                                 'insert-link': {
94                                         titleMsg: 'wikieditor-toolbar-tool-link-title',
95                                         id: 'wikieditor-toolbar-link-dialog',
96                                         htmlTemplate: 'dialogInsertLink.html',
97
98                                         init: function () {
99                                                 var api = new mw.Api();
100
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' ) {
109                                                                 // Cache the regex
110                                                                 arguments.callee.regex =
111                                                                         new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + '|www\\.)', 'i' );
112                                                         }
113                                                         return s.match( arguments.callee.regex );
114                                                         /* eslint-enable no-caller */
115                                                 }
116
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()
121                                                                 .removeClass(
122                                                                         'status-invalid status-external status-notexists status-exists status-loading'
123                                                                 );
124                                                         if ( status ) {
125                                                                 $( '#wikieditor-toolbar-link-int-target-status-' + status ).show();
126                                                                 $( '#wikieditor-toolbar-link-int-target' ).parent().addClass( 'status-' + status );
127                                                         }
128                                                         if ( status === 'invalid' ) {
129                                                                 $( '.ui-dialog:visible .ui-dialog-buttonpane button:first' )
130                                                                         .prop( 'disabled', true )
131                                                                         .addClass( 'disabled' );
132                                                         } else {
133                                                                 $( '.ui-dialog:visible .ui-dialog-buttonpane button:first' )
134                                                                         .prop( 'disabled', false )
135                                                                         .removeClass( 'disabled' );
136                                                         }
137                                                 }
138
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 ) {
148                                                                 internal = false;
149                                                         }
150                                                         if ( request ) {
151                                                                 request.abort();
152                                                         }
153                                                         if ( hasOwn.call( cache, target ) ) {
154                                                                 updateWidget( cache[ target ] );
155                                                                 return;
156                                                         }
157                                                         if ( target.replace( /^\s+$/, '' ) === '' ) {
158                                                                 // Hide the widget when the textbox is empty
159                                                                 updateWidget( false );
160                                                                 return;
161                                                         }
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' );
165                                                                 return;
166                                                         }
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' );
171                                                                 return;
172                                                         }
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
176                                                         // necessary.
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(
181                                                                 'request',
182                                                                 api.get( {
183                                                                         formatversion: 2,
184                                                                         action: 'query',
185                                                                         prop: 'pageprops',
186                                                                         titles: target,
187                                                                         ppprop: 'disambiguation'
188                                                                 } ).done( function ( data ) {
189                                                                         var status, page;
190                                                                         if ( !data.query || !data.query.pages ) {
191                                                                                 // This happens in some weird cases like interwiki links
192                                                                                 status = false;
193                                                                         } else {
194                                                                                 page = data.query.pages[ 0 ];
195                                                                                 status = 'exists';
196                                                                                 if ( page.missing ) {
197                                                                                         status = 'notexists';
198                                                                                 } else if ( page.invalid ) {
199                                                                                         status = 'invalid';
200                                                                                 } else if ( page.pageprops ) {
201                                                                                         status = 'disambig';
202                                                                                 }
203                                                                         }
204                                                                         // Cache the status of the link target if the force internal
205                                                                         // parameter was not passed
206                                                                         if ( !internal ) {
207                                                                                 cache[ target ] = status;
208                                                                         }
209                                                                         updateWidget( status );
210                                                                 } )
211                                                         );
212                                                 }
213                                                 $( '#wikieditor-toolbar-link-type-int, #wikieditor-toolbar-link-type-ext' ).click( function () {
214                                                         var request;
215                                                         if ( $( '#wikieditor-toolbar-link-type-ext' ).prop( 'checked' ) ) {
216                                                                 // Abort previous request
217                                                                 request = $( '#wikieditor-toolbar-link-int-target-status' ).data( 'request' );
218                                                                 if ( request ) {
219                                                                         request.abort();
220                                                                 }
221                                                                 updateWidget( 'external' );
222                                                         }
223                                                         if ( $( '#wikieditor-toolbar-link-type-int' ).prop( 'checked' ) ) {
224                                                                 updateExistence( true );
225                                                         }
226                                                 } );
227                                                 // Set labels of tabs based on rel values
228                                                 $( this ).find( '[rel]' ).each( function () {
229                                                         $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
230                                                 } );
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' )
239                                                         .each( function () {
240                                                                 if ( $( this ).val() === '' ) {
241                                                                         $( this )
242                                                                                 .addClass( 'wikieditor-toolbar-dialog-hint' )
243                                                                                 .val( $( this ).data( 'tooltip' ) )
244                                                                                 .data( 'tooltip-mode', true );
245                                                                 }
246                                                         } )
247                                                         .on( 'focus', function () {
248                                                                 if ( $( this ).val() === $( this ).data( 'tooltip' ) ) {
249                                                                         $( this )
250                                                                                 .val( '' )
251                                                                                 .removeClass( 'wikieditor-toolbar-dialog-hint' )
252                                                                                 .data( 'tooltip-mode', false );
253                                                                 }
254                                                         } )
255                                                         .on( 'change', function () {
256                                                                 if ( $( this ).val() !== $( this ).data( 'tooltip' ) ) {
257                                                                         $( this )
258                                                                                 .removeClass( 'wikieditor-toolbar-dialog-hint' )
259                                                                                 .data( 'tooltip-mode', false );
260                                                                 }
261                                                         } )
262                                                         .on( 'blur', function () {
263                                                                 if ( $( this ).val() === '' ) {
264                                                                         $( this )
265                                                                                 .addClass( 'wikieditor-toolbar-dialog-hint' )
266                                                                                 .val( $( this ).data( 'tooltip' ) )
267                                                                                 .data( 'tooltip-mode', true );
268                                                                 }
269                                                         } );
270
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
276                                                         // been updated
277                                                         setTimeout( function () {
278                                                                 if ( isExternalLink( $( '#wikieditor-toolbar-link-int-target' ).val() ) ) {
279                                                                         $( '#wikieditor-toolbar-link-type-ext' ).prop( 'checked', true );
280                                                                         updateWidget( 'external' );
281                                                                 } else {
282                                                                         $( '#wikieditor-toolbar-link-type-int' ).prop( 'checked', true );
283                                                                         updateExistence();
284                                                                 }
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' )
289                                                                         ) {
290                                                                                 $( '#wikieditor-toolbar-link-int-text' )
291                                                                                         .addClass( 'wikieditor-toolbar-dialog-hint' )
292                                                                                         .val( $( '#wikieditor-toolbar-link-int-text' ).data( 'tooltip' ) )
293                                                                                         .change();
294                                                                         } else {
295                                                                                 $( '#wikieditor-toolbar-link-int-text' )
296                                                                                         .val( $( '#wikieditor-toolbar-link-int-target' ).val() )
297                                                                                         .change();
298                                                                         }
299                                                                 }
300                                                         }, 0 );
301                                                 } );
302                                                 $( '#wikieditor-toolbar-link-int-text' ).on( 'change keydown paste cut', function () {
303                                                         var oldVal = $( this ).val(),
304                                                                 that = this;
305                                                         setTimeout( function () {
306                                                                 if ( $( that ).val() !== oldVal ) {
307                                                                         $( that ).data( 'untouched', false );
308                                                                 }
309                                                         }, 0 );
310                                                 } );
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' ) )
317                                                         )
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' ) )
321                                                         )
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' ) )
325                                                         )
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' ) )
329                                                         )
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' ) )
333                                                         )
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' ) )
337                                                         )
338                                                         .data( 'existencecache', {} )
339                                                         .children().hide();
340
341                                                 $( '#wikieditor-toolbar-link-int-target' )
342                                                         .on( 'keyup paste cut', function () {
343                                                                 var timerID;
344                                                                 // Cancel the running timer if applicable
345                                                                 if ( typeof $( this ).data( 'timerID' ) !== 'undefined' ) {
346                                                                         clearTimeout( $( this ).data( 'timerID' ) );
347                                                                 }
348                                                                 // Delay fetch for a while
349                                                                 // FIXME: Make 120 configurable elsewhere
350                                                                 timerID = setTimeout( updateExistence, 120 );
351                                                                 $( this ).data( 'timerID', timerID );
352                                                         } )
353                                                         .on( 'change', function () {
354                                                                 // Cancel the running timer if applicable
355                                                                 if ( typeof $( this ).data( 'timerID' ) !== 'undefined' ) {
356                                                                         clearTimeout( $( this ).data( 'timerID' ) );
357                                                                 }
358                                                                 // Fetch right now
359                                                                 updateExistence();
360                                                         } );
361
362                                                 // Title suggestions
363                                                 $( '#wikieditor-toolbar-link-int-target' ).data( 'suggcache', {} ).suggestions( {
364                                                         fetch: function () {
365                                                                 var cache, request,
366                                                                         that = this,
367                                                                         title = $( this ).val();
368
369                                                                 if ( isExternalLink( title ) || title.indexOf( '|' ) !== -1 || title === '' ) {
370                                                                         $( this ).suggestions( 'suggestions', [] );
371                                                                         return;
372                                                                 }
373
374                                                                 cache = $( this ).data( 'suggcache' );
375                                                                 if ( hasOwn.call( cache, title ) ) {
376                                                                         $( this ).suggestions( 'suggestions', cache[ title ] );
377                                                                         return;
378                                                                 }
379
380                                                                 request = api.get( {
381                                                                         formatversion: 2,
382                                                                         action: 'opensearch',
383                                                                         search: title,
384                                                                         namespace: 0,
385                                                                         suggest: ''
386                                                                 } )
387                                                                 .done( function ( data ) {
388                                                                         cache[ title ] = data[ 1 ];
389                                                                         $( that ).suggestions( 'suggestions', data[ 1 ] );
390                                                                 } );
391                                                                 $( this ).data( 'request', request );
392                                                         },
393                                                         cancel: function () {
394                                                                 var request = $( this ).data( 'request' );
395                                                                 if ( request ) {
396                                                                         request.abort();
397                                                                 }
398                                                         }
399                                                 } );
400                                         },
401                                         dialog: {
402                                                 width: 500,
403                                                 dialogClass: 'wikiEditor-toolbar-dialog',
404                                                 buttons: {
405                                                         'wikieditor-toolbar-tool-link-insert': function () {
406                                                                 var match, buttons, escTarget, escText,
407                                                                         that = this,
408                                                                         insertText = '',
409                                                                         whitespace = $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace' ),
410                                                                         target = $( '#wikieditor-toolbar-link-int-target' ).val(),
411                                                                         text = $( '#wikieditor-toolbar-link-int-text' ).val();
412
413                                                                 function escapeInternalText( s ) {
414                                                                         return s.replace( /(\]{2,})/g, '<nowiki>$1</nowiki>' );
415                                                                 }
416                                                                 function escapeExternalTarget( s ) {
417                                                                         return s.replace( / /g, '%20' )
418                                                                                 .replace( /\[/g, '%5B' )
419                                                                                 .replace( /\]/g, '%5D' );
420                                                                 }
421                                                                 function escapeExternalText( s ) {
422                                                                         return s.replace( /(\]+)/g, '<nowiki>$1</nowiki>' );
423                                                                 }
424                                                                 // check if the tooltips were passed as target or text
425                                                                 if ( $( '#wikieditor-toolbar-link-int-target' ).data( 'tooltip-mode' ) ) {
426                                                                         target = '';
427                                                                 }
428                                                                 if ( $( '#wikieditor-toolbar-link-int-text' ).data( 'tooltip-mode' ) ) {
429                                                                         text = '';
430                                                                 }
431                                                                 if ( target === '' ) {
432                                                                         // eslint-disable-next-line no-alert
433                                                                         alert( mw.msg( 'wikieditor-toolbar-tool-link-empty' ) );
434                                                                         return;
435                                                                 }
436                                                                 if ( $.trim( text ) === '' ) {
437                                                                         // [[Foo| ]] creates an invisible link
438                                                                         // Instead, generate [[Foo|]]
439                                                                         text = '';
440                                                                 }
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' ) );
447                                                                                 return;
448                                                                         }
449
450                                                                         if ( target === text || !text.length ) {
451                                                                                 insertText = '[[' + target + ']]';
452                                                                         } else {
453                                                                                 insertText = '[[' + target + '|' + escapeInternalText( text ) + ']]';
454                                                                         }
455                                                                 } else {
456                                                                         target = $.trim( target );
457                                                                         // Prepend http:// if there is no protocol
458                                                                         if ( !target.match( /^[a-z]+:\/\/./ ) ) {
459                                                                                 target = 'http://' + target;
460                                                                         }
461
462                                                                         // Detect if this is really an internal link in disguise
463                                                                         match = target.match( $( this ).data( 'articlePathRegex' ) );
464                                                                         if ( match && !$( this ).data( 'ignoreLooksInternal' ) ) {
465                                                                                 buttons = {};
466                                                                                 buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-int' ) ] =
467                                                                                         function () {
468                                                                                                 $( '#wikieditor-toolbar-link-int-target' ).val( match[ 1 ] ).change();
469                                                                                                 $( this ).dialog( 'close' );
470                                                                                         };
471                                                                                 buttons[ mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal-ext' ) ] =
472                                                                                         function () {
473                                                                                                 $( that ).data( 'ignoreLooksInternal', true );
474                                                                                                 $( that ).closest( '.ui-dialog' ).find( 'button:first' ).click();
475                                                                                                 $( that ).data( 'ignoreLooksInternal', false );
476                                                                                                 $( this ).dialog( 'close' );
477                                                                                         };
478                                                                                 $.wikiEditor.modules.dialogs.quickDialog(
479                                                                                         mw.msg( 'wikieditor-toolbar-tool-link-lookslikeinternal', match[ 1 ] ),
480                                                                                         { buttons: buttons }
481                                                                                 );
482                                                                                 return;
483                                                                         }
484
485                                                                         escTarget = escapeExternalTarget( target );
486                                                                         escText = escapeExternalText( text );
487
488                                                                         if ( escTarget === escText ) {
489                                                                                 insertText = escTarget;
490                                                                         } else if ( text === '' ) {
491                                                                                 insertText = '[' + escTarget + ']';
492                                                                         } else {
493                                                                                 insertText = '[' + escTarget + ' ' + escText + ']';
494                                                                         }
495                                                                 }
496                                                                 // Preserve whitespace in selection when replacing
497                                                                 if ( whitespace ) {
498                                                                         insertText = whitespace[ 0 ] + insertText + whitespace[ 1 ];
499                                                                 }
500                                                                 $( this ).dialog( 'close' );
501                                                                 $.wikiEditor.modules.toolbar.fn.doAction( $( this ).data( 'context' ), {
502                                                                         type: 'replace',
503                                                                         options: {
504                                                                                 pre: insertText
505                                                                         }
506                                                                 }, $( this ) );
507
508                                                                 // Blank form
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 );
512                                                         },
513                                                         'wikieditor-toolbar-tool-link-cancel': function () {
514                                                                 $( this ).dialog( 'close' );
515                                                         }
516                                                 },
517                                                 open: function () {
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, '(.*)' ) + '$'
525                                                         ) );
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 ] );
538                                                                         type = 'int';
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 ] || '' );
545                                                                         type = 'ext';
546                                                                         // Preserve whitespace when replacing
547                                                                         $( '#wikieditor-toolbar-link-dialog' ).data( 'whitespace', [ matches[ 1 ], matches[ 5 ] ] );
548                                                                 } else {
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 ) ) ),
555                                                                                         selection.substr(
556                                                                                                 selection.lastIndexOf( target.charAt( target.length - 1 ) ) + 1
557                                                                                         ) ]
558                                                                                 );
559                                                                         }
560                                                                 }
561
562                                                                 // Change the value by calling val() doesn't trigger the change event, so let's do that
563                                                                 // ourselves
564                                                                 if ( typeof text !== 'undefined' ) {
565                                                                         $( '#wikieditor-toolbar-link-int-text' ).val( text ).change();
566                                                                 }
567                                                                 if ( typeof target !== 'undefined' ) {
568                                                                         $( '#wikieditor-toolbar-link-int-target' ).val( target ).change();
569                                                                 }
570                                                                 if ( typeof type !== 'undefined' ) {
571                                                                         $( '#wikieditor-toolbar-link-' + type ).prop( 'checked', true );
572                                                                 }
573                                                         }
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' )
578                                                         );
579                                                         $( '#wikieditor-toolbar-link-int-target' ).suggestions();
580
581                                                         // don't overwrite user's text
582                                                         if ( selection !== '' ) {
583                                                                 $( '#wikieditor-toolbar-link-int-text' ).data( 'untouched', false );
584                                                         }
585
586                                                         $( '#wikieditor-toolbar-link-int-text, #wikiedit-toolbar-link-int-target' )
587                                                                 .each( function () {
588                                                                         if ( $( this ).val() === '' ) {
589                                                                                 $( this ).parent().find( 'label' ).show();
590                                                                         }
591                                                                 } );
592
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 ) {
598                                                                         var button;
599                                                                         if ( ( e.keyCode || e.which ) === 13 ) {
600                                                                                 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
601                                                                                 button.click();
602                                                                                 e.preventDefault();
603                                                                         }
604                                                                 } );
605
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 );
610                                                                 } );
611                                                         }
612                                                 }
613                                         }
614                                 },
615                                 'insert-reference': {
616                                         titleMsg: 'wikieditor-toolbar-tool-reference-title',
617                                         id: 'wikieditor-toolbar-reference-dialog',
618                                         htmlTemplate: 'dialogInsertReference.html',
619                                         init: function () {
620                                                 // Insert translated strings into labels
621                                                 $( this ).find( '[rel]' ).each( function () {
622                                                         $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
623                                                 } );
624
625                                         },
626                                         dialog: {
627                                                 dialogClass: 'wikiEditor-toolbar-dialog',
628                                                 width: 590,
629                                                 buttons: {
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' );
634                                                                 // Close the dialog
635                                                                 $( this ).dialog( 'close' );
636                                                                 $.wikiEditor.modules.toolbar.fn.doAction(
637                                                                         $( this ).data( 'context' ),
638                                                                         {
639                                                                                 type: 'replace',
640                                                                                 options: {
641                                                                                         pre: whitespace[ 0 ] + '<ref' + attributes + '>',
642                                                                                         peri: insertText,
643                                                                                         post: '</ref>' + whitespace[ 1 ]
644                                                                                 }
645                                                                         },
646                                                                         $( this )
647                                                                 );
648                                                                 // Restore form state
649                                                                 $( '#wikieditor-toolbar-reference-text' ).val( '' );
650                                                         },
651                                                         'wikieditor-toolbar-tool-reference-cancel': function () {
652                                                                 $( this ).dialog( 'close' );
653                                                         }
654                                                 },
655                                                 open: function () {
656                                                         // Pre-fill the text fields based on the current selection
657                                                         var matches, text,
658                                                                 context = $( this ).data( 'context' ),
659                                                                 selection = context.$textarea.textSelection( 'getSelection' );
660                                                         // set focus
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*)$/ ) ) ) {
667                                                                         text = matches[ 3 ];
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 ] );
672                                                                 } else {
673                                                                         text = selection;
674                                                                 }
675                                                                 $( '#wikieditor-toolbar-reference-text' ).val( text );
676                                                         }
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 ) {
682                                                                         var button;
683                                                                         if ( ( e.keyCode || e.which ) === 13 ) {
684                                                                                 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
685                                                                                 button.click();
686                                                                                 e.preventDefault();
687                                                                         }
688                                                                 } );
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 );
693                                                                 } );
694                                                         }
695                                                 }
696                                         }
697                                 },
698                                 'insert-file': {
699                                         titleMsg: 'wikieditor-toolbar-tool-file-title',
700                                         id: 'wikieditor-toolbar-file-dialog',
701                                         htmlTemplate: 'dialogInsertFile.html',
702                                         init: function () {
703                                                 var magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' ),
704                                                         defaultMsg = mw.msg( 'wikieditor-toolbar-file-default' );
705                                                 $( this )
706                                                         .find( '[data-i18n-magic]' )
707                                                                 .text( function () {
708                                                                         return magicWordsI18N[ $( this ).attr( 'data-i18n-magic' ) ];
709                                                                 } )
710                                                                 .removeAttr( 'data-i18n-magic' )
711                                                                 .end()
712                                                         .find( '#wikieditor-toolbar-file-size' )
713                                                                 .attr( 'placeholder', defaultMsg )
714                                                                 // The message may be long in some languages
715                                                                 .attr( 'size', defaultMsg.length )
716                                                                 .end()
717                                                         .find( '[rel]' )
718                                                                 .text( function () {
719                                                                         return mw.msg( $( this ).attr( 'rel' ) );
720                                                                 } )
721                                                                 .removeAttr( 'rel' )
722                                                                 .end();
723                                         },
724                                         dialog: {
725                                                 resizable: false,
726                                                 dialogClass: 'wikiEditor-toolbar-dialog',
727                                                 width: 590,
728                                                 buttons: {
729                                                         'wikieditor-toolbar-tool-file-insert': function () {
730                                                                 var fileName, caption, fileFloat, fileFormat, fileSize, fileTitle,
731                                                                         options, fileUse,
732                                                                         hasPxRgx = /.+px$/,
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 ) ) {
741                                                                         fileSize += 'px';
742                                                                 }
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 );
748                                                                         }
749                                                                         fileName = fileTitle.toText();
750                                                                 }
751                                                                 options = [ fileSize, fileFormat, fileFloat ];
752                                                                 // Filter empty values
753                                                                 options = $.grep( options, function ( val ) {
754                                                                         return val.length && val !== 'default';
755                                                                 } );
756                                                                 if ( caption.length ) {
757                                                                         options.push( caption );
758                                                                 }
759                                                                 fileUse = options.length === 0 ? fileName : ( fileName + '|' + options.join( '|' ) );
760                                                                 $( this ).dialog( 'close' );
761                                                                 $.wikiEditor.modules.toolbar.fn.doAction(
762                                                                         $( this ).data( 'context' ),
763                                                                         {
764                                                                                 type: 'replace',
765                                                                                 options: {
766                                                                                         pre: '[[',
767                                                                                         peri: fileUse,
768                                                                                         post: ']]',
769                                                                                         ownline: true
770                                                                                 }
771                                                                         },
772                                                                         $( this )
773                                                                 );
774
775                                                                 // Restore form state
776                                                                 $( [ '#wikieditor-toolbar-file-target',
777                                                                         '#wikieditor-toolbar-file-caption',
778                                                                         '#wikieditor-toolbar-file-size' ].join( ',' )
779                                                                 ).val( '' );
780                                                                 $( '#wikieditor-toolbar-file-float' ).val( 'default' );
781                                                                 $( '#wikieditor-toolbar-file-format' ).val( magicWordsI18N.img_thumbnail );
782                                                         },
783                                                         'wikieditor-toolbar-tool-file-cancel': function () {
784                                                                 $( this ).dialog( 'close' );
785                                                         },
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
790                                                                         } );
791
792                                                                 $( this ).dialog( 'close' );
793                                                                 $( 'body' ).append( windowManager.$element );
794                                                                 windowManager.addWindows( [ uploadDialog ] );
795                                                                 windowManager.openWindow( uploadDialog );
796
797                                                                 uploadDialog.uploadBooklet.on( 'fileSaved', function ( imageInfo ) {
798                                                                         uploadDialog.close();
799                                                                         windowManager.$element.remove();
800
801                                                                         $.wikiEditor.modules.dialogs.api.openDialog( this, 'insert-file' );
802                                                                         $( '#wikieditor-toolbar-file-target' ).val( imageInfo.canonicaltitle );
803                                                                 } );
804                                                         }
805                                                 },
806                                                 open: function () {
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 ) {
813                                                                         var button;
814                                                                         if ( e.which === 13 ) {
815                                                                                 button = $( this ).data( 'dialogaction' ) ||
816                                                                                         $( this ).find( 'button:first' );
817                                                                                 button.click();
818                                                                                 e.preventDefault();
819                                                                         }
820                                                                 } );
821
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 );
826                                                                 } );
827                                                         }
828                                                 }
829                                         }
830                                 },
831                                 'insert-table': {
832                                         titleMsg: 'wikieditor-toolbar-tool-table-title',
833                                         id: 'wikieditor-toolbar-table-dialog',
834                                         htmlTemplate: 'dialogInsertTable.html',
835                                         init: function () {
836                                                 $( this ).find( '[rel]' ).each( function () {
837                                                         $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
838                                                 } );
839                                                 // Set tabindexes on form fields
840                                                 $.wikiEditor.modules.dialogs.fn.setTabindexes( $( this ).find( 'input' ).not( '[tabindex]' ) );
841
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' );
846                                                 } );
847
848                                                 // Hack for sortable preview: dynamically adding
849                                                 // sortable class doesn't work, so we use a clone
850                                                 $( '#wikieditor-toolbar-table-preview' )
851                                                         .clone()
852                                                         .attr( 'id', 'wikieditor-toolbar-table-preview2' )
853                                                         .addClass( 'sortable' )
854                                                         .insertAfter( $( '#wikieditor-toolbar-table-preview' ) )
855                                                         .hide();
856
857                                                 mw.loader.using( 'jquery.tablesorter', function () {
858                                                         $( '#wikieditor-toolbar-table-preview2' ).tablesorter();
859                                                 } );
860
861                                                 $( '#wikieditor-toolbar-table-sortable' ).click( function () {
862                                                         // Swap the currently shown one clone with the other one
863                                                         $( '#wikieditor-toolbar-table-preview' )
864                                                                 .hide()
865                                                                 .attr( 'id', 'wikieditor-toolbar-table-preview3' );
866                                                         $( '#wikieditor-toolbar-table-preview2' )
867                                                                 .attr( 'id', 'wikieditor-toolbar-table-preview' )
868                                                                 .show();
869                                                         $( '#wikieditor-toolbar-table-preview3' ).attr( 'id', 'wikieditor-toolbar-table-preview2' );
870                                                 } );
871
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,
875                                                         // visible or not
876                                                         var $sortable,
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();
885                                                         } );
886                                                 } );
887                                         },
888                                         dialog: {
889                                                 resizable: false,
890                                                 dialogClass: 'wikiEditor-toolbar-dialog',
891                                                 width: 590,
892                                                 buttons: {
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' ) );
904                                                                         return;
905                                                                 }
906                                                                 if ( rows + header === 0 || cols === 0 ) {
907                                                                         // eslint-disable-next-line no-alert
908                                                                         alert( mw.msg( 'wikieditor-toolbar-tool-table-zero' ) );
909                                                                         return;
910                                                                 }
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 ) );
915                                                                         return;
916                                                                 }
917                                                                 headerText = mw.msg( 'wikieditor-toolbar-tool-table-example-header' );
918                                                                 normalText = mw.msg( 'wikieditor-toolbar-tool-table-example' );
919                                                                 table = '';
920                                                                 for ( r = 0; r < rows + header; r++ ) {
921                                                                         table += '|-\n';
922                                                                         for ( c = 0; c < cols; c++ ) {
923                                                                                 isHeader = ( header && r === 0 );
924                                                                                 delim = isHeader ? '!' : '|';
925                                                                                 if ( c > 0 ) {
926                                                                                         delim += delim;
927                                                                                 }
928                                                                                 table += delim + ' ' + ( isHeader ? headerText : normalText ) + ' ';
929                                                                         }
930                                                                         // Replace trailing space by newline
931                                                                         // table[table.length - 1] is read-only
932                                                                         table = table.substr( 0, table.length - 1 ) + '\n';
933                                                                 }
934                                                                 classes = [];
935                                                                 if ( $( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
936                                                                         classes.push( 'wikitable' );
937                                                                 }
938                                                                 if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
939                                                                         classes.push( 'sortable' );
940                                                                 }
941                                                                 classStr = classes.length > 0 ? ' class="' + classes.join( ' ' ) + '"' : '';
942                                                                 $( this ).dialog( 'close' );
943                                                                 $.wikiEditor.modules.toolbar.fn.doAction(
944                                                                         $( this ).data( 'context' ),
945                                                                         {
946                                                                                 type: 'replace',
947                                                                                 options: {
948                                                                                         pre: '{|' + classStr + '\n',
949                                                                                         peri: table,
950                                                                                         post: '|}',
951                                                                                         ownline: true
952                                                                                 }
953                                                                         },
954                                                                         $( this )
955                                                                 );
956
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();
964                                                                 }
965                                                                 if ( !$( '#wikieditor-toolbar-table-wikitable' ).is( ':checked' ) ) {
966                                                                         $( '#wikieditor-toolbar-table-wikitable' ).click();
967                                                                 }
968                                                                 if ( $( '#wikieditor-toolbar-table-sortable' ).is( ':checked' ) ) {
969                                                                         $( '#wikieditor-toolbar-table-sortable' ).click();
970                                                                 }
971                                                         },
972                                                         'wikieditor-toolbar-tool-table-cancel': function () {
973                                                                 $( this ).dialog( 'close' );
974                                                         }
975                                                 },
976                                                 open: function () {
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 ) {
983                                                                         var button;
984                                                                         if ( ( e.keyCode || e.which ) === 13 ) {
985                                                                                 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
986                                                                                 button.click();
987                                                                                 e.preventDefault();
988                                                                         }
989                                                                 } );
990
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 );
995                                                                 } );
996                                                         }
997                                                 }
998                                         }
999                                 },
1000                                 'search-and-replace': {
1001                                         browsers: {
1002                                                 // Left-to-right languages
1003                                                 ltr: {
1004                                                         msie: [ [ '>=', 11 ] ] // Known to work on 11.
1005                                                 },
1006                                                 // Right-to-left languages
1007                                                 rtl: {
1008                                                         msie: [ [ '>=', 11 ] ] // Works on 11 but dialog positioning is cruddy.
1009                                                 }
1010                                         },
1011                                         titleMsg: 'wikieditor-toolbar-tool-replace-title',
1012                                         id: 'wikieditor-toolbar-replace-dialog',
1013                                         htmlTemplate: 'dialogReplace.html',
1014                                         init: function () {
1015                                                 $( this ).find( '[rel]' ).each( function () {
1016                                                         $( this ).text( mw.msg( $( this ).attr( 'rel' ) ) );
1017                                                 } );
1018                                                 // Set tabindexes on form fields
1019                                                 $.wikiEditor.modules.dialogs.fn.setTabindexes( $( this ).find( 'input' ).not( '[tabindex]' ) );
1020
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,
1027                                                                 actualReplacement,
1028                                                                 start, end;
1029
1030                                                         $( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
1031
1032                                                         // Search string cannot be empty
1033                                                         searchStr = $( '#wikieditor-toolbar-replace-search' ).val();
1034                                                         if ( searchStr === '' ) {
1035                                                                 $( '#wikieditor-toolbar-replace-emptysearch' ).show();
1036                                                                 return;
1037                                                         }
1038
1039                                                         // Replace string can be empty
1040                                                         replaceStr = $( '#wikieditor-toolbar-replace-replace' ).val();
1041
1042                                                         // Prepare the regular expression flags
1043                                                         flags = 'm';
1044                                                         matchCase = $( '#wikieditor-toolbar-replace-case' ).is( ':checked' );
1045                                                         if ( !matchCase ) {
1046                                                                 flags += 'i';
1047                                                         }
1048                                                         isRegex = $( '#wikieditor-toolbar-replace-regex' ).is( ':checked' );
1049                                                         if ( !isRegex ) {
1050                                                                 searchStr = mw.RegExp.escape( searchStr );
1051                                                         }
1052                                                         if ( mode === 'replaceAll' ) {
1053                                                                 flags += 'g';
1054                                                         }
1055
1056                                                         try {
1057                                                                 regex = new RegExp( searchStr, flags );
1058                                                         } catch ( e ) {
1059                                                                 $( '#wikieditor-toolbar-replace-invalidregex' )
1060                                                                         .text( mw.msg( 'wikieditor-toolbar-tool-replace-invalidregex',
1061                                                                                 e.message ) )
1062                                                                         .show();
1063                                                                 return;
1064                                                         }
1065
1066                                                         $textarea = $( this ).data( 'context' ).$textarea;
1067                                                         text = $textarea.textSelection( 'getContents' );
1068                                                         match = false;
1069                                                         if ( mode !== 'replaceAll' ) {
1070                                                                 if ( mode === 'replace' ) {
1071                                                                         offset = $( this ).data( 'matchIndex' );
1072                                                                 } else {
1073                                                                         offset = $( this ).data( 'offset' );
1074                                                                 }
1075                                                                 textRemainder = text.substr( offset );
1076                                                                 match = textRemainder.match( regex );
1077                                                         }
1078                                                         if ( !match ) {
1079                                                                 // Search hit BOTTOM, continuing at TOP
1080                                                                 // TODO: Add a "Wrap around" option.
1081                                                                 offset = 0;
1082                                                                 textRemainder = text;
1083                                                                 match = textRemainder.match( regex );
1084                                                         }
1085
1086                                                         if ( !match ) {
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
1099                                                                                 break;
1100                                                                         }
1101                                                                         matchedText = textRemainder.substr( index, match[ i ].length );
1102                                                                         textRemainder = textRemainder.substr( index + match[ i ].length );
1103
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;
1109                                                                         $textarea
1110                                                                                 .textSelection( 'setSelection', { start: start, end: end } )
1111                                                                                 .textSelection( 'encapsulateSelection', {
1112                                                                                         peri: replace,
1113                                                                                         replace: true } )
1114                                                                                 .textSelection( 'setSelection', { start: start, end: newEnd } );
1115                                                                         offset = newEnd;
1116                                                                 }
1117                                                                 $( '#wikieditor-toolbar-replace-success' )
1118                                                                         .text( mw.msg( 'wikieditor-toolbar-tool-replace-success', match.length ) )
1119                                                                         .show();
1120                                                                 $( this ).data( 'offset', 0 );
1121                                                         } else {
1122
1123                                                                 if ( mode === 'replace' ) {
1124
1125                                                                         if ( isRegex ) {
1126                                                                                 // If backreferences (like $1) are used, the actual actual replacement string will be different
1127                                                                                 actualReplacement = match[ 0 ].replace( regex, replaceStr );
1128                                                                         } else {
1129                                                                                 actualReplacement = replaceStr;
1130                                                                         }
1131
1132                                                                         if ( match ) {
1133                                                                                 // Do the replacement
1134                                                                                 $textarea.textSelection( 'encapsulateSelection', {
1135                                                                                         peri: actualReplacement,
1136                                                                                         replace: true } );
1137                                                                                 // Reload the text after replacement
1138                                                                                 text = $textarea.textSelection( 'getContents' );
1139                                                                         }
1140
1141                                                                         // Find the next instance
1142                                                                         offset = offset + match[ 0 ].length + actualReplacement.length;
1143                                                                         textRemainder = text.substr( offset );
1144                                                                         match = textRemainder.match( regex );
1145
1146                                                                         if ( match ) {
1147                                                                                 start = offset + match.index;
1148                                                                                 end = start + match[ 0 ].length;
1149                                                                         } else {
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 );
1154                                                                                 if ( match ) {
1155                                                                                         start = match.index;
1156                                                                                         end = start + match[ 0 ].length;
1157                                                                                 } else {
1158                                                                                         // Give up
1159                                                                                         start = 0;
1160                                                                                         end = 0;
1161                                                                                 }
1162                                                                         }
1163                                                                 } else {
1164                                                                         start = offset + match.index;
1165                                                                         end = start + match[ 0 ].length;
1166                                                                 }
1167
1168                                                                 $( this ).data( 'matchIndex', start );
1169
1170                                                                 $textarea.textSelection( 'setSelection', {
1171                                                                         start: start,
1172                                                                         end: end } );
1173                                                                 $textarea.textSelection( 'scrollToCaretPosition' );
1174                                                                 $( this ).data( 'offset', end );
1175                                                                 $textarea[ 0 ].focus();
1176                                                         }
1177                                                 } );
1178                                         },
1179                                         dialog: {
1180                                                 width: 500,
1181                                                 dialogClass: 'wikiEditor-toolbar-dialog',
1182                                                 modal: false,
1183                                                 buttons: {
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' );
1187                                                         },
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' );
1191                                                         },
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' );
1195                                                         },
1196                                                         'wikieditor-toolbar-tool-replace-close': function () {
1197                                                                 $( this ).dialog( 'close' );
1198                                                         }
1199                                                 },
1200                                                 open: function () {
1201                                                         var dialog, context, textbox,
1202                                                                 that = this;
1203                                                         $( this ).data( 'offset', 0 );
1204                                                         $( this ).data( 'matchIndex', 0 );
1205
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 ) {
1213                                                                         var button;
1214                                                                         if ( ( e.keyCode || e.which ) === 13 ) {
1215                                                                                 button = $( this ).data( 'dialogaction' ) || $( this ).find( 'button:first' );
1216                                                                                 button.click();
1217                                                                                 e.preventDefault();
1218                                                                         }
1219                                                                 } );
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 );
1224                                                                 } );
1225                                                         }
1226                                                         dialog = $( this ).closest( '.ui-dialog' );
1227                                                         that = this;
1228                                                         context = $( this ).data( 'context' );
1229                                                         textbox = context.$textarea;
1230
1231                                                         $( textbox )
1232                                                                 .on( 'keypress.srdialog', function ( e ) {
1233                                                                         var button;
1234                                                                         if ( e.which === 13 ) {
1235                                                                                 // Enter
1236                                                                                 button = dialog.data( 'dialogaction' ) || dialog.find( 'button:first' );
1237                                                                                 button.click();
1238                                                                                 e.preventDefault();
1239                                                                         } else if ( e.which === 27 ) {
1240                                                                                 // Escape
1241                                                                                 $( that ).dialog( 'close' );
1242                                                                         }
1243                                                                 } );
1244                                                 },
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 );
1250                                                 }
1251                                         }
1252                                 }
1253                         } };
1254                 }
1255
1256         };
1257
1258 }( jQuery, mediaWiki, OO ) );