JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.2
[ckeditor.git] / _source / plugins / find / dialogs / find.js
index bc9f8a4..d845166 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -71,8 +71,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                // Style object for highlights: (#5018)\r
                // 1. Defined as full match style to avoid compromising ordinary text color styles.\r
                // 2. Must be apply onto inner-most text to avoid conflicting with ordinary text color styles visually.\r
-               var highlightStyle = new CKEDITOR.style( CKEDITOR.tools.extend( { fullMatch : true, childRule : function(){ return false; } },\r
-                       editor.config.find_highlight ) );\r
+               var highlightStyle = new CKEDITOR.style(\r
+                       CKEDITOR.tools.extend( { attributes : { 'data-cke-highlight': 1 }, fullMatch : 1, ignoreReadonly : 1, childRule : function(){ return 0; } },\r
+                       editor.config.find_highlight, true ) );\r
 \r
                /**\r
                 * Iterator which walk through the specified range char by char. By\r
@@ -83,11 +84,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 */\r
                var characterWalker = function( range , matchWord )\r
                {\r
+                       var self = this;\r
                        var walker =\r
                                new CKEDITOR.dom.walker( range );\r
-                       walker.guard = matchWord ? nonCharactersBoundary : null;\r
+                       walker.guard = matchWord ? nonCharactersBoundary : function( node )\r
+                       {\r
+                               !nonCharactersBoundary( node ) && ( self._.matchBoundary = true );\r
+                       };\r
                        walker[ 'evaluator' ] = findEvaluator;\r
-                       walker.breakOnFalse = true;\r
+                       walker.breakOnFalse = 1;\r
+\r
+                       if ( range.startContainer.type == CKEDITOR.NODE_TEXT )\r
+                       {\r
+                               this.textNode = range.startContainer;\r
+                               this.offset = range.startOffset - 1;\r
+                       }\r
 \r
                        this._ = {\r
                                matchWord : matchWord,\r
@@ -111,7 +122,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                var currentTextNode = this.textNode;\r
                                // Already at the end of document, no more character available.\r
-                               if (  currentTextNode === null )\r
+                               if ( currentTextNode === null )\r
                                        return cursorStep.call( this );\r
 \r
                                this._.matchBoundary = false;\r
@@ -142,14 +153,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                // Stop searching if we're need full word match OR\r
                                                // already reach document end.\r
                                                if ( this._.matchWord && !currentTextNode\r
-                                                        ||this._.walker._.end )\r
+                                                        || this._.walker._.end )\r
                                                        break;\r
-\r
-                                               // Marking as match character boundaries.\r
-                                               if ( !currentTextNode\r
-                                                  && !nonCharactersBoundary( this._.walker.current ) )\r
-                                                       this._.matchBoundary = true;\r
-\r
                                        }\r
                                        // Found a fresh text node.\r
                                        this.textNode = currentTextNode;\r
@@ -175,7 +180,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                cursors : [],\r
                                rangeLength : rangeLength,\r
                                highlightRange : null,\r
-                               isMatched : false\r
+                               isMatched : 0\r
                        };\r
                };\r
 \r
@@ -511,7 +516,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // Re-run the finding once for cyclic.(#3517)\r
                                if ( matchCyclic && !cyclicRerun )\r
                                {\r
-                                       this.searchRange = getSearchRange( true );\r
+                                       this.searchRange = getSearchRange( 1 );\r
                                        this.matchRange = null;\r
                                        return arguments.callee.apply( this,\r
                                                Array.prototype.slice.call( arguments ).concat( [ true ] ) );\r
@@ -531,7 +536,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                isReplace = 1;\r
 \r
                                // Successiveness of current replace/find.\r
-                               var result = false;\r
+                               var result = 0;\r
 \r
                                // 1. Perform the replace when there's already a match here.\r
                                // 2. Otherwise perform the find but don't replace it immediately.\r
@@ -561,7 +566,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                this.matchRange.highlight();\r
                                        this.matchRange._.isReplaced = true;\r
                                        this.replaceCounter++;\r
-                                       result = true;\r
+                                       result = 1;\r
                                }\r
                                else\r
                                        result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );\r
@@ -595,17 +600,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return searchRange;\r
                }\r
 \r
+               var lang = editor.lang.findAndReplace;\r
                return {\r
-                       title : editor.lang.findAndReplace.title,\r
+                       title : lang.title,\r
                        resizable : CKEDITOR.DIALOG_RESIZE_NONE,\r
                        minWidth : 350,\r
-                       minHeight : 165,\r
-                       buttons : [ CKEDITOR.dialog.cancelButton ],             //Cancel button only.\r
+                       minHeight : 170,\r
+                       buttons : [ CKEDITOR.dialog.cancelButton ],             // Cancel button only.\r
                        contents : [\r
                                {\r
                                        id : 'find',\r
-                                       label : editor.lang.findAndReplace.find,\r
-                                       title : editor.lang.findAndReplace.find,\r
+                                       label : lang.find,\r
+                                       title : lang.find,\r
                                        accessKey : '',\r
                                        elements : [\r
                                                {\r
@@ -616,16 +622,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                {\r
                                                                        type : 'text',\r
                                                                        id : 'txtFindFind',\r
-                                                                       label : editor.lang.findAndReplace.findWhat,\r
+                                                                       label : lang.findWhat,\r
                                                                        isChanged : false,\r
                                                                        labelLayout : 'horizontal',\r
                                                                        accessKey : 'F'\r
                                                                },\r
                                                                {\r
                                                                        type : 'button',\r
+                                                                       id : 'btnFind',\r
                                                                        align : 'left',\r
                                                                        style : 'width:100%',\r
-                                                                       label : editor.lang.findAndReplace.find,\r
+                                                                       label : lang.find,\r
                                                                        onClick : function()\r
                                                                        {\r
                                                                                var dialog = this.getDialog();\r
@@ -633,36 +640,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        dialog.getValueOf( 'find', 'txtFindCaseChk' ),\r
                                                                                                        dialog.getValueOf( 'find', 'txtFindWordChk' ),\r
                                                                                                        dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )\r
-                                                                                       alert( editor.lang.findAndReplace\r
+                                                                                       alert( lang\r
                                                                                                .notFoundMsg );\r
                                                                        }\r
                                                                }\r
                                                        ]\r
                                                },\r
                                                {\r
-                                                       type : 'vbox',\r
-                                                       padding : 0,\r
+                                                       type : 'fieldset',\r
+                                                       label : CKEDITOR.tools.htmlEncode( lang.findOptions ),\r
+                                                       style : 'margin-top:29px',\r
                                                        children :\r
                                                        [\r
                                                                {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtFindCaseChk',\r
-                                                                       isChanged : false,\r
-                                                                       style : 'margin-top:28px',\r
-                                                                       label : editor.lang.findAndReplace.matchCase\r
-                                                               },\r
-                                                               {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtFindWordChk',\r
-                                                                       isChanged : false,\r
-                                                                       label : editor.lang.findAndReplace.matchWord\r
-                                                               },\r
-                                                               {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtFindCyclic',\r
-                                                                       isChanged : false,\r
-                                                                       'default' : true,\r
-                                                                       label : editor.lang.findAndReplace.matchCyclic\r
+                                                                       type : 'vbox',\r
+                                                                       padding : 0,\r
+                                                                       children :\r
+                                                                       [\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtFindCaseChk',\r
+                                                                                       isChanged : false,\r
+                                                                                       label : lang.matchCase\r
+                                                                               },\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtFindWordChk',\r
+                                                                                       isChanged : false,\r
+                                                                                       label : lang.matchWord\r
+                                                                               },\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtFindCyclic',\r
+                                                                                       isChanged : false,\r
+                                                                                       'default' : true,\r
+                                                                                       label : lang.matchCyclic\r
+                                                                               }\r
+                                                                       ]\r
                                                                }\r
                                                        ]\r
                                                }\r
@@ -670,7 +684,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                },\r
                                {\r
                                        id : 'replace',\r
-                                       label : editor.lang.findAndReplace.replace,\r
+                                       label : lang.replace,\r
                                        accessKey : 'M',\r
                                        elements : [\r
                                                {\r
@@ -681,16 +695,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                {\r
                                                                        type : 'text',\r
                                                                        id : 'txtFindReplace',\r
-                                                                       label : editor.lang.findAndReplace.findWhat,\r
+                                                                       label : lang.findWhat,\r
                                                                        isChanged : false,\r
                                                                        labelLayout : 'horizontal',\r
                                                                        accessKey : 'F'\r
                                                                },\r
                                                                {\r
                                                                        type : 'button',\r
+                                                                       id : 'btnFindReplace',\r
                                                                        align : 'left',\r
                                                                        style : 'width:100%',\r
-                                                                       label : editor.lang.findAndReplace.replace,\r
+                                                                       label : lang.replace,\r
                                                                        onClick : function()\r
                                                                        {\r
                                                                                var dialog = this.getDialog();\r
@@ -700,7 +715,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),\r
                                                                                                        dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),\r
                                                                                                        dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )\r
-                                                                                       alert( editor.lang.findAndReplace\r
+                                                                                       alert( lang\r
                                                                                                .notFoundMsg );\r
                                                                        }\r
                                                                }\r
@@ -714,16 +729,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                {\r
                                                                        type : 'text',\r
                                                                        id : 'txtReplace',\r
-                                                                       label : editor.lang.findAndReplace.replaceWith,\r
+                                                                       label : lang.replaceWith,\r
                                                                        isChanged : false,\r
                                                                        labelLayout : 'horizontal',\r
                                                                        accessKey : 'R'\r
                                                                },\r
                                                                {\r
                                                                        type : 'button',\r
+                                                                       id : 'btnReplaceAll',\r
                                                                        align : 'left',\r
                                                                        style : 'width:100%',\r
-                                                                       label : editor.lang.findAndReplace.replaceAll,\r
+                                                                       label : lang.replaceAll,\r
                                                                        isChanged : false,\r
                                                                        onClick : function()\r
                                                                        {\r
@@ -733,7 +749,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                finder.replaceCounter = 0;\r
 \r
                                                                                // Scope to full document.\r
-                                                                               finder.searchRange = getSearchRange( true );\r
+                                                                               finder.searchRange = getSearchRange( 1 );\r
                                                                                if ( finder.matchRange )\r
                                                                                {\r
                                                                                        finder.matchRange.removeHighlight();\r
@@ -750,41 +766,45 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                                if ( finder.replaceCounter )\r
                                                                                {\r
-                                                                                       alert( editor.lang.findAndReplace.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );\r
+                                                                                       alert( lang.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );\r
                                                                                        editor.fire( 'saveSnapshot' );\r
                                                                                }\r
                                                                                else\r
-                                                                                       alert( editor.lang.findAndReplace.notFoundMsg );\r
+                                                                                       alert( lang.notFoundMsg );\r
                                                                        }\r
                                                                }\r
                                                        ]\r
                                                },\r
                                                {\r
-                                                       type : 'vbox',\r
-                                                       padding : 0,\r
+                                                       type : 'fieldset',\r
+                                                       label : CKEDITOR.tools.htmlEncode( lang.findOptions ),\r
                                                        children :\r
                                                        [\r
                                                                {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtReplaceCaseChk',\r
-                                                                       isChanged : false,\r
-                                                                       label : editor.lang.findAndReplace\r
-                                                                               .matchCase\r
-                                                               },\r
-                                                               {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtReplaceWordChk',\r
-                                                                       isChanged : false,\r
-                                                                       label : editor.lang.findAndReplace\r
-                                                                               .matchWord\r
-                                                               },\r
-                                                               {\r
-                                                                       type : 'checkbox',\r
-                                                                       id : 'txtReplaceCyclic',\r
-                                                                       isChanged : false,\r
-                                                                       'default' : true,\r
-                                                                       label : editor.lang.findAndReplace\r
-                                                                               .matchCyclic\r
+                                                                       type : 'vbox',\r
+                                                                       padding : 0,\r
+                                                                       children :\r
+                                                                       [\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtReplaceCaseChk',\r
+                                                                                       isChanged : false,\r
+                                                                                       label : lang.matchCase\r
+                                                                               },\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtReplaceWordChk',\r
+                                                                                       isChanged : false,\r
+                                                                                       label : lang.matchWord\r
+                                                                               },\r
+                                                                               {\r
+                                                                                       type : 'checkbox',\r
+                                                                                       id : 'txtReplaceCyclic',\r
+                                                                                       isChanged : false,\r
+                                                                                       'default' : true,\r
+                                                                                       label : lang.matchCyclic\r
+                                                                               }\r
+                                                                       ]\r
                                                                }\r
                                                        ]\r
                                                }\r
@@ -795,19 +815,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                var dialog = this;\r
 \r
-                               //keep track of the current pattern field in use.\r
+                               // Keep track of the current pattern field in use.\r
                                var patternField, wholeWordChkField;\r
 \r
-                               //Ignore initial page select on dialog show\r
-                               var isUserSelect = false;\r
-                               this.on('hide', function()\r
+                               // Ignore initial page select on dialog show\r
+                               var isUserSelect = 0;\r
+                               this.on( 'hide', function()\r
                                                {\r
-                                                       isUserSelect = false;\r
-                                               } );\r
-                               this.on('show', function()\r
+                                                       isUserSelect = 0;\r
+                                               });\r
+                               this.on( 'show', function()\r
                                                {\r
-                                                       isUserSelect = true;\r
-                                               } );\r
+                                                       isUserSelect = 1;\r
+                                               });\r
 \r
                                this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )\r
                                        {\r
@@ -825,7 +845,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        wholeWordChkField = dialog.getContentElement( pageId,\r
                                                                wholeWordChkFieldId );\r
 \r
-                                                       // prepare for check pattern text filed 'keyup' event\r
+                                                       // Prepare for check pattern text filed 'keyup' event\r
                                                        if ( !currPage.initialized )\r
                                                        {\r
                                                                patternFieldInput = CKEDITOR.document\r
@@ -833,8 +853,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                currPage.initialized = true;\r
                                                        }\r
 \r
+                                                       // Synchronize fields on tab switch.\r
                                                        if ( isUserSelect )\r
-                                                               // synchronize fields on tab switch.\r
                                                                syncFieldsBetweenTabs.call( this, pageId );\r
                                                };\r
                                        } );\r
@@ -845,7 +865,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // Establish initial searching start position.\r
                                finder.searchRange = getSearchRange();\r
 \r
+                               // Fill in the find field with selected text.\r
+                               var selectedText = this.getParentEditor().getSelection().getSelectedText(),\r
+                                       patternFieldId = ( startupPage == 'find' ? 'txtFindFind' : 'txtFindReplace' );\r
+\r
+                               var field = this.getContentElement( startupPage, patternFieldId );\r
+                               field.setValue( selectedText );\r
+                               field.select();\r
+\r
                                this.selectPage( startupPage );\r
+\r
+                               this[ ( startupPage == 'find' && this._.editor.readOnly? 'hide' : 'show' ) + 'Page' ]( 'replace');\r
                        },\r
                        onHide : function()\r
                        {\r