root / trunk / web / dojo / dijit / Editor.js @ 9
History | View | Annotate | Download (11.3 KB)
1 |
/*
|
---|---|
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["dijit.Editor"]){ |
9 |
dojo._hasResource["dijit.Editor"]=true; |
10 |
dojo.provide("dijit.Editor");
|
11 |
dojo.require("dijit._editor.RichText");
|
12 |
dojo.require("dijit.Toolbar");
|
13 |
dojo.require("dijit.ToolbarSeparator");
|
14 |
dojo.require("dijit._editor._Plugin");
|
15 |
dojo.require("dijit._editor.plugins.EnterKeyHandling");
|
16 |
dojo.require("dijit._editor.range");
|
17 |
dojo.require("dijit._Container");
|
18 |
dojo.require("dojo.i18n");
|
19 |
dojo.require("dijit.layout._LayoutWidget");
|
20 |
dojo.require("dijit._editor.range");
|
21 |
dojo.requireLocalization("dijit._editor","commands",null,"ROOT,ar,ca,cs,da,de,el,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw"); |
22 |
dojo.declare("dijit.Editor",dijit._editor.RichText,{plugins:null,extraPlugins:null,constructor:function(){ |
23 |
if(!dojo.isArray(this.plugins)){ |
24 |
this.plugins=["undo","redo","|","cut","copy","paste","|","bold","italic","underline","strikethrough","|","insertOrderedList","insertUnorderedList","indent","outdent","|","justifyLeft","justifyRight","justifyCenter","justifyFull","dijit._editor.plugins.EnterKeyHandling"]; |
25 |
} |
26 |
this._plugins=[];
|
27 |
this._editInterval=this.editActionInterval*1000; |
28 |
if(dojo.isIE){
|
29 |
this.events.push("onBeforeDeactivate"); |
30 |
this.events.push("onBeforeActivate"); |
31 |
} |
32 |
},postCreate:function(){ |
33 |
this._steps=this._steps.slice(0); |
34 |
this._undoedSteps=this._undoedSteps.slice(0); |
35 |
if(dojo.isArray(this.extraPlugins)){ |
36 |
this.plugins=this.plugins.concat(this.extraPlugins); |
37 |
} |
38 |
this.setValueDeferred=new dojo.Deferred(); |
39 |
this.inherited(arguments); |
40 |
this.commands=dojo.i18n.getLocalization("dijit._editor","commands",this.lang); |
41 |
if(!this.toolbar){ |
42 |
this.toolbar=new dijit.Toolbar({dir:this.dir,lang:this.lang}); |
43 |
this.header.appendChild(this.toolbar.domNode); |
44 |
} |
45 |
dojo.forEach(this.plugins,this.addPlugin,this); |
46 |
this.setValueDeferred.callback(true); |
47 |
dojo.addClass(this.iframe.parentNode,"dijitEditorIFrameContainer"); |
48 |
dojo.addClass(this.iframe,"dijitEditorIFrame"); |
49 |
dojo.attr(this.iframe,"allowTransparency",true); |
50 |
if(dojo.isWebKit){
|
51 |
dojo.style(this.domNode,"KhtmlUserSelect","none"); |
52 |
} |
53 |
this.toolbar.startup();
|
54 |
this.onNormalizedDisplayChanged();
|
55 |
},destroy:function(){ |
56 |
dojo.forEach(this._plugins,function(p){ |
57 |
if(p&&p.destroy){
|
58 |
p.destroy(); |
59 |
} |
60 |
}); |
61 |
this._plugins=[];
|
62 |
this.toolbar.destroyRecursive();
|
63 |
delete this.toolbar; |
64 |
this.inherited(arguments); |
65 |
},addPlugin:function(_1,_2){ |
66 |
var _3=dojo.isString(_1)?{name:_1}:_1; |
67 |
if(!_3.setEditor){
|
68 |
var o={"args":_3,"plugin":null,"editor":this}; |
69 |
dojo.publish(dijit._scopeName+".Editor.getPlugin",[o]);
|
70 |
if(!o.plugin){
|
71 |
var pc=dojo.getObject(_3.name);
|
72 |
if(pc){
|
73 |
o.plugin=new pc(_3);
|
74 |
} |
75 |
} |
76 |
if(!o.plugin){
|
77 |
console.warn("Cannot find plugin",_1);
|
78 |
return;
|
79 |
} |
80 |
_1=o.plugin; |
81 |
} |
82 |
if(arguments.length>1){ |
83 |
this._plugins[_2]=_1;
|
84 |
}else{
|
85 |
this._plugins.push(_1);
|
86 |
} |
87 |
_1.setEditor(this);
|
88 |
if(dojo.isFunction(_1.setToolbar)){
|
89 |
_1.setToolbar(this.toolbar);
|
90 |
} |
91 |
},startup:function(){ |
92 |
},resize:function(_4){ |
93 |
if(_4){
|
94 |
dijit.layout._LayoutWidget.prototype.resize.apply(this,arguments); |
95 |
} |
96 |
},layout:function(){ |
97 |
var _5=(this._contentBox.h-(this.getHeaderHeight()+this.getFooterHeight()+dojo._getPadBorderExtents(this.iframe.parentNode).h+dojo._getMarginExtents(this.iframe.parentNode).h)); |
98 |
this.editingArea.style.height=_5+"px"; |
99 |
if(this.iframe){ |
100 |
this.iframe.style.height="100%"; |
101 |
} |
102 |
this._layoutMode=true; |
103 |
},_onIEMouseDown:function(e){ |
104 |
var _6;
|
105 |
var b=this.document.body; |
106 |
var _7=b.clientWidth;
|
107 |
var _8=b.clientHeight;
|
108 |
var _9=b.clientLeft;
|
109 |
var _a=b.offsetWidth;
|
110 |
var _b=b.offsetHeight;
|
111 |
var _c=b.offsetLeft;
|
112 |
bodyDir=b.dir?b.dir.toLowerCase():"";
|
113 |
if(bodyDir!="rtl"){ |
114 |
if(_7<_a&&e.x>_7&&e.x<_a){
|
115 |
_6=true;
|
116 |
} |
117 |
}else{
|
118 |
if(e.x<_9&&e.x>_c){
|
119 |
_6=true;
|
120 |
} |
121 |
} |
122 |
if(!_6){
|
123 |
if(_8<_b&&e.y>_8&&e.y<_b){
|
124 |
_6=true;
|
125 |
} |
126 |
} |
127 |
if(!_6){
|
128 |
delete this._cursorToStart; |
129 |
delete this._savedSelection; |
130 |
if(e.target.tagName=="BODY"){ |
131 |
setTimeout(dojo.hitch(this,"placeCursorAtEnd"),0); |
132 |
} |
133 |
this.inherited(arguments); |
134 |
} |
135 |
},onBeforeActivate:function(e){ |
136 |
this._restoreSelection();
|
137 |
},onBeforeDeactivate:function(e){ |
138 |
if(this.customUndo){ |
139 |
this.endEditing(true); |
140 |
} |
141 |
if(e.target.tagName!="BODY"){ |
142 |
this._saveSelection();
|
143 |
} |
144 |
},customUndo:dojo.isIE||dojo.isWebKit,editActionInterval:3,beginEditing:function(_d){ |
145 |
if(!this._inEditing){ |
146 |
this._inEditing=true; |
147 |
this._beginEditing(_d);
|
148 |
} |
149 |
if(this.editActionInterval>0){ |
150 |
if(this._editTimer){ |
151 |
clearTimeout(this._editTimer);
|
152 |
} |
153 |
this._editTimer=setTimeout(dojo.hitch(this,this.endEditing),this._editInterval); |
154 |
} |
155 |
},_steps:[],_undoedSteps:[],execCommand:function(_e){ |
156 |
if(this.customUndo&&(_e=="undo"||_e=="redo")){ |
157 |
return this[_e](); |
158 |
}else{
|
159 |
if(this.customUndo){ |
160 |
this.endEditing();
|
161 |
this._beginEditing();
|
162 |
} |
163 |
var r;
|
164 |
try{
|
165 |
r=this.inherited("execCommand",arguments); |
166 |
if(dojo.isWebKit&&_e=="paste"&&!r){ |
167 |
throw {code:1011}; |
168 |
} |
169 |
} |
170 |
catch(e){
|
171 |
if(e.code==1011&&/copy|cut|paste/.test(_e)){ |
172 |
var _f=dojo.string.substitute,_10={cut:"X",copy:"C",paste:"V"}; |
173 |
alert(_f(this.commands.systemShortcut,[this.commands[_e],_f(this.commands[dojo.isMac?"appleKey":"ctrlKey"],[_10[_e]])])); |
174 |
} |
175 |
r=false;
|
176 |
} |
177 |
if(this.customUndo){ |
178 |
this._endEditing();
|
179 |
} |
180 |
return r;
|
181 |
} |
182 |
},queryCommandEnabled:function(cmd){ |
183 |
if(this.customUndo&&(cmd=="undo"||cmd=="redo")){ |
184 |
return cmd=="undo"?(this._steps.length>1):(this._undoedSteps.length>0); |
185 |
}else{
|
186 |
return this.inherited("queryCommandEnabled",arguments); |
187 |
} |
188 |
},_moveToBookmark:function(b){ |
189 |
var _11=b.mark;
|
190 |
var _12=b.mark;
|
191 |
var col=b.isCollapsed;
|
192 |
var r,_13,_14,sel;
|
193 |
if(_12){
|
194 |
if(dojo.isIE){
|
195 |
if(dojo.isArray(_12)){
|
196 |
_11=[]; |
197 |
dojo.forEach(_12,function(n){
|
198 |
_11.push(dijit.range.getNode(n,this.editNode));
|
199 |
},this);
|
200 |
dojo.withGlobal(this.window,"moveToBookmark",dijit,[{mark:_11,isCollapsed:col}]); |
201 |
}else{
|
202 |
if(_12.startContainer&&_12.endContainer){
|
203 |
sel=dijit.range.getSelection(this.window);
|
204 |
if(sel&&sel.removeAllRanges){
|
205 |
sel.removeAllRanges(); |
206 |
r=dijit.range.create(this.window);
|
207 |
_13=dijit.range.getNode(_12.startContainer,this.editNode);
|
208 |
_14=dijit.range.getNode(_12.endContainer,this.editNode);
|
209 |
if(_13&&_14){
|
210 |
r.setStart(_13,_12.startOffset); |
211 |
r.setEnd(_14,_12.endOffset); |
212 |
sel.addRange(r); |
213 |
} |
214 |
} |
215 |
} |
216 |
} |
217 |
}else{
|
218 |
sel=dijit.range.getSelection(this.window);
|
219 |
if(sel&&sel.removeAllRanges){
|
220 |
sel.removeAllRanges(); |
221 |
r=dijit.range.create(this.window);
|
222 |
_13=dijit.range.getNode(_12.startContainer,this.editNode);
|
223 |
_14=dijit.range.getNode(_12.endContainer,this.editNode);
|
224 |
if(_13&&_14){
|
225 |
r.setStart(_13,_12.startOffset); |
226 |
r.setEnd(_14,_12.endOffset); |
227 |
sel.addRange(r); |
228 |
} |
229 |
} |
230 |
} |
231 |
} |
232 |
},_changeToStep:function(_15,to){ |
233 |
this.setValue(to.text);
|
234 |
var b=to.bookmark;
|
235 |
if(!b){
|
236 |
return;
|
237 |
} |
238 |
this._moveToBookmark(b);
|
239 |
},undo:function(){ |
240 |
var ret=false; |
241 |
if(!this._undoRedoActive){ |
242 |
this._undoRedoActive=true; |
243 |
this.endEditing(true); |
244 |
var s=this._steps.pop(); |
245 |
if(s&&this._steps.length>0){ |
246 |
this.focus();
|
247 |
this._changeToStep(s,this._steps[this._steps.length-1]); |
248 |
this._undoedSteps.push(s);
|
249 |
this.onDisplayChanged();
|
250 |
delete this._undoRedoActive; |
251 |
ret=true;
|
252 |
} |
253 |
delete this._undoRedoActive; |
254 |
} |
255 |
return ret;
|
256 |
},redo:function(){ |
257 |
var ret=false; |
258 |
if(!this._undoRedoActive){ |
259 |
this._undoRedoActive=true; |
260 |
this.endEditing(true); |
261 |
var s=this._undoedSteps.pop(); |
262 |
if(s&&this._steps.length>0){ |
263 |
this.focus();
|
264 |
this._changeToStep(this._steps[this._steps.length-1],s); |
265 |
this._steps.push(s);
|
266 |
this.onDisplayChanged();
|
267 |
ret=true;
|
268 |
} |
269 |
delete this._undoRedoActive; |
270 |
} |
271 |
return ret;
|
272 |
},endEditing:function(_16){ |
273 |
if(this._editTimer){ |
274 |
clearTimeout(this._editTimer);
|
275 |
} |
276 |
if(this._inEditing){ |
277 |
this._endEditing(_16);
|
278 |
this._inEditing=false; |
279 |
} |
280 |
},_getBookmark:function(){ |
281 |
var b=dojo.withGlobal(this.window,dijit.getBookmark); |
282 |
var tmp=[];
|
283 |
if(b&&b.mark){
|
284 |
var _17=b.mark;
|
285 |
if(dojo.isIE){
|
286 |
var sel=dijit.range.getSelection(this.window); |
287 |
if(!dojo.isArray(_17)){
|
288 |
if(sel){
|
289 |
var _18;
|
290 |
if(sel.rangeCount){
|
291 |
_18=sel.getRangeAt(0);
|
292 |
} |
293 |
if(_18){
|
294 |
b.mark=_18.cloneRange(); |
295 |
}else{
|
296 |
b.mark=dojo.withGlobal(this.window,dijit.getBookmark);
|
297 |
} |
298 |
} |
299 |
}else{
|
300 |
dojo.forEach(b.mark,function(n){
|
301 |
tmp.push(dijit.range.getIndex(n,this.editNode).o);
|
302 |
},this);
|
303 |
b.mark=tmp; |
304 |
} |
305 |
} |
306 |
try{
|
307 |
if(b.mark&&b.mark.startContainer){
|
308 |
tmp=dijit.range.getIndex(b.mark.startContainer,this.editNode).o;
|
309 |
b.mark={startContainer:tmp,startOffset:b.mark.startOffset,endContainer:b.mark.endContainer===b.mark.startContainer?tmp:dijit.range.getIndex(b.mark.endContainer,this.editNode).o,endOffset:b.mark.endOffset}; |
310 |
} |
311 |
} |
312 |
catch(e){
|
313 |
b.mark=null;
|
314 |
} |
315 |
} |
316 |
return b;
|
317 |
},_beginEditing:function(cmd){ |
318 |
if(this._steps.length===0){ |
319 |
this._steps.push({"text":dijit._editor.getChildrenHtml(this.editNode),"bookmark":this._getBookmark()}); |
320 |
} |
321 |
},_endEditing:function(_19){ |
322 |
var v=dijit._editor.getChildrenHtml(this.editNode); |
323 |
this._undoedSteps=[];
|
324 |
this._steps.push({text:v,bookmark:this._getBookmark()}); |
325 |
},onKeyDown:function(e){ |
326 |
if(!dojo.isIE&&!this.iframe&&e.keyCode==dojo.keys.TAB&&!this.tabIndent){ |
327 |
this._saveSelection();
|
328 |
} |
329 |
if(!this.customUndo){ |
330 |
this.inherited(arguments); |
331 |
return;
|
332 |
} |
333 |
var k=e.keyCode,ks=dojo.keys;
|
334 |
if(e.ctrlKey&&!e.altKey){
|
335 |
if(k==90||k==122){ |
336 |
dojo.stopEvent(e); |
337 |
this.undo();
|
338 |
return;
|
339 |
}else{
|
340 |
if(k==89||k==121){ |
341 |
dojo.stopEvent(e); |
342 |
this.redo();
|
343 |
return;
|
344 |
} |
345 |
} |
346 |
} |
347 |
this.inherited(arguments); |
348 |
switch(k){
|
349 |
case ks.ENTER:
|
350 |
case ks.BACKSPACE:
|
351 |
case ks.DELETE:
|
352 |
this.beginEditing();
|
353 |
break;
|
354 |
case 88: |
355 |
case 86: |
356 |
if(e.ctrlKey&&!e.altKey&&!e.metaKey){
|
357 |
this.endEditing();
|
358 |
if(e.keyCode==88){ |
359 |
this.beginEditing("cut"); |
360 |
setTimeout(dojo.hitch(this,this.endEditing),1); |
361 |
}else{
|
362 |
this.beginEditing("paste"); |
363 |
setTimeout(dojo.hitch(this,this.endEditing),1); |
364 |
} |
365 |
break;
|
366 |
} |
367 |
default:
|
368 |
if(!e.ctrlKey&&!e.altKey&&!e.metaKey&&(e.keyCode<dojo.keys.F1||e.keyCode>dojo.keys.F15)){
|
369 |
this.beginEditing();
|
370 |
break;
|
371 |
} |
372 |
case ks.ALT:
|
373 |
this.endEditing();
|
374 |
break;
|
375 |
case ks.UP_ARROW:
|
376 |
case ks.DOWN_ARROW:
|
377 |
case ks.LEFT_ARROW:
|
378 |
case ks.RIGHT_ARROW:
|
379 |
case ks.HOME:
|
380 |
case ks.END:
|
381 |
case ks.PAGE_UP:
|
382 |
case ks.PAGE_DOWN:
|
383 |
this.endEditing(true); |
384 |
break;
|
385 |
case ks.CTRL:
|
386 |
case ks.SHIFT:
|
387 |
case ks.TAB:
|
388 |
break;
|
389 |
} |
390 |
},_onBlur:function(){ |
391 |
this.inherited("_onBlur",arguments); |
392 |
this.endEditing(true); |
393 |
},_saveSelection:function(){ |
394 |
this._savedSelection=this._getBookmark(); |
395 |
},_restoreSelection:function(){ |
396 |
if(this._savedSelection){ |
397 |
delete this._cursorToStart; |
398 |
if(dojo.withGlobal(this.window,"isCollapsed",dijit)){ |
399 |
this._moveToBookmark(this._savedSelection); |
400 |
} |
401 |
delete this._savedSelection; |
402 |
} |
403 |
},onClick:function(){ |
404 |
this.endEditing(true); |
405 |
this.inherited(arguments); |
406 |
},_setDisabledAttr:function(_1a){ |
407 |
if(!this.disabled&&_1a){ |
408 |
this._buttonEnabledPlugins=dojo.filter(this._plugins,function(p){ |
409 |
if(p&&p.button&&!p.button.get("disabled")){ |
410 |
p.button.set("disabled",true); |
411 |
return true; |
412 |
} |
413 |
return false; |
414 |
}); |
415 |
}else{
|
416 |
if(this.disabled&&!_1a){ |
417 |
dojo.forEach(this._buttonEnabledPlugins,function(p){ |
418 |
p.button.attr("disabled",false); |
419 |
p.updateState&&p.updateState(); |
420 |
}); |
421 |
} |
422 |
} |
423 |
this.inherited(arguments); |
424 |
},_setStateClass:function(){ |
425 |
this.inherited(arguments); |
426 |
if(this.document&&this.document.body){ |
427 |
dojo.style(this.document.body,"color",dojo.style(this.iframe,"color")); |
428 |
} |
429 |
}}); |
430 |
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){ |
431 |
if(o.plugin){
|
432 |
return;
|
433 |
} |
434 |
var _1b=o.args,p;
|
435 |
var _1c=dijit._editor._Plugin;
|
436 |
var _1d=_1b.name;
|
437 |
switch(_1d){
|
438 |
case "undo": |
439 |
case "redo": |
440 |
case "cut": |
441 |
case "copy": |
442 |
case "paste": |
443 |
case "insertOrderedList": |
444 |
case "insertUnorderedList": |
445 |
case "indent": |
446 |
case "outdent": |
447 |
case "justifyCenter": |
448 |
case "justifyFull": |
449 |
case "justifyLeft": |
450 |
case "justifyRight": |
451 |
case "delete": |
452 |
case "selectAll": |
453 |
case "removeFormat": |
454 |
case "unlink": |
455 |
case "insertHorizontalRule": |
456 |
p=new _1c({command:_1d}); |
457 |
break;
|
458 |
case "bold": |
459 |
case "italic": |
460 |
case "underline": |
461 |
case "strikethrough": |
462 |
case "subscript": |
463 |
case "superscript": |
464 |
p=new _1c({buttonClass:dijit.form.ToggleButton,command:_1d}); |
465 |
break;
|
466 |
case "|": |
467 |
p=new _1c({button:new dijit.ToolbarSeparator(),setEditor:function(_1e){ |
468 |
this.editor=_1e;
|
469 |
}}); |
470 |
} |
471 |
o.plugin=p; |
472 |
}); |
473 |
} |