Debian Codegraph @

dojox/editor/plugins/FindReplace.js

raw symbols SCIP
1 define([
2 "dojo",
3 "dijit",
4 "dojox",
5 "dijit/_base/manager", // getUniqueId
6 "dijit/_base/popup",
7 "dijit/_Widget",
8 "dijit/_TemplatedMixin",
9 "dijit/_KeyNavContainer",
10 "dijit/_WidgetsInTemplateMixin",
11 "dijit/TooltipDialog",
12 "dijit/Toolbar",
13 "dijit/form/CheckBox",
14 "dijit/form/_TextBoxMixin", // selectInputText
15 "dijit/form/TextBox",
16 "dijit/_editor/_Plugin",
17 "dijit/form/Button",
18 "dijit/form/DropDownButton",
19 "dijit/form/ToggleButton",
20 "./ToolbarLineBreak",
21 "dojo/_base/connect",
22 "dojo/_base/declare",
23 "dojo/i18n",
24 "dojo/string",
25 "dojo/i18n!dojox/editor/plugins/nls/FindReplace"
26 ], function(dojo, dijit, dojox, manager, popup,
27 _Widget, _TemplatedMixin, _KeyNavContainer, _WidgetsInTemplateMixin, TooltipDialog,
28 Toolbar, CheckBox, _TextBoxMixin, TextBox, _Plugin) {
29
30 dojo.experimental("dojox.editor.plugins.FindReplace");
31
32 var FindReplaceCloseBox = dojo.declare("dojox.editor.plugins._FindReplaceCloseBox",
33 [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
34 // summary:
35 // Base class for widgets that contains a button labeled X
36 // to close the tool bar.
37
38 btnId: "",
39 widget: null,
40 widgetsInTemplate: true,
41
42 templateString:
43 "<span style='float: right' class='dijitInline' tabindex='-1'>" +
44 "<button class='dijit dijitReset dijitInline' " +
45 "id='${btnId}' dojoAttachPoint='button' dojoType='dijit.form.Button' tabindex='-1' iconClass='dijitEditorIconsFindReplaceClose' showLabel='false'>X</button>" +
46 "</span>",
47
48 postMixInProperties: function(){
49 // Set some substitution variables used in the template
50 this.id = dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
51 this.btnId = this.id + "_close";
52 this.inherited(arguments);
53 },
54 startup: function(){
55 this.connect(this.button, "onClick", "onClick");
56 },
57 onClick: function(){
58 }
59 });
60
61
62 var FindReplaceTextBox = dojo.declare("dojox.editor.plugins._FindReplaceTextBox",
63 [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin],{
64 // summary:
65 // Base class for widgets that contains a label (like "Font:")
66 // and a TextBox to pick a value.
67 // Used as Toolbar entry.
68
69 // textId: [public] String
70 // The id of the enhanced textbox
71 textId: "",
72
73 // label: [public] String
74 // The label of the enhanced textbox
75 label: "",
76
77 // tooltip: [public] String
78 // The tooltip of the enhanced textbox when the mouse is hovering on it
79 toolTip: "",
80 widget: null,
81 widgetsInTemplate: true,
82
83 templateString:
84 "<span style='white-space: nowrap' class='dijit dijitReset dijitInline dijitEditorFindReplaceTextBox' " +
85 "title='${tooltip}' tabindex='-1'>" +
86 "<label class='dijitLeft dijitInline' for='${textId}' tabindex='-1'>${label}</label>" +
87 "<input dojoType='dijit.form.TextBox' intermediateChanges='true' class='focusTextBox' " +
88 "tabIndex='0' id='${textId}' dojoAttachPoint='textBox, focusNode' value='' dojoAttachEvent='onKeyPress: _onKeyPress'/>" +
89 "</span>",
90
91 postMixInProperties: function(){
92 // Set some substitution variables used in the template
93 this.id = dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
94 this.textId = this.id + "_text";
95
96 this.inherited(arguments);
97 },
98
99 postCreate: function(){
100 this.textBox.set("value", "");
101 this.disabled = this.textBox.get("disabled");
102 this.connect(this.textBox, "onChange", "onChange");
103 dojo.attr(this.textBox.textbox, "formnovalidate", "true");
104 },
105
106 _setValueAttr: function(/*String*/ value){
107 //If the value is not a permitted value, just set empty string to prevent showing the warning icon
108 this.value = value;
109 this.textBox.set("value", value);
110 },
111
112 focus: function(){
113 this.textBox.focus();
114 },
115
116 _setDisabledAttr: function(/*Boolean*/ value){
117 // summary:
118 // Over-ride for the textbox's 'disabled' attribute so that it can be
119 // disabled programmatically.
120 // value:
121 // The boolean value to indicate if the textbox should be disabled or not
122 // tags:
123 // private
124 this.disabled = value;
125 this.textBox.set("disabled", value);
126 },
127
128 onChange: function(/*String*/ val){
129 // summary:
130 // Stub function for change events on the box.
131 // tags:
132 // public
133 this.value= val;
134 },
135
136 _onKeyPress: function(/*Event*/ evt){
137 // summary:
138 // Handle the arrow key events
139 // evt:
140 // Event object passed to this handler
141 // tags:
142 // private
143 var start = 0;
144 var end = 0;
145
146 // If CTRL, ALT or SHIFT is not held on
147 if(evt.target && !evt.ctrlKey && !evt.altKey && !evt.shiftKey){
148 if(evt.keyCode == dojo.keys.LEFT_ARROW){
149 start = evt.target.selectionStart;
150 end = evt.target.selectionEnd;
151 if(start < end){
152 dijit.selectInputText(evt.target, start, start);
153 dojo.stopEvent(evt);
154 }
155 }else if(evt.keyCode == dojo.keys.RIGHT_ARROW){
156 start = evt.target.selectionStart;
157 end = evt.target.selectionEnd;
158 if(start < end){
159 dijit.selectInputText(evt.target, end, end);
160 dojo.stopEvent(evt);
161 }
162 }
163 }
164 }
165 });
166
167
168 var FindReplaceCheckBox = dojo.declare("dojox.editor.plugins._FindReplaceCheckBox",
169 [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin],{
170 // summary:
171 // Base class for widgets that contains a label (like "Match case: ")
172 // and a checkbox to indicate if it is checked or not.
173 // Used as Toolbar entry.
174
175 // checkId: [public] String
176 // The id of the enhanced checkbox
177 checkId: "",
178
179 // label: [public] String
180 // The label of the enhanced checkbox
181 label: "",
182
183 // tooltip: [public] String
184 // The tooltip of the enhanced checkbox when the mouse is hovering it
185 tooltip: "",
186
187 widget: null,
188 widgetsInTemplate: true,
189
190 templateString:
191 "<span style='white-space: nowrap' tabindex='-1' " +
192 "class='dijit dijitReset dijitInline dijitEditorFindReplaceCheckBox' title='${tooltip}' >" +
193 "<input dojoType='dijit.form.CheckBox' " +
194 "tabIndex='0' id='${checkId}' dojoAttachPoint='checkBox, focusNode' value=''/>" +
195 "<label tabindex='-1' class='dijitLeft dijitInline' for='${checkId}'>${label}</label>" +
196 "</span>",
197
198 postMixInProperties: function(){
199 // Set some substitution variables used in the template
200 this.id = dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
201 this.checkId = this.id + "_check";
202 this.inherited(arguments);
203 },
204
205 postCreate: function(){
206 this.checkBox.set("checked", false);
207 this.disabled = this.checkBox.get("disabled");
208 this.checkBox.isFocusable = function(){ return false; };
209 },
210
211 _setValueAttr: function(/*Boolean*/ value){
212 // summary:
213 // Passthrough for checkbox.
214 // tags:
215 // private
216 this.checkBox.set('value', value);
217 },
218
219 _getValueAttr: function(){
220 // summary:
221 // Passthrough for checkbox.
222 // tags:
223 // private
224 return this.checkBox.get('value');
225 },
226
227 focus: function(){
228 // summary:
229 // Handle the focus event when this widget gets focused
230 // tags:
231 // private
232 this.checkBox.focus();
233 },
234
235 _setDisabledAttr: function(/*Boolean*/ value){
236 // summary:
237 // Over-ride for the button's 'disabled' attribute so that it can be
238 // disabled programmatically.
239 // value:
240 // The flag that indicates if the checkbox is disabled or not.
241 // tags:
242 // private
243 this.disabled = value;
244 this.checkBox.set("disabled", value);
245 }
246 });
247
248
249 var FindReplaceToolbar = dojo.declare("dojox.editor.plugins._FindReplaceToolbar", Toolbar, {
250 // summary:
251 // A toolbar that derived from dijit.Toolbar, which
252 // eliminates some unnecessary event response such as LEFT_ARROW pressing
253 // and click bubbling.
254
255 postCreate: function(){
256 this.connectKeyNavHandlers([], []); // Prevent arrow key navigation
257 this.connect(this.containerNode, "onclick", "_onToolbarEvent");
258 this.connect(this.containerNode, "onkeydown", "_onToolbarEvent");
259 dojo.addClass(this.domNode, "dijitToolbar");
260 },
261
262 addChild: function(/*dijit._Widget*/ widget, /*int?*/ insertIndex){
263 // summary:
264 // Add a child to our _Container and prevent the default
265 // arrow key navigation function. This function may bring in
266 // side effect
267 dijit._KeyNavContainer.superclass.addChild.apply(this, arguments);
268 },
269
270 _onToolbarEvent: function(/*Event*/ evt){
271 // Editor may have special treatment to some events, so stop the bubbling.
272 // evt:
273 // The Event object
274 // tages:
275 // private
276 evt.stopPropagation();
277 }
278 });
279
280 var FindReplace = dojo.declare("dojox.editor.plugins.FindReplace",[_Plugin],{
281 // summary:
282 // This plugin provides a Find/Replace capability for the editor.
283 // Note that this plugin is NOT supported on Opera currently, as opera
284 // does not implement a window.find or equiv function.
285
286 // buttonClass: [protected]
287 // Define the class of button the editor uses.
288 buttonClass: dijit.form.ToggleButton,
289
290 // iconClassPrefix: [const] String
291 // The CSS class name for the button node is formed from `iconClassPrefix` and `command`
292 iconClassPrefix: "dijitEditorIconsFindReplace",
293
294 // editor: [protected]
295 // The editor this plugin belongs to
296 editor: null,
297
298 // button: [protected]
299 // The toggle button
300 button: null,
301
302 // _frToolbar: [private]
303 // The toolbar that contain all the entries and buttons
304 _frToolbar: null,
305
306 // _closeBox: [private]
307 // The close button of the F/R toolbar
308 _closeBox: null,
309
310 // _findField: [private]
311 // The Find field of the F/R toolbar
312 _findField: null,
313
314 // _replaceField: [private]
315 // The Replace field of the F/R toolbar
316 _replaceField: null,
317
318 // _findButton: [private]
319 // The Find button of the F/R toolbar
320 _findButton: null,
321
322 // _replaceButton: [private]
323 // The Replace button of the F/R toolbar
324 _replaceButton: null,
325
326 // _replaceAllButton: [private]
327 // The ReplaceAll button of the F/R toolbar
328 _replaceAllButton: null,
329
330 // _caseSensitive: [private]
331 // The case sensitive checkbox
332 _caseSensitive: null,
333
334 // _backwards: [private]
335 // The backwards checkbox
336 _backwards: null,
337
338 // _promDialog: [private]
339 // The prompt message box that shows the user some messages
340 // such as the end of a search, the end of a replacement, etc.
341 _promDialog: null,
342 _promDialogTimeout: null,
343
344 // _strings: [private]
345 // The array that contains globalized strings
346 _strings: null,
347
348 _bookmark: null,
349
350 _initButton: function(){
351 // summary:
352 // Over-ride for creation of the resize button.
353 this._strings = dojo.i18n.getLocalization("dojox.editor.plugins", "FindReplace");
354 this.button = new dijit.form.ToggleButton({
355 label: this._strings["findReplace"],
356 showLabel: false,
357 iconClass: this.iconClassPrefix + " dijitEditorIconFindString",
358 tabIndex: "-1",
359 onChange: dojo.hitch(this, "_toggleFindReplace")
360 });
361 if(dojo.isOpera){
362 // Not currently supported on Opera!
363 this.button.set("disabled", true);
364 }
365 //Link up so that if the toggle is disabled, then the view of Find/Replace is closed.
366 this.connect(this.button, "set", dojo.hitch(this, function(attr, val){
367 if(attr === "disabled"){
368 this._toggleFindReplace((!val && this._displayed), true, true);
369 }
370 }));
371 },
372
373 setEditor: function(editor){
374 // summary:
375 // This is a callback handler that set a reference to the editor this plugin
376 // hosts in
377 this.editor = editor;
378 this._initButton();
379 },
380
381 toggle: function(){
382 // summary:
383 // Function to allow programmatic toggling of the find toolbar.
384 // tags:
385 // public
386 this.button.set("checked", !this.button.get("checked"));
387 },
388
389 _toggleFindReplace: function(/*Boolean*/ show, /*Boolean?*/ ignoreState, /*Boolean?*/ buttonDisabled){
390 // summary:
391 // Function to toggle whether or not find/replace is displayed.
392 // show:
393 // Indicate if the toolbar is shown or not
394 // ignoreState:
395 // Indicate if the status should be ignored or not
396 // blurEditor:
397 // Indicate if the focus should be removed from the editor or not
398 // tags:
399 // private
400 var size = dojo.marginBox(this.editor.domNode);
401 if(show && !dojo.isOpera){
402 dojo.style(this._frToolbar.domNode, "display", "block");
403 // Auto populate the Find field
404 this._populateFindField();
405 if(!ignoreState){
406 this._displayed = true;
407 }
408 }else{
409 dojo.style(this._frToolbar.domNode, "display", "none");
410 if(!ignoreState){
411 this._displayed = false;
412 }
413
414 // If the toggle button is disabled, it is most likely that
415 // another plugin such as ViewSource disables it.
416 // So we do not need to focus the text area of the editor to
417 // prevent the editor from an invalid status.
418 // Please refer to dijit._editor.plugins.ViewSource for more details.
419 if(!buttonDisabled){
420 this.editor.focus();
421 }
422 }
423
424 // Resize the editor.
425 this.editor.resize({h: size.h});
426 },
427
428 _populateFindField: function(){
429 // summary:
430 // Populate the Find field with selected text when dialog initially displayed.
431 // Auto-select text in Find field after it is populated.
432 // If nothing selected, restore previous entry from the same session.
433 // tags:
434 // private
435 var ed = this.editor;
436 var win = ed.window;
437 var selectedTxt = ed._sCall("getSelectedText", [null]);
438 if(this._findField && this._findField.textBox){
439 if(selectedTxt){
440 this._findField.textBox.set("value", selectedTxt);
441 }
442 this._findField.textBox.focus();
443 dijit.selectInputText(this._findField.textBox.focusNode);
444 }
445 },
446
447 setToolbar: function(/*dijit.Toolbar*/ toolbar){
448 // summary:
449 // Over-ride so that find/replace toolbar is appended after the current toolbar.
450 // toolbar:
451 // The current toolbar of the editor
452 // tags:
453 // public
454 this.inherited(arguments);
455 if(!dojo.isOpera){
456 var _tb = (this._frToolbar = new FindReplaceToolbar());
457 dojo.style(_tb.domNode, "display", "none");
458 dojo.place(_tb.domNode, toolbar.domNode, "after");
459 _tb.startup();
460
461 // IE6 will put the close box in a new line when its style is "float: right".
462 // So place the close box ahead of the other fields, which makes it align with
463 // the other components.
464 this._closeBox = new FindReplaceCloseBox();
465 _tb.addChild(this._closeBox);
466
467 // Define the search/replace fields.
468 this._findField = new FindReplaceTextBox(
469 {label: this._strings["findLabel"], tooltip: this._strings["findTooltip"]});
470 _tb.addChild(this._findField);
471
472 this._replaceField = new FindReplaceTextBox(
473 {label: this._strings["replaceLabel"], tooltip: this._strings["replaceTooltip"]});
474 _tb.addChild(this._replaceField);
475
476 // Define the Find/Replace/ReplaceAll buttons.
477 _tb.addChild(new dojox.editor.plugins.ToolbarLineBreak());
478
479 this._findButton = new dijit.form.Button({label: this._strings["findButton"], showLabel: true,
480 iconClass: this.iconClassPrefix + " dijitEditorIconFind"});
481 this._findButton.titleNode.title = this._strings["findButtonTooltip"];
482 _tb.addChild(this._findButton);
483
484 this._replaceButton = new dijit.form.Button({label: this._strings["replaceButton"], showLabel: true,
485 iconClass: this.iconClassPrefix + " dijitEditorIconReplace"});
486 this._replaceButton.titleNode.title = this._strings["replaceButtonTooltip"];
487 _tb.addChild(this._replaceButton);
488
489 this._replaceAllButton = new dijit.form.Button({label: this._strings["replaceAllButton"], showLabel: true,
490 iconClass: this.iconClassPrefix + " dijitEditorIconReplaceAll"});
491 this._replaceAllButton.titleNode.title = this._strings["replaceAllButtonTooltip"];
492 _tb.addChild(this._replaceAllButton);
493
494 // Define the option checkboxes.
495 this._caseSensitive = new FindReplaceCheckBox(
496 {label: this._strings["matchCase"], tooltip: this._strings["matchCaseTooltip"]});
497 _tb.addChild(this._caseSensitive);
498
499 this._backwards = new FindReplaceCheckBox(
500 {label: this._strings["backwards"], tooltip: this._strings["backwardsTooltip"]});
501 _tb.addChild(this._backwards);
502
503 // Set initial states, buttons should be disabled unless content is
504 // present in the fields.
505 this._findButton.set("disabled", true);
506 this._replaceButton.set("disabled", true);
507 this._replaceAllButton.set("disabled", true);
508
509 // Connect the event to the status of the items
510 this.connect(this._findField, "onChange", "_checkButtons");
511 this.connect(this._findField, "onKeyDown", "_onFindKeyDown");
512 this.connect(this._replaceField, "onKeyDown", "_onReplaceKeyDown");
513
514 // Connect up the actual search events.
515 this.connect(this._findButton, "onClick", "_find");
516 this.connect(this._replaceButton, "onClick", "_replace");
517 this.connect(this._replaceAllButton, "onClick", "_replaceAll");
518
519 // Connect up the close event
520 this.connect(this._closeBox, "onClick", "toggle");
521
522 // Prompt for the message
523 this._promDialog = new dijit.TooltipDialog();
524 this._promDialog.startup();
525 this._promDialog.set("content", "");
526 }
527 },
528
529 _checkButtons: function(){
530 // summary:
531 // Ensure that all the buttons are in a correct status
532 // when certain events are fired.
533 var fText = this._findField.get("value");
534
535 if(fText){
536 // Only enable if find text is not empty or just blank/spaces.
537 this._findButton.set("disabled", false);
538 this._replaceButton.set("disabled", false);
539 this._replaceAllButton.set("disabled", false);
540 }else{
541 this._findButton.set("disabled", true);
542 this._replaceButton.set("disabled", true);
543 this._replaceAllButton.set("disabled", true);
544 }
545 },
546
547 _onFindKeyDown: function(evt){
548 if(evt.keyCode == dojo.keys.ENTER){
549 // Perform the default search action
550 this._find();
551 dojo.stopEvent(evt);
552 }
553 },
554
555 _onReplaceKeyDown: function(evt){
556 if(evt.keyCode == dojo.keys.ENTER){
557 // Perform the default replace action
558 if(!this._replace()) this._replace();
559 dojo.stopEvent(evt);
560 }
561 },
562
563 _find: function(/*Boolean?*/ showMessage){
564 // summary:
565 // This function invokes a find on the editor document with the noted options for
566 // find.
567 // showMessage:
568 // Indicated whether the tooltip is shown or not when the search reaches the end
569 // tags:
570 // private.
571 // returns:
572 // Boolean indicating if the content was found or not.
573 var txt = this._findField.get("value") || "";
574 if(txt){
575 var caseSensitive = this._caseSensitive.get("value");
576 var backwards = this._backwards.get("value");
577 var isFound = this._findText(txt, caseSensitive, backwards);
578 if(!isFound && showMessage){
579 this._promDialog.set("content", dojo.string.substitute(
580 this._strings["eofDialogText"], {"0": this._strings["eofDialogTextFind"]}));
581 dijit.popup.open({popup: this._promDialog, around: this._findButton.domNode});
582 this._promDialogTimeout = setTimeout(dojo.hitch(this, function(){
583 clearTimeout(this._promDialogTimeout);
584 this._promDialogTimeout = null;
585 dijit.popup.close(this._promDialog);
586 }), 3000);
587 setTimeout(dojo.hitch(this, function(){
588 this.editor.focus();
589 }), 0);
590 }
591 return isFound;
592 }
593
594 return false;
595 },
596
597 _replace: function(/*Boolean?*/ showMessage){
598 // summary:
599 // This function invokes a replace on the editor document with the noted options for replace
600 // showMessage:
601 // Indicate if the prompt message is shown or not when the replacement
602 // reaches the end
603 // tags:
604 // private
605 // returns:
606 // Boolean indicating if the content was replaced or not.
607 var isReplaced = false;
608 var ed = this.editor;
609 ed.focus();
610 var txt = this._findField.get("value") || "";
611 var repTxt = this._replaceField.get("value") || "";
612
613 if(txt){
614 var caseSensitive = this._caseSensitive.get("value");
615 // Check if it is forced to be forwards or backwards
616 var backwards = this._backwards.get("value");
617
618 //Replace the current selected text if it matches the pattern
619 var selected = ed._sCall("getSelectedText", [null]);
620 // Handle checking/replacing current selection. For some reason on Moz
621 // leading whitespace is trimmed, so we have to trim it down on this check
622 // or we don't always replace. Moz bug!
623 if(dojo.isMoz){
624 txt = dojo.trim(txt);
625 selected = dojo.trim(selected);
626 }
627
628 var regExp = this._filterRegexp(txt, !caseSensitive);
629 if(selected && regExp.test(selected)){
630 ed.execCommand("inserthtml", repTxt);
631 isReplaced = true;
632
633 if(backwards){
634 // Move to the beginning of the replaced text
635 // to avoid the infinite recursive replace
636 this._findText(repTxt, caseSensitive, backwards);
637 ed._sCall("collapse", [true]);
638 }
639 }
640
641 if(!this._find(false) && showMessage){ // Find the next
642 this._promDialog.set("content", dojo.string.substitute(
643 this._strings["eofDialogText"], {"0": this._strings["eofDialogTextReplace"]}));
644 dijit.popup.open({popup: this._promDialog, around: this._replaceButton.domNode});
645 this._promDialogTimeout = setTimeout(dojo.hitch(this, function(){
646 clearTimeout(this._promDialogTimeout);
647 this._promDialogTimeout = null;
648 dijit.popup.close(this._promDialog);
649 }), 3000);
650 setTimeout(dojo.hitch(this, function(){
651 this.editor.focus();
652 }), 0);
653 }
654 return isReplaced;
655 }
656 return null;
657 },
658
659 _replaceAll: function(/*Boolean?*/ showMessage){
660 // summary:
661 // This function replaces all the matched content on the editor document
662 // with the noted options for replace
663 // showMessage:
664 // Indicate if the prompt message is shown or not when the action is done.
665 // tags:
666 // private
667 var replaced = 0;
668 var backwards = this._backwards.get("value");
669
670 if(backwards){
671 this.editor.placeCursorAtEnd();
672 }else{
673 this.editor.placeCursorAtStart();
674 }
675
676 // The _replace will return false if the current selection deos not match
677 // the searched text. So try the first attempt so that the selection
678 // is always the searched text if there is one that matches
679 if(this._replace(false)) { replaced++; }
680 // Do the replace via timeouts to avoid locking the browser up for a lot of replaces.
681 var loopBody = dojo.hitch(this, function(){
682 if(this._replace(false)){
683 replaced++;
684 setTimeout(loopBody, 10);
685 }else{
686 if(showMessage){
687 this._promDialog.set("content", dojo.string.substitute(
688 this._strings["replaceDialogText"], {"0": "" + replaced}));
689 dijit.popup.open({
690 popup: this._promDialog,
691 around: this._replaceAllButton.domNode
692 });
693 this._promDialogTimeout = setTimeout(dojo.hitch(this, function(){
694 clearTimeout(this._promDialogTimeout);
695 this._promDialogTimeout = null;
696 dijit.popup.close(this._promDialog);
697 }), 3000);
698 setTimeout(dojo.hitch(this, function(){
699 this._findField.focus();
700 this._findField.textBox.focusNode.select();
701 }), 0);
702 }
703 }
704 });
705 loopBody();
706 },
707
708 _findText: function(/*String*/ txt, /*Boolean*/ caseSensitive, /*Boolean*/ backwards){
709 // summary:
710 // This function invokes a find with specific options
711 // txt: String
712 // The text to locate in the document.
713 // caseSensitive: Boolean
714 // Whether or ot to search case-sensitively.
715 // backwards: Boolean
716 // Whether or not to search backwards in the document.
717 // tags:
718 // private.
719 // returns:
720 // Boolean indicating if the content was found or not.
721 var ed = this.editor;
722 var win = ed.window;
723 var found = false;
724 if(txt){
725 if(win.find){
726 found = win.find(txt, caseSensitive, backwards, false, false, false, false);
727 }else{
728 var doc = ed.document;
729 if(doc.selection || win.getSelection){
730 /* IE */
731 // Focus to restore position/selection,
732 // then shift to search from current position.
733 this.editor.focus();
734 var txtRg = doc.body.createTextRange();
735 var txtRg2 = txtRg.duplicate();
736 var sel1 = win.getSelection();
737 var sRange = sel1.getRangeAt(0);
738 var curPos = doc.selection?doc.selection.createRange():null;
739 if(curPos){
740 if(backwards){
741 txtRg.setEndPoint("EndToStart", curPos);
742 }else{
743 txtRg.setEndPoint("StartToEnd", curPos);
744 }
745 }else if(this._bookmark){
746 var currentText = win.getSelection().toString();
747 txtRg.moveToBookmark(this._bookmark);
748 //if selection is different to previous bookmark, need to search from that position
749 if ( txtRg.text != currentText ) {
750 txtRg = txtRg2.duplicate();
751 this._bookmark = null;
752 }else if(backwards){
753 txtRg2.setEndPoint("EndToStart", txtRg);
754 txtRg = txtRg2.duplicate();
755 }else{
756 txtRg2.setEndPoint("StartToEnd", txtRg);
757 txtRg = txtRg2.duplicate();
758 }
759 }
760 var flags = caseSensitive?4:0;
761 if(backwards){
762 flags = flags | 1;
763 }
764 //flags = flags |
765 found = txtRg.findText(txt,txtRg.text.length,flags);
766 if(found){
767 txtRg.select();
768 this._bookmark = txtRg.getBookmark();
769 }
770 }
771 }
772 }
773 return found;
774 },
775
776 _filterRegexp: function(/*String*/ pattern, /*Boolean*/ ignoreCase){
777 // summary:
778 // Helper function to convert a simple pattern to a regular expression for matching.
779 // description:
780 // Returns a regular expression object that conforms to the defined conversion rules.
781 // For example:
782 //
783 // - ca* -> /^ca.*$/
784 // - *ca* -> /^.*ca.*$/
785 // - *c\*a* -> /^.*c\*a.*$/
786 // - *c\*a?* -> /^.*c\*a..*$/
787 //
788 // and so on.
789 // pattern: string
790 // A simple matching pattern to convert that follows basic rules:
791 //
792 // - * Means match anything, so ca* means match anything starting with ca
793 // - ? Means match single character. So, b?b will match to bob and bab, and so on.
794 // - \ is an escape character. So for example, \* means do not treat * as a match, but literal character *.
795 // To use a \ as a character in the string, it must be escaped. So in the pattern it should be
796 // represented by \\ to be treated as an ordinary \ character instead of an escape.
797 // ignoreCase:
798 // An optional flag to indicate if the pattern matching should be treated as case-sensitive or not when comparing
799 // By default, it is assumed case sensitive.
800 // tags:
801 // private
802
803 var rxp = "";
804 var c = null;
805 for(var i = 0; i < pattern.length; i++){
806 c = pattern.charAt(i);
807 switch(c){
808 case '\\':
809 rxp += c;
810 i++;
811 rxp += pattern.charAt(i);
812 break;
813 case '$':
814 case '^':
815 case '/':
816 case '+':
817 case '.':
818 case '|':
819 case '(':
820 case ')':
821 case '{':
822 case '}':
823 case '[':
824 case ']':
825 rxp += "\\"; //fallthrough
826 default:
827 rxp += c;
828 }
829 }
830 rxp = "^" + rxp + "$";
831 if(ignoreCase){
832 return new RegExp(rxp,"mi"); //RegExp
833 }else{
834 return new RegExp(rxp,"m"); //RegExp
835 }
836
837 },
838
839 updateState: function(){
840 // summary:
841 // Over-ride for button state control for disabled to work.
842 this.button.set("disabled", this.get("disabled"));
843 },
844
845 destroy: function(){
846 // summary:
847 // Cleanup of our custom toolbar.
848 this.inherited(arguments);
849 if(this._promDialogTimeout){
850 clearTimeout(this._promDialogTimeout);
851 this._promDialogTimeout = null;
852 dijit.popup.close(this._promDialog);
853 }
854 if(this._frToolbar){
855 this._frToolbar.destroyRecursive();
856 this._frToolbar = null;
857 }
858 if(this._promDialog){
859 this._promDialog.destroyRecursive();
860 this._promDialog = null;
861 }
862 }
863 });
864
865 // For monkey patching
866 FindReplace._FindReplaceCloseBox = FindReplaceCloseBox;
867 FindReplace._FindReplaceTextBox = FindReplaceTextBox;
868 FindReplace._FindReplaceCheckBox = FindReplaceCheckBox;
869 FindReplace._FindReplaceToolbar = FindReplaceToolbar;
870
871 // Register this plugin.
872 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
873 if(o.plugin){ return; }
874 var name = o.args.name.toLowerCase();
875 if(name === "findreplace"){
876 o.plugin = new FindReplace({});
877 }
878 });
879
880 return FindReplace;
881
882 });