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