]> git.wh0rd.org - tt-rss.git/blob - lib/dijit/_editor/plugins/ViewSource.js
add dijit/dojo stuff; initial ui mockup
[tt-rss.git] / lib / dijit / _editor / plugins / ViewSource.js
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.plugins.ViewSource"]){
9 dojo._hasResource["dijit._editor.plugins.ViewSource"]=true;
10 dojo.provide("dijit._editor.plugins.ViewSource");
11 dojo.require("dojo.window");
12 dojo.require("dojo.i18n");
13 dojo.require("dijit._editor._Plugin");
14 dojo.require("dijit.form.Button");
15 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");
16 dojo.declare("dijit._editor.plugins.ViewSource",dijit._editor._Plugin,{stripScripts:true,stripComments:true,stripIFrames:true,readOnly:false,_fsPlugin:null,toggle:function(){
17 if(dojo.isWebKit){
18 this._vsFocused=true;
19 }
20 this.button.set("checked",!this.button.get("checked"));
21 },_initButton:function(){
22 var _1=dojo.i18n.getLocalization("dijit._editor","commands"),_2=this.editor;
23 this.button=new dijit.form.ToggleButton({label:_1["viewSource"],dir:_2.dir,lang:_2.lang,showLabel:false,iconClass:this.iconClassPrefix+" "+this.iconClassPrefix+"ViewSource",tabIndex:"-1",onChange:dojo.hitch(this,"_showSource")});
24 if(dojo.isIE==7){
25 this._ieFixNode=dojo.create("div",{style:{opacity:"0",zIndex:"-1000",position:"absolute",top:"-1000px"}},dojo.body());
26 }
27 this.button.set("readOnly",false);
28 },setEditor:function(_3){
29 this.editor=_3;
30 this._initButton();
31 this.editor.addKeyHandler(dojo.keys.F12,true,true,dojo.hitch(this,function(e){
32 this.button.focus();
33 this.toggle();
34 dojo.stopEvent(e);
35 setTimeout(dojo.hitch(this,function(){
36 this.editor.focus();
37 }),100);
38 }));
39 },_showSource:function(_4){
40 var ed=this.editor;
41 var _5=ed._plugins;
42 var _6;
43 this._sourceShown=_4;
44 var _7=this;
45 try{
46 if(!this.sourceArea){
47 this._createSourceView();
48 }
49 if(_4){
50 ed._sourceQueryCommandEnabled=ed.queryCommandEnabled;
51 ed.queryCommandEnabled=function(_8){
52 var _9=_8.toLowerCase();
53 if(_9==="viewsource"){
54 return true;
55 }else{
56 return false;
57 }
58 };
59 this.editor.onDisplayChanged();
60 _6=ed.get("value");
61 _6=this._filter(_6);
62 ed.set("value",_6);
63 this._pluginList=[];
64 this._disabledPlugins=dojo.filter(_5,function(p){
65 if(p&&p.button&&!p.button.get("disabled")&&!(p instanceof dijit._editor.plugins.ViewSource)){
66 p._vs_updateState=p.updateState;
67 p.updateState=function(){
68 return false;
69 };
70 p.button.set("disabled",true);
71 if(p.command){
72 switch(p.command){
73 case "bold":
74 case "italic":
75 case "underline":
76 case "strikethrough":
77 case "superscript":
78 case "subscript":
79 p.button.set("checked",false);
80 break;
81 default:
82 break;
83 }
84 }
85 return true;
86 }
87 });
88 if(this._fsPlugin){
89 this._fsPlugin._getAltViewNode=function(){
90 return _7.sourceArea;
91 };
92 }
93 this.sourceArea.value=_6;
94 var is=dojo.marginBox(ed.iframe.parentNode);
95 dojo.marginBox(this.sourceArea,{w:is.w,h:is.h});
96 dojo.style(ed.iframe,"display","none");
97 dojo.style(this.sourceArea,{display:"block"});
98 var _a=function(){
99 var vp=dojo.window.getBox();
100 if("_prevW" in this&&"_prevH" in this){
101 if(vp.w===this._prevW&&vp.h===this._prevH){
102 return;
103 }else{
104 this._prevW=vp.w;
105 this._prevH=vp.h;
106 }
107 }else{
108 this._prevW=vp.w;
109 this._prevH=vp.h;
110 }
111 if(this._resizer){
112 clearTimeout(this._resizer);
113 delete this._resizer;
114 }
115 this._resizer=setTimeout(dojo.hitch(this,function(){
116 delete this._resizer;
117 this._resize();
118 }),10);
119 };
120 this._resizeHandle=dojo.connect(window,"onresize",this,_a);
121 setTimeout(dojo.hitch(this,this._resize),100);
122 this.editor.onNormalizedDisplayChanged();
123 }else{
124 if(!ed._sourceQueryCommandEnabled){
125 return;
126 }
127 dojo.disconnect(this._resizeHandle);
128 delete this._resizeHandle;
129 ed.queryCommandEnabled=ed._sourceQueryCommandEnabled;
130 if(!this._readOnly){
131 _6=this.sourceArea.value;
132 _6=this._filter(_6);
133 ed.beginEditing();
134 ed.set("value",_6);
135 ed.endEditing();
136 }
137 dojo.forEach(this._disabledPlugins,function(p){
138 p.button.set("disabled",false);
139 if(p._vs_updateState){
140 p.updateState=p._vs_updateState;
141 }
142 });
143 this._disabledPlugins=null;
144 dojo.style(this.sourceArea,"display","none");
145 dojo.style(ed.iframe,"display","block");
146 delete ed._sourceQueryCommandEnabled;
147 this.editor.onDisplayChanged();
148 }
149 setTimeout(dojo.hitch(this,function(){
150 var _b=ed.domNode.parentNode;
151 if(_b){
152 var _c=dijit.getEnclosingWidget(_b);
153 if(_c&&_c.resize){
154 _c.resize();
155 }
156 }
157 ed.resize();
158 }),300);
159 }
160 catch(e){
161 }
162 },_resize:function(){
163 var ed=this.editor;
164 var _d=ed.getHeaderHeight();
165 var fH=ed.getFooterHeight();
166 var eb=dojo.position(ed.domNode);
167 var _e=dojo._getPadBorderExtents(ed.iframe.parentNode);
168 var _f=dojo._getMarginExtents(ed.iframe.parentNode);
169 var _10=dojo._getPadBorderExtents(ed.domNode);
170 var _11=dojo._getMarginExtents(ed.domNode);
171 var edb={w:eb.w-(_10.w+_11.w),h:eb.h-(_d+_10.h+_11.h+fH)};
172 if(this._fsPlugin&&this._fsPlugin.isFullscreen){
173 var vp=dojo.window.getBox();
174 edb.w=(vp.w-_10.w);
175 edb.h=(vp.h-(_d+_10.h+fH));
176 }
177 if(dojo.isIE){
178 edb.h-=2;
179 }
180 if(this._ieFixNode){
181 var _12=-this._ieFixNode.offsetTop/1000;
182 edb.w=Math.floor((edb.w+0.9)/_12);
183 edb.h=Math.floor((edb.h+0.9)/_12);
184 }
185 dojo.marginBox(this.sourceArea,{w:edb.w-(_e.w+_f.w),h:edb.h-(_e.h+_f.h)});
186 dojo.marginBox(ed.iframe.parentNode,{h:edb.h});
187 },_createSourceView:function(){
188 var ed=this.editor;
189 var _13=ed._plugins;
190 this.sourceArea=dojo.create("textarea");
191 if(this.readOnly){
192 dojo.attr(this.sourceArea,"readOnly",true);
193 this._readOnly=true;
194 }
195 dojo.style(this.sourceArea,{padding:"0px",margin:"0px",borderWidth:"0px",borderStyle:"none"});
196 dojo.place(this.sourceArea,ed.iframe,"before");
197 if(dojo.isIE&&ed.iframe.parentNode.lastChild!==ed.iframe){
198 dojo.style(ed.iframe.parentNode.lastChild,{width:"0px",height:"0px",padding:"0px",margin:"0px",borderWidth:"0px",borderStyle:"none"});
199 }
200 ed._viewsource_oldFocus=ed.focus;
201 var _14=this;
202 ed.focus=function(){
203 if(_14._sourceShown){
204 _14.setSourceAreaCaret();
205 }else{
206 try{
207 if(this._vsFocused){
208 delete this._vsFocused;
209 dijit.focus(ed.editNode);
210 }else{
211 ed._viewsource_oldFocus();
212 }
213 }
214 catch(e){
215 }
216 }
217 };
218 var i,p;
219 for(i=0;i<_13.length;i++){
220 p=_13[i];
221 if(p&&(p.declaredClass==="dijit._editor.plugins.FullScreen"||p.declaredClass===(dijit._scopeName+"._editor.plugins.FullScreen"))){
222 this._fsPlugin=p;
223 break;
224 }
225 }
226 if(this._fsPlugin){
227 this._fsPlugin._viewsource_getAltViewNode=this._fsPlugin._getAltViewNode;
228 this._fsPlugin._getAltViewNode=function(){
229 return _14._sourceShown?_14.sourceArea:this._viewsource_getAltViewNode();
230 };
231 }
232 this.connect(this.sourceArea,"onkeydown",dojo.hitch(this,function(e){
233 if(this._sourceShown&&e.keyCode==dojo.keys.F12&&e.ctrlKey&&e.shiftKey){
234 this.button.focus();
235 this.button.set("checked",false);
236 setTimeout(dojo.hitch(this,function(){
237 ed.focus();
238 }),100);
239 dojo.stopEvent(e);
240 }
241 }));
242 },_stripScripts:function(_15){
243 if(_15){
244 _15=_15.replace(/<\s*script[^>]*>((.|\s)*?)<\\?\/\s*script\s*>/ig,"");
245 _15=_15.replace(/<\s*script\b([^<>]|\s)*>?/ig,"");
246 _15=_15.replace(/<[^>]*=(\s|)*[("|')]javascript:[^$1][(\s|.)]*[$1][^>]*>/ig,"");
247 }
248 return _15;
249 },_stripComments:function(_16){
250 if(_16){
251 _16=_16.replace(/<!--(.|\s){1,}?-->/g,"");
252 }
253 return _16;
254 },_stripIFrames:function(_17){
255 if(_17){
256 _17=_17.replace(/<\s*iframe[^>]*>((.|\s)*?)<\\?\/\s*iframe\s*>/ig,"");
257 }
258 return _17;
259 },_filter:function(_18){
260 if(_18){
261 if(this.stripScripts){
262 _18=this._stripScripts(_18);
263 }
264 if(this.stripComments){
265 _18=this._stripComments(_18);
266 }
267 if(this.stripIFrames){
268 _18=this._stripIFrames(_18);
269 }
270 }
271 return _18;
272 },setSourceAreaCaret:function(){
273 var win=dojo.global;
274 var _19=this.sourceArea;
275 dijit.focus(_19);
276 if(this._sourceShown&&!this.readOnly){
277 if(dojo.isIE){
278 if(this.sourceArea.createTextRange){
279 var _1a=_19.createTextRange();
280 _1a.collapse(true);
281 _1a.moveStart("character",-99999);
282 _1a.moveStart("character",0);
283 _1a.moveEnd("character",0);
284 _1a.select();
285 }
286 }else{
287 if(win.getSelection){
288 if(_19.setSelectionRange){
289 _19.setSelectionRange(0,0);
290 }
291 }
292 }
293 }
294 },destroy:function(){
295 if(this._ieFixNode){
296 dojo.body().removeChild(this._ieFixNode);
297 }
298 if(this._resizer){
299 clearTimeout(this._resizer);
300 delete this._resizer;
301 }
302 if(this._resizeHandle){
303 dojo.disconnect(this._resizeHandle);
304 delete this._resizeHandle;
305 }
306 this.inherited(arguments);
307 }});
308 dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
309 if(o.plugin){
310 return;
311 }
312 var _1b=o.args.name.toLowerCase();
313 if(_1b==="viewsource"){
314 o.plugin=new dijit._editor.plugins.ViewSource({readOnly:("readOnly" in o.args)?o.args.readOnly:false,stripComments:("stripComments" in o.args)?o.args.stripComments:true,stripScripts:("stripScripts" in o.args)?o.args.stripScripts:true,stripIFrames:("stripIFrames" in o.args)?o.args.stripIFrames:true});
315 }
316 });
317 }