]>
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/sniff", // has("ie")
8 "dojo/_base/query", // query
9 "dojo/string", // string.substitute
12 "../../form/DropDownButton",
14 ], function(require
, declare
, domAttr
, keys
, lang
, has
, query
, string
,
15 _Widget
, _Plugin
, DropDownButton
, rangeapi
){
19 // dijit/_editor/plugins/LinkDialog
21 // Editor plugins: LinkDialog (for inserting links) and ImgLinkDialog (for inserting images)
24 var LinkDialog
= declare("dijit._editor.plugins.LinkDialog", _Plugin
, {
26 // This plugin provides the basis for an 'anchor' (link) dialog and an extension of it
27 // provides the image link dialog.
29 // The command provided by this plugin is:
33 // Override _Plugin.buttonClass. This plugin is controlled by a DropDownButton
34 // (which triggers a TooltipDialog).
35 buttonClass
: DropDownButton
,
37 // Override _Plugin.useDefaultCommand... processing is handled by this plugin, not by dijit/Editor.
38 useDefaultCommand
: false,
40 // urlRegExp: [protected] String
41 // Used for validating input as correct URL. While file:// urls are not terribly
42 // useful, they are technically valid.
43 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/]*)?(?:#.*)?)?)?",
45 // emailRegExp: [protected] String
46 // Used for validating input as correct email address. Taken from dojox.validate
47 emailRegExp
: "<?(mailto\\:)([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+" /*username*/ + "@" +
48 "((?:(?:[\\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.
50 // htmlTemplate: [protected] String
51 // String used for templating the HTML to insert at the desired point.
52 htmlTemplate
: "<a href=\"${urlInput}\" _djrealurl=\"${urlInput}\"" +
53 " target=\"${targetSelect}\"" +
56 // tag: [protected] String
57 // Tag used for the link type.
60 // _hostRxp [private] RegExp
61 // Regular expression used to validate url fragments (ip address, hostname, etc)
62 _hostRxp
: /^((([^\[:]+):)?([^@]+)@)?(\[([^\]]+)\]|([^\[:]*))(:([0-9]+))?$/,
64 // _userAtRxp [private] RegExp
65 // Regular expression used to validate e-mail address fragment.
66 _userAtRxp
: /^([!#-'*+\-\/-9=?A-Z^-~]+[.])*[!#-'*+\-\/-9=?A-Z^-~]+@/i,
68 // linkDialogTemplate: [protected] String
69 // Template for contents of TooltipDialog to pick URL
71 "<table role='presentation'><tr><td>",
72 "<label for='${id}_urlInput'>${url}</label>",
74 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' " +
75 "id='${id}_urlInput' name='urlInput' data-dojo-props='intermediateChanges:true'/>",
77 "<label for='${id}_textInput'>${text}</label>",
79 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' " +
80 "name='textInput' data-dojo-props='intermediateChanges:true'/>",
82 "<label for='${id}_targetSelect'>${target}</label>",
84 "<select id='${id}_targetSelect' name='targetSelect' data-dojo-type='dijit.form.Select'>",
85 "<option selected='selected' value='_self'>${currentWindow}</option>",
86 "<option value='_blank'>${newWindow}</option>",
87 "<option value='_top'>${topWindow}</option>",
88 "<option value='_parent'>${parentWindow}</option>",
90 "</td></tr><tr><td colspan='2'>",
91 "<button data-dojo-type='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
92 "<button data-dojo-type='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
96 _initButton: function(){
97 this.inherited(arguments
);
99 // Setup to lazy create TooltipDialog first time the button is clicked
100 this.button
.loadDropDown
= lang
.hitch(this, "_loadDropDown");
102 this._connectTagEvents();
104 _loadDropDown: function(callback
){
105 // Called the first time the button is pressed. Initialize TooltipDialog.
107 "dojo/i18n", // i18n.getLocalization
108 "../../TooltipDialog",
109 "../../registry", // registry.byId, registry.getUniqueId
110 "../../form/Button", // used by template
111 "../../form/Select", // used by template
112 "../../form/ValidationTextBox", // used by template
113 "dojo/i18n!../../nls/common",
114 "dojo/i18n!../nls/LinkDialog"
115 ], lang
.hitch(this, function(i18n
, TooltipDialog
, registry
){
117 this.tag
= this.command
== 'insertImage' ? 'img' : 'a';
118 var messages
= lang
.delegate(i18n
.getLocalization("dijit", "common", this.lang
),
119 i18n
.getLocalization("dijit._editor", "LinkDialog", this.lang
));
120 var dropDown
= (this.dropDown
= this.button
.dropDown
= new TooltipDialog({
121 title
: messages
[this.command
+ "Title"],
122 ownerDocument
: this.editor
.ownerDocument
,
123 dir
: this.editor
.dir
,
124 execute
: lang
.hitch(this, "setValue"),
126 _this
._onOpenDialog();
127 TooltipDialog
.prototype.onOpen
.apply(this, arguments
);
129 onCancel: function(){
130 setTimeout(lang
.hitch(_this
, "_onCloseDialog"),0);
133 messages
.urlRegExp
= this.urlRegExp
;
134 messages
.id
= registry
.getUniqueId(this.editor
.id
);
135 this._uniqueId
= messages
.id
;
136 this._setContent(dropDown
.title
+
137 "<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>" +
138 string
.substitute(this.linkDialogTemplate
, messages
));
140 this._urlInput
= registry
.byId(this._uniqueId
+ "_urlInput");
141 this._textInput
= registry
.byId(this._uniqueId
+ "_textInput");
142 this._setButton
= registry
.byId(this._uniqueId
+ "_setButton");
143 this.connect(registry
.byId(this._uniqueId
+ "_cancelButton"), "onClick", function(){
144 this.dropDown
.onCancel();
147 this.connect(this._urlInput
, "onChange", "_checkAndFixInput");
150 this.connect(this._textInput
, "onChange", "_checkAndFixInput");
153 // Build up the dual check for http/https/file:, and mailto formats.
154 this._urlRegExp
= new RegExp("^" + this.urlRegExp
+ "$", "i");
155 this._emailRegExp
= new RegExp("^" + this.emailRegExp
+ "$", "i");
156 this._urlInput
.isValid
= lang
.hitch(this, function(){
157 // Function over-ride of isValid to test if the input matches a url or a mailto style link.
158 var value
= this._urlInput
.get("value");
159 return this._urlRegExp
.test(value
) || this._emailRegExp
.test(value
);
162 // Listen for enter and execute if valid.
163 this.connect(dropDown
.domNode
, "onkeypress", function(e
){
164 if(e
&& e
.charOrCode
== keys
.ENTER
&&
165 !e
.shiftKey
&& !e
.metaKey
&& !e
.ctrlKey
&& !e
.altKey
){
166 if(!this._setButton
.get("disabled")){
167 dropDown
.onExecute();
168 dropDown
.execute(dropDown
.get('value'));
177 _checkAndFixInput: function(){
179 // A function to listen for onChange events and test the input contents
180 // for valid information, such as valid urls with http/https/ftp and if
181 // not present, try and guess if the input url is relative or not, and if
182 // not, append http:// to it. Also validates other fields as determined by
183 // the internal _isValid function.
185 var url
= this._urlInput
.get("value");
186 var fixupUrl = function(url
){
187 var appendHttp
= false;
188 var appendMailto
= false;
189 if(url
&& url
.length
> 1){
190 url
= lang
.trim(url
);
191 if(url
.indexOf("mailto:") !== 0){
192 if(url
.indexOf("/") > 0){
193 if(url
.indexOf("://") === -1){
194 // Check that it doesn't start with /, ./, or ../, which would
195 // imply 'target server relativeness'
196 if(url
.charAt(0) !== '/' && url
.indexOf("./") && url
.indexOf("../") !== 0){
197 if(self
._hostRxp
.test(url
)){
202 }else if(self
._userAtRxp
.test(url
)){
203 // If it looks like a foo@, append a mailto.
209 self
._urlInput
.set("value", "http://" + url
);
212 self
._urlInput
.set("value", "mailto:" + url
);
214 self
._setButton
.set("disabled", !self
._isValid());
216 if(this._delayedCheck
){
217 clearTimeout(this._delayedCheck
);
218 this._delayedCheck
= null;
220 this._delayedCheck
= setTimeout(function(){
225 _connectTagEvents: function(){
227 // Over-ridable function that connects tag specific events.
228 this.editor
.onLoadDeferred
.then(lang
.hitch(this, function(){
229 this.connect(this.editor
.editNode
, "ondblclick", this._onDblClick
);
233 _isValid: function(){
235 // Internal function to allow validating of the inputs
236 // for a link to determine if set should be disabled or not
239 return this._urlInput
.isValid() && this._textInput
.isValid();
242 _setContent: function(staticPanel
){
244 // Helper for _initButton above. Not sure why it's a separate method.
246 parserScope
: "dojo", // make parser search for dojoType/data-dojo-type even if page is multi-version
251 _checkValues: function(args
){
253 // Function to check the values in args and 'fix' them up as needed.
255 // Content being set.
258 if(args
&& args
.urlInput
){
259 args
.urlInput
= args
.urlInput
.replace(/"/g, ""
;");
264 setValue: function(args){
266 // Callback from the dialog when user presses "set" button.
270 // TODO: prevent closing popup if the text is empty
271 this._onCloseDialog();
272 if(has("ie
") < 9){ //see #4151
273 var sel = rangeapi.getSelection(this.editor.window);
274 var range = sel.getRangeAt(0);
275 var a = range.endContainer;
276 if(a.nodeType === 3){
277 // Text node, may be the link contents, so check parent.
278 // This plugin doesn't really support nested HTML elements
279 // in the link, it assumes all link content is text.
282 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
283 // Still nothing, one last thing to try on IE, as it might be 'img'
284 // and thus considered a control.
285 a = this.editor._sCall("getSelectedElement
", [this.tag]);
287 if(a && (a.nodeName && a.nodeName.toLowerCase() === this.tag)){
288 // Okay, we do have a match. IE, for some reason, sometimes pastes before
289 // instead of removing the targeted paste-over element, so we unlink the
290 // old one first. If we do not the <a> tag remains, but it has no content,
291 // so isn't readily visible (but is wrong for the action).
292 if(this.editor.queryCommandEnabled("unlink
")){
293 // Select all the link children, then unlink. The following insert will
294 // then replace the selected text.
295 this.editor._sCall("selectElementChildren
", [a]);
296 this.editor.execCommand("unlink
");
300 // make sure values are properly escaped, etc.
301 args = this._checkValues(args);
302 this.editor.execCommand('inserthtml',
303 string.substitute(this.htmlTemplate, args));
305 // IE sometimes leaves a blank link, so we need to fix it up.
306 // Go ahead and do this for everyone just to avoid blank links
308 query("a
", this.editor.document).forEach(function(a){
309 if(!a.innerHTML && !domAttr.has(a, "name
")){
310 // Remove empty anchors that do not have "name
" set.
311 // Empty ones with a name set could be a hidden hash
313 a.parentNode.removeChild(a);
318 _onCloseDialog: function(){
320 // Handler for close event on the dialog
324 _getCurrentValues: function(a){
326 // Over-ride for getting the values to set in the dropdown.
328 // The anchor/link to process for data for the dropdown.
331 var url, text, target;
332 if(a && a.tagName.toLowerCase() === this.tag){
333 url = a.getAttribute('_djrealurl') || a.getAttribute('href');
334 target = a.getAttribute('target') || "_self
";
335 text = a.textContent || a.innerText;
336 this.editor._sCall("selectElement
", [a, true]);
338 text = this.editor._sCall("getSelectedText
");
340 return {urlInput: url || '', textInput: text || '', targetSelect: target || ''}; //Object;
343 _onOpenDialog: function(){
345 // Handler for when the dialog is opened.
346 // If the caret is currently in a URL then populate the URL's info into the dialog.
349 // IE, even IE10, is difficult to select the element in, using the range unified
350 // API seems to work reasonably well.
351 var sel = rangeapi.getSelection(this.editor.window);
353 var range = sel.getRangeAt(0);
354 a = range.endContainer;
355 if(a.nodeType === 3){
356 // Text node, may be the link contents, so check parent.
357 // This plugin doesn't really support nested HTML elements
358 // in the link, it assumes all link content is text.
361 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
362 // Still nothing, one last thing to try on IE, as it might be 'img'
363 // and thus considered a control.
364 a = this.editor._sCall("getSelectedElement
", [this.tag]);
366 if(!a || (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
367 // Try another lookup, IE's selection is just terrible.
368 b = this.editor._sCall("getAncestorElement
", [this.tag]);
369 if(b && (b.nodeName && b.nodeName.toLowerCase() == this.tag)){
370 // Looks like we found an A tag, use it and make sure just it is
373 this.editor._sCall("selectElement
", [a]);
374 }else if (range.startContainer === range.endContainer){
375 // STILL nothing. Trying one more thing. Lets look at the first child.
376 // It might be an anchor tag in a div by itself or the like. If it is,
377 // we'll use it otherwise we give up. The selection is not easily
378 // determinable to be on an existing anchor tag.
379 fc = range.startContainer.firstChild;
380 if(fc && (fc.nodeName && fc.nodeName.toLowerCase() == this.tag)){
382 this.editor._sCall("selectElement
", [a]);
388 a = this.editor._sCall("getAncestorElement
", [this.tag]);
390 this.dropDown.reset();
391 this._setButton.set("disabled
", true);
392 this.dropDown.set("value
", this._getCurrentValues(a));
395 _onDblClick: function(e){
397 // Function to define a behavior on double clicks on the element
398 // type this dialog edits to select it and pop up the editor
401 // The double-click event.
406 var tg = t.tagName ? t.tagName.toLowerCase() : "";
407 if(tg === this.tag && domAttr.get(t,"href
")){
408 var editor = this.editor;
410 this.editor._sCall("selectElement
", [t]);
411 editor.onDisplayChanged();
413 // Call onNormalizedDisplayChange() now, rather than on timer.
414 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
415 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
416 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
417 // since (for unknown reasons) focus.js ignores disabled controls.
418 if(editor._updateTimer){
419 editor._updateTimer.remove();
420 delete editor._updateTimer;
422 editor.onNormalizedDisplayChanged();
424 var button = this.button;
425 setTimeout(function(){
426 // Focus shift outside the event handler.
427 // IE doesn't like focus changes in event handles.
428 button.set("disabled
", false);
429 button.loadAndOpenDropDown().then(function(){
430 if(button.dropDown.focus){
431 button.dropDown.focus();
440 var ImgLinkDialog = declare("dijit
._editor
.plugins
.ImgLinkDialog
", [LinkDialog], {
442 // This plugin extends LinkDialog and adds in a plugin for handling image links.
443 // provides the image link dialog.
445 // The command provided by this plugin is:
449 // linkDialogTemplate: [protected] String
450 // Over-ride for template since img dialog doesn't need target that anchor tags may.
451 linkDialogTemplate: [
452 "<table role
='presentation'><tr
><td
>",
453 "<label
for='${id}_urlInput'>${url}
</label
>",
455 "<input dojoType
='dijit.form.ValidationTextBox' regExp
='${urlRegExp}' " +
456 "required
='true' id
='${id}_urlInput' name
='urlInput' data
-dojo
-props
='intermediateChanges:true'/>",
457 "</td></tr
><tr
><td
>",
458 "<label
for='${id}_textInput'>${text}
</label
>",
460 "<input data
-dojo
-type
='dijit.form.ValidationTextBox' required
='false' id
='${id}_textInput' " +
461 "name
='textInput' data
-dojo
-props
='intermediateChanges:true'/>",
462 "</td></tr
><tr
><td
>",
464 "</td></tr
><tr
><td colspan
='2'>",
465 "<button data
-dojo
-type
='dijit.form.Button' type
='submit' id
='${id}_setButton'>${set}
</button
>",
466 "<button data
-dojo
-type
='dijit.form.Button' type
='button' id
='${id}_cancelButton'>${buttonCancel}
</button
>",
470 // htmlTemplate: [protected] String
471 // String used for templating the `<img>` HTML to insert at the desired point.
472 htmlTemplate: "<img src
=\"${urlInput}
\" _djrealurl
=\"${urlInput}
\" alt
=\"${textInput}
\" />",
474 // tag: [protected] String
475 // Tag used for the link type (img).
478 _getCurrentValues: function(img){
480 // Over-ride for getting the values to set in the dropdown.
482 // The anchor/link to process for data for the dropdown.
486 if(img && img.tagName.toLowerCase() === this.tag){
487 url = img.getAttribute('_djrealurl') || img.getAttribute('src');
488 text = img.getAttribute('alt');
489 this.editor._sCall("selectElement
", [img, true]);
491 text = this.editor._sCall("getSelectedText
", []);
493 return {urlInput: url || '', textInput: text || ''}; //Object
496 _isValid: function(){
498 // Over-ride for images. You can have alt text of blank, it is valid.
501 return this._urlInput.isValid();
504 _connectTagEvents: function(){
506 // Over-ridable function that connects tag specific events.
507 this.inherited(arguments);
508 this.editor.onLoadDeferred.then(lang.hitch(this, function(){
509 // Use onmousedown instead of onclick. Seems that IE eats the first onclick
510 // to wrap it in a selector box, then the second one acts as onclick. See #10420
511 this.connect(this.editor.editNode, "onmousedown
", this._selectTag);
515 _selectTag: function(e){
517 // A simple event handler that lets me select an image if it is clicked on.
518 // makes it easier to select images in a standard way across browsers. Otherwise
519 // selecting an image for edit becomes difficult.
521 // The mousedown event.
526 var tg = t.tagName? t.tagName.toLowerCase() : "";
528 this.editor._sCall("selectElement
", [t]);
533 _checkValues: function(args){
535 // Function to check the values in args and 'fix' them up as needed
536 // (special characters in the url or alt text)
538 // Content being set.
541 if(args && args.urlInput){
542 args.urlInput = args.urlInput.replace(/"/g
, """);
544 if(args
&& args
.textInput
){
545 args
.textInput
= args
.textInput
.replace(/"/g, ""
;");
550 _onDblClick: function(e){
552 // Function to define a behavior on double clicks on the element
553 // type this dialog edits to select it and pop up the editor
556 // The double-click event.
561 var tg = t.tagName ? t.tagName.toLowerCase() : "";
562 if(tg === this.tag && domAttr.get(t,"src
")){
563 var editor = this.editor;
565 this.editor._sCall("selectElement
", [t]);
566 editor.onDisplayChanged();
568 // Call onNormalizedDisplayChange() now, rather than on timer.
569 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
570 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
571 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
572 // since (for unknown reasons) focus.js ignores disabled controls.
573 if(editor._updateTimer){
574 editor._updateTimer.remove();
575 delete editor._updateTimer;
577 editor.onNormalizedDisplayChanged();
579 var button = this.button;
580 setTimeout(function(){
581 // Focus shift outside the event handler.
582 // IE doesn't like focus changes in event handles.
583 button.set("disabled
", false);
584 button.loadAndOpenDropDown().then(function(){
585 if(button.dropDown.focus){
586 button.dropDown.focus();
595 // Register these plugins
596 _Plugin.registry["createLink
"] = function(){
597 return new LinkDialog({command: "createLink
"});
599 _Plugin.registry["insertImage
"] = function(){
600 return new ImgLinkDialog({command: "insertImage
"});
604 // Export both LinkDialog and ImgLinkDialog
605 // TODO for 2.0: either return both classes in a hash, or split this file into two separate files.
606 // Then the documentation for the module can be applied to the hash, and will show up in the API doc.
607 LinkDialog.ImgLinkDialog = ImgLinkDialog;