Debian Codegraph @

closure-library/closure/goog/ui/editor/linkdialog.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview A dialog for editing/creating a link.
17 *
18 * @author [email protected] (Robby Walker)
19 */
20
21 goog.provide('goog.ui.editor.LinkDialog');
22 goog.provide('goog.ui.editor.LinkDialog.BeforeTestLinkEvent');
23 goog.provide('goog.ui.editor.LinkDialog.EventType');
24 goog.provide('goog.ui.editor.LinkDialog.OkEvent');
25
26 goog.require('goog.a11y.aria');
27 goog.require('goog.a11y.aria.State');
28 goog.require('goog.dom');
29 goog.require('goog.dom.InputType');
30 goog.require('goog.dom.TagName');
31 goog.require('goog.dom.safe');
32 goog.require('goog.editor.BrowserFeature');
33 goog.require('goog.editor.Link');
34 goog.require('goog.editor.focus');
35 goog.require('goog.editor.node');
36 goog.require('goog.events.Event');
37 goog.require('goog.events.EventHandler');
38 goog.require('goog.events.InputHandler');
39 goog.require('goog.html.SafeHtml');
40 goog.require('goog.html.SafeHtmlFormatter');
41 goog.require('goog.string');
42 goog.require('goog.string.Unicode');
43 goog.require('goog.style');
44 goog.require('goog.ui.Button');
45 goog.require('goog.ui.Component');
46 goog.require('goog.ui.LinkButtonRenderer');
47 goog.require('goog.ui.editor.AbstractDialog');
48 goog.require('goog.ui.editor.TabPane');
49 goog.require('goog.ui.editor.messages');
50 goog.require('goog.userAgent');
51 goog.require('goog.window');
52
53
54
55 /**
56 * A type of goog.ui.editor.AbstractDialog for editing/creating a link.
57 * @param {goog.dom.DomHelper} domHelper DomHelper to be used to create the
58 * dialog's dom structure.
59 * @param {goog.editor.Link} link The target link.
60 * @constructor
61 * @extends {goog.ui.editor.AbstractDialog}
62 * @final
63 */
64 goog.ui.editor.LinkDialog = function(domHelper, link) {
65 goog.ui.editor.LinkDialog.base(this, 'constructor', domHelper);
66
67 /**
68 * The link being modified by this dialog.
69 * @type {goog.editor.Link}
70 * @private
71 */
72 this.targetLink_ = link;
73
74 /**
75 * The event handler for this dialog.
76 * @type {goog.events.EventHandler<!goog.ui.editor.LinkDialog>}
77 * @private
78 */
79 this.eventHandler_ = new goog.events.EventHandler(this);
80 this.registerDisposable(this.eventHandler_);
81
82 /**
83 * Optional warning to show about email addresses.
84 * @type {goog.html.SafeHtml}
85 * @private
86 */
87 this.emailWarning_ = null;
88
89 /**
90 * Whether to show a checkbox where the user can choose to have the link open
91 * in a new window.
92 * @type {boolean}
93 * @private
94 */
95 this.showOpenLinkInNewWindow_ = false;
96
97 /**
98 * Whether the "open link in new window" checkbox should be checked when the
99 * dialog is shown, and also whether it was checked last time the dialog was
100 * closed.
101 * @type {boolean}
102 * @private
103 */
104 this.isOpenLinkInNewWindowChecked_ = false;
105
106 /**
107 * Whether to show a checkbox where the user can choose to have 'rel=nofollow'
108 * attribute added to the link.
109 * @type {boolean}
110 * @private
111 */
112 this.showRelNoFollow_ = false;
113
114 /**
115 * InputHandler object to listen for changes in the url input field.
116 * @type {goog.events.InputHandler}
117 * @private
118 */
119 this.urlInputHandler_ = null;
120
121 /**
122 * InputHandler object to listen for changes in the email input field.
123 * @type {goog.events.InputHandler}
124 * @private
125 */
126 this.emailInputHandler_ = null;
127
128 /**
129 * InputHandler object to listen for changes in the text to display input
130 * field.
131 * @type {goog.events.InputHandler}
132 * @private
133 */
134 this.textInputHandler_ = null;
135
136 /**
137 * The tab bar where the url and email tabs are.
138 * @type {goog.ui.editor.TabPane}
139 * @private
140 */
141 this.tabPane_ = null;
142
143 /**
144 * The div element holding the link's display text input.
145 * @type {HTMLDivElement}
146 * @private
147 */
148 this.textToDisplayDiv_ = null;
149
150 /**
151 * The input element holding the link's display text.
152 * @type {HTMLInputElement}
153 * @private
154 */
155 this.textToDisplayInput_ = null;
156
157 /**
158 * Whether or not the feature of automatically generating the display text is
159 * enabled.
160 * @type {boolean}
161 * @private
162 */
163 this.autogenFeatureEnabled_ = true;
164
165 /**
166 * Whether or not we should automatically generate the display text.
167 * @type {boolean}
168 * @private
169 */
170 this.autogenerateTextToDisplay_ = false;
171
172 /**
173 * Whether or not automatic generation of the display text is disabled.
174 * @type {boolean}
175 * @private
176 */
177 this.disableAutogen_ = false;
178
179 /**
180 * The input element (checkbox) to indicate that the link should open in a new
181 * window.
182 * @type {HTMLInputElement}
183 * @private
184 */
185 this.openInNewWindowCheckbox_ = null;
186
187 /**
188 * The input element (checkbox) to indicate that the link should have
189 * 'rel=nofollow' attribute.
190 * @type {HTMLInputElement}
191 * @private
192 */
193 this.relNoFollowCheckbox_ = null;
194
195 /**
196 * Whether to stop leaking the page's url via the referrer header when the
197 * "test this link" link is clicked.
198 * @type {boolean}
199 * @private
200 */
201 this.stopReferrerLeaks_ = false;
202 };
203 goog.inherits(goog.ui.editor.LinkDialog, goog.ui.editor.AbstractDialog);
204
205
206 /**
207 * Events specific to the link dialog.
208 * @enum {string}
209 */
210 goog.ui.editor.LinkDialog.EventType = {
211 BEFORE_TEST_LINK: 'beforetestlink'
212 };
213
214
215
216 /**
217 * OK event object for the link dialog.
218 * @param {string} linkText Text the user chose to display for the link.
219 * @param {string} linkUrl Url the user chose for the link to point to.
220 * @param {boolean} openInNewWindow Whether the link should open in a new window
221 * when clicked.
222 * @param {boolean} noFollow Whether the link should have 'rel=nofollow'
223 * attribute.
224 * @constructor
225 * @extends {goog.events.Event}
226 * @final
227 */
228 goog.ui.editor.LinkDialog.OkEvent = function(
229 linkText, linkUrl, openInNewWindow, noFollow) {
230 goog.ui.editor.LinkDialog.OkEvent.base(
231 this, 'constructor', goog.ui.editor.AbstractDialog.EventType.OK);
232
233 /**
234 * The text of the link edited in the dialog.
235 * @type {string}
236 */
237 this.linkText = linkText;
238
239 /**
240 * The url of the link edited in the dialog.
241 * @type {string}
242 */
243 this.linkUrl = linkUrl;
244
245 /**
246 * Whether the link should open in a new window when clicked.
247 * @type {boolean}
248 */
249 this.openInNewWindow = openInNewWindow;
250
251 /**
252 * Whether the link should have 'rel=nofollow' attribute.
253 * @type {boolean}
254 */
255 this.noFollow = noFollow;
256 };
257 goog.inherits(goog.ui.editor.LinkDialog.OkEvent, goog.events.Event);
258
259
260
261 /**
262 * Event fired before testing a link by opening it in another window.
263 * Calling preventDefault will stop the link from being opened.
264 * @param {string} url Url of the link being tested.
265 * @constructor
266 * @extends {goog.events.Event}
267 * @final
268 */
269 goog.ui.editor.LinkDialog.BeforeTestLinkEvent = function(url) {
270 goog.ui.editor.LinkDialog.BeforeTestLinkEvent.base(
271 this, 'constructor',
272 goog.ui.editor.LinkDialog.EventType.BEFORE_TEST_LINK);
273
274 /**
275 * The url of the link being tested.
276 * @type {string}
277 */
278 this.url = url;
279 };
280 goog.inherits(goog.ui.editor.LinkDialog.BeforeTestLinkEvent, goog.events.Event);
281
282
283 /**
284 * Sets the warning message to show to users about including email addresses on
285 * public web pages.
286 * @param {!goog.html.SafeHtml} emailWarning Warning message to show users about
287 * including email addresses on the web.
288 */
289 goog.ui.editor.LinkDialog.prototype.setEmailWarning = function(emailWarning) {
290 this.emailWarning_ = emailWarning;
291 };
292
293
294 /**
295 * Tells the dialog to show a checkbox where the user can choose to have the
296 * link open in a new window.
297 * @param {boolean} startChecked Whether to check the checkbox the first
298 * time the dialog is shown. Subesquent times the checkbox will remember its
299 * previous state.
300 */
301 goog.ui.editor.LinkDialog.prototype.showOpenLinkInNewWindow = function(
302 startChecked) {
303 this.showOpenLinkInNewWindow_ = true;
304 this.isOpenLinkInNewWindowChecked_ = startChecked;
305 };
306
307
308 /**
309 * Tells the dialog to show a checkbox where the user can choose to add
310 * 'rel=nofollow' attribute to the link.
311 */
312 goog.ui.editor.LinkDialog.prototype.showRelNoFollow = function() {
313 this.showRelNoFollow_ = true;
314 };
315
316
317 /** @override */
318 goog.ui.editor.LinkDialog.prototype.show = function() {
319 goog.ui.editor.LinkDialog.base(this, 'show');
320
321
322 this.selectAppropriateTab_(
323 this.textToDisplayInput_.value, this.getTargetUrl_());
324 this.syncOkButton_();
325
326 if (this.showOpenLinkInNewWindow_) {
327 if (!this.targetLink_.isNew()) {
328 // If link is not new, checkbox should reflect current target.
329 this.isOpenLinkInNewWindowChecked_ =
330 this.targetLink_.getAnchor().target == '_blank';
331 }
332 this.openInNewWindowCheckbox_.checked = this.isOpenLinkInNewWindowChecked_;
333 }
334
335 if (this.showRelNoFollow_) {
336 this.relNoFollowCheckbox_.checked =
337 goog.ui.editor.LinkDialog.hasNoFollow(this.targetLink_.getAnchor().rel);
338 }
339 };
340
341
342 /** @override */
343 goog.ui.editor.LinkDialog.prototype.hide = function() {
344 this.disableAutogenFlag_(false);
345 goog.ui.editor.LinkDialog.base(this, 'hide');
346 };
347
348
349 /**
350 * Tells the dialog whether to show the 'text to display' div.
351 * When the target element of the dialog is an image, there is no link text
352 * to modify. This function can be used for this kind of situations.
353 * @param {boolean} visible Whether to make 'text to display' div visible.
354 */
355 goog.ui.editor.LinkDialog.prototype.setTextToDisplayVisible = function(
356 visible) {
357 if (this.textToDisplayDiv_) {
358 goog.style.setStyle(
359 this.textToDisplayDiv_, 'display', visible ? 'block' : 'none');
360 }
361 };
362
363
364 /**
365 * Tells the plugin whether to stop leaking the page's url via the referrer
366 * header when the "test this link" link is clicked.
367 * @param {boolean} stop Whether to stop leaking the referrer.
368 */
369 goog.ui.editor.LinkDialog.prototype.setStopReferrerLeaks = function(stop) {
370 this.stopReferrerLeaks_ = stop;
371 };
372
373
374 /**
375 * Tells the dialog whether the autogeneration of text to display is to be
376 * enabled.
377 * @param {boolean} enable Whether to enable the feature.
378 */
379 goog.ui.editor.LinkDialog.prototype.setAutogenFeatureEnabled = function(
380 enable) {
381 this.autogenFeatureEnabled_ = enable;
382 };
383
384
385 /**
386 * Checks if {@code str} contains {@code "nofollow"} as a separate word.
387 * @param {string} str String to be tested. This is usually {@code rel}
388 * attribute of an {@code HTMLAnchorElement} object.
389 * @return {boolean} {@code true} if {@code str} contains {@code nofollow}.
390 */
391 goog.ui.editor.LinkDialog.hasNoFollow = function(str) {
392 return goog.ui.editor.LinkDialog.NO_FOLLOW_REGEX_.test(str);
393 };
394
395
396 /**
397 * Removes {@code "nofollow"} from {@code rel} if it's present as a separate
398 * word.
399 * @param {string} rel Input string. This is usually {@code rel} attribute of
400 * an {@code HTMLAnchorElement} object.
401 * @return {string} {@code rel} with any {@code "nofollow"} removed.
402 */
403 goog.ui.editor.LinkDialog.removeNoFollow = function(rel) {
404 return rel.replace(goog.ui.editor.LinkDialog.NO_FOLLOW_REGEX_, '');
405 };
406
407
408 // *** Protected interface ************************************************** //
409
410
411 /** @override */
412 goog.ui.editor.LinkDialog.prototype.createDialogControl = function() {
413 var builder = new goog.ui.editor.AbstractDialog.Builder(this);
414 builder.setTitle(goog.ui.editor.messages.MSG_EDIT_LINK)
415 .setContent(this.createDialogContent_());
416 return builder.build();
417 };
418
419
420 /**
421 * Creates and returns the event object to be used when dispatching the OK
422 * event to listeners based on which tab is currently selected and the contents
423 * of the input fields of that tab.
424 * @return {!goog.ui.editor.LinkDialog.OkEvent} The event object to be used when
425 * dispatching the OK event to listeners.
426 * @protected
427 * @override
428 */
429 goog.ui.editor.LinkDialog.prototype.createOkEvent = function() {
430 if (this.tabPane_.getCurrentTabId() ==
431 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB) {
432 return this.createOkEventFromEmailTab_();
433 } else {
434 return this.createOkEventFromWebTab_();
435 }
436 };
437
438
439 // *** Private implementation *********************************************** //
440
441
442 /**
443 * Regular expression that matches {@code nofollow} value in an
444 * {@code * HTMLAnchorElement}'s {@code rel} element.
445 * @type {RegExp}
446 * @private
447 */
448 goog.ui.editor.LinkDialog.NO_FOLLOW_REGEX_ = /\bnofollow\b/i;
449
450
451 /**
452 * Creates contents of this dialog.
453 * @return {!Element} Contents of the dialog as a DOM element.
454 * @private
455 */
456 goog.ui.editor.LinkDialog.prototype.createDialogContent_ = function() {
457 this.textToDisplayDiv_ =
458 /** @type {!HTMLDivElement} */ (this.buildTextToDisplayDiv_());
459 var content =
460 this.dom.createDom(goog.dom.TagName.DIV, null, this.textToDisplayDiv_);
461
462 this.tabPane_ =
463 new goog.ui.editor.TabPane(this.dom, goog.ui.editor.messages.MSG_LINK_TO);
464 this.registerDisposable(this.tabPane_);
465 this.tabPane_.addTab(
466 goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB,
467 goog.ui.editor.messages.MSG_ON_THE_WEB,
468 goog.ui.editor.messages.MSG_ON_THE_WEB_TIP,
469 goog.ui.editor.LinkDialog.BUTTON_GROUP_, this.buildTabOnTheWeb_());
470 this.tabPane_.addTab(
471 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB,
472 goog.ui.editor.messages.MSG_EMAIL_ADDRESS,
473 goog.ui.editor.messages.MSG_EMAIL_ADDRESS_TIP,
474 goog.ui.editor.LinkDialog.BUTTON_GROUP_, this.buildTabEmailAddress_());
475 this.tabPane_.render(content);
476
477 this.eventHandler_.listen(
478 this.tabPane_, goog.ui.Component.EventType.SELECT, this.onChangeTab_);
479
480 if (this.showOpenLinkInNewWindow_) {
481 content.appendChild(this.buildOpenInNewWindowDiv_());
482 }
483 if (this.showRelNoFollow_) {
484 content.appendChild(this.buildRelNoFollowDiv_());
485 }
486
487 return content;
488 };
489
490
491 /**
492 * Builds and returns the text to display section of the edit link dialog.
493 * @return {!Element} A div element to be appended into the dialog div.
494 * @private
495 */
496 goog.ui.editor.LinkDialog.prototype.buildTextToDisplayDiv_ = function() {
497 var table = this.dom.createTable(1, 2);
498 table.cellSpacing = '0';
499 table.cellPadding = '0';
500 table.style.fontSize = '10pt';
501 // Build the text to display input.
502 var textToDisplayDiv = this.dom.createDom(goog.dom.TagName.DIV);
503 var html = goog.html.SafeHtml.create(
504 'span', {
505 'style': {
506 'position': 'relative',
507 'bottom': '2px',
508 'padding-right': '1px',
509 'white-space': 'nowrap'
510 },
511 id: goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY_LABEL
512 },
513 [goog.ui.editor.messages.MSG_TEXT_TO_DISPLAY, goog.string.Unicode.NBSP]);
514 goog.dom.safe.setInnerHtml(table.rows[0].cells[0], html);
515 this.textToDisplayInput_ = this.dom.createDom(
516 goog.dom.TagName.INPUT,
517 {id: goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY});
518 var textInput = this.textToDisplayInput_;
519 // 98% prevents scroll bars in standards mode.
520 // TODO(robbyw): Is this necessary for quirks mode?
521 goog.style.setStyle(textInput, 'width', '98%');
522 goog.style.setStyle(table.rows[0].cells[1], 'width', '100%');
523 goog.dom.appendChild(table.rows[0].cells[1], textInput);
524
525 goog.a11y.aria.setState(
526 /** @type {!Element} */ (textInput), goog.a11y.aria.State.LABELLEDBY,
527 goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY_LABEL);
528 textInput.value = this.targetLink_.getCurrentText();
529
530 this.textInputHandler_ = new goog.events.InputHandler(textInput);
531 this.registerDisposable(this.textInputHandler_);
532 this.eventHandler_.listen(
533 this.textInputHandler_, goog.events.InputHandler.EventType.INPUT,
534 this.onTextToDisplayEdit_);
535
536 goog.dom.appendChild(textToDisplayDiv, table);
537 return textToDisplayDiv;
538 };
539
540
541 /**
542 * Builds and returns the "checkbox to open the link in a new window" section of
543 * the edit link dialog.
544 * @return {!Element} A div element to be appended into the dialog div.
545 * @private
546 */
547 goog.ui.editor.LinkDialog.prototype.buildOpenInNewWindowDiv_ = function() {
548 this.openInNewWindowCheckbox_ = this.dom.createDom(
549 goog.dom.TagName.INPUT, {'type': goog.dom.InputType.CHECKBOX});
550 return this.dom.createDom(
551 goog.dom.TagName.DIV, null,
552 this.dom.createDom(
553 goog.dom.TagName.LABEL, null, this.openInNewWindowCheckbox_,
554 goog.ui.editor.messages.MSG_OPEN_IN_NEW_WINDOW));
555 };
556
557
558 /**
559 * Creates a DIV with a checkbox for {@code rel=nofollow} option.
560 * @return {!Element} Newly created DIV element.
561 * @private
562 */
563 goog.ui.editor.LinkDialog.prototype.buildRelNoFollowDiv_ = function() {
564 var formatter = new goog.html.SafeHtmlFormatter();
565 /** @desc Checkbox text for adding 'rel=nofollow' attribute to a link. */
566 var MSG_ADD_REL_NOFOLLOW_ATTR = goog.getMsg(
567 "Add '{$relNoFollow}' attribute ({$linkStart}Learn more{$linkEnd})", {
568 'relNoFollow': 'rel=nofollow',
569 'linkStart': formatter.startTag('a', {
570 'href': 'http://support.google.com/webmasters/bin/' +
571 'answer.py?hl=en&answer=96569',
572 'target': '_blank'
573 }),
574 'linkEnd': formatter.endTag('a')
575 });
576
577 this.relNoFollowCheckbox_ = this.dom.createDom(
578 goog.dom.TagName.INPUT, {'type': goog.dom.InputType.CHECKBOX});
579 return this.dom.createDom(
580 goog.dom.TagName.DIV, null,
581 this.dom.createDom(
582 goog.dom.TagName.LABEL, null, this.relNoFollowCheckbox_,
583 goog.dom.safeHtmlToNode(
584 formatter.format(MSG_ADD_REL_NOFOLLOW_ATTR))));
585 };
586
587
588 /**
589 * Builds and returns the div containing the tab "On the web".
590 * @return {!Element} The div element containing the tab.
591 * @private
592 */
593 goog.ui.editor.LinkDialog.prototype.buildTabOnTheWeb_ = function() {
594 var onTheWebDiv = this.dom.createElement(goog.dom.TagName.DIV);
595
596 var headingDiv = this.dom.createDom(
597 goog.dom.TagName.DIV, {},
598 this.dom.createDom(
599 goog.dom.TagName.B, {}, goog.ui.editor.messages.MSG_WHAT_URL));
600 var urlInput = this.dom.createDom(goog.dom.TagName.INPUT, {
601 id: goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT,
602 className: goog.ui.editor.LinkDialog.TARGET_INPUT_CLASSNAME_
603 });
604 goog.a11y.aria.setState(
605 urlInput, goog.a11y.aria.State.LABELLEDBY,
606 goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
607 // IE throws on unknown values for type, but IE10+ supports type=url
608 if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10')) {
609 // On browsers that support Web Forms 2.0, allow autocompletion of URLs.
610 urlInput.type = goog.dom.InputType.URL;
611 }
612
613 if (goog.editor.BrowserFeature.NEEDS_99_WIDTH_IN_STANDARDS_MODE &&
614 goog.editor.node.isStandardsMode(urlInput)) {
615 urlInput.style.width = '99%';
616 }
617
618 var inputDiv = this.dom.createDom(goog.dom.TagName.DIV, null, urlInput);
619
620 this.urlInputHandler_ = new goog.events.InputHandler(urlInput);
621 this.registerDisposable(this.urlInputHandler_);
622 this.eventHandler_.listen(
623 this.urlInputHandler_, goog.events.InputHandler.EventType.INPUT,
624 this.onUrlOrEmailInputChange_);
625
626 var testLink = new goog.ui.Button(
627 goog.ui.editor.messages.MSG_TEST_THIS_LINK,
628 goog.ui.LinkButtonRenderer.getInstance(), this.dom);
629 testLink.render(inputDiv);
630 testLink.getElement().style.marginTop = '1em';
631 this.eventHandler_.listen(
632 testLink, goog.ui.Component.EventType.ACTION, this.onWebTestLink_);
633
634 // Build the "On the web" explanation text div.
635 var explanationDiv = this.dom.createDom(
636 goog.dom.TagName.DIV,
637 goog.ui.editor.LinkDialog.EXPLANATION_TEXT_CLASSNAME_);
638 goog.dom.safe.setInnerHtml(
639 explanationDiv, goog.ui.editor.messages.getTrLinkExplanationSafeHtml());
640 onTheWebDiv.appendChild(headingDiv);
641 onTheWebDiv.appendChild(inputDiv);
642 onTheWebDiv.appendChild(explanationDiv);
643
644 return onTheWebDiv;
645 };
646
647
648 /**
649 * Builds and returns the div containing the tab "Email address".
650 * @return {!Element} the div element containing the tab.
651 * @private
652 */
653 goog.ui.editor.LinkDialog.prototype.buildTabEmailAddress_ = function() {
654 var emailTab = this.dom.createDom(goog.dom.TagName.DIV);
655
656 var headingDiv = this.dom.createDom(
657 goog.dom.TagName.DIV, {},
658 this.dom.createDom(
659 goog.dom.TagName.B, {}, goog.ui.editor.messages.MSG_WHAT_EMAIL));
660 goog.dom.appendChild(emailTab, headingDiv);
661 var emailInput = this.dom.createDom(goog.dom.TagName.INPUT, {
662 id: goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT,
663 className: goog.ui.editor.LinkDialog.TARGET_INPUT_CLASSNAME_
664 });
665 goog.a11y.aria.setState(
666 emailInput, goog.a11y.aria.State.LABELLEDBY,
667 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB);
668
669 if (goog.editor.BrowserFeature.NEEDS_99_WIDTH_IN_STANDARDS_MODE &&
670 goog.editor.node.isStandardsMode(emailInput)) {
671 // Standards mode sizes this too large.
672 emailInput.style.width = '99%';
673 }
674
675 goog.dom.appendChild(emailTab, emailInput);
676
677 this.emailInputHandler_ = new goog.events.InputHandler(emailInput);
678 this.registerDisposable(this.emailInputHandler_);
679 this.eventHandler_.listen(
680 this.emailInputHandler_, goog.events.InputHandler.EventType.INPUT,
681 this.onUrlOrEmailInputChange_);
682
683 goog.dom.appendChild(
684 emailTab,
685 this.dom.createDom(
686 goog.dom.TagName.DIV, {
687 id: goog.ui.editor.LinkDialog.Id_.EMAIL_WARNING,
688 className: goog.ui.editor.LinkDialog.EMAIL_WARNING_CLASSNAME_,
689 style: 'visibility:hidden'
690 },
691 goog.ui.editor.messages.MSG_INVALID_EMAIL));
692
693 if (this.emailWarning_) {
694 var explanationDiv = this.dom.createDom(
695 goog.dom.TagName.DIV,
696 goog.ui.editor.LinkDialog.EXPLANATION_TEXT_CLASSNAME_);
697 goog.dom.safe.setInnerHtml(explanationDiv, this.emailWarning_);
698 goog.dom.appendChild(emailTab, explanationDiv);
699 }
700 return emailTab;
701 };
702
703
704 /**
705 * Returns the url that the target points to.
706 * @return {string} The url that the target points to.
707 * @private
708 */
709 goog.ui.editor.LinkDialog.prototype.getTargetUrl_ = function() {
710 // Get the href-attribute through getAttribute() rather than the href property
711 // because Google-Toolbar on Firefox with "Send with Gmail" turned on
712 // modifies the href-property of 'mailto:' links but leaves the attribute
713 // untouched.
714 return this.targetLink_.getAnchor().getAttribute('href') || '';
715 };
716
717
718 /**
719 * Selects the correct tab based on the URL, and fills in its inputs.
720 * For new links, it suggests a url based on the link text.
721 * @param {string} text The inner text of the link.
722 * @param {string} url The href for the link.
723 * @private
724 */
725 goog.ui.editor.LinkDialog.prototype.selectAppropriateTab_ = function(
726 text, url) {
727 if (this.isNewLink_()) {
728 // Newly created non-empty link: try to infer URL from the link text.
729 this.guessUrlAndSelectTab_(text);
730 } else if (goog.editor.Link.isMailto(url)) {
731 // The link is for an email.
732 this.tabPane_.setSelectedTabId(
733 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB);
734 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT)
735 .value = url.substring(url.indexOf(':') + 1);
736 this.setAutogenFlagFromCurInput_();
737 } else {
738 // No specific tab was appropriate, default to on the web tab.
739 this.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
740 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value =
741 this.isNewLink_() ? 'http://' : url;
742 this.setAutogenFlagFromCurInput_();
743 }
744 };
745
746
747 /**
748 * Select a url/tab based on the link's text. This function is simply
749 * the isNewLink_() == true case of selectAppropriateTab_().
750 * @param {string} text The inner text of the link.
751 * @private
752 */
753 goog.ui.editor.LinkDialog.prototype.guessUrlAndSelectTab_ = function(text) {
754 if (goog.editor.Link.isLikelyEmailAddress(text)) {
755 // The text is for an email address.
756 this.tabPane_.setSelectedTabId(
757 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB);
758 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT)
759 .value = text;
760 this.setAutogenFlag_(true);
761 // TODO(user): Why disable right after enabling? What bug are we
762 // working around?
763 this.disableAutogenFlag_(true);
764 } else if (goog.editor.Link.isLikelyUrl(text)) {
765 // The text is for a web URL.
766 this.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
767 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value =
768 text;
769 this.setAutogenFlag_(true);
770 this.disableAutogenFlag_(true);
771 } else {
772 // No meaning could be deduced from text, choose a default tab.
773 if (!this.targetLink_.getCurrentText()) {
774 this.setAutogenFlag_(true);
775 }
776 this.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
777 }
778 };
779
780
781 /**
782 * Called on a change to the url or email input. If either one of those tabs
783 * is active, sets the OK button to enabled/disabled accordingly.
784 * @private
785 */
786 goog.ui.editor.LinkDialog.prototype.syncOkButton_ = function() {
787 var inputValue;
788 if (this.tabPane_.getCurrentTabId() ==
789 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB) {
790 inputValue =
791 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT)
792 .value;
793 this.toggleInvalidEmailWarning_(
794 inputValue != '' && !goog.editor.Link.isLikelyEmailAddress(inputValue));
795 } else if (
796 this.tabPane_.getCurrentTabId() ==
797 goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB) {
798 inputValue =
799 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value;
800 } else {
801 return;
802 }
803 this.getOkButtonElement().disabled =
804 goog.string.isEmptyOrWhitespace(inputValue);
805 };
806
807
808 /**
809 * Show/hide the Invalid Email Address warning.
810 * @param {boolean} on Whether to show the warning.
811 * @private
812 */
813 goog.ui.editor.LinkDialog.prototype.toggleInvalidEmailWarning_ = function(on) {
814 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_WARNING)
815 .style.visibility = (on ? 'visible' : 'hidden');
816 };
817
818
819 /**
820 * Changes the autogenerateTextToDisplay flag so that text to
821 * display stops autogenerating.
822 * @private
823 */
824 goog.ui.editor.LinkDialog.prototype.onTextToDisplayEdit_ = function() {
825 var inputEmpty = this.textToDisplayInput_.value == '';
826 if (inputEmpty) {
827 this.setAutogenFlag_(true);
828 } else {
829 this.setAutogenFlagFromCurInput_();
830 }
831 };
832
833
834 /**
835 * The function called when hitting OK with the "On the web" tab current.
836 * @return {!goog.ui.editor.LinkDialog.OkEvent} The event object to be used when
837 * dispatching the OK event to listeners.
838 * @private
839 */
840 goog.ui.editor.LinkDialog.prototype.createOkEventFromWebTab_ = function() {
841 var input = /** @type {HTMLInputElement} */ (
842 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT));
843 var linkURL = input.value;
844 if (goog.editor.Link.isLikelyEmailAddress(linkURL)) {
845 // Make sure that if user types in an e-mail address, it becomes "mailto:".
846 return this.createOkEventFromEmailTab_(
847 goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT);
848 } else {
849 if (linkURL.search(/:/) < 0) {
850 linkURL = 'http://' + goog.string.trimLeft(linkURL);
851 }
852 return this.createOkEventFromUrl_(linkURL);
853 }
854 };
855
856
857 /**
858 * The function called when hitting OK with the "email address" tab current.
859 * @param {string=} opt_inputId Id of an alternate input to check.
860 * @return {!goog.ui.editor.LinkDialog.OkEvent} The event object to be used when
861 * dispatching the OK event to listeners.
862 * @private
863 */
864 goog.ui.editor.LinkDialog.prototype.createOkEventFromEmailTab_ = function(
865 opt_inputId) {
866 var linkURL =
867 this.dom
868 .getElement(
869 opt_inputId || goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT)
870 .value;
871 linkURL = 'mailto:' + linkURL;
872 return this.createOkEventFromUrl_(linkURL);
873 };
874
875
876 /**
877 * Function to test a link from the on the web tab.
878 * @private
879 */
880 goog.ui.editor.LinkDialog.prototype.onWebTestLink_ = function() {
881 var input = /** @type {HTMLInputElement} */ (
882 this.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT));
883 var url = input.value;
884 if (url.search(/:/) < 0) {
885 url = 'http://' + goog.string.trimLeft(url);
886 }
887 if (this.dispatchEvent(
888 new goog.ui.editor.LinkDialog.BeforeTestLinkEvent(url))) {
889 var win = this.dom.getWindow();
890 var size = goog.dom.getViewportSize(win);
891 var openOptions = {
892 target: '_blank',
893 width: Math.max(size.width - 50, 50),
894 height: Math.max(size.height - 50, 50),
895 toolbar: true,
896 scrollbars: true,
897 location: true,
898 statusbar: false,
899 menubar: true, 'resizable': true, 'noreferrer': this.stopReferrerLeaks_
900 };
901 goog.window.open(url, openOptions, win);
902 }
903 };
904
905
906 /**
907 * Called whenever the url or email input is edited. If the text to display
908 * matches the text to display, turn on auto. Otherwise if auto is on, update
909 * the text to display based on the url.
910 * @private
911 */
912 goog.ui.editor.LinkDialog.prototype.onUrlOrEmailInputChange_ = function() {
913 if (this.autogenerateTextToDisplay_) {
914 this.setTextToDisplayFromAuto_();
915 } else if (this.textToDisplayInput_.value == '') {
916 this.setAutogenFlagFromCurInput_();
917 }
918 this.syncOkButton_();
919 };
920
921
922 /**
923 * Called when the currently selected tab changes.
924 * @param {goog.events.Event} e The tab change event.
925 * @private
926 */
927 goog.ui.editor.LinkDialog.prototype.onChangeTab_ = function(e) {
928 var tab = /** @type {goog.ui.Tab} */ (e.target);
929
930 // Focus on the input field in the selected tab.
931 var input = /** @type {!HTMLElement} */ (
932 this.dom.getElement(
933 tab.getId() + goog.ui.editor.LinkDialog.Id_.TAB_INPUT_SUFFIX));
934 goog.editor.focus.focusInputField(input);
935
936 // For some reason, IE does not fire onpropertychange events when the width
937 // is specified as a percentage, which breaks the InputHandlers.
938 input.style.width = '';
939 input.style.width = input.offsetWidth + 'px';
940
941 this.syncOkButton_();
942 this.setTextToDisplayFromAuto_();
943 };
944
945
946 /**
947 * If autogen is turned on, set the value of text to display based on the
948 * current selection or url.
949 * @private
950 */
951 goog.ui.editor.LinkDialog.prototype.setTextToDisplayFromAuto_ = function() {
952 if (this.autogenFeatureEnabled_ && this.autogenerateTextToDisplay_) {
953 var inputId = this.tabPane_.getCurrentTabId() +
954 goog.ui.editor.LinkDialog.Id_.TAB_INPUT_SUFFIX;
955 this.textToDisplayInput_.value =
956 /** @type {HTMLInputElement} */ (this.dom.getElement(inputId)).value;
957 }
958 };
959
960
961 /**
962 * Turn on the autogenerate text to display flag, and set some sort of indicator
963 * that autogen is on.
964 * @param {boolean} val Boolean value to set autogenerate to.
965 * @private
966 */
967 goog.ui.editor.LinkDialog.prototype.setAutogenFlag_ = function(val) {
968 // TODO(user): This whole autogen thing is very confusing. It needs
969 // to be refactored and/or explained.
970 this.autogenerateTextToDisplay_ = val;
971 };
972
973
974 /**
975 * Disables autogen so that onUrlOrEmailInputChange_ doesn't act in cases
976 * that are undesirable.
977 * @param {boolean} autogen Boolean value to set disableAutogen to.
978 * @private
979 */
980 goog.ui.editor.LinkDialog.prototype.disableAutogenFlag_ = function(autogen) {
981 this.setAutogenFlag_(!autogen);
982 this.disableAutogen_ = autogen;
983 };
984
985
986 /**
987 * Creates an OK event from the text to display input and the specified link.
988 * If text to display input is empty, then generate the auto value for it.
989 * @return {!goog.ui.editor.LinkDialog.OkEvent} The event object to be used when
990 * dispatching the OK event to listeners.
991 * @param {string} url Url the target element should point to.
992 * @private
993 */
994 goog.ui.editor.LinkDialog.prototype.createOkEventFromUrl_ = function(url) {
995 // Fill in the text to display input in case it is empty.
996 this.setTextToDisplayFromAuto_();
997 if (this.showOpenLinkInNewWindow_) {
998 // Save checkbox state for next time.
999 this.isOpenLinkInNewWindowChecked_ = this.openInNewWindowCheckbox_.checked;
1000 }
1001 return new goog.ui.editor.LinkDialog.OkEvent(
1002 this.textToDisplayInput_.value, url,
1003 this.showOpenLinkInNewWindow_ && this.isOpenLinkInNewWindowChecked_,
1004 this.showRelNoFollow_ && this.relNoFollowCheckbox_.checked);
1005 };
1006
1007
1008 /**
1009 * If an email or url is being edited, set autogenerate to on if the text to
1010 * display matches the url.
1011 * @private
1012 */
1013 goog.ui.editor.LinkDialog.prototype.setAutogenFlagFromCurInput_ = function() {
1014 var autogen = false;
1015 if (!this.disableAutogen_) {
1016 var tabInput = this.dom.getElement(
1017 this.tabPane_.getCurrentTabId() +
1018 goog.ui.editor.LinkDialog.Id_.TAB_INPUT_SUFFIX);
1019 autogen = (tabInput.value == this.textToDisplayInput_.value);
1020 }
1021 this.setAutogenFlag_(autogen);
1022 };
1023
1024
1025 /**
1026 * @return {boolean} Whether the link is new.
1027 * @private
1028 */
1029 goog.ui.editor.LinkDialog.prototype.isNewLink_ = function() {
1030 return this.targetLink_.isNew();
1031 };
1032
1033
1034 /**
1035 * IDs for relevant DOM elements.
1036 * @enum {string}
1037 * @private
1038 */
1039 goog.ui.editor.LinkDialog.Id_ = {
1040 TEXT_TO_DISPLAY: 'linkdialog-text',
1041 TEXT_TO_DISPLAY_LABEL: 'linkdialog-text-label',
1042 ON_WEB_TAB: 'linkdialog-onweb',
1043 ON_WEB_INPUT: 'linkdialog-onweb-tab-input',
1044 EMAIL_ADDRESS_TAB: 'linkdialog-email',
1045 EMAIL_ADDRESS_INPUT: 'linkdialog-email-tab-input',
1046 EMAIL_WARNING: 'linkdialog-email-warning',
1047 TAB_INPUT_SUFFIX: '-tab-input'
1048 };
1049
1050
1051 /**
1052 * Base name for the radio buttons group.
1053 * @type {string}
1054 * @private
1055 */
1056 goog.ui.editor.LinkDialog.BUTTON_GROUP_ = 'linkdialog-buttons';
1057
1058
1059 /**
1060 * Class name for the url and email input elements.
1061 * @type {string}
1062 * @private
1063 */
1064 goog.ui.editor.LinkDialog.TARGET_INPUT_CLASSNAME_ =
1065 goog.getCssName('tr-link-dialog-target-input');
1066
1067
1068 /**
1069 * Class name for the email address warning element.
1070 * @type {string}
1071 * @private
1072 */
1073 goog.ui.editor.LinkDialog.EMAIL_WARNING_CLASSNAME_ =
1074 goog.getCssName('tr-link-dialog-email-warning');
1075
1076
1077 /**
1078 * Class name for the explanation text elements.
1079 * @type {string}
1080 * @private
1081 */
1082 goog.ui.editor.LinkDialog.EXPLANATION_TEXT_CLASSNAME_ =
1083 goog.getCssName('tr-link-dialog-explanation-text');