root / trunk / web / dojo / dijit / _editor / plugins / LinkDialog.js
History | View | Annotate | Download (10.8 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.LinkDialog"]){ |
9 |
dojo._hasResource["dijit._editor.plugins.LinkDialog"]=true; |
10 |
dojo.provide("dijit._editor.plugins.LinkDialog");
|
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("dijit._editor","LinkDialog",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"); |
23 |
dojo.declare("dijit._editor.plugins.LinkDialog",dijit._editor._Plugin,{buttonClass:dijit.form.DropDownButton,useDefaultCommand:false,urlRegExp:"((https?|ftps?|file)\\://|./|/|)(/[a-zA-Z]{1,1}:/|)(((?:(?:[\\da-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)\\.)*(?:[a-zA-Z](?:[-\\da-zA-Z]{0,80}[\\da-zA-Z])?)\\.?)|(((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])|(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]|(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]|(0|[1-9]\\d{0,8}|[1-3]\\d{9}|4[01]\\d{8}|42[0-8]\\d{7}|429[0-3]\\d{6}|4294[0-8]\\d{5}|42949[0-5]\\d{4}|429496[0-6]\\d{3}|4294967[01]\\d{2}|42949672[0-8]\\d|429496729[0-5])|0[xX]0*[\\da-fA-F]{1,8}|([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}|([\\da-fA-F]{1,4}\\:){6}((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])))(\\:\\d+)?(/(?:[^?#\\s/]+/)*(?:[^?#\\s/]+(?:\\?[^?#\\s/]*)?(?:#.*)?)?)?",emailRegExp:"<?(mailto\\:)([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+"+"@"+"((?:(?:[\\da-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)\\.)+(?:[a-zA-Z](?:[-\\da-zA-Z]{0,6}[\\da-zA-Z])?)\\.?)|localhost|^[^-][a-zA-Z0-9_-]*>?",htmlTemplate:"<a href=\"${urlInput}\" _djrealurl=\"${urlInput}\""+" target=\"${targetSelect}\""+">${textInput}</a>",tag:"a",_hostRxp:new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$"),_userAtRxp:new RegExp("^([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+@","i"),linkDialogTemplate:["<table><tr><td>","<label for='${id}_urlInput'>${url}</label>","</td><td>","<input dojoType='dijit.form.ValidationTextBox' required='true' "+"id='${id}_urlInput' name='urlInput' 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>","<label for='${id}_targetSelect'>${target}</label>","</td><td>","<select id='${id}_targetSelect' name='targetSelect' dojoType='dijit.form.Select'>","<option selected='selected' value='_self'>${currentWindow}</option>","<option value='_blank'>${newWindow}</option>","<option value='_top'>${topWindow}</option>","<option value='_parent'>${parentWindow}</option>","</select>","</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'>${buttonCancel}</button>","</td></tr></table>"].join(""),_initButton:function(){ |
24 |
var _1=this; |
25 |
this.tag=this.command=="insertImage"?"img":"a"; |
26 |
var _2=dojo.mixin(dojo.i18n.getLocalization("dijit","common",this.lang),dojo.i18n.getLocalization("dijit._editor","LinkDialog",this.lang)); |
27 |
var _3=(this.dropDown=new dijit.TooltipDialog({title:_2[this.command+"Title"],execute:dojo.hitch(this,"setValue"),onOpen:function(){ |
28 |
_1._onOpenDialog(); |
29 |
dijit.TooltipDialog.prototype.onOpen.apply(this,arguments); |
30 |
},onCancel:function(){ |
31 |
setTimeout(dojo.hitch(_1,"_onCloseDialog"),0); |
32 |
}})); |
33 |
_2.urlRegExp=this.urlRegExp;
|
34 |
_2.id=dijit.getUniqueId(this.editor.id);
|
35 |
this._uniqueId=_2.id;
|
36 |
this._setContent(_3.title+"<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>"+dojo.string.substitute(this.linkDialogTemplate,_2)); |
37 |
_3.startup(); |
38 |
this._urlInput=dijit.byId(this._uniqueId+"_urlInput"); |
39 |
this._textInput=dijit.byId(this._uniqueId+"_textInput"); |
40 |
this._setButton=dijit.byId(this._uniqueId+"_setButton"); |
41 |
this.connect(dijit.byId(this._uniqueId+"_cancelButton"),"onClick",function(){ |
42 |
this.dropDown.onCancel();
|
43 |
}); |
44 |
if(this._urlInput){ |
45 |
this.connect(this._urlInput,"onChange","_checkAndFixInput"); |
46 |
} |
47 |
if(this._textInput){ |
48 |
this.connect(this._textInput,"onChange","_checkAndFixInput"); |
49 |
} |
50 |
this._urlRegExp=new RegExp("^"+this.urlRegExp+"$","i"); |
51 |
this._emailRegExp=new RegExp("^"+this.emailRegExp+"$","i"); |
52 |
this._urlInput.isValid=dojo.hitch(this,function(){ |
53 |
var _4=this._urlInput.get("value"); |
54 |
return this._urlRegExp.test(_4)||this._emailRegExp.test(_4); |
55 |
}); |
56 |
this._connectTagEvents();
|
57 |
this.inherited(arguments); |
58 |
},_checkAndFixInput:function(){ |
59 |
var _5=this; |
60 |
var _6=this._urlInput.get("value"); |
61 |
var _7=function(_8){ |
62 |
var _9=false; |
63 |
var _a=false; |
64 |
if(_8&&_8.length>1){ |
65 |
_8=dojo.trim(_8); |
66 |
if(_8.indexOf("mailto:")!==0){ |
67 |
if(_8.indexOf("/")>0){ |
68 |
if(_8.indexOf("://")===-1){ |
69 |
if(_8.charAt(0)!=="/"&&_8.indexOf("./")!==0){ |
70 |
if(_5._hostRxp.test(_8)){
|
71 |
_9=true;
|
72 |
} |
73 |
} |
74 |
} |
75 |
}else{
|
76 |
if(_5._userAtRxp.test(_8)){
|
77 |
_a=true;
|
78 |
} |
79 |
} |
80 |
} |
81 |
} |
82 |
if(_9){
|
83 |
_5._urlInput.set("value","http://"+_8); |
84 |
} |
85 |
if(_a){
|
86 |
_5._urlInput.set("value","mailto:"+_8); |
87 |
} |
88 |
_5._setButton.set("disabled",!_5._isValid());
|
89 |
}; |
90 |
if(this._delayedCheck){ |
91 |
clearTimeout(this._delayedCheck);
|
92 |
this._delayedCheck=null; |
93 |
} |
94 |
this._delayedCheck=setTimeout(function(){ |
95 |
_7(_6); |
96 |
},250);
|
97 |
},_connectTagEvents:function(){ |
98 |
this.editor.onLoadDeferred.addCallback(dojo.hitch(this,function(){ |
99 |
this.connect(this.editor.editNode,"ondblclick",this._onDblClick); |
100 |
})); |
101 |
},_isValid:function(){ |
102 |
return this._urlInput.isValid()&&this._textInput.isValid(); |
103 |
},_setContent:function(_b){ |
104 |
this.dropDown.set("content",_b); |
105 |
},_checkValues:function(_c){ |
106 |
if(_c&&_c.urlInput){
|
107 |
_c.urlInput=_c.urlInput.replace(/"/g,"""); |
108 |
} |
109 |
return _c;
|
110 |
},setValue:function(_d){ |
111 |
this._onCloseDialog();
|
112 |
if(dojo.isIE){
|
113 |
var _e=dijit.range.getSelection(this.editor.window); |
114 |
var _f=_e.getRangeAt(0); |
115 |
var a=_f.endContainer;
|
116 |
if(a.nodeType===3){ |
117 |
a=a.parentNode; |
118 |
} |
119 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()!==this.tag)){ |
120 |
a=dojo.withGlobal(this.editor.window,"getSelectedElement",dijit._editor.selection,[this.tag]); |
121 |
} |
122 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()===this.tag)){ |
123 |
if(this.editor.queryCommandEnabled("unlink")){ |
124 |
dojo.withGlobal(this.editor.window,"selectElementChildren",dijit._editor.selection,[a]); |
125 |
this.editor.execCommand("unlink"); |
126 |
} |
127 |
} |
128 |
} |
129 |
_d=this._checkValues(_d);
|
130 |
this.editor.execCommand("inserthtml",dojo.string.substitute(this.htmlTemplate,_d)); |
131 |
},_onCloseDialog:function(){ |
132 |
this.editor.focus();
|
133 |
},_getCurrentValues:function(a){ |
134 |
var url,_10,_11;
|
135 |
if(a&&a.tagName.toLowerCase()===this.tag){ |
136 |
url=a.getAttribute("_djrealurl")||a.getAttribute("href"); |
137 |
_11=a.getAttribute("target")||"_self"; |
138 |
_10=a.textContent||a.innerText; |
139 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[a,true]); |
140 |
}else{
|
141 |
_10=dojo.withGlobal(this.editor.window,dijit._editor.selection.getSelectedText);
|
142 |
} |
143 |
return {urlInput:url||"",textInput:_10||"",targetSelect:_11||""}; |
144 |
},_onOpenDialog:function(){ |
145 |
var a;
|
146 |
if(dojo.isIE){
|
147 |
var sel=dijit.range.getSelection(this.editor.window); |
148 |
var _12=sel.getRangeAt(0); |
149 |
a=_12.endContainer; |
150 |
if(a.nodeType===3){ |
151 |
a=a.parentNode; |
152 |
} |
153 |
if(a&&(a.nodeName&&a.nodeName.toLowerCase()!==this.tag)){ |
154 |
a=dojo.withGlobal(this.editor.window,"getSelectedElement",dijit._editor.selection,[this.tag]); |
155 |
} |
156 |
}else{
|
157 |
a=dojo.withGlobal(this.editor.window,"getAncestorElement",dijit._editor.selection,[this.tag]); |
158 |
} |
159 |
this.dropDown.reset();
|
160 |
this._setButton.set("disabled",true); |
161 |
this.dropDown.set("value",this._getCurrentValues(a)); |
162 |
},_onDblClick:function(e){ |
163 |
if(e&&e.target){
|
164 |
var t=e.target;
|
165 |
var tg=t.tagName?t.tagName.toLowerCase():""; |
166 |
if(tg===this.tag&&dojo.attr(t,"href")){ |
167 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[t]); |
168 |
this.editor.onDisplayChanged();
|
169 |
setTimeout(dojo.hitch(this,function(){ |
170 |
this.button.set("disabled",false); |
171 |
this.button.openDropDown();
|
172 |
}),10);
|
173 |
} |
174 |
} |
175 |
}}); |
176 |
dojo.declare("dijit._editor.plugins.ImgLinkDialog",[dijit._editor.plugins.LinkDialog],{linkDialogTemplate:["<table><tr><td>","<label for='${id}_urlInput'>${url}</label>","</td><td>","<input dojoType='dijit.form.ValidationTextBox' regExp='${urlRegExp}' "+"required='true' id='${id}_urlInput' name='urlInput' intermediateChanges='true'>","</td></tr><tr><td>","<label for='${id}_textInput'>${text}</label>","</td><td>","<input dojoType='dijit.form.ValidationTextBox' required='false' id='${id}_textInput' "+"name='textInput' intermediateChanges='true'>","</td></tr><tr><td>","</td><td>","</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'>${buttonCancel}</button>","</td></tr></table>"].join(""),htmlTemplate:"<img src=\"${urlInput}\" _djrealurl=\"${urlInput}\" alt=\"${textInput}\" />",tag:"img",_getCurrentValues:function(img){ |
177 |
var url,_13;
|
178 |
if(img&&img.tagName.toLowerCase()===this.tag){ |
179 |
url=img.getAttribute("_djrealurl")||img.getAttribute("src"); |
180 |
_13=img.getAttribute("alt");
|
181 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[img,true]); |
182 |
}else{
|
183 |
_13=dojo.withGlobal(this.editor.window,dijit._editor.selection.getSelectedText);
|
184 |
} |
185 |
return {urlInput:url||"",textInput:_13||""}; |
186 |
},_isValid:function(){ |
187 |
return this._urlInput.isValid(); |
188 |
},_connectTagEvents:function(){ |
189 |
this.inherited(arguments); |
190 |
this.editor.onLoadDeferred.addCallback(dojo.hitch(this,function(){ |
191 |
this.connect(this.editor.editNode,"onmousedown",this._selectTag); |
192 |
})); |
193 |
},_selectTag:function(e){ |
194 |
if(e&&e.target){
|
195 |
var t=e.target;
|
196 |
var tg=t.tagName?t.tagName.toLowerCase():""; |
197 |
if(tg===this.tag){ |
198 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[t]); |
199 |
} |
200 |
} |
201 |
},_checkValues:function(_14){ |
202 |
if(_14&&_14.urlInput){
|
203 |
_14.urlInput=_14.urlInput.replace(/"/g,"""); |
204 |
} |
205 |
if(_14&&_14.textInput){
|
206 |
_14.textInput=_14.textInput.replace(/"/g,"""); |
207 |
} |
208 |
return _14;
|
209 |
},_onDblClick:function(e){ |
210 |
if(e&&e.target){
|
211 |
var t=e.target;
|
212 |
var tg=t.tagName?t.tagName.toLowerCase():""; |
213 |
if(tg===this.tag&&dojo.attr(t,"src")){ |
214 |
dojo.withGlobal(this.editor.window,"selectElement",dijit._editor.selection,[t]); |
215 |
this.editor.onDisplayChanged();
|
216 |
setTimeout(dojo.hitch(this,function(){ |
217 |
this.button.set("disabled",false); |
218 |
this.button.openDropDown();
|
219 |
}),10);
|
220 |
} |
221 |
} |
222 |
}}); |
223 |
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){ |
224 |
if(o.plugin){
|
225 |
return;
|
226 |
} |
227 |
switch(o.args.name){
|
228 |
case "createLink": |
229 |
o.plugin=new dijit._editor.plugins.LinkDialog({command:o.args.name}); |
230 |
break;
|
231 |
case "insertImage": |
232 |
o.plugin=new dijit._editor.plugins.ImgLinkDialog({command:o.args.name}); |
233 |
break;
|
234 |
} |
235 |
}); |
236 |
} |