Debian Codegraph @

dijit/_editor/plugins/LinkDialog.js

raw symbols SCIP
1 define([
2 "require",
3 "dojo/_base/array",
4 "dojo/_base/declare", // declare
5 "dojo/dom-attr", // domAttr.get
6 "dojo/keys", // keys.ENTER
7 "dojo/_base/lang", // lang.delegate lang.hitch lang.trim
8 "dojo/on",
9 "dojo/sniff", // has("ie")
10 "dojo/query", // query
11 "dojo/string", // string.substitute
12 "../_Plugin",
13 "../../form/DropDownButton",
14 "../range"
15 ], function(require, array, declare, domAttr, keys, lang, on, has, query, string,
16 _Plugin, DropDownButton, rangeapi){
17
18 // module:
19 // dijit/_editor/plugins/LinkDialog
20
21 var LinkDialog = declare("dijit._editor.plugins.LinkDialog", _Plugin, {
22 // summary:
23 // This plugin provides the basis for an 'anchor' (link) dialog and an extension of it
24 // provides the image link dialog.
25 // description:
26 // The command provided by this plugin is:
27 //
28 // - createLink
29
30 // allowUnsafeHtml: boolean
31 // If false (default), the link description will be filtered to prevent HTML content.
32 // If true no filtering is done, allowing for HTML content within the link element.
33 // The filter can be specified with the 'linkFilter' option.
34 allowUnsafeHtml: false,
35
36 // linkFilter: function or array of replacement pairs
37 // If 'allowUnsafeHtml' is false then this filter will be applied to the link Description value.
38 // function: the function will be invoked with the string value of the Description field and its
39 // return value will be used
40 // array: each array item should be an array of two values to pass to String#replace
41 linkFilter: [
42 [/</g, "&lt;"]
43 ],
44
45 // Override _Plugin.buttonClass. This plugin is controlled by a DropDownButton
46 // (which triggers a TooltipDialog).
47 buttonClass: DropDownButton,
48
49 // Override _Plugin.useDefaultCommand... processing is handled by this plugin, not by dijit/Editor.
50 useDefaultCommand: false,
51
52 // urlRegExp: [protected] String
53 // Used for validating input as correct URL. While file:// urls are not terribly
54 // useful, they are technically valid.
55 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/]*)?(?:#.*)?)?)?",
56
57 // emailRegExp: [protected] String
58 // Used for validating input as correct email address. Taken from dojox.validate
59 emailRegExp: "<?(mailto\\:)([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+" /*username*/ + "@" +
60 "((?:(?:[\\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.
61
62 // htmlTemplate: [protected] String
63 // String used for templating the HTML to insert at the desired point.
64 htmlTemplate: "<a href=\"${urlInput}\" _djrealurl=\"${urlInput}\"" +
65 " target=\"${targetSelect}\"" +
66 ">${textInput}</a>",
67
68 // tag: [protected] String
69 // Tag used for the link type.
70 tag: "a",
71
72 // _hostRxp [private] RegExp
73 // Regular expression used to validate url fragments (ip address, hostname, etc)
74 _hostRxp: /^((([^\[:]+):)?([^@]+)@)?(\[([^\]]+)\]|([^\[:]*))(:([0-9]+))?$/,
75
76 // _userAtRxp [private] RegExp
77 // Regular expression used to validate e-mail address fragment.
78 _userAtRxp: /^([!#-'*+\-\/-9=?A-Z^-~]+[.])*[!#-'*+\-\/-9=?A-Z^-~]+@/i,
79
80 // linkDialogTemplate: [protected] String
81 // Template for contents of TooltipDialog to pick URL
82 linkDialogTemplate: [
83 "<table role='presentation'><tr><td>",
84 "<label for='${id}_urlInput'>${url}</label>",
85 "</td><td>",
86 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' " +
87 "id='${id}_urlInput' name='urlInput' data-dojo-props='intermediateChanges:true'/>",
88 "</td></tr><tr><td>",
89 "<label for='${id}_textInput'>${text}</label>",
90 "</td><td>",
91 "<input data-dojo-type='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' " +
92 "name='textInput' data-dojo-props='intermediateChanges:true'/>",
93 "</td></tr><tr><td>",
94 "<label for='${id}_targetSelect'>${target}</label>",
95 "</td><td>",
96 "<select id='${id}_targetSelect' name='targetSelect' data-dojo-type='dijit.form.Select'>",
97 "<option selected='selected' value='_self'>${currentWindow}</option>",
98 "<option value='_blank'>${newWindow}</option>",
99 "<option value='_top'>${topWindow}</option>",
100 "<option value='_parent'>${parentWindow}</option>",
101 "</select>",
102 "</td></tr><tr><td colspan='2'>",
103 "<button data-dojo-type='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
104 "<button data-dojo-type='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
105 "</td></tr></table>"
106 ].join(""),
107
108 _initButton: function(){
109 this.inherited(arguments);
110
111 // Setup to lazy create TooltipDialog first time the button is clicked
112 this.button.loadDropDown = lang.hitch(this, "_loadDropDown");
113
114 this._connectTagEvents();
115 },
116 _loadDropDown: function(callback){
117 // Called the first time the button is pressed. Initialize TooltipDialog.
118 require([
119 "dojo/i18n", // i18n.getLocalization
120 "../../TooltipDialog",
121 "../../registry", // registry.byId, registry.getUniqueId
122 "../../form/Button", // used by template
123 "../../form/Select", // used by template
124 "../../form/ValidationTextBox", // used by template
125 "dojo/i18n!../../nls/common",
126 "dojo/i18n!../nls/LinkDialog"
127 ], lang.hitch(this, function(i18n, TooltipDialog, registry){
128 var _this = this;
129 this.tag = this.command == 'insertImage' ? 'img' : 'a';
130 var messages = lang.delegate(i18n.getLocalization("dijit", "common", this.lang),
131 i18n.getLocalization("dijit._editor", "LinkDialog", this.lang));
132 var dropDown = (this.dropDown = this.button.dropDown = new TooltipDialog({
133 title: messages[this.command + "Title"],
134 ownerDocument: this.editor.ownerDocument,
135 dir: this.editor.dir,
136 execute: lang.hitch(this, "setValue"),
137 onOpen: function(){
138 _this._onOpenDialog();
139 TooltipDialog.prototype.onOpen.apply(this, arguments);
140 },
141 onCancel: function(){
142 setTimeout(lang.hitch(_this, "_onCloseDialog"), 0);
143 }
144 }));
145 messages.urlRegExp = this.urlRegExp;
146 messages.id = registry.getUniqueId(this.editor.id);
147 this._uniqueId = messages.id;
148 this._setContent(dropDown.title +
149 "<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>" +
150 string.substitute(this.linkDialogTemplate, messages));
151 dropDown.startup();
152 this._urlInput = registry.byId(this._uniqueId + "_urlInput");
153 this._textInput = registry.byId(this._uniqueId + "_textInput");
154 this._setButton = registry.byId(this._uniqueId + "_setButton");
155 this.own(registry.byId(this._uniqueId + "_cancelButton").on("click", lang.hitch(this.dropDown, "onCancel")));
156 if(this._urlInput){
157 this.own(this._urlInput.on("change", lang.hitch(this, "_checkAndFixInput")));
158 }
159 if(this._textInput){
160 this.own(this._textInput.on("change", lang.hitch(this, "_checkAndFixInput")));
161 }
162
163 // Build up the dual check for http/https/file:, and mailto formats.
164 this._urlRegExp = new RegExp("^" + this.urlRegExp + "$", "i");
165 this._emailRegExp = new RegExp("^" + this.emailRegExp + "$", "i");
166 this._urlInput.isValid = lang.hitch(this, function(){
167 // Function over-ride of isValid to test if the input matches a url or a mailto style link.
168 var value = this._urlInput.get("value");
169 return this._urlRegExp.test(value) || this._emailRegExp.test(value);
170 });
171
172 // Listen for enter and execute if valid.
173 this.own(on(dropDown.domNode, "keydown", lang.hitch(this, lang.hitch(this, function(e){
174 if(e && e.keyCode == keys.ENTER && !e.shiftKey && !e.metaKey && !e.ctrlKey && !e.altKey){
175 if(!this._setButton.get("disabled")){
176 dropDown.onExecute();
177 dropDown.execute(dropDown.get('value'));
178 }
179 }
180 }))));
181
182 callback();
183 }));
184 },
185
186 _checkAndFixInput: function(){
187 // summary:
188 // A function to listen for onChange events and test the input contents
189 // for valid information, such as valid urls with http/https/ftp and if
190 // not present, try and guess if the input url is relative or not, and if
191 // not, append http:// to it. Also validates other fields as determined by
192 // the internal _isValid function.
193 var self = this;
194 var url = this._urlInput.get("value");
195 var fixupUrl = function(url){
196 var appendHttp = false;
197 var appendMailto = false;
198 if(url && url.length > 1){
199 url = lang.trim(url);
200 if(url.indexOf("mailto:") !== 0){
201 if(url.indexOf("/") > 0){
202 if(url.indexOf("://") === -1){
203 // Check that it doesn't start with /, ./, or ../, which would
204 // imply 'target server relativeness'
205 if(url.charAt(0) !== '/' && url.indexOf("./") && url.indexOf("../") !== 0){
206 if(self._hostRxp.test(url)){
207 appendHttp = true;
208 }
209 }
210 }
211 }else if(self._userAtRxp.test(url)){
212 // If it looks like a foo@, append a mailto.
213 appendMailto = true;
214 }
215 }
216 }
217 if(appendHttp){
218 self._urlInput.set("value", "http://" + url);
219 }
220 if(appendMailto){
221 self._urlInput.set("value", "mailto:" + url);
222 }
223 self._setButton.set("disabled", !self._isValid());
224 };
225 if(this._delayedCheck){
226 clearTimeout(this._delayedCheck);
227 this._delayedCheck = null;
228 }
229 this._delayedCheck = setTimeout(function(){
230 fixupUrl(url);
231 }, 250);
232 },
233
234 _connectTagEvents: function(){
235 // summary:
236 // Over-ridable function that connects tag specific events.
237 this.editor.onLoadDeferred.then(lang.hitch(this, function(){
238 this.own(on(this.editor.editNode, "mouseup", lang.hitch(this, "_onMouseUp")));
239 this.own(on(this.editor.editNode, "dblclick", lang.hitch(this, "_onDblClick")));
240 }));
241 },
242
243 _isValid: function(){
244 // summary:
245 // Internal function to allow validating of the inputs
246 // for a link to determine if set should be disabled or not
247 // tags:
248 // protected
249 return this._urlInput.isValid() && this._textInput.isValid();
250 },
251
252 _setContent: function(staticPanel){
253 // summary:
254 // Helper for _initButton above. Not sure why it's a separate method.
255 this.dropDown.set({
256 parserScope: "dojo", // make parser search for dojoType/data-dojo-type even if page is multi-version
257 content: staticPanel
258 });
259 },
260
261 _checkValues: function(args){
262 // summary:
263 // Function to check the values in args and 'fix' them up as needed.
264 // args: Object
265 // Content being set.
266 // tags:
267 // protected
268 if(args && args.urlInput){
269 args.urlInput = args.urlInput.replace(/"/g, "&quot;");
270 }
271 if(!this.allowUnsafeHtml && args && args.textInput){
272 if(typeof this.linkFilter === 'function'){
273 args.textInput = this.linkFilter(args.textInput);
274 }
275 else{
276 array.forEach(this.linkFilter, function (currentFilter) {
277 args.textInput = args.textInput.replace(currentFilter[0], currentFilter[1]);
278 });
279 }
280 }
281 return args;
282 },
283
284 _createlinkEnabledImpl: function() {
285 // summary:
286 // This function implements the test for if the create link
287 // command should be enabled or not. This plugin supports
288 // link creation even without selected text.
289 // tags:
290 // protected
291 return true;
292 },
293
294 setValue: function(args){
295 // summary:
296 // Callback from the dialog when user presses "set" button.
297 // tags:
298 // private
299
300 // TODO: prevent closing popup if the text is empty
301 this._onCloseDialog();
302 if(has("ie") < 9){ //see #4151
303 var sel = rangeapi.getSelection(this.editor.window);
304 var range = sel.getRangeAt(0);
305 var a = range.endContainer;
306 if(a.nodeType === 3){
307 // Text node, may be the link contents, so check parent.
308 // This plugin doesn't really support nested HTML elements
309 // in the link, it assumes all link content is text.
310 a = a.parentNode;
311 }
312 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
313 // Still nothing, one last thing to try on IE, as it might be 'img'
314 // and thus considered a control.
315 a = this.editor.selection.getSelectedElement(this.tag);
316 }
317 if(a && (a.nodeName && a.nodeName.toLowerCase() === this.tag)){
318 // Okay, we do have a match. IE, for some reason, sometimes pastes before
319 // instead of removing the targeted paste-over element, so we unlink the
320 // old one first. If we do not the <a> tag remains, but it has no content,
321 // so isn't readily visible (but is wrong for the action).
322 if(this.editor.queryCommandEnabled("unlink")){
323 // Select all the link children, then unlink. The following insert will
324 // then replace the selected text.
325 this.editor.selection.selectElementChildren(a);
326 this.editor.execCommand("unlink");
327 }
328 }
329 }
330 // make sure values are properly escaped, etc.
331 args = this._checkValues(args);
332 this.editor.execCommand('inserthtml',
333 string.substitute(this.htmlTemplate, args));
334
335 // IE sometimes leaves a blank link, so we need to fix it up.
336 // Go ahead and do this for everyone just to avoid blank links
337 // in the page.
338 query("a", this.editor.document).forEach(function(a){
339 if(!a.innerHTML && !domAttr.has(a, "name")){
340 // Remove empty anchors that do not have "name" set.
341 // Empty ones with a name set could be a hidden hash
342 // anchor.
343 a.parentNode.removeChild(a);
344 }
345 }, this);
346 },
347
348 _onCloseDialog: function(){
349 // summary:
350 // Handler for close event on the dialog
351
352 if(this.editor.focused){
353 // put focus back in the edit area, unless the dialog closed because the user clicked somewhere else
354 this.editor.focus();
355 }
356 },
357
358 _getCurrentValues: function(a){
359 // summary:
360 // Over-ride for getting the values to set in the dropdown.
361 // a:
362 // The anchor/link to process for data for the dropdown.
363 // tags:
364 // protected
365 var url, text, target;
366 if(a && a.tagName.toLowerCase() === this.tag){
367 url = a.getAttribute('_djrealurl') || a.getAttribute('href');
368 target = a.getAttribute('target') || "_self";
369 text = a.textContent || a.innerText;
370 this.editor.selection.selectElement(a, true);
371 }else{
372 text = this.editor.selection.getSelectedText();
373 }
374 return {urlInput: url || '', textInput: text || '', targetSelect: target || ''}; //Object;
375 },
376
377 _onOpenDialog: function(){
378 // summary:
379 // Handler for when the dialog is opened.
380 // If the caret is currently in a URL then populate the URL's info into the dialog.
381 var a, b, fc;
382 if(has("ie")){
383 // IE, even IE10, is difficult to select the element in, using the range unified
384 // API seems to work reasonably well.
385 var sel = rangeapi.getSelection(this.editor.window);
386 if(sel.rangeCount){
387 var range = sel.getRangeAt(0);
388 a = range.endContainer;
389 if(a.nodeType === 3){
390 // Text node, may be the link contents, so check parent.
391 // This plugin doesn't really support nested HTML elements
392 // in the link, it assumes all link content is text.
393 a = a.parentNode;
394 }
395 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
396 // Still nothing, one last thing to try on IE, as it might be 'img'
397 // and thus considered a control.
398 a = this.editor.selection.getSelectedElement(this.tag);
399 }
400 if(!a || (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
401 // Try another lookup, IE's selection is just terrible.
402 b = this.editor.selection.getAncestorElement(this.tag);
403 if(b && (b.nodeName && b.nodeName.toLowerCase() == this.tag)){
404 // Looks like we found an A tag, use it and make sure just it is
405 // selected.
406 a = b;
407 this.editor.selection.selectElement(a);
408 }else if(range.startContainer === range.endContainer){
409 // STILL nothing. Trying one more thing. Lets look at the first child.
410 // It might be an anchor tag in a div by itself or the like. If it is,
411 // we'll use it otherwise we give up. The selection is not easily
412 // determinable to be on an existing anchor tag.
413 fc = range.startContainer.firstChild;
414 if(fc && (fc.nodeName && fc.nodeName.toLowerCase() == this.tag)){
415 a = fc;
416 this.editor.selection.selectElement(a);
417 }
418 }
419 }
420 }
421 }else{
422 a = this.editor.selection.getAncestorElement(this.tag);
423 }
424 this.dropDown.reset();
425 this._setButton.set("disabled", true);
426 this.dropDown.set("value", this._getCurrentValues(a));
427 },
428
429 _onDblClick: function(e){
430 // summary:
431 // Function to define a behavior on double clicks on the element
432 // type this dialog edits to select it and pop up the editor
433 // dialog.
434 // e: Object
435 // The double-click event.
436 // tags:
437 // protected.
438 if(e && e.target){
439 var t = e.target;
440 var tg = t.tagName ? t.tagName.toLowerCase() : "";
441 if(tg === this.tag && domAttr.get(t, "href")){
442 var editor = this.editor;
443
444 this.editor.selection.selectElement(t);
445 editor.onDisplayChanged();
446
447 // Call onNormalizedDisplayChange() now, rather than on timer.
448 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
449 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
450 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
451 // since (for unknown reasons) focus.js ignores disabled controls.
452 if(editor._updateTimer){
453 editor._updateTimer.remove();
454 delete editor._updateTimer;
455 }
456 editor.onNormalizedDisplayChanged();
457
458 var button = this.button;
459 setTimeout(function(){
460 // Focus shift outside the event handler.
461 // IE doesn't like focus changes in event handles.
462 button.set("disabled", false);
463 button.loadAndOpenDropDown().then(function(){
464 if(button.dropDown.focus){
465 button.dropDown.focus();
466 }
467 });
468 }, 10);
469 }
470 }
471 },
472
473 _onMouseUp: function(){
474 // summary:
475 // Function to define a behavior on mouse up on the element
476 // type this dialog edits to move the cursor just outside
477 // anchor tags when clicking on their edges.
478 // tags:
479 // protected.
480 if(has('ff')){
481 var a = this.editor.selection.getAncestorElement(this.tag);
482 if(a){
483 var selection = rangeapi.getSelection(this.editor.window);
484 var range = selection.getRangeAt(0);
485 if(range.collapsed && a.childNodes.length){
486 var test = range.cloneRange();
487 test.selectNodeContents(a.childNodes[a.childNodes.length - 1]);
488 test.setStart(a.childNodes[0], 0);
489 if(range.compareBoundaryPoints(test.START_TO_START, test) !== 1){
490 // cursor is before or at the test start
491 range.setStartBefore(a);
492 }else if(range.compareBoundaryPoints(test.END_TO_START, test) !== -1){
493 // cursor is before or at the test end
494 range.setStartAfter(a);
495 }
496 }
497 }
498 }
499 }
500 });
501
502 var ImgLinkDialog = declare("dijit._editor.plugins.ImgLinkDialog", [LinkDialog], {
503 // summary:
504 // This plugin extends LinkDialog and adds in a plugin for handling image links.
505 // provides the image link dialog.
506 // description:
507 // The command provided by this plugin is:
508 //
509 // - insertImage
510
511 // linkDialogTemplate: [protected] String
512 // Over-ride for template since img dialog doesn't need target that anchor tags may.
513 linkDialogTemplate: [
514 "<table role='presentation'><tr><td>",
515 "<label for='${id}_urlInput'>${url}</label>",
516 "</td><td>",
517 "<input dojoType='dijit.form.ValidationTextBox' regExp='${urlRegExp}' " +
518 "required='true' id='${id}_urlInput' name='urlInput' data-dojo-props='intermediateChanges:true'/>",
519 "</td></tr><tr><td>",
520 "<label for='${id}_textInput'>${text}</label>",
521 "</td><td>",
522 "<input data-dojo-type='dijit.form.ValidationTextBox' required='false' id='${id}_textInput' " +
523 "name='textInput' data-dojo-props='intermediateChanges:true'/>",
524 "</td></tr><tr><td>",
525 "</td><td>",
526 "</td></tr><tr><td colspan='2'>",
527 "<button data-dojo-type='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
528 "<button data-dojo-type='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
529 "</td></tr></table>"
530 ].join(""),
531
532 // htmlTemplate: [protected] String
533 // String used for templating the `<img>` HTML to insert at the desired point.
534 htmlTemplate: "<img src=\"${urlInput}\" _djrealurl=\"${urlInput}\" alt=\"${textInput}\" />",
535
536 // tag: [protected] String
537 // Tag used for the link type (img).
538 tag: "img",
539
540 _getCurrentValues: function(img){
541 // summary:
542 // Over-ride for getting the values to set in the dropdown.
543 // a:
544 // The anchor/link to process for data for the dropdown.
545 // tags:
546 // protected
547 var url, text;
548 if(img && img.tagName.toLowerCase() === this.tag){
549 url = img.getAttribute('_djrealurl') || img.getAttribute('src');
550 text = img.getAttribute('alt');
551 this.editor.selection.selectElement(img, true);
552 }else{
553 text = this.editor.selection.getSelectedText();
554 }
555 return {urlInput: url || '', textInput: text || ''}; //Object
556 },
557
558 _isValid: function(){
559 // summary:
560 // Over-ride for images. You can have alt text of blank, it is valid.
561 // tags:
562 // protected
563 return this._urlInput.isValid();
564 },
565
566 _connectTagEvents: function(){
567 // summary:
568 // Over-ridable function that connects tag specific events.
569 this.inherited(arguments);
570 this.editor.onLoadDeferred.then(lang.hitch(this, function(){
571 // Use onmousedown instead of onclick. Seems that IE eats the first onclick
572 // to wrap it in a selector box, then the second one acts as onclick. See #10420
573 this.own(on(this.editor.editNode, "mousedown", lang.hitch(this, "_selectTag")));
574 }));
575 },
576
577 _selectTag: function(e){
578 // summary:
579 // A simple event handler that lets me select an image if it is clicked on.
580 // makes it easier to select images in a standard way across browsers. Otherwise
581 // selecting an image for edit becomes difficult.
582 // e: Event
583 // The mousedown event.
584 // tags:
585 // private
586 if(e && e.target){
587 var t = e.target;
588 var tg = t.tagName ? t.tagName.toLowerCase() : "";
589 if(tg === this.tag){
590 this.editor.selection.selectElement(t);
591 }
592 }
593 },
594
595 _checkValues: function(args){
596 // summary:
597 // Function to check the values in args and 'fix' them up as needed
598 // (special characters in the url or alt text)
599 // args: Object
600 // Content being set.
601 // tags:
602 // protected
603 if(args && args.urlInput){
604 args.urlInput = args.urlInput.replace(/"/g, "&quot;");
605 }
606 if(args && args.textInput){
607 args.textInput = args.textInput.replace(/"/g, "&quot;");
608 }
609 return args;
610 },
611
612 _onDblClick: function(e){
613 // summary:
614 // Function to define a behavior on double clicks on the element
615 // type this dialog edits to select it and pop up the editor
616 // dialog.
617 // e: Object
618 // The double-click event.
619 // tags:
620 // protected.
621 if(e && e.target){
622 var t = e.target;
623 var tg = t.tagName ? t.tagName.toLowerCase() : "";
624 if(tg === this.tag && domAttr.get(t, "src")){
625 var editor = this.editor;
626
627 this.editor.selection.selectElement(t);
628 editor.onDisplayChanged();
629
630 // Call onNormalizedDisplayChange() now, rather than on timer.
631 // On IE, when focus goes to the first <input> in the TooltipDialog, the editor loses it's selection.
632 // Later if onNormalizedDisplayChange() gets called via the timer it will disable the LinkDialog button
633 // (actually, all the toolbar buttons), at which point clicking the <input> will close the dialog,
634 // since (for unknown reasons) focus.js ignores disabled controls.
635 if(editor._updateTimer){
636 editor._updateTimer.remove();
637 delete editor._updateTimer;
638 }
639 editor.onNormalizedDisplayChanged();
640
641 var button = this.button;
642 setTimeout(function(){
643 // Focus shift outside the event handler.
644 // IE doesn't like focus changes in event handles.
645 button.set("disabled", false);
646 button.loadAndOpenDropDown().then(function(){
647 if(button.dropDown.focus){
648 button.dropDown.focus();
649 }
650 });
651 }, 10);
652 }
653 }
654 }
655 });
656
657 // Register these plugins
658 _Plugin.registry["createLink"] = function(args){
659 var pluginOptions = {
660 command: "createLink",
661 allowUnsafeHtml: ("allowUnsafeHtml" in args) ? args.allowUnsafeHtml : false
662 };
663 if("linkFilter" in args){
664 pluginOptions.linkFilter = args.linkFilter;
665 }
666 return new LinkDialog(pluginOptions);
667 };
668 _Plugin.registry["insertImage"] = function(){
669 return new ImgLinkDialog({command: "insertImage"});
670 };
671
672
673 // Export both LinkDialog and ImgLinkDialog
674 // TODO for 2.0: either return both classes in a hash, or split this file into two separate files.
675 // Then the documentation for the module can be applied to the hash, and will show up in the API doc.
676 LinkDialog.ImgLinkDialog = ImgLinkDialog;
677 return LinkDialog;
678 });