]>
git.wh0rd.org - tt-rss.git/blob - lib/dijit/_editor/plugins/LinkDialog.js.uncompressed.js
1 define("dijit/_editor/plugins/LinkDialog", [
3 "dojo/_base/declare", // declare
4 "dojo/dom-attr", // domAttr.get
5 "dojo/keys", // keys.ENTER
6 "dojo/_base/lang", // lang.delegate lang.hitch lang.trim
7 "dojo/_base/sniff", // has("ie")
8 "dojo/string", // string.substitute
9 "dojo/_base/window", // win.withGlobal
12 "../../form/DropDownButton",
15 ], function(require
, declare
, domAttr
, keys
, lang
, has
, string
, win
,
16 _Widget
, _Plugin
, DropDownButton
, rangeapi
, selectionapi
){
19 var _Plugin = dijit._editor._Plugin;
23 // dijit/_editor/plugins/LinkDialog
25 // Editor plugins: LinkDialog (for inserting links) and ImgLinkDialog (for inserting images)
28 var LinkDialog
= declare("dijit._editor.plugins.LinkDialog", _Plugin
, {
30 // This plugin provides the basis for an 'anchor' (link) dialog and an extension of it
31 // provides the image link dialog.
34 // The command provided by this plugin is:
37 // Override _Plugin.buttonClass. This plugin is controlled by a DropDownButton
38 // (which triggers a TooltipDialog).
39 buttonClass
: DropDownButton
,
41 // Override _Plugin.useDefaultCommand... processing is handled by this plugin, not by dijit.Editor.
42 useDefaultCommand
: false,
44 // urlRegExp: [protected] String
45 // Used for validating input as correct URL. While file:// urls are not terribly
46 // useful, they are technically valid.
47 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/]{0,}(?:\\?[^?#\\s/]*)?(?:#.*)?)?)?",
49 // emailRegExp: [protected] String
50 // Used for validating input as correct email address. Taken from dojox.validate
51 emailRegExp
: "<?(mailto\\:)([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+" /*username*/ + "@" +
52 "((?:(?:[\\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_-]*>?", // host.
54 // htmlTemplate: [protected] String
55 // String used for templating the HTML to insert at the desired point.
56 htmlTemplate
: "<a href=\"${urlInput}\" _djrealurl=\"${urlInput}\"" +
57 " target=\"${targetSelect}\"" +
60 // tag: [protected] String
61 // Tag used for the link type.
64 // _hostRxp [private] RegExp
65 // Regular expression used to validate url fragments (ip address, hostname, etc)
66 _hostRxp
: /^((([^\[:]+):)?([^@]+)@)?(\[([^\]]+)\]|([^\[:]*))(:([0-9]+))?$/,
68 // _userAtRxp [private] RegExp
69 // Regular expression used to validate e-mail address fragment.
70 _userAtRxp
: /^([!#-'*+\-\/-9=?A-Z^-~]+[.])*[!#-'*+\-\/-9=?A-Z^-~]+@/i,
72 // linkDialogTemplate: [protected] String
73 // Template for contents of TooltipDialog to pick URL
76 "<label for='${id}_urlInput'>${url}</label>",
78 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' " +
79 "id='${id}_urlInput' name='urlInput' data-dojo-props='intermediateChanges:true'/>",
81 "<label for='${id}_textInput'>${text}</label>",
83 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' " +
84 "name='textInput' data-dojo-props='intermediateChanges:true'/>",
86 "<label for='${id}_targetSelect'>${target}</label>",
88 "<select id='${id}_targetSelect' name='targetSelect' data-dojo-type='dijit.form.Select'>",
89 "<option selected='selected' value='_self'>${currentWindow}</option>",
90 "<option value='_blank'>${newWindow}</option>",
91 "<option value='_top'>${topWindow}</option>",
92 "<option value='_parent'>${parentWindow}</option>",
94 "</td></tr><tr><td colspan='2'>",
95 "<button data-dojo-type='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
96 "<button data-dojo-type='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
100 _initButton: function(){
101 this.inherited(arguments
);
103 // Setup to lazy create TooltipDialog first time the button is clicked
104 this.button
.loadDropDown
= lang
.hitch(this, "_loadDropDown");
106 this._connectTagEvents();
108 _loadDropDown: function(callback
){
109 // Called the first time the button is pressed. Initialize TooltipDialog.
111 "dojo/i18n", // i18n.getLocalization
112 "../../TooltipDialog",
113 "../../registry", // registry.byId, registry.getUniqueId
114 "../../form/Button", // used by template
115 "../../form/Select", // used by template
116 "../../form/ValidationTextBox", // used by template
117 "dojo/i18n!../../nls/common",
118 "dojo/i18n!../nls/LinkDialog"
119 ], lang
.hitch(this, function(i18n
, TooltipDialog
, registry
){
121 this.tag
= this.command
== 'insertImage' ? 'img' : 'a';
122 var messages
= lang
.delegate(i18n
.getLocalization("dijit", "common", this.lang
),
123 i18n
.getLocalization("dijit._editor", "LinkDialog", this.lang
));
124 var dropDown
= (this.dropDown
= this.button
.dropDown
= new TooltipDialog({
125 title
: messages
[this.command
+ "Title"],
126 execute
: lang
.hitch(this, "setValue"),
128 _this
._onOpenDialog();
129 TooltipDialog
.prototype.onOpen
.apply(this, arguments
);
131 onCancel: function(){
132 setTimeout(lang
.hitch(_this
, "_onCloseDialog"),0);
135 messages
.urlRegExp
= this.urlRegExp
;
136 messages
.id
= registry
.getUniqueId(this.editor
.id
);
137 this._uniqueId
= messages
.id
;
138 this._setContent(dropDown
.title
+
139 "<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>" +
140 string
.substitute(this.linkDialogTemplate
, messages
));
142 this._urlInput
= registry
.byId(this._uniqueId
+ "_urlInput");
143 this._textInput
= registry
.byId(this._uniqueId
+ "_textInput");
144 this._setButton
= registry
.byId(this._uniqueId
+ "_setButton");
145 this.connect(registry
.byId(this._uniqueId
+ "_cancelButton"), "onClick", function(){
146 this.dropDown
.onCancel();
149 this.connect(this._urlInput
, "onChange", "_checkAndFixInput");
152 this.connect(this._textInput
, "onChange", "_checkAndFixInput");
155 // Build up the dual check for http/https/file:, and mailto formats.
156 this._urlRegExp
= new RegExp("^" + this.urlRegExp
+ "$", "i");
157 this._emailRegExp
= new RegExp("^" + this.emailRegExp
+ "$", "i");
158 this._urlInput
.isValid
= lang
.hitch(this, function(){
159 // Function over-ride of isValid to test if the input matches a url or a mailto style link.
160 var value
= this._urlInput
.get("value");
161 return this._urlRegExp
.test(value
) || this._emailRegExp
.test(value
);
164 // Listen for enter and execute if valid.
165 this.connect(dropDown
.domNode
, "onkeypress", function(e
){
166 if(e
&& e
.charOrCode
== keys
.ENTER
&&
167 !e
.shiftKey
&& !e
.metaKey
&& !e
.ctrlKey
&& !e
.altKey
){
168 if(!this._setButton
.get("disabled")){
169 dropDown
.onExecute();
170 dropDown
.execute(dropDown
.get('value'));
179 _checkAndFixInput: function(){
181 // A function to listen for onChange events and test the input contents
182 // for valid information, such as valid urls with http/https/ftp and if
183 // not present, try and guess if the input url is relative or not, and if
184 // not, append http:// to it. Also validates other fields as determined by
185 // the internal _isValid function.
187 var url
= this._urlInput
.get("value");
188 var fixupUrl = function(url
){
189 var appendHttp
= false;
190 var appendMailto
= false;
191 if(url
&& url
.length
> 1){
192 url
= lang
.trim(url
);
193 if(url
.indexOf("mailto:") !== 0){
194 if(url
.indexOf("/") > 0){
195 if(url
.indexOf("://") === -1){
196 // Check that it doesn't start with / or ./, which would
197 // imply 'target server relativeness'
198 if(url
.charAt(0) !== '/' && url
.indexOf("./") !== 0){
199 if(self
._hostRxp
.test(url
)){
204 }else if(self
._userAtRxp
.test(url
)){
205 // If it looks like a foo@, append a mailto.
211 self
._urlInput
.set("value", "http://" + url
);
214 self
._urlInput
.set("value", "mailto:" + url
);
216 self
._setButton
.set("disabled", !self
._isValid());
218 if(this._delayedCheck
){
219 clearTimeout(this._delayedCheck
);
220 this._delayedCheck
= null;
222 this._delayedCheck
= setTimeout(function(){
227 _connectTagEvents: function(){
229 // Over-ridable function that connects tag specific events.
230 this.editor
.onLoadDeferred
.addCallback(lang
.hitch(this, function(){
231 this.connect(this.editor
.editNode
, "ondblclick", this._onDblClick
);
235 _isValid: function(){
237 // Internal function to allow validating of the inputs
238 // for a link to determine if set should be disabled or not
241 return this._urlInput
.isValid() && this._textInput
.isValid();
244 _setContent: function(staticPanel
){
246 // Helper for _initButton above. Not sure why it's a separate method.
248 parserScope
: "dojo", // make parser search for dojoType/data-dojo-type even if page is multi-version
253 _checkValues: function(args
){
255 // Function to check the values in args and 'fix' them up as needed.
257 // Content being set.
260 if(args
&& args
.urlInput
){
261 args
.urlInput
= args
.urlInput
.replace(/"/g, ""
;");
266 setValue: function(args){
268 // Callback from the dialog when user presses "set" button.
271 //TODO: prevent closing popup if the text is empty
272 this._onCloseDialog();
273 if(has("ie
") < 9){ //see #4151
274 var sel = rangeapi.getSelection(this.editor.window);
275 var range = sel.getRangeAt(0);
276 var a = range.endContainer;
277 if(a.nodeType === 3){
278 // Text node, may be the link contents, so check parent.
279 // This plugin doesn't really support nested HTML elements
280 // in the link, it assumes all link content is text.
283 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
284 // Still nothing, one last thing to try on IE, as it might be 'img'
285 // and thus considered a control.
286 a = win.withGlobal(this.editor.window,
287 "getSelectedElement
", selectionapi, [this.tag]);
289 if(a && (a.nodeName && a.nodeName.toLowerCase() === this.tag)){
290 // Okay, we do have a match. IE, for some reason, sometimes pastes before
291 // instead of removing the targeted paste-over element, so we unlink the
292 // old one first. If we do not the <a> tag remains, but it has no content,
293 // so isn't readily visible (but is wrong for the action).
294 if(this.editor.queryCommandEnabled("unlink
")){
295 // Select all the link children, then unlink. The following insert will
296 // then replace the selected text.
297 win.withGlobal(this.editor.window,
298 "selectElementChildren
", selectionapi, [a]);
299 this.editor.execCommand("unlink
");
303 // make sure values are properly escaped, etc.
304 args = this._checkValues(args);
305 this.editor.execCommand('inserthtml',
306 string.substitute(this.htmlTemplate, args));
309 _onCloseDialog: function(){
311 // Handler for close event on the dialog
315 _getCurrentValues: function(a){
317 // Over-ride for getting the values to set in the dropdown.
319 // The anchor/link to process for data for the dropdown.
322 var url, text, target;
323 if(a && a.tagName.toLowerCase() === this.tag){
324 url = a.getAttribute('_djrealurl') || a.getAttribute('href');
325 target = a.getAttribute('target') || "_self
";
326 text = a.textContent || a.innerText;
327 win.withGlobal(this.editor.window, "selectElement
", selectionapi, [a, true]);
329 text = win.withGlobal(this.editor.window, selectionapi.getSelectedText);
331 return {urlInput: url || '', textInput: text || '', targetSelect: target || ''}; //Object;
334 _onOpenDialog: function(){
336 // Handler for when the dialog is opened.
337 // If the caret is currently in a URL then populate the URL's info into the dialog.
340 // IE is difficult to select the element in, using the range unified
341 // API seems to work reasonably well.
342 var sel = rangeapi.getSelection(this.editor.window);
343 var range = sel.getRangeAt(0);
344 a = range.endContainer;
345 if(a.nodeType === 3){
346 // Text node, may be the link contents, so check parent.
347 // This plugin doesn't really support nested HTML elements
348 // in the link, it assumes all link content is text.
351 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
352 // Still nothing, one last thing to try on IE, as it might be 'img'
353 // and thus considered a control.
354 a = win.withGlobal(this.editor.window,
355 "getSelectedElement
", selectionapi, [this.tag]);
358 a = win.withGlobal(this.editor.window,
359 "getAncestorElement
", selectionapi, [this.tag]);
361 this.dropDown.reset();
362 this._setButton.set("disabled
", true);
363 this.dropDown.set("value
", this._getCurrentValues(a));
366 _onDblClick: function(e){
368 // Function to define a behavior on double clicks on the element
369 // type this dialog edits to select it and pop up the editor
372 // The double-click event.
377 var tg = t.tagName? t.tagName.toLowerCase() : "";
378 if(tg === this.tag && domAttr.get(t,"href
")){
379 var editor = this.editor;
381 win.withGlobal(editor.window,
385 editor.onDisplayChanged();
387 // Call onNormalizedDisplayChange() now, rather than on timer.
388 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
389 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
390 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
391 // since (for unknown reasons) focus.js ignores disabled controls.
392 if(editor._updateTimer){
393 clearTimeout(editor._updateTimer);
394 delete editor._updateTimer;
396 editor.onNormalizedDisplayChanged();
398 var button = this.button;
399 setTimeout(function(){
400 // Focus shift outside the event handler.
401 // IE doesn't like focus changes in event handles.
402 button.set("disabled
", false);
403 button.loadAndOpenDropDown().then(function(){
404 if(button.dropDown.focus){
405 button.dropDown.focus();
414 var ImgLinkDialog = declare("dijit
._editor
.plugins
.ImgLinkDialog
", [LinkDialog], {
416 // This plugin extends LinkDialog and adds in a plugin for handling image links.
417 // provides the image link dialog.
420 // The command provided by this plugin is:
423 // linkDialogTemplate: [protected] String
424 // Over-ride for template since img dialog doesn't need target that anchor tags may.
425 linkDialogTemplate: [
427 "<label
for='${id}_urlInput'>${url}
</label
>",
429 "<input dojoType
='dijit.form.ValidationTextBox' regExp
='${urlRegExp}' " +
430 "required
='true' id
='${id}_urlInput' name
='urlInput' data
-dojo
-props
='intermediateChanges:true'/>",
431 "</td></tr
><tr
><td
>",
432 "<label
for='${id}_textInput'>${text}
</label
>",
434 "<input data
-dojo
-type
='dijit.form.ValidationTextBox' required
='false' id
='${id}_textInput' " +
435 "name
='textInput' data
-dojo
-props
='intermediateChanges:true'/>",
436 "</td></tr
><tr
><td
>",
438 "</td></tr
><tr
><td colspan
='2'>",
439 "<button data
-dojo
-type
='dijit.form.Button' type
='submit' id
='${id}_setButton'>${set}
</button
>",
440 "<button data
-dojo
-type
='dijit.form.Button' type
='button' id
='${id}_cancelButton'>${buttonCancel}
</button
>",
444 // htmlTemplate: [protected] String
445 // String used for templating the <img> HTML to insert at the desired point.
446 htmlTemplate: "<img src
=\"${urlInput}
\" _djrealurl
=\"${urlInput}
\" alt
=\"${textInput}
\" />",
448 // tag: [protected] String
449 // Tag used for the link type (img).
452 _getCurrentValues: function(img){
454 // Over-ride for getting the values to set in the dropdown.
456 // The anchor/link to process for data for the dropdown.
460 if(img && img.tagName.toLowerCase() === this.tag){
461 url = img.getAttribute('_djrealurl') || img.getAttribute('src');
462 text = img.getAttribute('alt');
463 win.withGlobal(this.editor.window,
464 "selectElement
", selectionapi, [img, true]);
466 text = win.withGlobal(this.editor.window, selectionapi.getSelectedText);
468 return {urlInput: url || '', textInput: text || ''}; //Object;
471 _isValid: function(){
473 // Over-ride for images. You can have alt text of blank, it is valid.
476 return this._urlInput.isValid();
479 _connectTagEvents: function(){
481 // Over-ridable function that connects tag specific events.
482 this.inherited(arguments);
483 this.editor.onLoadDeferred.addCallback(lang.hitch(this, function(){
484 // Use onmousedown instead of onclick. Seems that IE eats the first onclick
485 // to wrap it in a selector box, then the second one acts as onclick. See #10420
486 this.connect(this.editor.editNode, "onmousedown
", this._selectTag);
490 _selectTag: function(e){
492 // A simple event handler that lets me select an image if it is clicked on.
493 // makes it easier to select images in a standard way across browsers. Otherwise
494 // selecting an image for edit becomes difficult.
496 // The mousedown event.
501 var tg = t.tagName? t.tagName.toLowerCase() : "";
503 win.withGlobal(this.editor.window,
510 _checkValues: function(args){
512 // Function to check the values in args and 'fix' them up as needed
513 // (special characters in the url or alt text)
515 // Content being set.
518 if(args && args.urlInput){
519 args.urlInput = args.urlInput.replace(/"/g
, """);
521 if(args
&& args
.textInput
){
522 args
.textInput
= args
.textInput
.replace(/"/g, ""
;");
527 _onDblClick: function(e){
529 // Function to define a behavior on double clicks on the element
530 // type this dialog edits to select it and pop up the editor
533 // The double-click event.
538 var tg = t.tagName ? t.tagName.toLowerCase() : "";
539 if(tg === this.tag && domAttr.get(t,"src
")){
540 var editor = this.editor;
542 win.withGlobal(editor.window,
545 editor.onDisplayChanged();
547 // Call onNormalizedDisplayChange() now, rather than on timer.
548 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
549 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
550 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
551 // since (for unknown reasons) focus.js ignores disabled controls.
552 if(editor._updateTimer){
553 clearTimeout(editor._updateTimer);
554 delete editor._updateTimer;
556 editor.onNormalizedDisplayChanged();
558 var button = this.button;
559 setTimeout(function(){
560 // Focus shift outside the event handler.
561 // IE doesn't like focus changes in event handles.
562 button.set("disabled
", false);
563 button.loadAndOpenDropDown().then(function(){
564 if(button.dropDown.focus){
565 button.dropDown.focus();
574 // Register these plugins
575 _Plugin.registry["createLink
"] = function(){
576 return new LinkDialog({command: "createLink
"});
578 _Plugin.registry["insertImage
"] = function(){
579 return new ImgLinkDialog({command: "insertImage
"});
583 // Export both LinkDialog and ImgLinkDialog
584 LinkDialog.ImgLinkDialog = ImgLinkDialog;