root / trunk / web / dojo / dijit / _editor / plugins / FontChoice.js
History | View | Annotate | Download (7.73 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.plugins.FontChoice"]){ |
9 |
dojo._hasResource["dijit._editor.plugins.FontChoice"]=true; |
10 |
dojo.provide("dijit._editor.plugins.FontChoice");
|
11 |
dojo.require("dijit._editor._Plugin");
|
12 |
dojo.require("dijit._editor.range");
|
13 |
dojo.require("dijit._editor.selection");
|
14 |
dojo.require("dijit.form.FilteringSelect");
|
15 |
dojo.require("dojo.data.ItemFileReadStore");
|
16 |
dojo.require("dojo.i18n");
|
17 |
dojo.requireLocalization("dijit._editor","FontChoice",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"); |
18 |
dojo.declare("dijit._editor.plugins._FontDropDown",[dijit._Widget,dijit._Templated],{label:"",widgetsInTemplate:true,plainText:false,templateString:"<span style='white-space: nowrap' class='dijit dijitReset dijitInline'>"+"<label class='dijitLeft dijitInline' for='${selectId}'>${label}</label>"+"<input dojoType='dijit.form.FilteringSelect' required=false labelType=html labelAttr=label searchAttr=name "+"tabIndex='-1' id='${selectId}' dojoAttachPoint='select' value=''/>"+"</span>",postMixInProperties:function(){ |
19 |
this.inherited(arguments); |
20 |
this.strings=dojo.i18n.getLocalization("dijit._editor","FontChoice"); |
21 |
this.label=this.strings[this.command]; |
22 |
this.id=dijit.getUniqueId(this.declaredClass.replace(/\./g,"_")); |
23 |
this.selectId=this.id+"_select"; |
24 |
this.inherited(arguments); |
25 |
},postCreate:function(){ |
26 |
var _1=dojo.map(this.values,function(_2){ |
27 |
var _3=this.strings[_2]||_2; |
28 |
return {label:this.getLabel(_2,_3),name:_3,value:_2}; |
29 |
},this);
|
30 |
this.select.store=new dojo.data.ItemFileReadStore({data:{identifier:"value",items:_1}}); |
31 |
this.select.set("value","",false); |
32 |
this.disabled=this.select.get("disabled"); |
33 |
},_setValueAttr:function(_4,_5){ |
34 |
_5=_5!==false?true:false; |
35 |
this.select.set("value",dojo.indexOf(this.values,_4)<0?"":_4,_5); |
36 |
if(!_5){
|
37 |
this.select._lastValueReported=null; |
38 |
} |
39 |
},_getValueAttr:function(){ |
40 |
return this.select.get("value"); |
41 |
},focus:function(){ |
42 |
this.select.focus();
|
43 |
},_setDisabledAttr:function(_6){ |
44 |
this.disabled=_6;
|
45 |
this.select.set("disabled",_6); |
46 |
}}); |
47 |
dojo.declare("dijit._editor.plugins._FontNameDropDown",dijit._editor.plugins._FontDropDown,{generic:false,command:"fontName",postMixInProperties:function(){ |
48 |
if(!this.values){ |
49 |
this.values=this.generic?["serif","sans-serif","monospace","cursive","fantasy"]:["Arial","Times New Roman","Comic Sans MS","Courier New"]; |
50 |
} |
51 |
this.inherited(arguments); |
52 |
},getLabel:function(_7,_8){ |
53 |
if(this.plainText){ |
54 |
return _8;
|
55 |
}else{
|
56 |
return "<div style='font-family: "+_7+"'>"+_8+"</div>"; |
57 |
} |
58 |
},_setValueAttr:function(_9,_a){ |
59 |
_a=_a!==false?true:false; |
60 |
if(this.generic){ |
61 |
var _b={"Arial":"sans-serif","Helvetica":"sans-serif","Myriad":"sans-serif","Times":"serif","Times New Roman":"serif","Comic Sans MS":"cursive","Apple Chancery":"cursive","Courier":"monospace","Courier New":"monospace","Papyrus":"fantasy"}; |
62 |
_9=_b[_9]||_9; |
63 |
} |
64 |
this.inherited(arguments,[_9,_a]); |
65 |
}}); |
66 |
dojo.declare("dijit._editor.plugins._FontSizeDropDown",dijit._editor.plugins._FontDropDown,{command:"fontSize",values:[1,2,3,4,5,6,7],getLabel:function(_c,_d){ |
67 |
if(this.plainText){ |
68 |
return _d;
|
69 |
}else{
|
70 |
return "<font size="+_c+"'>"+_d+"</font>"; |
71 |
} |
72 |
},_setValueAttr:function(_e,_f){ |
73 |
_f=_f!==false?true:false; |
74 |
if(_e.indexOf&&_e.indexOf("px")!=-1){ |
75 |
var _10=parseInt(_e,10); |
76 |
_e={10:1,13:2,16:3,18:4,24:5,32:6,48:7}[_10]||_e; |
77 |
} |
78 |
this.inherited(arguments,[_e,_f]); |
79 |
}}); |
80 |
dojo.declare("dijit._editor.plugins._FormatBlockDropDown",dijit._editor.plugins._FontDropDown,{command:"formatBlock",values:["noFormat","p","h1","h2","h3","pre"],postCreate:function(){ |
81 |
this.inherited(arguments); |
82 |
this.set("value","noFormat",false); |
83 |
},getLabel:function(_11,_12){ |
84 |
if(this.plainText){ |
85 |
return _12;
|
86 |
}else{
|
87 |
return "<"+_11+">"+_12+"</"+_11+">"; |
88 |
} |
89 |
},_execCommand:function(_13,_14,_15){ |
90 |
if(_15==="noFormat"){ |
91 |
var _16;
|
92 |
var end;
|
93 |
var sel=dijit.range.getSelection(_13.window);
|
94 |
if(sel&&sel.rangeCount>0){ |
95 |
var _17=sel.getRangeAt(0); |
96 |
var _18,tag;
|
97 |
if(_17){
|
98 |
_16=_17.startContainer; |
99 |
end=_17.endContainer; |
100 |
while(_16&&_16!==_13.editNode&&_16!==_13.document.body&&_16.nodeType!==1){ |
101 |
_16=_16.parentNode; |
102 |
} |
103 |
while(end&&end!==_13.editNode&&end!==_13.document.body&&end.nodeType!==1){ |
104 |
end=end.parentNode; |
105 |
} |
106 |
var _19=dojo.hitch(this,function(_1a,_1b){ |
107 |
if(_1a.childNodes&&_1a.childNodes.length){
|
108 |
var i;
|
109 |
for(i=0;i<_1a.childNodes.length;i++){ |
110 |
var c=_1a.childNodes[i];
|
111 |
if(c.nodeType==1){ |
112 |
if(dojo.withGlobal(_13.window,"inSelection",dijit._editor.selection,[c])){ |
113 |
var tag=c.tagName?c.tagName.toLowerCase():""; |
114 |
if(dojo.indexOf(this.values,tag)!==-1){ |
115 |
_1b.push(c); |
116 |
} |
117 |
_19(c,_1b); |
118 |
} |
119 |
} |
120 |
} |
121 |
} |
122 |
}); |
123 |
var _1c=dojo.hitch(this,function(_1d){ |
124 |
if(_1d&&_1d.length){
|
125 |
_13.beginEditing(); |
126 |
while(_1d.length){
|
127 |
this._removeFormat(_13,_1d.pop());
|
128 |
} |
129 |
_13.endEditing(); |
130 |
} |
131 |
}); |
132 |
var _1e=[];
|
133 |
if(_16==end){
|
134 |
var _1f;
|
135 |
_18=_16; |
136 |
while(_18&&_18!==_13.editNode&&_18!==_13.document.body){
|
137 |
if(_18.nodeType==1){ |
138 |
tag=_18.tagName?_18.tagName.toLowerCase():"";
|
139 |
if(dojo.indexOf(this.values,tag)!==-1){ |
140 |
_1f=_18; |
141 |
break;
|
142 |
} |
143 |
} |
144 |
_18=_18.parentNode; |
145 |
} |
146 |
_19(_16,_1e); |
147 |
if(_1f){
|
148 |
_1e=[_1f].concat(_1e); |
149 |
} |
150 |
_1c(_1e); |
151 |
}else{
|
152 |
_18=_16; |
153 |
while(dojo.withGlobal(_13.window,"inSelection",dijit._editor.selection,[_18])){ |
154 |
if(_18.nodeType==1){ |
155 |
tag=_18.tagName?_18.tagName.toLowerCase():"";
|
156 |
if(dojo.indexOf(this.values,tag)!==-1){ |
157 |
_1e.push(_18); |
158 |
} |
159 |
_19(_18,_1e); |
160 |
} |
161 |
_18=_18.nextSibling; |
162 |
} |
163 |
_1c(_1e); |
164 |
} |
165 |
_13.onDisplayChanged(); |
166 |
} |
167 |
} |
168 |
}else{
|
169 |
_13.execCommand(_14,_15); |
170 |
} |
171 |
},_removeFormat:function(_20,_21){ |
172 |
if(_20.customUndo){
|
173 |
while(_21.firstChild){
|
174 |
dojo.place(_21.firstChild,_21,"before");
|
175 |
} |
176 |
_21.parentNode.removeChild(_21); |
177 |
}else{
|
178 |
dojo.withGlobal(_20.window,"selectElementChildren",dijit._editor.selection,[_21]);
|
179 |
var _22=dojo.withGlobal(_20.window,"getSelectedHtml",dijit._editor.selection,[null]); |
180 |
dojo.withGlobal(_20.window,"selectElement",dijit._editor.selection,[_21]);
|
181 |
_20.execCommand("inserthtml",_22||""); |
182 |
} |
183 |
}}); |
184 |
dojo.declare("dijit._editor.plugins.FontChoice",dijit._editor._Plugin,{useDefaultCommand:false,_initButton:function(){ |
185 |
var _23={fontName:dijit._editor.plugins._FontNameDropDown,fontSize:dijit._editor.plugins._FontSizeDropDown,formatBlock:dijit._editor.plugins._FormatBlockDropDown}[this.command],_24=this.params; |
186 |
if(this.params.custom){ |
187 |
_24.values=this.params.custom;
|
188 |
} |
189 |
var _25=this.editor; |
190 |
this.button=new _23(dojo.delegate({dir:_25.dir,lang:_25.lang},_24)); |
191 |
this.connect(this.button.select,"onChange",function(_26){ |
192 |
this.editor.focus();
|
193 |
if(this.command=="fontName"&&_26.indexOf(" ")!=-1){ |
194 |
_26="'"+_26+"'"; |
195 |
} |
196 |
if(this.button._execCommand){ |
197 |
this.button._execCommand(this.editor,this.command,_26); |
198 |
}else{
|
199 |
this.editor.execCommand(this.command,_26); |
200 |
} |
201 |
this.editor.customUndo=this.editor.customUndo||dojo.isWebKit; |
202 |
}); |
203 |
},updateState:function(){ |
204 |
var _27=this.editor; |
205 |
var _28=this.command; |
206 |
if(!_27||!_27.isLoaded||!_28.length){
|
207 |
return;
|
208 |
} |
209 |
if(this.button){ |
210 |
var _29;
|
211 |
try{
|
212 |
_29=_27.queryCommandValue(_28)||"";
|
213 |
} |
214 |
catch(e){
|
215 |
_29="";
|
216 |
} |
217 |
var _2a=dojo.isString(_29)&&_29.match(/'([^']*)'/); |
218 |
if(_2a){
|
219 |
_29=_2a[1];
|
220 |
} |
221 |
if(_28==="formatBlock"){ |
222 |
if(!_29||_29=="p"){ |
223 |
_29=null;
|
224 |
var _2b;
|
225 |
var sel=dijit.range.getSelection(this.editor.window); |
226 |
if(sel&&sel.rangeCount>0){ |
227 |
var _2c=sel.getRangeAt(0); |
228 |
if(_2c){
|
229 |
_2b=_2c.endContainer; |
230 |
} |
231 |
} |
232 |
while(_2b&&_2b!==_27.editNode&&_2b!==_27.document){
|
233 |
var tg=_2b.tagName?_2b.tagName.toLowerCase():""; |
234 |
if(tg&&dojo.indexOf(this.button.values,tg)>-1){ |
235 |
_29=tg; |
236 |
break;
|
237 |
} |
238 |
_2b=_2b.parentNode; |
239 |
} |
240 |
if(!_29){
|
241 |
_29="noFormat";
|
242 |
} |
243 |
}else{
|
244 |
if(dojo.indexOf(this.button.values,_29)<0){ |
245 |
_29="noFormat";
|
246 |
} |
247 |
} |
248 |
} |
249 |
if(_29!==this.button.get("value")){ |
250 |
this.button.set("value",_29,false); |
251 |
} |
252 |
} |
253 |
}}); |
254 |
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){ |
255 |
if(o.plugin){
|
256 |
return;
|
257 |
} |
258 |
switch(o.args.name){
|
259 |
case "fontName": |
260 |
case "fontSize": |
261 |
case "formatBlock": |
262 |
o.plugin=new dijit._editor.plugins.FontChoice({command:o.args.name,plainText:o.args.plainText?o.args.plainText:false}); |
263 |
} |
264 |
}); |
265 |
} |