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