root / trunk / web / dojo / dojox / editor / plugins / InsertAnchor.js
History | View | Annotate | Download (8.03 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["dojox.editor.plugins.InsertAnchor"]){ |
9 |
dojo._hasResource["dojox.editor.plugins.InsertAnchor"]=true; |
10 |
dojo.provide("dojox.editor.plugins.InsertAnchor");
|
11 |
dojo.require("dijit._Widget");
|
12 |
dojo.require("dijit._Templated");
|
13 |
dojo.require("dijit._editor._Plugin");
|
14 |
dojo.require("dijit.TooltipDialog");
|
15 |
dojo.require("dijit.form.Button");
|
16 |
dojo.require("dijit.form.ValidationTextBox");
|
17 |
dojo.require("dijit.form.Select");
|
18 |
dojo.require("dijit._editor.range");
|
19 |
dojo.require("dojo.i18n");
|
20 |
dojo.require("dojo.string");
|
21 |
dojo.requireLocalization("dijit","common",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.requireLocalization("dojox.editor.plugins","InsertAnchor",null,"ROOT,ro"); |
23 |
dojo.declare("dojox.editor.plugins.InsertAnchor",dijit._editor._Plugin,{htmlTemplate:"<a name=\"${anchorInput}\" class=\"dijitEditorPluginInsertAnchorStyle\">${textInput}</a>",iconClassPrefix:"dijitAdditionalEditorIcon",_template:["<table><tr><td>","<label for='${id}_anchorInput'>${anchor}</label>","</td><td>","<input dojoType='dijit.form.ValidationTextBox' required='true' "+"id='${id}_anchorInput' name='anchorInput' intermediateChanges='true'>","</td></tr><tr><td>","<label for='${id}_textInput'>${text}</label>","</td><td>","<input dojoType='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' "+"name='textInput' intermediateChanges='true'>","</td></tr>","<tr><td colspan='2'>","<button dojoType='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>","<button dojoType='dijit.form.Button' type='button' id='${id}_cancelButton'>${cancel}</button>","</td></tr></table>"].join(""),_initButton:function(){ |
24 |
var _1=this; |
25 |
var _2=dojo.i18n.getLocalization("dojox.editor.plugins","InsertAnchor",this.lang); |
26 |
var _3=(this.dropDown=new dijit.TooltipDialog({title:_2["title"],execute:dojo.hitch(this,"setValue"),onOpen:function(){ |
27 |
_1._onOpenDialog(); |
28 |
dijit.TooltipDialog.prototype.onOpen.apply(this,arguments); |
29 |
},onCancel:function(){ |
30 |
setTimeout(dojo.hitch(_1,"_onCloseDialog"),0); |
31 |
}})); |
32 |
this.button=new dijit.form.DropDownButton({label:_2["insertAnchor"],showLabel:false,iconClass:this.iconClassPrefix+" "+this.iconClassPrefix+"InsertAnchor",tabIndex:"-1",dropDown:this.dropDown}); |
33 |
_2.id=dijit.getUniqueId(this.editor.id);
|
34 |
this._uniqueId=_2.id;
|
35 |
this.dropDown.set("content",_3.title+"<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>"+dojo.string.substitute(this._template,_2)); |
36 |
_3.startup(); |
37 |
this._anchorInput=dijit.byId(this._uniqueId+"_anchorInput"); |
38 |
this._textInput=dijit.byId(this._uniqueId+"_textInput"); |
39 |
this._setButton=dijit.byId(this._uniqueId+"_setButton"); |
40 |
this.connect(dijit.byId(this._uniqueId+"_cancelButton"),"onClick",function(){ |
41 |
this.dropDown.onCancel();
|
42 |
}); |
43 |
if(this._anchorInput){ |
44 |
this.connect(this._anchorInput,"onChange","_checkInput"); |
45 |
} |
46 |
if(this._textInput){ |
47 |
this.connect(this._anchorInput,"onChange","_checkInput"); |
48 |
} |
49 |
this.editor.contentDomPreFilters.push(dojo.hitch(this,this._preDomFilter)); |
50 |
this.editor.contentDomPostFilters.push(dojo.hitch(this,this._postDomFilter)); |
51 |
this._setup();
|
52 |
},setEditor:function(_4){ |
53 |
this.editor=_4;
|
54 |
this._initButton();
|
55 |
},_checkInput:function(){ |
56 |
var _5=true; |
57 |
if(this._anchorInput.isValid()){ |
58 |
_5=false;
|
59 |
} |
60 |
this._setButton.set("disabled",_5); |
61 |
},_setup:function(){ |
62 |
this.editor.onLoadDeferred.addCallback(dojo.hitch(this,function(){ |
63 |
this.connect(this.editor.editNode,"ondblclick",this._onDblClick); |
64 |
setTimeout(dojo.hitch(this,function(){ |
65 |
this._applyStyles();
|
66 |
}),100);
|
67 |
})); |
68 |
},getAnchorStyle:function(){ |
69 |
var _6="@media screen {\n"+"\t.dijitEditorPluginInsertAnchorStyle {\n"+"\t\tbackground-image: url({MODURL}/images/anchor.gif);\n"+"\t\tbackground-repeat: no-repeat;\n"+"\t\tbackground-position: top left;\n"+"\t\tborder-width: 1px;\n"+"\t\tborder-style: dashed;\n"+"\t\tborder-color: #D0D0D0;\n"+"\t\tpadding-left: 20px;\n"+"\t}\n"+"}\n"; |
70 |
var _7=dojo.moduleUrl(dojox._scopeName,"editor/plugins/resources").toString(); |
71 |
if(!(_7.match(/^https?:\/\//i))&&!(_7.match(/^file:\/\//i))){ |
72 |
var _8;
|
73 |
if(_7.charAt(0)==="/"){ |
74 |
var _9=dojo.doc.location.protocol;
|
75 |
var _a=dojo.doc.location.host;
|
76 |
_8=_9+"//"+_a;
|
77 |
}else{
|
78 |
_8=this._calcBaseUrl(dojo.global.location.href);
|
79 |
} |
80 |
if(_8[_8.length-1]!=="/"&&_7.charAt(0)!=="/"){ |
81 |
_8+="/";
|
82 |
} |
83 |
_7=_8+_7; |
84 |
} |
85 |
return _6.replace(/\{MODURL\}/gi,_7); |
86 |
},_applyStyles:function(){ |
87 |
if(!this._styled){ |
88 |
try{
|
89 |
this._styled=true; |
90 |
var _b=this.editor.document; |
91 |
var _c=this.getAnchorStyle(); |
92 |
if(!dojo.isIE){
|
93 |
var _d=_b.createElement("style"); |
94 |
_d.appendChild(_b.createTextNode(_c)); |
95 |
_b.getElementsByTagName("head")[0].appendChild(_d); |
96 |
}else{
|
97 |
var ss=_b.createStyleSheet(""); |
98 |
ss.cssText=_c; |
99 |
} |
100 |
} |
101 |
catch(e){
|
102 |
} |
103 |
} |
104 |
},_calcBaseUrl:function(_e){ |
105 |
var _f=null; |
106 |
if(_e!==null){ |
107 |
var _10=_e.indexOf("?"); |
108 |
if(_10!=-1){ |
109 |
_e=_e.substring(0,_10);
|
110 |
} |
111 |
_10=_e.lastIndexOf("/");
|
112 |
if(_10>0&&_10<_e.length){ |
113 |
_f=_e.substring(0,_10);
|
114 |
}else{
|
115 |
_f=_e; |
116 |
} |
117 |
} |
118 |
return _f;
|
119 |
},_checkValues:function(_11){ |
120 |
if(_11){
|
121 |
if(_11.anchorInput){
|
122 |
_11.anchorInput=_11.anchorInput.replace(/"/g,"""); |
123 |
} |
124 |
if(!_11.textInput){
|
125 |
_11.textInput=" ";
|
126 |
} |
127 |
} |
128 |
return _11;
|
129 |
},setValue:function(_12){ |
130 |
this._onCloseDialog();
|
131 |
if(!this.editor.window.getSelection){ |
132 |
var sel=dijit.range.getSelection(this.editor.window); |
133 |
var _13=sel.getRangeAt(0); |
134 |
var a=_13.endContainer;
|
135 |
if(a.nodeType===3){ |
136 |
a=a.parentNode; |
137 |
} |
138 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()!=="a")){ |
139 |
a=dojo.withGlobal(this.editor.window,"getSelectedElement",dijit._editor.selection,["a"]); |
140 |
} |
141 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()==="a")){ |
142 |
if(this.editor.queryCommandEnabled("unlink")){ |
143 |
dojo.withGlobal(this.editor.window,"selectElementChildren",dijit._editor.selection,[a]); |
144 |
this.editor.execCommand("unlink"); |
145 |
} |
146 |
} |
147 |
} |
148 |
_12=this._checkValues(_12);
|
149 |
this.editor.execCommand("inserthtml",dojo.string.substitute(this.htmlTemplate,_12)); |
150 |
},_onCloseDialog:function(){ |
151 |
this.editor.focus();
|
152 |
},_getCurrentValues:function(a){ |
153 |
var _14,_15;
|
154 |
if(a&&a.tagName.toLowerCase()==="a"&&dojo.attr(a,"name")){ |
155 |
_14=dojo.attr(a,"name");
|
156 |
_15=a.textContent||a.innerText; |
157 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[a,true]); |
158 |
}else{
|
159 |
_15=dojo.withGlobal(this.editor.window,dijit._editor.selection.getSelectedText);
|
160 |
} |
161 |
return {anchorInput:_14||"",textInput:_15||""}; |
162 |
},_onOpenDialog:function(){ |
163 |
var a;
|
164 |
if(!this.editor.window.getSelection){ |
165 |
var sel=dijit.range.getSelection(this.editor.window); |
166 |
var _16=sel.getRangeAt(0); |
167 |
a=_16.endContainer; |
168 |
if(a.nodeType===3){ |
169 |
a=a.parentNode; |
170 |
} |
171 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()!=="a")){ |
172 |
a=dojo.withGlobal(this.editor.window,"getSelectedElement",dijit._editor.selection,["a"]); |
173 |
} |
174 |
}else{
|
175 |
a=dojo.withGlobal(this.editor.window,"getAncestorElement",dijit._editor.selection,["a"]); |
176 |
} |
177 |
this.dropDown.reset();
|
178 |
this._setButton.set("disabled",true); |
179 |
this.dropDown.set("value",this._getCurrentValues(a)); |
180 |
},_onDblClick:function(e){ |
181 |
if(e&&e.target){
|
182 |
var t=e.target;
|
183 |
var tg=t.tagName?t.tagName.toLowerCase():""; |
184 |
if(tg==="a"&&dojo.attr(t,"name")){ |
185 |
this.editor.onDisplayChanged();
|
186 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[t]); |
187 |
setTimeout(dojo.hitch(this,function(){ |
188 |
this.button.set("disabled",false); |
189 |
this.button.openDropDown();
|
190 |
}),10);
|
191 |
} |
192 |
} |
193 |
},_preDomFilter:function(_17){ |
194 |
var ed=this.editor; |
195 |
dojo.withGlobal(ed.window,function(){
|
196 |
dojo.query("a",ed.editNode).forEach(function(a){ |
197 |
if(dojo.attr(a,"name")&&!dojo.attr(a,"href")){ |
198 |
if(!dojo.hasClass(a,"dijitEditorPluginInsertAnchorStyle")){ |
199 |
dojo.addClass(a,"dijitEditorPluginInsertAnchorStyle");
|
200 |
} |
201 |
} |
202 |
}); |
203 |
}); |
204 |
},_postDomFilter:function(_18){ |
205 |
var ed=this.editor; |
206 |
dojo.withGlobal(ed.window,function(){
|
207 |
dojo.query("a",_18).forEach(function(a){ |
208 |
if(dojo.attr(a,"name")&&!dojo.attr(a,"href")){ |
209 |
if(dojo.hasClass(a,"dijitEditorPluginInsertAnchorStyle")){ |
210 |
dojo.removeClass(a,"dijitEditorPluginInsertAnchorStyle");
|
211 |
} |
212 |
} |
213 |
}); |
214 |
}); |
215 |
return _18;
|
216 |
}}); |
217 |
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){ |
218 |
if(o.plugin){
|
219 |
return;
|
220 |
} |
221 |
var _19=o.args.name;
|
222 |
if(_19){
|
223 |
_19=_19.toLowerCase(); |
224 |
} |
225 |
if(_19==="insertanchor"){ |
226 |
o.plugin=new dojox.editor.plugins.InsertAnchor();
|
227 |
} |
228 |
}); |
229 |
} |