]> git.wh0rd.org - tt-rss.git/blame - lib/dijit/_editor/plugins/LinkDialog.js
remove call-by-reference to comply with php 5.4
[tt-rss.git] / lib / dijit / _editor / plugins / LinkDialog.js
CommitLineData
2f01fe57 1/*
81bea17a 2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
2f01fe57
AD
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5*/
6
7
81bea17a
AD
8if(!dojo._hasResource["dijit._editor.plugins.LinkDialog"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dijit._editor.plugins.LinkDialog"] = true;
2f01fe57
AD
10dojo.provide("dijit._editor.plugins.LinkDialog");
11dojo.require("dijit._Widget");
2f01fe57
AD
12dojo.require("dijit._editor._Plugin");
13dojo.require("dijit.TooltipDialog");
81bea17a 14dojo.require("dijit.form.DropDownButton");
2f01fe57
AD
15dojo.require("dijit.form.ValidationTextBox");
16dojo.require("dijit.form.Select");
17dojo.require("dijit._editor.range");
18dojo.require("dojo.i18n");
19dojo.require("dojo.string");
81bea17a
AD
20dojo.requireLocalization("dijit", "common", null, "ROOT,ar,ca,cs,da,de,el,es,fi,fr,he,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
21dojo.requireLocalization("dijit._editor", "LinkDialog", null, "ROOT,ar,ca,cs,da,de,el,es,fi,fr,he,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
22
23
24dojo.declare("dijit._editor.plugins.LinkDialog", dijit._editor._Plugin, {
25 // summary:
26 // This plugin provides the basis for an 'anchor' (link) dialog and an extension of it
27 // provides the image link dialog.
28 //
29 // description:
30 // The command provided by this plugin is:
31 // * createLink
32
33 // Override _Plugin.buttonClass. This plugin is controlled by a DropDownButton
34 // (which triggers a TooltipDialog).
35 buttonClass: dijit.form.DropDownButton,
36
37 // Override _Plugin.useDefaultCommand... processing is handled by this plugin, not by dijit.Editor.
38 useDefaultCommand: false,
39
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/]*)?(?:#.*)?)?)?",
44
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.
49
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}\"" +
54 ">${textInput}</a>",
55
56 // tag: [protected] String
57 // Tag used for the link type.
58 tag: "a",
59
60 // _hostRxp [private] RegExp
61 // Regular expression used to validate url fragments (ip address, hostname, etc)
62 _hostRxp: new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$"),
63
64 // _userAtRxp [private] RegExp
65 // Regular expression used to validate e-mail address fragment.
66 _userAtRxp: new RegExp("^([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+@", "i"),
67
68 // linkDialogTemplate: [protected] String
69 // Template for contents of TooltipDialog to pick URL
70 linkDialogTemplate: [
71 "<table><tr><td>",
72 "<label for='${id}_urlInput'>${url}</label>",
73 "</td><td>",
74 "<input dojoType='dijit.form.ValidationTextBox' required='true' " +
75 "id='${id}_urlInput' name='urlInput' intermediateChanges='true'/>",
76 "</td></tr><tr><td>",
77 "<label for='${id}_textInput'>${text}</label>",
78 "</td><td>",
79 "<input dojoType='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' " +
80 "name='textInput' intermediateChanges='true'/>",
81 "</td></tr><tr><td>",
82 "<label for='${id}_targetSelect'>${target}</label>",
83 "</td><td>",
84 "<select id='${id}_targetSelect' name='targetSelect' dojoType='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>",
89 "</select>",
90 "</td></tr><tr><td colspan='2'>",
91 "<button dojoType='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
92 "<button dojoType='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
93 "</td></tr></table>"
94 ].join(""),
95
96 _initButton: function(){
97 // Override _Plugin._initButton() to initialize DropDownButton and TooltipDialog.
98 var _this = this;
99 this.tag = this.command == 'insertImage' ? 'img' : 'a';
100 var messages = dojo.mixin(dojo.i18n.getLocalization("dijit", "common", this.lang),
101 dojo.i18n.getLocalization("dijit._editor", "LinkDialog", this.lang));
102 var dropDown = (this.dropDown = new dijit.TooltipDialog({
103 title: messages[this.command + "Title"],
104 execute: dojo.hitch(this, "setValue"),
105 onOpen: function(){
106 _this._onOpenDialog();
107 dijit.TooltipDialog.prototype.onOpen.apply(this, arguments);
108 },
109 onCancel: function(){
110 setTimeout(dojo.hitch(_this, "_onCloseDialog"),0);
111 }
112 }));
113 messages.urlRegExp = this.urlRegExp;
114 messages.id = dijit.getUniqueId(this.editor.id);
115 this._uniqueId = messages.id;
116 this._setContent(dropDown.title +
117 "<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>" +
118 dojo.string.substitute(this.linkDialogTemplate, messages));
119 dropDown.startup();
120 this._urlInput = dijit.byId(this._uniqueId + "_urlInput");
121 this._textInput = dijit.byId(this._uniqueId + "_textInput");
122 this._setButton = dijit.byId(this._uniqueId + "_setButton");
123 this.connect(dijit.byId(this._uniqueId + "_cancelButton"), "onClick", function(){
124 this.dropDown.onCancel();
125 });
126 if(this._urlInput){
127 this.connect(this._urlInput, "onChange", "_checkAndFixInput");
128 }
129 if(this._textInput){
130 this.connect(this._textInput, "onChange", "_checkAndFixInput");
131 }
132
133 // Build up the dual check for http/https/file:, and mailto formats.
134 this._urlRegExp = new RegExp("^" + this.urlRegExp + "$", "i");
135 this._emailRegExp = new RegExp("^" + this.emailRegExp + "$", "i");
136 this._urlInput.isValid = dojo.hitch(this, function(){
137 // Function over-ride of isValid to test if the input matches a url or a mailto style link.
138 var value = this._urlInput.get("value");
139 return this._urlRegExp.test(value) || this._emailRegExp.test(value);
140 });
141
142 this._connectTagEvents();
143 this.inherited(arguments);
144 },
145
146 _checkAndFixInput: function(){
147 // summary:
148 // A function to listen for onChange events and test the input contents
149 // for valid information, such as valid urls with http/https/ftp and if
150 // not present, try and guess if the input url is relative or not, and if
151 // not, append http:// to it. Also validates other fields as determined by
152 // the internal _isValid function.
153 var self = this;
154 var url = this._urlInput.get("value");
155 var fixupUrl = function(url){
156 var appendHttp = false;
157 var appendMailto = false;
158 if(url && url.length > 1){
159 url = dojo.trim(url);
160 if(url.indexOf("mailto:") !== 0){
161 if(url.indexOf("/") > 0){
162 if(url.indexOf("://") === -1){
163 // Check that it doesn't start with / or ./, which would
164 // imply 'target server relativeness'
165 if(url.charAt(0) !== '/' && url.indexOf("./") !== 0){
166 if(self._hostRxp.test(url)){
167 appendHttp = true;
168 }
169 }
170 }
171 }else if(self._userAtRxp.test(url)){
172 // If it looks like a foo@, append a mailto.
173 appendMailto = true;
174 }
175 }
176 }
177 if(appendHttp){
178 self._urlInput.set("value", "http://" + url);
179 }
180 if(appendMailto){
181 self._urlInput.set("value", "mailto:" + url);
182 }
183 self._setButton.set("disabled", !self._isValid());
184 };
185 if(this._delayedCheck){
186 clearTimeout(this._delayedCheck);
187 this._delayedCheck = null;
188 }
189 this._delayedCheck = setTimeout(function(){
190 fixupUrl(url);
191 }, 250);
192 },
193
194 _connectTagEvents: function(){
195 // summary:
196 // Over-ridable function that connects tag specific events.
197 this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
198 this.connect(this.editor.editNode, "ondblclick", this._onDblClick);
199 }));
200 },
201
202 _isValid: function(){
203 // summary:
204 // Internal function to allow validating of the inputs
205 // for a link to determine if set should be disabled or not
206 // tags:
207 // protected
208 return this._urlInput.isValid() && this._textInput.isValid();
209 },
210
211 _setContent: function(staticPanel){
212 // summary:
213 // Helper for _initButton above. Not sure why it's a separate method.
214 this.dropDown.set({
215 parserScope: "dojo", // make parser search for dojoType/data-dojo-type even if page is multi-version
216 content: staticPanel
217 });
218 },
219
220 _checkValues: function(args){
221 // summary:
222 // Function to check the values in args and 'fix' them up as needed.
223 // args: Object
224 // Content being set.
225 // tags:
226 // protected
227 if(args && args.urlInput){
228 args.urlInput = args.urlInput.replace(/"/g, "&quot;");
229 }
230 return args;
231 },
232
233 setValue: function(args){
234 // summary:
235 // Callback from the dialog when user presses "set" button.
236 // tags:
237 // private
238 //TODO: prevent closing popup if the text is empty
239 this._onCloseDialog();
240 if(dojo.isIE < 9){ //see #4151
241 var sel = dijit.range.getSelection(this.editor.window);
242 var range = sel.getRangeAt(0);
243 var a = range.endContainer;
244 if(a.nodeType === 3){
245 // Text node, may be the link contents, so check parent.
246 // This plugin doesn't really support nested HTML elements
247 // in the link, it assumes all link content is text.
248 a = a.parentNode;
249 }
250 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
251 // Stll nothing, one last thing to try on IE, as it might be 'img'
252 // and thus considered a control.
253 a = dojo.withGlobal(this.editor.window,
254 "getSelectedElement", dijit._editor.selection, [this.tag]);
255 }
256 if(a && (a.nodeName && a.nodeName.toLowerCase() === this.tag)){
257 // Okay, we do have a match. IE, for some reason, sometimes pastes before
258 // instead of removing the targetted paste-over element, so we unlink the
259 // old one first. If we do not the <a> tag remains, but it has no content,
260 // so isn't readily visible (but is wrong for the action).
261 if(this.editor.queryCommandEnabled("unlink")){
262 // Select all the link childent, then unlink. The following insert will
263 // then replace the selected text.
264 dojo.withGlobal(this.editor.window,
265 "selectElementChildren", dijit._editor.selection, [a]);
266 this.editor.execCommand("unlink");
267 }
268 }
269 }
270 // make sure values are properly escaped, etc.
271 args = this._checkValues(args);
272 this.editor.execCommand('inserthtml',
273 dojo.string.substitute(this.htmlTemplate, args));
274 },
275
276 _onCloseDialog: function(){
277 // summary:
278 // Handler for close event on the dialog
279 this.editor.focus();
280 },
281
282 _getCurrentValues: function(a){
283 // summary:
284 // Over-ride for getting the values to set in the dropdown.
285 // a:
286 // The anchor/link to process for data for the dropdown.
287 // tags:
288 // protected
289 var url, text, target;
290 if(a && a.tagName.toLowerCase() === this.tag){
291 url = a.getAttribute('_djrealurl') || a.getAttribute('href');
292 target = a.getAttribute('target') || "_self";
293 text = a.textContent || a.innerText;
294 dojo.withGlobal(this.editor.window, "selectElement", dijit._editor.selection, [a, true]);
295 }else{
296 text = dojo.withGlobal(this.editor.window, dijit._editor.selection.getSelectedText);
297 }
298 return {urlInput: url || '', textInput: text || '', targetSelect: target || ''}; //Object;
299 },
300
301 _onOpenDialog: function(){
302 // summary:
303 // Handler for when the dialog is opened.
304 // If the caret is currently in a URL then populate the URL's info into the dialog.
305 var a;
306 if(dojo.isIE < 9){
307 // IE is difficult to select the element in, using the range unified
308 // API seems to work reasonably well.
309 var sel = dijit.range.getSelection(this.editor.window);
310 var range = sel.getRangeAt(0);
311 a = range.endContainer;
312 if(a.nodeType === 3){
313 // Text node, may be the link contents, so check parent.
314 // This plugin doesn't really support nested HTML elements
315 // in the link, it assumes all link content is text.
316 a = a.parentNode;
317 }
318 if(a && (a.nodeName && a.nodeName.toLowerCase() !== this.tag)){
319 // Stll nothing, one last thing to try on IE, as it might be 'img'
320 // and thus considered a control.
321 a = dojo.withGlobal(this.editor.window,
322 "getSelectedElement", dijit._editor.selection, [this.tag]);
323 }
324 }else{
325 a = dojo.withGlobal(this.editor.window,
326 "getAncestorElement", dijit._editor.selection, [this.tag]);
327 }
328 this.dropDown.reset();
329 this._setButton.set("disabled", true);
330 this.dropDown.set("value", this._getCurrentValues(a));
331 },
332
333 _onDblClick: function(e){
334 // summary:
335 // Function to define a behavior on double clicks on the element
336 // type this dialog edits to select it and pop up the editor
337 // dialog.
338 // e: Object
339 // The double-click event.
340 // tags:
341 // protected.
342 if(e && e.target){
343 var t = e.target;
344 var tg = t.tagName? t.tagName.toLowerCase() : "";
345 if(tg === this.tag && dojo.attr(t,"href")){
346 dojo.withGlobal(this.editor.window,
347 "selectElement",
348 dijit._editor.selection, [t]);
349 this.editor.onDisplayChanged();
350
351 setTimeout(dojo.hitch(this, function(){
352 // Focus shift outside the event handler.
353 // IE doesn't like focus changes in event handles.
354 this.button.set("disabled", false);
355 this.button.openDropDown();
356 }), 10);
357 }
358 }
359 }
2f01fe57 360});
81bea17a
AD
361
362dojo.declare("dijit._editor.plugins.ImgLinkDialog", [dijit._editor.plugins.LinkDialog], {
363 // summary:
364 // This plugin extends LinkDialog and adds in a plugin for handling image links.
365 // provides the image link dialog.
366 //
367 // description:
368 // The command provided by this plugin is:
369 // * insertImage
370
371 // linkDialogTemplate: [protected] String
372 // Over-ride for template since img dialog doesn't need target that anchor tags may.
373 linkDialogTemplate: [
374 "<table><tr><td>",
375 "<label for='${id}_urlInput'>${url}</label>",
376 "</td><td>",
377 "<input dojoType='dijit.form.ValidationTextBox' regExp='${urlRegExp}' " +
378 "required='true' id='${id}_urlInput' name='urlInput' intermediateChanges='true'/>",
379 "</td></tr><tr><td>",
380 "<label for='${id}_textInput'>${text}</label>",
381 "</td><td>",
382 "<input dojoType='dijit.form.ValidationTextBox' required='false' id='${id}_textInput' " +
383 "name='textInput' intermediateChanges='true'/>",
384 "</td></tr><tr><td>",
385 "</td><td>",
386 "</td></tr><tr><td colspan='2'>",
387 "<button dojoType='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>",
388 "<button dojoType='dijit.form.Button' type='button' id='${id}_cancelButton'>${buttonCancel}</button>",
389 "</td></tr></table>"
390 ].join(""),
391
392 // htmlTemplate: [protected] String
393 // String used for templating the <img> HTML to insert at the desired point.
394 htmlTemplate: "<img src=\"${urlInput}\" _djrealurl=\"${urlInput}\" alt=\"${textInput}\" />",
395
396 // tag: [protected] String
397 // Tag used for the link type (img).
398 tag: "img",
399
400 _getCurrentValues: function(img){
401 // summary:
402 // Over-ride for getting the values to set in the dropdown.
403 // a:
404 // The anchor/link to process for data for the dropdown.
405 // tags:
406 // protected
407 var url, text;
408 if(img && img.tagName.toLowerCase() === this.tag){
409 url = img.getAttribute('_djrealurl') || img.getAttribute('src');
410 text = img.getAttribute('alt');
411 dojo.withGlobal(this.editor.window,
412 "selectElement", dijit._editor.selection, [img, true]);
413 }else{
414 text = dojo.withGlobal(this.editor.window, dijit._editor.selection.getSelectedText);
415 }
416 return {urlInput: url || '', textInput: text || ''}; //Object;
417 },
418
419 _isValid: function(){
420 // summary:
421 // Over-ride for images. You can have alt text of blank, it is valid.
422 // tags:
423 // protected
424 return this._urlInput.isValid();
425 },
426
427 _connectTagEvents: function(){
428 // summary:
429 // Over-ridable function that connects tag specific events.
430 this.inherited(arguments);
431 this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
432 // Use onmousedown instead of onclick. Seems that IE eats the first onclick
433 // to wrap it in a selector box, then the second one acts as onclick. See #10420
434 this.connect(this.editor.editNode, "onmousedown", this._selectTag);
435 }));
436 },
437
438 _selectTag: function(e){
439 // summary:
440 // A simple event handler that lets me select an image if it is clicked on.
441 // makes it easier to select images in a standard way across browsers. Otherwise
442 // selecting an image for edit becomes difficult.
443 // e: Event
444 // The mousedown event.
445 // tags:
446 // private
447 if(e && e.target){
448 var t = e.target;
449 var tg = t.tagName? t.tagName.toLowerCase() : "";
450 if(tg === this.tag){
451 dojo.withGlobal(this.editor.window,
452 "selectElement",
453 dijit._editor.selection, [t]);
454 }
455 }
456 },
457
458 _checkValues: function(args){
459 // summary:
460 // Function to check the values in args and 'fix' them up as needed
461 // (special characters in the url or alt text)
462 // args: Object
463 // Content being set.
464 // tags:
465 // protected
466 if(args && args.urlInput){
467 args.urlInput = args.urlInput.replace(/"/g, "&quot;");
468 }
469 if(args && args.textInput){
470 args.textInput = args.textInput.replace(/"/g, "&quot;");
471 }
472 return args;
473 },
474
475 _onDblClick: function(e){
476 // summary:
477 // Function to define a behavior on double clicks on the element
478 // type this dialog edits to select it and pop up the editor
479 // dialog.
480 // e: Object
481 // The double-click event.
482 // tags:
483 // protected.
484 if(e && e.target){
485 var t = e.target;
486 var tg = t.tagName? t.tagName.toLowerCase() : "";
487 if(tg === this.tag && dojo.attr(t,"src")){
488 dojo.withGlobal(this.editor.window,
489 "selectElement",
490 dijit._editor.selection, [t]);
491 this.editor.onDisplayChanged();
492 setTimeout(dojo.hitch(this, function(){
493 // Focus shift outside the event handler.
494 // IE doesn't like focus changes in event handles.
495 this.button.set("disabled", false);
496 this.button.openDropDown();
497 }), 10);
498 }
499 }
500 }
2f01fe57 501});
81bea17a
AD
502
503// Register this plugin.
504dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
505 if(o.plugin){ return; }
506 switch(o.args.name){
507 case "createLink":
508 o.plugin = new dijit._editor.plugins.LinkDialog({command: o.args.name});
509 break;
510 case "insertImage":
511 o.plugin = new dijit._editor.plugins.ImgLinkDialog({command: o.args.name});
512 break;
513 }
2f01fe57 514});
81bea17a 515
2f01fe57 516}