JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / find / dialogs / find.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         function guardDomWalkerNonEmptyTextNode( node )\r
9         {\r
10                 return ( node.type == CKEDITOR.NODE_TEXT && node.getLength() > 0 );\r
11         }\r
12 \r
13         /**\r
14          * Elements which break characters been considered as sequence.\r
15         */\r
16         function checkCharactersBoundary ( node )\r
17         {\r
18                 var dtd = CKEDITOR.dtd;\r
19                 return node.isBlockBoundary(\r
20                         CKEDITOR.tools.extend( {}, dtd.$empty, dtd.$nonEditable ) );\r
21         }\r
22 \r
23         /**\r
24          * Get the cursor object which represent both current character and it's dom\r
25          * position thing.\r
26          */\r
27         var cursorStep = function()\r
28         {\r
29                 return {\r
30                         textNode : this.textNode,\r
31                         offset : this.offset,\r
32                         character : this.textNode ?\r
33                                 this.textNode.getText().charAt( this.offset ) : null,\r
34                         hitMatchBoundary : this._.matchBoundary\r
35                 };\r
36         };\r
37 \r
38         var pages = [ 'find', 'replace' ],\r
39                 fieldsMapping = [\r
40                 [ 'txtFindFind', 'txtFindReplace' ],\r
41                 [ 'txtFindCaseChk', 'txtReplaceCaseChk' ],\r
42                 [ 'txtFindWordChk', 'txtReplaceWordChk' ],\r
43                 [ 'txtFindCyclic', 'txtReplaceCyclic' ] ];\r
44 \r
45         /**\r
46          * Synchronize corresponding filed values between 'replace' and 'find' pages.\r
47          * @param {String} currentPageId        The page id which receive values.\r
48          */\r
49         function syncFieldsBetweenTabs( currentPageId )\r
50         {\r
51                 var sourceIndex, targetIndex,\r
52                         sourceField, targetField;\r
53 \r
54                 sourceIndex = currentPageId === 'find' ? 1 : 0;\r
55                 targetIndex = 1 - sourceIndex;\r
56                 var i, l = fieldsMapping.length;\r
57                 for ( i = 0 ; i < l ; i++ )\r
58                 {\r
59                         sourceField = this.getContentElement( pages[ sourceIndex ],\r
60                                         fieldsMapping[ i ][ sourceIndex ] );\r
61                         targetField = this.getContentElement( pages[ targetIndex ],\r
62                                         fieldsMapping[ i ][ targetIndex ] );\r
63 \r
64                         targetField.setValue( sourceField.getValue() );\r
65                 }\r
66         }\r
67 \r
68         var findDialog = function( editor, startupPage )\r
69         {\r
70                 // Style object for highlights.\r
71                 var highlightStyle = new CKEDITOR.style( editor.config.find_highlight );\r
72 \r
73                 /**\r
74                  * Iterator which walk through the specified range char by char. By\r
75                  * default the walking will not stop at the character boundaries, until\r
76                  * the end of the range is encountered.\r
77                  * @param { CKEDITOR.dom.range } range\r
78                  * @param {Boolean} matchWord Whether the walking will stop at character boundary.\r
79                  */\r
80                 var characterWalker = function( range , matchWord )\r
81                 {\r
82                         var walker =\r
83                                 new CKEDITOR.dom.walker( range );\r
84                         walker[ matchWord ? 'guard' : 'evaluator' ] =\r
85                                 guardDomWalkerNonEmptyTextNode;\r
86                         walker.breakOnFalse = true;\r
87 \r
88                         this._ = {\r
89                                 matchWord : matchWord,\r
90                                 walker : walker,\r
91                                 matchBoundary : false\r
92                         };\r
93                 };\r
94 \r
95                 characterWalker.prototype = {\r
96                         next : function()\r
97                         {\r
98                                 return this.move();\r
99                         },\r
100 \r
101                         back : function()\r
102                         {\r
103                                 return this.move( true );\r
104                         },\r
105 \r
106                         move : function( rtl )\r
107                         {\r
108                                 var currentTextNode = this.textNode;\r
109                                 // Already at the end of document, no more character available.\r
110                                 if(  currentTextNode === null )\r
111                                         return cursorStep.call( this );\r
112 \r
113                                 this._.matchBoundary = false;\r
114 \r
115                                 // There are more characters in the text node, step forward.\r
116                                 if( currentTextNode\r
117                                     && rtl\r
118                                         && this.offset > 0 )\r
119                                 {\r
120                                         this.offset--;\r
121                                         return cursorStep.call( this );\r
122                                 }\r
123                                 else if( currentTextNode\r
124                                         && this.offset < currentTextNode.getLength() - 1 )\r
125                                 {\r
126                                         this.offset++;\r
127                                         return cursorStep.call( this );\r
128                                 }\r
129                                 else\r
130                                 {\r
131                                         currentTextNode = null;\r
132                                         // At the end of the text node, walking foward for the next.\r
133                                         while ( !currentTextNode )\r
134                                         {\r
135                                                 currentTextNode =\r
136                                                         this._.walker[ rtl ? 'previous' : 'next' ].call( this._.walker );\r
137 \r
138                                                 // Stop searching if we're need full word match OR\r
139                                                 // already reach document end.\r
140                                                 if ( this._.matchWord && !currentTextNode\r
141                                                          ||this._.walker._.end )\r
142                                                         break;\r
143 \r
144                                                 // Marking as match character boundaries.\r
145                                                 if( !currentTextNode\r
146                                                    && checkCharactersBoundary( this._.walker.current ) )\r
147                                                         this._.matchBoundary = true;\r
148 \r
149                                         }\r
150                                         // Found a fresh text node.\r
151                                         this.textNode = currentTextNode;\r
152                                         if ( currentTextNode )\r
153                                                 this.offset = rtl ? currentTextNode.getLength() - 1 : 0;\r
154                                         else\r
155                                                 this.offset = 0;\r
156                                 }\r
157 \r
158                                 return cursorStep.call( this );\r
159                         }\r
160 \r
161                 };\r
162 \r
163                 /**\r
164                  * A range of cursors which represent a trunk of characters which try to\r
165                  * match, it has the same length as the pattern  string.\r
166                  */\r
167                 var characterRange = function( characterWalker, rangeLength )\r
168                 {\r
169                         this._ = {\r
170                                 walker : characterWalker,\r
171                                 cursors : [],\r
172                                 rangeLength : rangeLength,\r
173                                 highlightRange : null,\r
174                                 isMatched : false\r
175                         };\r
176                 };\r
177 \r
178                 characterRange.prototype = {\r
179                         /**\r
180                          * Translate this range to {@link CKEDITOR.dom.range}\r
181                          */\r
182                         toDomRange : function()\r
183                         {\r
184                                 var cursors = this._.cursors;\r
185                                 if ( cursors.length < 1 )\r
186                                         return null;\r
187 \r
188                                 var first = cursors[0],\r
189                                         last = cursors[ cursors.length - 1 ],\r
190                                         range = new CKEDITOR.dom.range( editor.document );\r
191 \r
192                                 range.setStart( first.textNode, first.offset );\r
193                                 range.setEnd( last.textNode, last.offset + 1 );\r
194                                 return range;\r
195                         },\r
196                         /**\r
197                          * Reflect the latest changes from dom range.\r
198                          */\r
199                         updateFromDomRange : function( domRange )\r
200                         {\r
201                                 var cursor,\r
202                                                 walker = new characterWalker( domRange );\r
203                                 this._.cursors = [];\r
204                                 do\r
205                                 {\r
206                                         cursor = walker.next();\r
207                                         if ( cursor.character )\r
208                                                 this._.cursors.push( cursor );\r
209                                 }\r
210                                 while ( cursor.character );\r
211                                 this._.rangeLength = this._.cursors.length;\r
212                         },\r
213 \r
214                         setMatched : function()\r
215                         {\r
216                                 this._.isMatched = true;\r
217                         },\r
218 \r
219                         clearMatched : function()\r
220                         {\r
221                                 this._.isMatched = false;\r
222                         },\r
223 \r
224                         isMatched : function()\r
225                         {\r
226                                 return this._.isMatched;\r
227                         },\r
228 \r
229                         /**\r
230                          * Hightlight the current matched chunk of text.\r
231                          */\r
232                         highlight : function()\r
233                         {\r
234                                 // Do not apply if nothing is found.\r
235                                 if ( this._.cursors.length < 1 )\r
236                                         return;\r
237 \r
238                                 // Remove the previous highlight if there's one.\r
239                                 if ( this._.highlightRange )\r
240                                         this.removeHighlight();\r
241 \r
242                                 // Apply the highlight.\r
243                                 var range = this.toDomRange();\r
244                                 highlightStyle.applyToRange( range );\r
245                                 this._.highlightRange = range;\r
246 \r
247                                 // Scroll the editor to the highlighted area.\r
248                                 var element = range.startContainer;\r
249                                 if ( element.type != CKEDITOR.NODE_ELEMENT )\r
250                                         element = element.getParent();\r
251                                 element.scrollIntoView();\r
252 \r
253                                 // Update the character cursors.\r
254                                 this.updateFromDomRange( range );\r
255                         },\r
256 \r
257                         /**\r
258                          * Remove highlighted find result.\r
259                          */\r
260                         removeHighlight : function()\r
261                         {\r
262                                 if ( !this._.highlightRange )\r
263                                         return;\r
264 \r
265                                 highlightStyle.removeFromRange( this._.highlightRange );\r
266                                 this.updateFromDomRange( this._.highlightRange );\r
267                                 this._.highlightRange = null;\r
268                         },\r
269 \r
270                         moveBack : function()\r
271                         {\r
272                                 var retval = this._.walker.back(),\r
273                                         cursors = this._.cursors;\r
274 \r
275                                 if ( retval.hitMatchBoundary )\r
276                                         this._.cursors = cursors = [];\r
277 \r
278                                 cursors.unshift( retval );\r
279                                 if ( cursors.length > this._.rangeLength )\r
280                                         cursors.pop();\r
281 \r
282                                 return retval;\r
283                         },\r
284 \r
285                         moveNext : function()\r
286                         {\r
287                                 var retval = this._.walker.next(),\r
288                                         cursors = this._.cursors;\r
289 \r
290                                 // Clear the cursors queue if we've crossed a match boundary.\r
291                                 if ( retval.hitMatchBoundary )\r
292                                         this._.cursors = cursors = [];\r
293 \r
294                                 cursors.push( retval );\r
295                                 if ( cursors.length > this._.rangeLength )\r
296                                         cursors.shift();\r
297 \r
298                                 return retval;\r
299                         },\r
300 \r
301                         getEndCharacter : function()\r
302                         {\r
303                                 var cursors = this._.cursors;\r
304                                 if ( cursors.length < 1 )\r
305                                         return null;\r
306 \r
307                                 return cursors[ cursors.length - 1 ].character;\r
308                         },\r
309 \r
310                         getNextCharacterRange : function( maxLength )\r
311                         {\r
312                                 var lastCursor,\r
313                                                 cursors = this._.cursors;\r
314                                 if ( !( lastCursor = cursors[ cursors.length - 1 ] ) )\r
315                                         return null;\r
316                                 return new characterRange(\r
317                                                                                 new characterWalker(\r
318                                                                                         getRangeAfterCursor( lastCursor ) ),\r
319                                                                                 maxLength );\r
320                         },\r
321 \r
322                         getCursors : function()\r
323                         {\r
324                                 return this._.cursors;\r
325                         }\r
326                 };\r
327 \r
328 \r
329                 // The remaining document range after the character cursor.\r
330                 function getRangeAfterCursor( cursor , inclusive )\r
331                 {\r
332                         var range = new CKEDITOR.dom.range();\r
333                         range.setStart( cursor.textNode,\r
334                                                    ( inclusive ? cursor.offset : cursor.offset + 1 ) );\r
335                         range.setEndAt( editor.document.getBody(),\r
336                                                         CKEDITOR.POSITION_BEFORE_END );\r
337                         return range;\r
338                 }\r
339 \r
340                 // The document range before the character cursor.\r
341                 function getRangeBeforeCursor( cursor )\r
342                 {\r
343                         var range = new CKEDITOR.dom.range();\r
344                         range.setStartAt( editor.document.getBody(),\r
345                                                         CKEDITOR.POSITION_AFTER_START );\r
346                         range.setEnd( cursor.textNode, cursor.offset );\r
347                         return range;\r
348                 }\r
349 \r
350                 var KMP_NOMATCH = 0,\r
351                         KMP_ADVANCED = 1,\r
352                         KMP_MATCHED = 2;\r
353                 /**\r
354                  * Examination the occurrence of a word which implement KMP algorithm.\r
355                  */\r
356                 var kmpMatcher = function( pattern, ignoreCase )\r
357                 {\r
358                         var overlap = [ -1 ];\r
359                         if ( ignoreCase )\r
360                                 pattern = pattern.toLowerCase();\r
361                         for ( var i = 0 ; i < pattern.length ; i++ )\r
362                         {\r
363                                 overlap.push( overlap[i] + 1 );\r
364                                 while ( overlap[ i + 1 ] > 0\r
365                                         && pattern.charAt( i ) != pattern\r
366                                                 .charAt( overlap[ i + 1 ] - 1 ) )\r
367                                         overlap[ i + 1 ] = overlap[ overlap[ i + 1 ] - 1 ] + 1;\r
368                         }\r
369 \r
370                         this._ = {\r
371                                 overlap : overlap,\r
372                                 state : 0,\r
373                                 ignoreCase : !!ignoreCase,\r
374                                 pattern : pattern\r
375                         };\r
376                 };\r
377 \r
378                 kmpMatcher.prototype =\r
379                 {\r
380                         feedCharacter : function( c )\r
381                         {\r
382                                 if ( this._.ignoreCase )\r
383                                         c = c.toLowerCase();\r
384 \r
385                                 while ( true )\r
386                                 {\r
387                                         if ( c == this._.pattern.charAt( this._.state ) )\r
388                                         {\r
389                                                 this._.state++;\r
390                                                 if ( this._.state == this._.pattern.length )\r
391                                                 {\r
392                                                         this._.state = 0;\r
393                                                         return KMP_MATCHED;\r
394                                                 }\r
395                                                 return KMP_ADVANCED;\r
396                                         }\r
397                                         else if ( !this._.state )\r
398                                                 return KMP_NOMATCH;\r
399                                         else\r
400                                                 this._.state = this._.overlap[ this._.state ];\r
401                                 }\r
402 \r
403                                 return null;\r
404                         },\r
405 \r
406                         reset : function()\r
407                         {\r
408                                 this._.state = 0;\r
409                         }\r
410                 };\r
411 \r
412                 var wordSeparatorRegex =\r
413                 /[.,"'?!;: \u0085\u00a0\u1680\u280e\u2028\u2029\u202f\u205f\u3000]/;\r
414 \r
415                 var isWordSeparator = function( c )\r
416                 {\r
417                         if ( !c )\r
418                                 return true;\r
419                         var code = c.charCodeAt( 0 );\r
420                         return ( code >= 9 && code <= 0xd )\r
421                                 || ( code >= 0x2000 && code <= 0x200a )\r
422                                 || wordSeparatorRegex.test( c );\r
423                 };\r
424 \r
425                 var finder = {\r
426                         searchRange : null,\r
427                         matchRange : null,\r
428                         find : function( pattern, matchCase, matchWord, matchCyclic, highlightMatched, cyclicRerun )\r
429                         {\r
430                                 if( !this.matchRange )\r
431                                         this.matchRange =\r
432                                                 new characterRange(\r
433                                                         new characterWalker( this.searchRange ),\r
434                                                         pattern.length );\r
435                                 else\r
436                                 {\r
437                                         this.matchRange.removeHighlight();\r
438                                         this.matchRange = this.matchRange.getNextCharacterRange( pattern.length );\r
439                                 }\r
440 \r
441                                 var matcher = new kmpMatcher( pattern, !matchCase ),\r
442                                         matchState = KMP_NOMATCH,\r
443                                         character = '%';\r
444 \r
445                                 while ( character !== null )\r
446                                 {\r
447                                         this.matchRange.moveNext();\r
448                                         while ( ( character = this.matchRange.getEndCharacter() ) )\r
449                                         {\r
450                                                 matchState = matcher.feedCharacter( character );\r
451                                                 if ( matchState == KMP_MATCHED )\r
452                                                         break;\r
453                                                 if ( this.matchRange.moveNext().hitMatchBoundary )\r
454                                                         matcher.reset();\r
455                                         }\r
456 \r
457                                         if ( matchState == KMP_MATCHED )\r
458                                         {\r
459                                                 if ( matchWord )\r
460                                                 {\r
461                                                         var cursors = this.matchRange.getCursors(),\r
462                                                                 tail = cursors[ cursors.length - 1 ],\r
463                                                                 head = cursors[ 0 ];\r
464 \r
465                                                         var headWalker = new characterWalker( getRangeBeforeCursor( head ), true ),\r
466                                                                 tailWalker = new characterWalker( getRangeAfterCursor( tail ), true );\r
467 \r
468                                                         if ( ! ( isWordSeparator( headWalker.back().character )\r
469                                                                                 && isWordSeparator( tailWalker.next().character ) ) )\r
470                                                                 continue;\r
471                                                 }\r
472                                                 this.matchRange.setMatched();\r
473                                                 if ( highlightMatched !== false )\r
474                                                         this.matchRange.highlight();\r
475                                                 return true;\r
476                                         }\r
477                                 }\r
478 \r
479                                 this.matchRange.clearMatched();\r
480                                 this.matchRange.removeHighlight();\r
481                                 // Clear current session and restart with the default search\r
482                                 // range.\r
483                                 // Re-run the finding once for cyclic.(#3517)\r
484                                 if ( matchCyclic && !cyclicRerun )\r
485                                 {\r
486                                         this.searchRange = getSearchRange( true );\r
487                                         this.matchRange = null;\r
488                                         return arguments.callee.apply( this,\r
489                                                 Array.prototype.slice.call( arguments ).concat( [ true ] ) );\r
490                                 }\r
491 \r
492                                 return false;\r
493                         },\r
494 \r
495                         /**\r
496                          * Record how much replacement occurred toward one replacing.\r
497                          */\r
498                         replaceCounter : 0,\r
499 \r
500                         replace : function( dialog, pattern, newString, matchCase, matchWord,\r
501                                 matchCyclic , isReplaceAll )\r
502                         {\r
503                                 // Successiveness of current replace/find.\r
504                                 var result = false;\r
505 \r
506                                 // 1. Perform the replace when there's already a match here.\r
507                                 // 2. Otherwise perform the find but don't replace it immediately.\r
508                                 if ( this.matchRange && this.matchRange.isMatched()\r
509                                                 && !this.matchRange._.isReplaced )\r
510                                 {\r
511                                         // Turn off highlight for a while when saving snapshots.\r
512                                         this.matchRange.removeHighlight();\r
513                                         var domRange = this.matchRange.toDomRange();\r
514                                         var text = editor.document.createText( newString );\r
515                                         if ( !isReplaceAll )\r
516                                         {\r
517                                                 // Save undo snaps before and after the replacement.\r
518                                                 var selection = editor.getSelection();\r
519                                                 selection.selectRanges( [ domRange ] );\r
520                                                 editor.fire( 'saveSnapshot' );\r
521                                         }\r
522                                         domRange.deleteContents();\r
523                                         domRange.insertNode( text );\r
524                                         if ( !isReplaceAll )\r
525                                         {\r
526                                                 selection.selectRanges( [ domRange ] );\r
527                                                 editor.fire( 'saveSnapshot' );\r
528                                         }\r
529                                         this.matchRange.updateFromDomRange( domRange );\r
530                                         if ( !isReplaceAll )\r
531                                                 this.matchRange.highlight();\r
532                                         this.matchRange._.isReplaced = true;\r
533                                         this.replaceCounter++;\r
534                                         result = true;\r
535                                 }\r
536                                 else\r
537                                         result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );\r
538 \r
539                                 return result;\r
540                         }\r
541                 };\r
542 \r
543                 /**\r
544                  * The range in which find/replace happened, receive from user\r
545                  * selection prior.\r
546                  */\r
547                 function getSearchRange( isDefault )\r
548                 {\r
549                         var searchRange,\r
550                                 sel = editor.getSelection(),\r
551                                 body = editor.document.getBody();\r
552                         if ( sel && !isDefault )\r
553                         {\r
554                                 searchRange = sel.getRanges()[ 0 ].clone();\r
555                                 searchRange.collapse( true );\r
556                         }\r
557                         else\r
558                         {\r
559                                 searchRange = new CKEDITOR.dom.range();\r
560                                 searchRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
561                         }\r
562                         searchRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );\r
563                         return searchRange;\r
564                 }\r
565 \r
566                 return {\r
567                         title : editor.lang.findAndReplace.title,\r
568                         resizable : CKEDITOR.DIALOG_RESIZE_NONE,\r
569                         minWidth : 350,\r
570                         minHeight : 165,\r
571                         buttons : [ CKEDITOR.dialog.cancelButton ],             //Cancel button only.\r
572                         contents : [\r
573                                 {\r
574                                         id : 'find',\r
575                                         label : editor.lang.findAndReplace.find,\r
576                                         title : editor.lang.findAndReplace.find,\r
577                                         accessKey : '',\r
578                                         elements : [\r
579                                                 {\r
580                                                         type : 'hbox',\r
581                                                         widths : [ '230px', '90px' ],\r
582                                                         children :\r
583                                                         [\r
584                                                                 {\r
585                                                                         type : 'text',\r
586                                                                         id : 'txtFindFind',\r
587                                                                         label : editor.lang.findAndReplace.findWhat,\r
588                                                                         isChanged : false,\r
589                                                                         labelLayout : 'horizontal',\r
590                                                                         accessKey : 'F'\r
591                                                                 },\r
592                                                                 {\r
593                                                                         type : 'button',\r
594                                                                         align : 'left',\r
595                                                                         style : 'width:100%',\r
596                                                                         label : editor.lang.findAndReplace.find,\r
597                                                                         onClick : function()\r
598                                                                         {\r
599                                                                                 var dialog = this.getDialog();\r
600                                                                                 if ( !finder.find( dialog.getValueOf( 'find', 'txtFindFind' ),\r
601                                                                                                         dialog.getValueOf( 'find', 'txtFindCaseChk' ),\r
602                                                                                                         dialog.getValueOf( 'find', 'txtFindWordChk' ),\r
603                                                                                                         dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )\r
604                                                                                         alert( editor.lang.findAndReplace\r
605                                                                                                 .notFoundMsg );\r
606                                                                         }\r
607                                                                 }\r
608                                                         ]\r
609                                                 },\r
610                                                 {\r
611                                                         type : 'vbox',\r
612                                                         padding : 0,\r
613                                                         children :\r
614                                                         [\r
615                                                                 {\r
616                                                                         type : 'checkbox',\r
617                                                                         id : 'txtFindCaseChk',\r
618                                                                         isChanged : false,\r
619                                                                         style : 'margin-top:28px',\r
620                                                                         label : editor.lang.findAndReplace.matchCase\r
621                                                                 },\r
622                                                                 {\r
623                                                                         type : 'checkbox',\r
624                                                                         id : 'txtFindWordChk',\r
625                                                                         isChanged : false,\r
626                                                                         label : editor.lang.findAndReplace.matchWord\r
627                                                                 },\r
628                                                                 {\r
629                                                                         type : 'checkbox',\r
630                                                                         id : 'txtFindCyclic',\r
631                                                                         isChanged : false,\r
632                                                                         'default' : true,\r
633                                                                         label : editor.lang.findAndReplace.matchCyclic\r
634                                                                 }\r
635                                                         ]\r
636                                                 }\r
637                                         ]\r
638                                 },\r
639                                 {\r
640                                         id : 'replace',\r
641                                         label : editor.lang.findAndReplace.replace,\r
642                                         accessKey : 'M',\r
643                                         elements : [\r
644                                                 {\r
645                                                         type : 'hbox',\r
646                                                         widths : [ '230px', '90px' ],\r
647                                                         children :\r
648                                                         [\r
649                                                                 {\r
650                                                                         type : 'text',\r
651                                                                         id : 'txtFindReplace',\r
652                                                                         label : editor.lang.findAndReplace.findWhat,\r
653                                                                         isChanged : false,\r
654                                                                         labelLayout : 'horizontal',\r
655                                                                         accessKey : 'F'\r
656                                                                 },\r
657                                                                 {\r
658                                                                         type : 'button',\r
659                                                                         align : 'left',\r
660                                                                         style : 'width:100%',\r
661                                                                         label : editor.lang.findAndReplace.replace,\r
662                                                                         onClick : function()\r
663                                                                         {\r
664                                                                                 var dialog = this.getDialog();\r
665                                                                                 if ( !finder.replace( dialog,\r
666                                                                                                         dialog.getValueOf( 'replace', 'txtFindReplace' ),\r
667                                                                                                         dialog.getValueOf( 'replace', 'txtReplace' ),\r
668                                                                                                         dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),\r
669                                                                                                         dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),\r
670                                                                                                         dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )\r
671                                                                                         alert( editor.lang.findAndReplace\r
672                                                                                                 .notFoundMsg );\r
673                                                                         }\r
674                                                                 }\r
675                                                         ]\r
676                                                 },\r
677                                                 {\r
678                                                         type : 'hbox',\r
679                                                         widths : [ '230px', '90px' ],\r
680                                                         children :\r
681                                                         [\r
682                                                                 {\r
683                                                                         type : 'text',\r
684                                                                         id : 'txtReplace',\r
685                                                                         label : editor.lang.findAndReplace.replaceWith,\r
686                                                                         isChanged : false,\r
687                                                                         labelLayout : 'horizontal',\r
688                                                                         accessKey : 'R'\r
689                                                                 },\r
690                                                                 {\r
691                                                                         type : 'button',\r
692                                                                         align : 'left',\r
693                                                                         style : 'width:100%',\r
694                                                                         label : editor.lang.findAndReplace.replaceAll,\r
695                                                                         isChanged : false,\r
696                                                                         onClick : function()\r
697                                                                         {\r
698                                                                                 var dialog = this.getDialog();\r
699                                                                                 var replaceNums;\r
700 \r
701                                                                                 finder.replaceCounter = 0;\r
702 \r
703                                                                                 // Scope to full document.\r
704                                                                                 finder.searchRange = getSearchRange( true );\r
705                                                                                 if ( finder.matchRange )\r
706                                                                                 {\r
707                                                                                         finder.matchRange.removeHighlight();\r
708                                                                                         finder.matchRange = null;\r
709                                                                                 }\r
710                                                                                 editor.fire( 'saveSnapshot' );\r
711                                                                                 while( finder.replace( dialog,\r
712                                                                                         dialog.getValueOf( 'replace', 'txtFindReplace' ),\r
713                                                                                         dialog.getValueOf( 'replace', 'txtReplace' ),\r
714                                                                                         dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),\r
715                                                                                         dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),\r
716                                                                                         false, true ) )\r
717                                                                                 { /*jsl:pass*/ }\r
718 \r
719                                                                                 if ( finder.replaceCounter )\r
720                                                                                 {\r
721                                                                                         alert( editor.lang.findAndReplace.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );\r
722                                                                                         editor.fire( 'saveSnapshot' );\r
723                                                                                 }\r
724                                                                                 else\r
725                                                                                         alert( editor.lang.findAndReplace.notFoundMsg );\r
726                                                                         }\r
727                                                                 }\r
728                                                         ]\r
729                                                 },\r
730                                                 {\r
731                                                         type : 'vbox',\r
732                                                         padding : 0,\r
733                                                         children :\r
734                                                         [\r
735                                                                 {\r
736                                                                         type : 'checkbox',\r
737                                                                         id : 'txtReplaceCaseChk',\r
738                                                                         isChanged : false,\r
739                                                                         label : editor.lang.findAndReplace\r
740                                                                                 .matchCase\r
741                                                                 },\r
742                                                                 {\r
743                                                                         type : 'checkbox',\r
744                                                                         id : 'txtReplaceWordChk',\r
745                                                                         isChanged : false,\r
746                                                                         label : editor.lang.findAndReplace\r
747                                                                                 .matchWord\r
748                                                                 },\r
749                                                                 {\r
750                                                                         type : 'checkbox',\r
751                                                                         id : 'txtReplaceCyclic',\r
752                                                                         isChanged : false,\r
753                                                                         'default' : true,\r
754                                                                         label : editor.lang.findAndReplace\r
755                                                                                 .matchCyclic\r
756                                                                 }\r
757                                                         ]\r
758                                                 }\r
759                                         ]\r
760                                 }\r
761                         ],\r
762                         onLoad : function()\r
763                         {\r
764                                 var dialog = this;\r
765 \r
766                                 //keep track of the current pattern field in use.\r
767                                 var patternField, wholeWordChkField;\r
768 \r
769                                 //Ignore initial page select on dialog show\r
770                                 var isUserSelect = false;\r
771                                 this.on('hide', function()\r
772                                                 {\r
773                                                         isUserSelect = false;\r
774                                                 } );\r
775                                 this.on('show', function()\r
776                                                 {\r
777                                                         isUserSelect = true;\r
778                                                 } );\r
779 \r
780                                 this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )\r
781                                         {\r
782                                                 return function( pageId )\r
783                                                 {\r
784                                                         originalFunc.call( dialog, pageId );\r
785 \r
786                                                         var currPage = dialog._.tabs[ pageId ];\r
787                                                         var patternFieldInput, patternFieldId, wholeWordChkFieldId;\r
788                                                         patternFieldId = pageId === 'find' ? 'txtFindFind' : 'txtFindReplace';\r
789                                                         wholeWordChkFieldId = pageId === 'find' ? 'txtFindWordChk' : 'txtReplaceWordChk';\r
790 \r
791                                                         patternField = dialog.getContentElement( pageId,\r
792                                                                 patternFieldId );\r
793                                                         wholeWordChkField = dialog.getContentElement( pageId,\r
794                                                                 wholeWordChkFieldId );\r
795 \r
796                                                         // prepare for check pattern text filed 'keyup' event\r
797                                                         if ( !currPage.initialized )\r
798                                                         {\r
799                                                                 patternFieldInput = CKEDITOR.document\r
800                                                                         .getById( patternField._.inputId );\r
801                                                                 currPage.initialized = true;\r
802                                                         }\r
803 \r
804                                                         if( isUserSelect )\r
805                                                                 // synchronize fields on tab switch.\r
806                                                                 syncFieldsBetweenTabs.call( this, pageId );\r
807                                                 };\r
808                                         } );\r
809 \r
810                         },\r
811                         onShow : function()\r
812                         {\r
813                                 // Establish initial searching start position.\r
814                                 finder.searchRange = getSearchRange();\r
815 \r
816                                 if ( startupPage == 'replace' )\r
817                                         this.getContentElement( 'replace', 'txtFindReplace' ).focus();\r
818                                 else\r
819                                         this.getContentElement( 'find', 'txtFindFind' ).focus();\r
820                         },\r
821                         onHide : function()\r
822                         {\r
823                                 if ( finder.matchRange && finder.matchRange.isMatched() )\r
824                                 {\r
825                                         finder.matchRange.removeHighlight();\r
826                                         editor.focus();\r
827                                         editor.getSelection().selectRanges(\r
828                                                 [ finder.matchRange.toDomRange() ] );\r
829                                 }\r
830 \r
831                                 // Clear current session before dialog close\r
832                                 delete finder.matchRange;\r
833                         }\r
834                 };\r
835         };\r
836 \r
837         CKEDITOR.dialog.add( 'find', function( editor )\r
838                 {\r
839                         return findDialog( editor, 'find' );\r
840                 });\r
841 \r
842         CKEDITOR.dialog.add( 'replace', function( editor )\r
843                 {\r
844                         return findDialog( editor, 'replace' );\r
845                 });\r
846 })();\r