Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / editor / plugins / FindReplace.js

History | View | Annotate | Download (13.5 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.editor.plugins.FindReplace"]){
9
dojo._hasResource["dojox.editor.plugins.FindReplace"]=true;
10
dojo.provide("dojox.editor.plugins.FindReplace");
11
dojo.require("dijit._editor._Plugin");
12
dojo.require("dijit.Toolbar");
13
dojo.require("dijit.form.TextBox");
14
dojo.require("dijit.form.CheckBox");
15
dojo.require("dijit.form.Button");
16
dojo.require("dijit.TooltipDialog");
17
dojo.require("dojox.editor.plugins.ToolbarLineBreak");
18
dojo.require("dojo.i18n");
19
dojo.require("dojo.string");
20
dojo.requireLocalization("dojox.editor.plugins","FindReplace",null,"ROOT,cs,de,es,fr,hu,it,ja,ko,pl,pt,ro,ru,zh,zh-tw");
21
dojo.experimental("dojox.editor.plugins.FindReplace");
22
dojo.declare("dojox.editor.plugins._FindReplaceCloseBox",[dijit._Widget,dijit._Templated],{btnId:"",widget:null,widgetsInTemplate:true,templateString:"<span style='float: right' class='dijitInline' tabindex='-1'>"+"<button class='dijit dijitReset dijitInline' "+"id='${btnId}' dojoAttachPoint='button' dojoType='dijit.form.Button' tabindex='-1' iconClass='dijitEditorIconsFindReplaceClose' showLabel='false'>X</button>"+"</span>",postMixInProperties:function(){
23
this.id=dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
24
this.btnId=this.id+"_close";
25
this.inherited(arguments);
26
},startup:function(){
27
this.connect(this.button,"onClick","onClick");
28
},onClick:function(){
29
}});
30
dojo.declare("dojox.editor.plugins._FindReplaceTextBox",[dijit._Widget,dijit._Templated],{textId:"",label:"",toolTip:"",widget:null,widgetsInTemplate:true,templateString:"<span style='white-space: nowrap' class='dijit dijitReset dijitInline dijitEditorFindReplaceTextBox' "+"title='${tooltip}' tabindex='-1'>"+"<label class='dijitLeft dijitInline' for='${textId}' tabindex='-1'>${label}</label>"+"<input dojoType='dijit.form.TextBox' required='false' intermediateChanges='true' class='focusTextBox'"+"tabIndex='0' id='${textId}' dojoAttachPoint='textBox, focusNode' value='' dojoAttachEvent='onKeyPress: _onKeyPress'/>"+"</span>",postMixInProperties:function(){
31
this.id=dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
32
this.textId=this.id+"_text";
33
this.inherited(arguments);
34
},postCreate:function(){
35
this.textBox.set("value","");
36
this.disabled=this.textBox.get("disabled");
37
this.connect(this.textBox,"onChange","onChange");
38
},_setValueAttr:function(_1){
39
this.value=_1;
40
this.textBox.set("value",_1);
41
},focus:function(){
42
this.textBox.focus();
43
},_setDisabledAttr:function(_2){
44
this.disabled=_2;
45
this.textBox.set("disabled",_2);
46
},onChange:function(_3){
47
this.value=_3;
48
},_onKeyPress:function(_4){
49
var _5=0;
50
var _6=0;
51
if(_4.target&&!_4.ctrlKey&&!_4.altKey&&!_4.shiftKey){
52
if(_4.keyCode==dojo.keys.LEFT_ARROW){
53
_5=_4.target.selectionStart;
54
_6=_4.target.selectionEnd;
55
if(_5<_6){
56
dijit.selectInputText(_4.target,_5,_5);
57
dojo.stopEvent(_4);
58
}
59
}else{
60
if(_4.keyCode==dojo.keys.RIGHT_ARROW){
61
_5=_4.target.selectionStart;
62
_6=_4.target.selectionEnd;
63
if(_5<_6){
64
dijit.selectInputText(_4.target,_6,_6);
65
dojo.stopEvent(_4);
66
}
67
}
68
}
69
}
70
}});
71
dojo.declare("dojox.editor.plugins._FindReplaceCheckBox",[dijit._Widget,dijit._Templated],{checkId:"",label:"",tooltip:"",widget:null,widgetsInTemplate:true,templateString:"<span style='white-space: nowrap' tabindex='-1' "+"class='dijit dijitReset dijitInline dijitEditorFindReplaceCheckBox' title='${tooltip}' >"+"<input dojoType='dijit.form.CheckBox' required=false "+"tabIndex='0' id='${checkId}' dojoAttachPoint='checkBox, focusNode' value=''/>"+"<label tabindex='-1' class='dijitLeft dijitInline' for='${checkId}'>${label}</label>"+"</span>",postMixInProperties:function(){
72
this.id=dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
73
this.checkId=this.id+"_check";
74
this.inherited(arguments);
75
},postCreate:function(){
76
this.checkBox.set("checked",false);
77
this.disabled=this.checkBox.get("disabled");
78
this.checkBox.isFocusable=function(){
79
return false;
80
};
81
},_setValueAttr:function(_7){
82
this.checkBox.set("value",_7);
83
},_getValueAttr:function(){
84
return this.checkBox.get("value");
85
},focus:function(){
86
this.checkBox.focus();
87
},_setDisabledAttr:function(_8){
88
this.disabled=_8;
89
this.checkBox.set("disabled",_8);
90
}});
91
dojo.declare("dojox.editor.plugins._FindReplaceToolbar",dijit.Toolbar,{postCreate:function(){
92
this.connectKeyNavHandlers([],[]);
93
this.connect(this.containerNode,"onclick","_onToolbarEvent");
94
this.connect(this.containerNode,"onkeydown","_onToolbarEvent");
95
dojo.addClass(this.domNode,"dijitToolbar");
96
},addChild:function(_9,_a){
97
dijit._KeyNavContainer.superclass.addChild.apply(this,arguments);
98
},_onToolbarEvent:function(_b){
99
_b.stopPropagation();
100
}});
101
dojo.declare("dojox.editor.plugins.FindReplace",[dijit._editor._Plugin],{buttonClass:dijit.form.ToggleButton,iconClassPrefix:"dijitEditorIconsFindReplace",editor:null,button:null,_frToolbar:null,_closeBox:null,_findField:null,_replaceField:null,_findButton:null,_replaceButton:null,_replaceAllButton:null,_caseSensitive:null,_backwards:null,_promDialog:null,_promDialogTimeout:null,_strings:null,_initButton:function(){
102
this._strings=dojo.i18n.getLocalization("dojox.editor.plugins","FindReplace");
103
this.button=new dijit.form.ToggleButton({label:this._strings["findReplace"],showLabel:false,iconClass:this.iconClassPrefix+" dijitEditorIconFindString",tabIndex:"-1",onChange:dojo.hitch(this,"_toggleFindReplace")});
104
if(dojo.isOpera){
105
this.button.set("disabled",true);
106
}
107
this.connect(this.button,"set",dojo.hitch(this,function(_c,_d){
108
if(_c==="disabled"){
109
this._toggleFindReplace((!_d&&this._displayed),true,true);
110
}
111
}));
112
},setEditor:function(_e){
113
this.editor=_e;
114
this._initButton();
115
},toggle:function(){
116
this.button.set("checked",!this.button.get("checked"));
117
},_toggleFindReplace:function(_f,_10,_11){
118
var _12=dojo.marginBox(this.editor.domNode);
119
if(_f&&!dojo.isOpera){
120
dojo.style(this._frToolbar.domNode,"display","block");
121
this._populateFindField();
122
if(!_10){
123
this._displayed=true;
124
}
125
}else{
126
dojo.style(this._frToolbar.domNode,"display","none");
127
if(!_10){
128
this._displayed=false;
129
}
130
if(!_11){
131
this.editor.focus();
132
}
133
}
134
this.editor.resize({h:_12.h});
135
},_populateFindField:function(){
136
var ed=this.editor;
137
var win=ed.window;
138
var _13=dojo.withGlobal(ed.window,"getSelectedText",dijit._editor.selection,[null]);
139
if(this._findField&&this._findField.textBox){
140
if(_13){
141
this._findField.textBox.set("value",_13);
142
}
143
this._findField.textBox.focus();
144
dijit.selectInputText(this._findField.textBox.focusNode);
145
}
146
},setToolbar:function(_14){
147
this.inherited(arguments);
148
if(!dojo.isOpera){
149
var _15=this._frToolbar=new dojox.editor.plugins._FindReplaceToolbar();
150
dojo.style(_15.domNode,"display","none");
151
dojo.place(_15.domNode,_14.domNode,"after");
152
_15.startup();
153
this._closeBox=new dojox.editor.plugins._FindReplaceCloseBox();
154
_15.addChild(this._closeBox);
155
this._findField=new dojox.editor.plugins._FindReplaceTextBox({label:this._strings["findLabel"],tooltip:this._strings["findTooltip"]});
156
_15.addChild(this._findField);
157
this._replaceField=new dojox.editor.plugins._FindReplaceTextBox({label:this._strings["replaceLabel"],tooltip:this._strings["replaceTooltip"]});
158
_15.addChild(this._replaceField);
159
_15.addChild(new dojox.editor.plugins._ToolbarLineBreak());
160
this._findButton=new dijit.form.Button({label:this._strings["findButton"],showLabel:true,iconClass:this.iconClassPrefix+" dijitEditorIconFind"});
161
this._findButton.titleNode.title=this._strings["findButtonTooltip"];
162
_15.addChild(this._findButton);
163
this._replaceButton=new dijit.form.Button({label:this._strings["replaceButton"],showLabel:true,iconClass:this.iconClassPrefix+" dijitEditorIconReplace"});
164
this._replaceButton.titleNode.title=this._strings["replaceButtonTooltip"];
165
_15.addChild(this._replaceButton);
166
this._replaceAllButton=new dijit.form.Button({label:this._strings["replaceAllButton"],showLabel:true,iconClass:this.iconClassPrefix+" dijitEditorIconReplaceAll"});
167
this._replaceAllButton.titleNode.title=this._strings["replaceAllButtonTooltip"];
168
_15.addChild(this._replaceAllButton);
169
this._caseSensitive=new dojox.editor.plugins._FindReplaceCheckBox({label:this._strings["matchCase"],tooltip:this._strings["matchCaseTooltip"]});
170
_15.addChild(this._caseSensitive);
171
this._backwards=new dojox.editor.plugins._FindReplaceCheckBox({label:this._strings["backwards"],tooltip:this._strings["backwardsTooltip"]});
172
_15.addChild(this._backwards);
173
this._findButton.set("disabled",true);
174
this._replaceButton.set("disabled",true);
175
this._replaceAllButton.set("disabled",true);
176
this.connect(this._findField,"onChange","_checkButtons");
177
this.connect(this._findField,"onKeyDown","_onFindKeyDown");
178
this.connect(this._replaceField,"onKeyDown","_onReplaceKeyDown");
179
this.connect(this._findButton,"onClick","_find");
180
this.connect(this._replaceButton,"onClick","_replace");
181
this.connect(this._replaceAllButton,"onClick","_replaceAll");
182
this.connect(this._closeBox,"onClick","toggle");
183
this._promDialog=new dijit.TooltipDialog();
184
this._promDialog.startup();
185
this._promDialog.set("content","");
186
}
187
},_checkButtons:function(){
188
var _16=this._findField.get("value");
189
if(_16){
190
this._findButton.set("disabled",false);
191
this._replaceButton.set("disabled",false);
192
this._replaceAllButton.set("disabled",false);
193
}else{
194
this._findButton.set("disabled",true);
195
this._replaceButton.set("disabled",true);
196
this._replaceAllButton.set("disabled",true);
197
}
198
},_onFindKeyDown:function(evt){
199
if(evt.keyCode==dojo.keys.ENTER){
200
this._find();
201
dojo.stopEvent(evt);
202
}
203
},_onReplaceKeyDown:function(evt){
204
if(evt.keyCode==dojo.keys.ENTER){
205
if(!this._replace()){
206
this._replace();
207
}
208
dojo.stopEvent(evt);
209
}
210
},_find:function(_17){
211
var txt=this._findField.get("value")||"";
212
if(txt){
213
var _18=this._caseSensitive.get("value");
214
var _19=this._backwards.get("value");
215
var _1a=this._findText(txt,_18,_19);
216
if(!_1a&&_17){
217
this._promDialog.set("content",dojo.string.substitute(this._strings["eofDialogText"],{"0":this._strings["eofDialogTextFind"]}));
218
dijit.popup.open({popup:this._promDialog,around:this._findButton.domNode});
219
this._promDialogTimeout=setTimeout(dojo.hitch(this,function(){
220
clearTimeout(this._promDialogTimeout);
221
this._promDialogTimeout=null;
222
dijit.popup.close(this._promDialog);
223
}),3000);
224
setTimeout(dojo.hitch(this,function(){
225
this.editor.focus();
226
}),0);
227
}
228
return _1a;
229
}
230
return false;
231
},_replace:function(_1b){
232
var _1c=false;
233
var ed=this.editor;
234
ed.focus();
235
var txt=this._findField.get("value")||"";
236
var _1d=this._replaceField.get("value")||"";
237
if(txt){
238
var _1e=this._caseSensitive.get("value");
239
var _1f=this._backwards.get("value");
240
var _20=dojo.withGlobal(ed.window,"getSelectedText",dijit._editor.selection,[null]);
241
if(dojo.isMoz){
242
txt=dojo.trim(txt);
243
_20=dojo.trim(_20);
244
}
245
var _21=this._filterRegexp(txt,!_1e);
246
if(_20&&_21.test(_20)){
247
ed.execCommand("inserthtml",_1d);
248
_1c=true;
249
if(_1f){
250
this._findText(_1d,_1e,_1f);
251
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
252
}
253
}
254
if(!this._find(false)&&_1b){
255
this._promDialog.set("content",dojo.string.substitute(this._strings["eofDialogText"],{"0":this._strings["eofDialogTextReplace"]}));
256
dijit.popup.open({popup:this._promDialog,around:this._replaceButton.domNode});
257
this._promDialogTimeout=setTimeout(dojo.hitch(this,function(){
258
clearTimeout(this._promDialogTimeout);
259
this._promDialogTimeout=null;
260
dijit.popup.close(this._promDialog);
261
}),3000);
262
setTimeout(dojo.hitch(this,function(){
263
this.editor.focus();
264
}),0);
265
}
266
return _1c;
267
}
268
},_replaceAll:function(_22){
269
var _23=0;
270
var _24=this._backwards.get("value");
271
if(_24){
272
this.editor.placeCursorAtEnd();
273
}else{
274
this.editor.placeCursorAtStart();
275
}
276
if(this._replace(false)){
277
_23++;
278
}
279
var _25=dojo.hitch(this,function(){
280
if(this._replace(false)){
281
_23++;
282
setTimeout(_25,10);
283
}else{
284
if(_22){
285
this._promDialog.set("content",dojo.string.substitute(this._strings["replaceDialogText"],{"0":""+_23}));
286
dijit.popup.open({popup:this._promDialog,around:this._replaceAllButton.domNode});
287
this._promDialogTimeout=setTimeout(dojo.hitch(this,function(){
288
clearTimeout(this._promDialogTimeout);
289
this._promDialogTimeout=null;
290
dijit.popup.close(this._promDialog);
291
}),3000);
292
setTimeout(dojo.hitch(this,function(){
293
this._findField.focus();
294
this._findField.textBox.focusNode.select();
295
}),0);
296
}
297
}
298
});
299
_25();
300
},_findText:function(txt,_26,_27){
301
var ed=this.editor;
302
var win=ed.window;
303
var _28=false;
304
if(txt){
305
if(win.find){
306
_28=win.find(txt,_26,_27,false,false,false,false);
307
}else{
308
var doc=ed.document;
309
if(doc.selection){
310
this.editor.focus();
311
var _29=doc.body.createTextRange();
312
var _2a=doc.selection?doc.selection.createRange():null;
313
if(_2a){
314
if(_27){
315
_29.setEndPoint("EndToStart",_2a);
316
}else{
317
_29.setEndPoint("StartToEnd",_2a);
318
}
319
}
320
var _2b=_26?4:0;
321
if(_27){
322
_2b=_2b|1;
323
}
324
_28=_29.findText(txt,_29.text.length,_2b);
325
if(_28){
326
_29.select();
327
}
328
}
329
}
330
}
331
return _28;
332
},_filterRegexp:function(_2c,_2d){
333
var rxp="";
334
var c=null;
335
for(var i=0;i<_2c.length;i++){
336
c=_2c.charAt(i);
337
switch(c){
338
case "\\":
339
rxp+=c;
340
i++;
341
rxp+=_2c.charAt(i);
342
break;
343
case "$":
344
case "^":
345
case "/":
346
case "+":
347
case ".":
348
case "|":
349
case "(":
350
case ")":
351
case "{":
352
case "}":
353
case "[":
354
case "]":
355
rxp+="\\";
356
default:
357
rxp+=c;
358
}
359
}
360
rxp="^"+rxp+"$";
361
if(_2d){
362
return new RegExp(rxp,"mi");
363
}else{
364
return new RegExp(rxp,"m");
365
}
366
},destroy:function(){
367
this.inherited(arguments);
368
if(this._promDialogTimeout){
369
clearTimeout(this._promDialogTimeout);
370
this._promDialogTimeout=null;
371
dijit.popup.close(this._promDialog);
372
}
373
if(this._frToolbar){
374
this._frToolbar.destroyRecursive();
375
this._frToolbar=null;
376
}
377
if(this._promDialog){
378
this._promDialog.destroyRecursive();
379
this._promDialog=null;
380
}
381
}});
382
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
383
if(o.plugin){
384
return;
385
}
386
var _2e=o.args.name.toLowerCase();
387
if(_2e==="findreplace"){
388
o.plugin=new dojox.editor.plugins.FindReplace({});
389
}
390
});
391
}