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