Debian Codegraph @

closure-library/closure/goog/ui/dialog.js

raw
1 // Copyright 2006 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 Class for showing simple modal dialog boxes.
17 *
18 * TODO(user):
19 * * Standardize CSS class names with other components
20 * * Add functionality to "host" other components in content area
21 * * Abstract out ButtonSet and make it more general
22 * @see ../demos/dialog.html
23 */
24
25 goog.provide('goog.ui.Dialog');
26 goog.provide('goog.ui.Dialog.ButtonSet');
27 goog.provide('goog.ui.Dialog.ButtonSet.DefaultButtons');
28 goog.provide('goog.ui.Dialog.DefaultButtonCaptions');
29 goog.provide('goog.ui.Dialog.DefaultButtonKeys');
30 goog.provide('goog.ui.Dialog.Event');
31 goog.provide('goog.ui.Dialog.EventType');
32
33 goog.require('goog.a11y.aria');
34 goog.require('goog.a11y.aria.Role');
35 goog.require('goog.a11y.aria.State');
36 goog.require('goog.asserts');
37 goog.require('goog.dom');
38 goog.require('goog.dom.NodeType');
39 goog.require('goog.dom.TagName');
40 goog.require('goog.dom.classlist');
41 goog.require('goog.dom.safe');
42 goog.require('goog.events');
43 goog.require('goog.events.Event');
44 goog.require('goog.events.EventType');
45 goog.require('goog.events.KeyCodes');
46 goog.require('goog.fx.Dragger');
47 goog.require('goog.html.SafeHtml');
48 goog.require('goog.math.Rect');
49 goog.require('goog.string');
50 goog.require('goog.structs.Map');
51 goog.require('goog.style');
52 goog.require('goog.ui.ModalPopup');
53
54
55
56 /**
57 * Class for showing simple dialog boxes.
58 * The Html structure of the dialog box is:
59 * <pre>
60 * Element Function Class-name, modal-dialog = default
61 * ----------------------------------------------------------------------------
62 * - iframe Iframe mask modal-dialog-bg
63 * - div Background mask modal-dialog-bg
64 * - div Dialog area modal-dialog
65 * - div Title bar modal-dialog-title
66 * - span modal-dialog-title-text
67 * - text Title text N/A
68 * - span modal-dialog-title-close
69 * - ?? Close box N/A
70 * - div Content area modal-dialog-content
71 * - ?? User specified content N/A
72 * - div Button area modal-dialog-buttons
73 * - button N/A
74 * - button
75 * - ...
76 * </pre>
77 * @constructor
78 * @param {string=} opt_class CSS class name for the dialog element, also used
79 * as a class name prefix for related elements; defaults to modal-dialog.
80 * This should be a single, valid CSS class name.
81 * @param {boolean=} opt_useIframeMask Work around windowed controls z-index
82 * issue by using an iframe instead of a div for bg element.
83 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link
84 * goog.ui.Component} for semantics.
85 * @extends {goog.ui.ModalPopup}
86 */
87 goog.ui.Dialog = function(opt_class, opt_useIframeMask, opt_domHelper) {
88 goog.ui.Dialog.base(this, 'constructor', opt_useIframeMask, opt_domHelper);
89
90 /**
91 * CSS class name for the dialog element, also used as a class name prefix for
92 * related elements. Defaults to goog.getCssName('modal-dialog').
93 * @type {string}
94 * @private
95 */
96 this.class_ = opt_class || goog.getCssName('modal-dialog');
97
98 this.buttons_ = goog.ui.Dialog.ButtonSet.createOkCancel();
99 };
100 goog.inherits(goog.ui.Dialog, goog.ui.ModalPopup);
101 goog.tagUnsealableClass(goog.ui.Dialog);
102
103
104 /**
105 * Button set. Default to Ok/Cancel.
106 * @type {goog.ui.Dialog.ButtonSet}
107 * @private
108 */
109 goog.ui.Dialog.prototype.buttons_;
110
111
112 /**
113 * Whether the escape key closes this dialog.
114 * @type {boolean}
115 * @private
116 */
117 goog.ui.Dialog.prototype.escapeToCancel_ = true;
118
119
120 /**
121 * Whether this dialog should include a title close button.
122 * @type {boolean}
123 * @private
124 */
125 goog.ui.Dialog.prototype.hasTitleCloseButton_ = true;
126
127
128 /**
129 * Whether the dialog is modal. Defaults to true.
130 * @type {boolean}
131 * @private
132 */
133 goog.ui.Dialog.prototype.modal_ = true;
134
135
136 /**
137 * Whether the dialog is draggable. Defaults to true.
138 * @type {boolean}
139 * @private
140 */
141 goog.ui.Dialog.prototype.draggable_ = true;
142
143
144 /**
145 * Opacity for background mask. Defaults to 50%.
146 * @type {number}
147 * @private
148 */
149 goog.ui.Dialog.prototype.backgroundElementOpacity_ = 0.50;
150
151
152 /**
153 * Dialog's title.
154 * @type {string}
155 * @private
156 */
157 goog.ui.Dialog.prototype.title_ = '';
158
159
160 /**
161 * Dialog's content (HTML).
162 * @type {goog.html.SafeHtml}
163 * @private
164 */
165 goog.ui.Dialog.prototype.content_ = null;
166
167
168 /**
169 * Dragger.
170 * @type {goog.fx.Dragger}
171 * @private
172 */
173 goog.ui.Dialog.prototype.dragger_ = null;
174
175
176 /**
177 * Whether the dialog should be disposed when it is hidden.
178 * @type {boolean}
179 * @private
180 */
181 goog.ui.Dialog.prototype.disposeOnHide_ = false;
182
183
184 /**
185 * Element for the title bar.
186 * @type {Element}
187 * @private
188 */
189 goog.ui.Dialog.prototype.titleEl_ = null;
190
191
192 /**
193 * Element for the text area of the title bar.
194 * @type {Element}
195 * @private
196 */
197 goog.ui.Dialog.prototype.titleTextEl_ = null;
198
199
200 /**
201 * Id of element for the text area of the title bar.
202 * @type {?string}
203 * @private
204 */
205 goog.ui.Dialog.prototype.titleTextId_ = null;
206
207
208 /**
209 * Element for the close box area of the title bar.
210 * @type {Element}
211 * @private
212 */
213 goog.ui.Dialog.prototype.titleCloseEl_ = null;
214
215
216 /**
217 * Element for the content area.
218 * @type {Element}
219 * @private
220 */
221 goog.ui.Dialog.prototype.contentEl_ = null;
222
223
224 /**
225 * Element for the button bar.
226 * @type {Element}
227 * @private
228 */
229 goog.ui.Dialog.prototype.buttonEl_ = null;
230
231
232 /**
233 * The dialog's preferred ARIA role.
234 * @type {goog.a11y.aria.Role}
235 * @private
236 */
237 goog.ui.Dialog.prototype.preferredAriaRole_ = goog.a11y.aria.Role.DIALOG;
238
239
240 /** @override */
241 goog.ui.Dialog.prototype.getCssClass = function() {
242 return this.class_;
243 };
244
245
246 /**
247 * Sets the title.
248 * @param {string} title The title text.
249 */
250 goog.ui.Dialog.prototype.setTitle = function(title) {
251 this.title_ = title;
252 if (this.titleTextEl_) {
253 goog.dom.setTextContent(this.titleTextEl_, title);
254 }
255 };
256
257
258 /**
259 * Gets the title.
260 * @return {string} The title.
261 */
262 goog.ui.Dialog.prototype.getTitle = function() {
263 return this.title_;
264 };
265
266
267 /**
268 * Allows plain text to be set in the content element.
269 * @param {string} text Content plain text. Newlines are preserved.
270 */
271 goog.ui.Dialog.prototype.setTextContent = function(text) {
272 this.setSafeHtmlContent(
273 goog.html.SafeHtml.htmlEscapePreservingNewlines(text));
274 };
275
276
277 /**
278 * Allows arbitrary HTML to be set in the content element.
279 * @param {!goog.html.SafeHtml} html Content HTML.
280 */
281 goog.ui.Dialog.prototype.setSafeHtmlContent = function(html) {
282 this.content_ = html;
283 if (this.contentEl_) {
284 goog.dom.safe.setInnerHtml(this.contentEl_, html);
285 }
286 };
287
288
289 /**
290 * Gets the content HTML of the content element as a plain string.
291 *
292 * Note that this method returns the HTML markup that was previously set via
293 * setContent(). In particular, the HTML returned by this method does not
294 * reflect any changes to the content element's DOM that were made my means
295 * other than setContent().
296 *
297 * @return {string} Content HTML.
298 */
299 goog.ui.Dialog.prototype.getContent = function() {
300 return this.content_ != null ? goog.html.SafeHtml.unwrap(this.content_) : '';
301 };
302
303
304 /**
305 * Gets the content HTML of the content element.
306 * @return {goog.html.SafeHtml} Content HTML.
307 */
308 goog.ui.Dialog.prototype.getSafeHtmlContent = function() {
309 return this.content_;
310 };
311
312
313 /**
314 * Returns the dialog's preferred ARIA role. This can be used to override the
315 * default dialog role, e.g. with an ARIA role of ALERTDIALOG for a simple
316 * warning or confirmation dialog.
317 * @return {goog.a11y.aria.Role} This dialog's preferred ARIA role.
318 */
319 goog.ui.Dialog.prototype.getPreferredAriaRole = function() {
320 return this.preferredAriaRole_;
321 };
322
323
324 /**
325 * Sets the dialog's preferred ARIA role. This can be used to override the
326 * default dialog role, e.g. with an ARIA role of ALERTDIALOG for a simple
327 * warning or confirmation dialog.
328 * @param {goog.a11y.aria.Role} role This dialog's preferred ARIA role.
329 */
330 goog.ui.Dialog.prototype.setPreferredAriaRole = function(role) {
331 this.preferredAriaRole_ = role;
332 };
333
334
335 /**
336 * Renders if the DOM is not created.
337 * @private
338 */
339 goog.ui.Dialog.prototype.renderIfNoDom_ = function() {
340 if (!this.getElement()) {
341 // TODO(gboyer): Ideally we'd only create the DOM, but many applications
342 // are requiring this behavior. Eventually, it would be best if the
343 // element getters could return null if the elements have not been
344 // created.
345 this.render();
346 }
347 };
348
349
350 /**
351 * Returns the content element so that more complicated things can be done with
352 * the content area. Renders if the DOM is not yet created. Overrides
353 * {@link goog.ui.Component#getContentElement}.
354 * @return {Element} The content element.
355 * @override
356 */
357 goog.ui.Dialog.prototype.getContentElement = function() {
358 this.renderIfNoDom_();
359 return this.contentEl_;
360 };
361
362
363 /**
364 * Returns the title element so that more complicated things can be done with
365 * the title. Renders if the DOM is not yet created.
366 * @return {Element} The title element.
367 */
368 goog.ui.Dialog.prototype.getTitleElement = function() {
369 this.renderIfNoDom_();
370 return this.titleEl_;
371 };
372
373
374 /**
375 * Returns the title text element so that more complicated things can be done
376 * with the text of the title. Renders if the DOM is not yet created.
377 * @return {Element} The title text element.
378 */
379 goog.ui.Dialog.prototype.getTitleTextElement = function() {
380 this.renderIfNoDom_();
381 return this.titleTextEl_;
382 };
383
384
385 /**
386 * Returns the title close element so that more complicated things can be done
387 * with the close area of the title. Renders if the DOM is not yet created.
388 * @return {Element} The close box.
389 */
390 goog.ui.Dialog.prototype.getTitleCloseElement = function() {
391 this.renderIfNoDom_();
392 return this.titleCloseEl_;
393 };
394
395
396 /**
397 * Returns the button element so that more complicated things can be done with
398 * the button area. Renders if the DOM is not yet created.
399 * @return {Element} The button container element.
400 */
401 goog.ui.Dialog.prototype.getButtonElement = function() {
402 this.renderIfNoDom_();
403 return this.buttonEl_;
404 };
405
406
407 /**
408 * Returns the dialog element so that more complicated things can be done with
409 * the dialog box. Renders if the DOM is not yet created.
410 * @return {Element} The dialog element.
411 */
412 goog.ui.Dialog.prototype.getDialogElement = function() {
413 this.renderIfNoDom_();
414 return this.getElement();
415 };
416
417
418 /**
419 * Returns the background mask element so that more complicated things can be
420 * done with the background region. Renders if the DOM is not yet created.
421 * @return {Element} The background mask element.
422 * @override
423 */
424 goog.ui.Dialog.prototype.getBackgroundElement = function() {
425 this.renderIfNoDom_();
426 return goog.ui.Dialog.base(this, 'getBackgroundElement');
427 };
428
429
430 /**
431 * Gets the opacity of the background mask.
432 * @return {number} Background mask opacity.
433 */
434 goog.ui.Dialog.prototype.getBackgroundElementOpacity = function() {
435 return this.backgroundElementOpacity_;
436 };
437
438
439 /**
440 * Sets the opacity of the background mask.
441 * @param {number} opacity Background mask opacity.
442 */
443 goog.ui.Dialog.prototype.setBackgroundElementOpacity = function(opacity) {
444 this.backgroundElementOpacity_ = opacity;
445
446 if (this.getElement()) {
447 var bgEl = this.getBackgroundElement();
448 if (bgEl) {
449 goog.style.setOpacity(bgEl, this.backgroundElementOpacity_);
450 }
451 }
452 };
453
454
455 /**
456 * Sets the modal property of the dialog. In case the dialog is already
457 * inDocument, renders the modal background elements according to the specified
458 * modal parameter.
459 *
460 * Note that non-modal dialogs cannot use an iframe mask.
461 *
462 * @param {boolean} modal Whether the dialog is modal.
463 */
464 goog.ui.Dialog.prototype.setModal = function(modal) {
465 if (modal != this.modal_) {
466 this.setModalInternal_(modal);
467 }
468 };
469
470
471 /**
472 * Sets the modal property of the dialog.
473 * @param {boolean} modal Whether the dialog is modal.
474 * @private
475 */
476 goog.ui.Dialog.prototype.setModalInternal_ = function(modal) {
477 this.modal_ = modal;
478 if (this.isInDocument()) {
479 var dom = this.getDomHelper();
480 var bg = this.getBackgroundElement();
481 var bgIframe = this.getBackgroundIframe();
482 if (modal) {
483 if (bgIframe) {
484 dom.insertSiblingBefore(bgIframe, this.getElement());
485 }
486 dom.insertSiblingBefore(bg, this.getElement());
487 } else {
488 dom.removeNode(bgIframe);
489 dom.removeNode(bg);
490 }
491 }
492 if (this.isVisible()) {
493 this.setA11YDetectBackground(modal);
494 }
495 };
496
497
498 /**
499 * @return {boolean} modal Whether the dialog is modal.
500 */
501 goog.ui.Dialog.prototype.getModal = function() {
502 return this.modal_;
503 };
504
505
506 /**
507 * @return {string} The CSS class name for the dialog element.
508 */
509 goog.ui.Dialog.prototype.getClass = function() {
510 return this.getCssClass();
511 };
512
513
514 /**
515 * Sets whether the dialog can be dragged.
516 * @param {boolean} draggable Whether the dialog can be dragged.
517 */
518 goog.ui.Dialog.prototype.setDraggable = function(draggable) {
519 this.draggable_ = draggable;
520 this.setDraggingEnabled_(draggable && this.isInDocument());
521 };
522
523
524 /**
525 * Returns a dragger for moving the dialog and adds a class for the move cursor.
526 * Defaults to allow dragging of the title only, but can be overridden if
527 * different drag targets or dragging behavior is desired.
528 * @return {!goog.fx.Dragger} The created dragger instance.
529 * @protected
530 */
531 goog.ui.Dialog.prototype.createDragger = function() {
532 return new goog.fx.Dragger(this.getElement(), this.titleEl_);
533 };
534
535
536 /**
537 * @return {boolean} Whether the dialog is draggable.
538 */
539 goog.ui.Dialog.prototype.getDraggable = function() {
540 return this.draggable_;
541 };
542
543
544 /**
545 * Enables or disables dragging.
546 * @param {boolean} enabled Whether to enable it.
547 * @private
548 */
549 goog.ui.Dialog.prototype.setDraggingEnabled_ = function(enabled) {
550 // This isn't ideal, but the quickest and easiest way to append
551 // title-draggable to the last class in the class_ string, then trim and
552 // split the string into an array (in case the dialog was set up with
553 // multiple, space-separated class names).
554 var classNames =
555 goog.string.trim(goog.getCssName(this.class_, 'title-draggable'))
556 .split(' ');
557
558 if (this.getElement()) {
559 if (enabled) {
560 goog.dom.classlist.addAll(goog.asserts.assert(this.titleEl_), classNames);
561 } else {
562 goog.dom.classlist.removeAll(
563 goog.asserts.assert(this.titleEl_), classNames);
564 }
565 }
566
567 if (enabled && !this.dragger_) {
568 this.dragger_ = this.createDragger();
569 goog.dom.classlist.addAll(goog.asserts.assert(this.titleEl_), classNames);
570 goog.events.listen(
571 this.dragger_, goog.fx.Dragger.EventType.START, this.setDraggerLimits_,
572 false, this);
573 } else if (!enabled && this.dragger_) {
574 this.dragger_.dispose();
575 this.dragger_ = null;
576 }
577 };
578
579
580 /** @override */
581 goog.ui.Dialog.prototype.createDom = function() {
582 goog.ui.Dialog.base(this, 'createDom');
583 var element = this.getElement();
584 goog.asserts.assert(element, 'getElement() returns null');
585
586 var dom = this.getDomHelper();
587 this.titleEl_ = dom.createDom(
588 goog.dom.TagName.DIV, goog.getCssName(this.class_, 'title'),
589 this.titleTextEl_ = dom.createDom(
590 goog.dom.TagName.SPAN, {
591 'className': goog.getCssName(this.class_, 'title-text'),
592 'id': this.getId()
593 },
594 this.title_),
595 this.titleCloseEl_ = dom.createDom(
596 goog.dom.TagName.SPAN, goog.getCssName(this.class_, 'title-close'))),
597 goog.dom.append(
598 element, this.titleEl_,
599 this.contentEl_ = dom.createDom(
600 goog.dom.TagName.DIV, goog.getCssName(this.class_, 'content')),
601 this.buttonEl_ = dom.createDom(
602 goog.dom.TagName.DIV, goog.getCssName(this.class_, 'buttons')));
603
604 // Make the title and close button behave correctly with screen readers.
605 // Note: this is only being added if the dialog is not decorated. Decorators
606 // are expected to add aria label, role, and tab indexing in their templates.
607 goog.a11y.aria.setRole(this.titleTextEl_, goog.a11y.aria.Role.HEADING);
608 goog.a11y.aria.setRole(this.titleCloseEl_, goog.a11y.aria.Role.BUTTON);
609 goog.dom.setFocusableTabIndex(this.titleCloseEl_, true);
610 goog.a11y.aria.setLabel(
611 this.titleCloseEl_, goog.ui.Dialog.MSG_GOOG_UI_DIALOG_CLOSE_);
612
613 this.titleTextId_ = this.titleTextEl_.id;
614 goog.a11y.aria.setRole(element, this.getPreferredAriaRole());
615 goog.a11y.aria.setState(
616 element, goog.a11y.aria.State.LABELLEDBY, this.titleTextId_ || '');
617 // If setContent() was called before createDom(), make sure the inner HTML of
618 // the content element is initialized.
619 if (this.content_) {
620 goog.dom.safe.setInnerHtml(this.contentEl_, this.content_);
621 }
622 goog.style.setElementShown(this.titleCloseEl_, this.hasTitleCloseButton_);
623
624 // Render the buttons.
625 if (this.buttons_) {
626 this.buttons_.attachToElement(this.buttonEl_);
627 }
628 goog.style.setElementShown(this.buttonEl_, !!this.buttons_);
629 this.setBackgroundElementOpacity(this.backgroundElementOpacity_);
630 };
631
632
633 /** @override */
634 goog.ui.Dialog.prototype.decorateInternal = function(element) {
635 goog.ui.Dialog.base(this, 'decorateInternal', element);
636 var dialogElement = this.getElement();
637 goog.asserts.assert(
638 dialogElement, 'The DOM element for dialog cannot be null.');
639 // Decorate or create the content element.
640 var contentClass = goog.getCssName(this.class_, 'content');
641 this.contentEl_ = goog.dom.getElementsByTagNameAndClass(
642 null, contentClass, dialogElement)[0];
643 if (!this.contentEl_) {
644 this.contentEl_ =
645 this.getDomHelper().createDom(goog.dom.TagName.DIV, contentClass);
646 if (this.content_) {
647 goog.dom.safe.setInnerHtml(this.contentEl_, this.content_);
648 }
649 dialogElement.appendChild(this.contentEl_);
650 }
651
652 // Decorate or create the title bar element.
653 var titleClass = goog.getCssName(this.class_, 'title');
654 var titleTextClass = goog.getCssName(this.class_, 'title-text');
655 var titleCloseClass = goog.getCssName(this.class_, 'title-close');
656 this.titleEl_ =
657 goog.dom.getElementsByTagNameAndClass(null, titleClass, dialogElement)[0];
658 if (this.titleEl_) {
659 // Only look for title text & title close elements if a title bar element
660 // was found. Otherwise assume that the entire title bar has to be
661 // created from scratch.
662 this.titleTextEl_ = goog.dom.getElementsByTagNameAndClass(
663 null, titleTextClass, this.titleEl_)[0];
664 this.titleCloseEl_ = goog.dom.getElementsByTagNameAndClass(
665 null, titleCloseClass, this.titleEl_)[0];
666 } else {
667 // Create the title bar element and insert it before the content area.
668 // This is useful if the element to decorate only includes a content area.
669 this.titleEl_ =
670 this.getDomHelper().createDom(goog.dom.TagName.DIV, titleClass);
671 dialogElement.insertBefore(this.titleEl_, this.contentEl_);
672 }
673
674 // Decorate or create the title text element.
675 if (this.titleTextEl_) {
676 this.title_ = goog.dom.getTextContent(this.titleTextEl_);
677 // Give the title text element an id if it doesn't already have one.
678 if (!this.titleTextEl_.id) {
679 this.titleTextEl_.id = this.getId();
680 }
681 } else {
682 this.titleTextEl_ = goog.dom.createDom(
683 goog.dom.TagName.SPAN,
684 {'className': titleTextClass, 'id': this.getId()});
685 this.titleEl_.appendChild(this.titleTextEl_);
686 }
687 this.titleTextId_ = this.titleTextEl_.id;
688 goog.a11y.aria.setState(
689 dialogElement, goog.a11y.aria.State.LABELLEDBY, this.titleTextId_ || '');
690 // Decorate or create the title close element.
691 if (!this.titleCloseEl_) {
692 this.titleCloseEl_ =
693 this.getDomHelper().createDom(goog.dom.TagName.SPAN, titleCloseClass);
694 this.titleEl_.appendChild(this.titleCloseEl_);
695 }
696 goog.style.setElementShown(this.titleCloseEl_, this.hasTitleCloseButton_);
697
698 // Decorate or create the button container element.
699 var buttonsClass = goog.getCssName(this.class_, 'buttons');
700 this.buttonEl_ = goog.dom.getElementsByTagNameAndClass(
701 null, buttonsClass, dialogElement)[0];
702 if (this.buttonEl_) {
703 // Button container element found. Create empty button set and use it to
704 // decorate the button container.
705 this.buttons_ = new goog.ui.Dialog.ButtonSet(this.getDomHelper());
706 this.buttons_.decorate(this.buttonEl_);
707 } else {
708 // Create new button container element, and render a button set into it.
709 this.buttonEl_ =
710 this.getDomHelper().createDom(goog.dom.TagName.DIV, buttonsClass);
711 dialogElement.appendChild(this.buttonEl_);
712 if (this.buttons_) {
713 this.buttons_.attachToElement(this.buttonEl_);
714 }
715 goog.style.setElementShown(this.buttonEl_, !!this.buttons_);
716 }
717 this.setBackgroundElementOpacity(this.backgroundElementOpacity_);
718 };
719
720
721 /** @override */
722 goog.ui.Dialog.prototype.enterDocument = function() {
723 goog.ui.Dialog.base(this, 'enterDocument');
724
725 // Listen for keyboard events while the dialog is visible.
726 this.getHandler()
727 .listen(this.getElement(), goog.events.EventType.KEYDOWN, this.onKey_)
728 .listen(this.getElement(), goog.events.EventType.KEYPRESS, this.onKey_);
729
730 // NOTE: see bug 1163154 for an example of an edge case where making the
731 // dialog visible in response to a KEYDOWN will result in a CLICK event
732 // firing on the default button (immediately closing the dialog) if the key
733 // that fired the KEYDOWN is also normally used to activate controls
734 // (i.e. SPACE/ENTER).
735 //
736 // This could be worked around by attaching the onButtonClick_ handler in a
737 // setTimeout, but that was deemed undesirable.
738 this.getHandler().listen(
739 this.buttonEl_, goog.events.EventType.CLICK, this.onButtonClick_);
740
741 // Add drag support.
742 this.setDraggingEnabled_(this.draggable_);
743
744 // Add event listeners to the close box and the button container.
745 this.getHandler().listen(
746 this.titleCloseEl_, goog.events.EventType.CLICK, this.onTitleCloseClick_);
747
748 var element = this.getElement();
749 goog.asserts.assert(element, 'The DOM element for dialog cannot be null');
750 goog.a11y.aria.setRole(element, this.getPreferredAriaRole());
751 if (this.titleTextEl_.id !== '') {
752 goog.a11y.aria.setState(
753 element, goog.a11y.aria.State.LABELLEDBY, this.titleTextEl_.id);
754 }
755
756 if (!this.modal_) {
757 this.setModalInternal_(false);
758 }
759 };
760
761
762 /** @override */
763 goog.ui.Dialog.prototype.exitDocument = function() {
764 if (this.isVisible()) {
765 this.setVisible(false);
766 }
767
768 // Remove drag support.
769 this.setDraggingEnabled_(false);
770
771 goog.ui.Dialog.base(this, 'exitDocument');
772 };
773
774
775 /**
776 * Sets the visibility of the dialog box and moves focus to the
777 * default button. Lazily renders the component if needed. After this
778 * method returns, isVisible() will always return the new state, even
779 * if there is a transition.
780 * @param {boolean} visible Whether the dialog should be visible.
781 * @override
782 */
783 goog.ui.Dialog.prototype.setVisible = function(visible) {
784 if (visible == this.isVisible()) {
785 return;
786 }
787
788 // If the dialog hasn't been rendered yet, render it now.
789 if (!this.isInDocument()) {
790 this.render();
791 }
792
793 goog.ui.Dialog.base(this, 'setVisible', visible);
794 };
795
796
797 /**
798 * @override
799 * @suppress {deprecated} AFTER_SHOW is deprecated earlier in this file.
800 */
801 goog.ui.Dialog.prototype.onShow = function() {
802 goog.ui.Dialog.base(this, 'onShow');
803 this.dispatchEvent(goog.ui.Dialog.EventType.AFTER_SHOW);
804 };
805
806
807 /**
808 * @override
809 * @suppress {deprecated} AFTER_HIDE is deprecated earlier in this file.
810 */
811 goog.ui.Dialog.prototype.onHide = function() {
812 goog.ui.Dialog.base(this, 'onHide');
813 this.dispatchEvent(goog.ui.Dialog.EventType.AFTER_HIDE);
814 if (this.disposeOnHide_) {
815 this.dispose();
816 }
817 };
818
819
820 /**
821 * Sets dragger limits when dragging is started.
822 * @param {!goog.events.Event} e goog.fx.Dragger.EventType.START event.
823 * @private
824 */
825 goog.ui.Dialog.prototype.setDraggerLimits_ = function(e) {
826 var doc = this.getDomHelper().getDocument();
827 var win = goog.dom.getWindow(doc) || window;
828
829 // Take the max of scroll height and view height for cases in which document
830 // does not fill screen.
831 var viewSize = goog.dom.getViewportSize(win);
832 var w = Math.max(doc.body.scrollWidth, viewSize.width);
833 var h = Math.max(doc.body.scrollHeight, viewSize.height);
834
835 var dialogSize = goog.style.getSize(this.getElement());
836 if (goog.style.getComputedPosition(this.getElement()) == 'fixed') {
837 // Ensure position:fixed dialogs can't be dragged beyond the viewport.
838 this.dragger_.setLimits(
839 new goog.math.Rect(
840 0, 0, Math.max(0, viewSize.width - dialogSize.width),
841 Math.max(0, viewSize.height - dialogSize.height)));
842 } else {
843 this.dragger_.setLimits(
844 new goog.math.Rect(0, 0, w - dialogSize.width, h - dialogSize.height));
845 }
846 };
847
848
849 /**
850 * Handles a click on the title close area.
851 * @param {goog.events.BrowserEvent} e Browser's event object.
852 * @private
853 */
854 goog.ui.Dialog.prototype.onTitleCloseClick_ = function(e) {
855 this.handleTitleClose_();
856 };
857
858
859 /**
860 * Performs the action of closing the dialog in response to the title close
861 * button being interacted with. General purpose method to be called by click
862 * and button event handlers.
863 * @private
864 */
865 goog.ui.Dialog.prototype.handleTitleClose_ = function() {
866 if (!this.hasTitleCloseButton_) {
867 return;
868 }
869
870 var bs = this.getButtonSet();
871 var key = bs && bs.getCancel();
872 // Only if there is a valid cancel button is an event dispatched.
873 if (key) {
874 var caption = /** @type {Element|string} */ (bs.get(key));
875 if (this.dispatchEvent(new goog.ui.Dialog.Event(key, caption))) {
876 this.setVisible(false);
877 }
878 } else {
879 this.setVisible(false);
880 }
881 };
882
883
884 /**
885 * @return {boolean} Whether this dialog has a title close button.
886 */
887 goog.ui.Dialog.prototype.getHasTitleCloseButton = function() {
888 return this.hasTitleCloseButton_;
889 };
890
891
892 /**
893 * Sets whether the dialog should have a close button in the title bar. There
894 * will always be an element for the title close button, but setting this
895 * parameter to false will cause it to be hidden and have no active listener.
896 * @param {boolean} b Whether this dialog should have a title close button.
897 */
898 goog.ui.Dialog.prototype.setHasTitleCloseButton = function(b) {
899 this.hasTitleCloseButton_ = b;
900 if (this.titleCloseEl_) {
901 goog.style.setElementShown(this.titleCloseEl_, this.hasTitleCloseButton_);
902 }
903 };
904
905
906 /**
907 * @return {boolean} Whether the escape key should close this dialog.
908 */
909 goog.ui.Dialog.prototype.isEscapeToCancel = function() {
910 return this.escapeToCancel_;
911 };
912
913
914 /**
915 * @param {boolean} b Whether the escape key should close this dialog.
916 */
917 goog.ui.Dialog.prototype.setEscapeToCancel = function(b) {
918 this.escapeToCancel_ = b;
919 };
920
921
922 /**
923 * Sets whether the dialog should be disposed when it is hidden. By default
924 * dialogs are not disposed when they are hidden.
925 * @param {boolean} b Whether the dialog should get disposed when it gets
926 * hidden.
927 */
928 goog.ui.Dialog.prototype.setDisposeOnHide = function(b) {
929 this.disposeOnHide_ = b;
930 };
931
932
933 /**
934 * @return {boolean} Whether the dialog should be disposed when it is hidden.
935 */
936 goog.ui.Dialog.prototype.getDisposeOnHide = function() {
937 return this.disposeOnHide_;
938 };
939
940
941 /** @override */
942 goog.ui.Dialog.prototype.disposeInternal = function() {
943 this.titleCloseEl_ = null;
944 this.buttonEl_ = null;
945 goog.ui.Dialog.base(this, 'disposeInternal');
946 };
947
948
949 /**
950 * Sets the button set to use.
951 * Note: Passing in null will cause no button set to be rendered.
952 * @param {goog.ui.Dialog.ButtonSet?} buttons The button set to use.
953 */
954 goog.ui.Dialog.prototype.setButtonSet = function(buttons) {
955 this.buttons_ = buttons;
956 if (this.buttonEl_) {
957 if (this.buttons_) {
958 this.buttons_.attachToElement(this.buttonEl_);
959 } else {
960 goog.dom.safe.setInnerHtml(this.buttonEl_, goog.html.SafeHtml.EMPTY);
961 }
962 goog.style.setElementShown(this.buttonEl_, !!this.buttons_);
963 }
964 };
965
966
967 /**
968 * Returns the button set being used.
969 * @return {goog.ui.Dialog.ButtonSet?} The button set being used.
970 */
971 goog.ui.Dialog.prototype.getButtonSet = function() {
972 return this.buttons_;
973 };
974
975
976 /**
977 * Handles a click on the button container.
978 * @param {goog.events.BrowserEvent} e Browser's event object.
979 * @private
980 */
981 goog.ui.Dialog.prototype.onButtonClick_ = function(e) {
982 var button = this.findParentButton_(/** @type {Element} */ (e.target));
983 if (button && !button.disabled) {
984 var key = button.name;
985 var caption = /** @type {Element|string} */ (this.getButtonSet().get(key));
986 if (this.dispatchEvent(new goog.ui.Dialog.Event(key, caption))) {
987 this.setVisible(false);
988 }
989 }
990 };
991
992
993 /**
994 * Finds the parent button of an element (or null if there was no button
995 * parent).
996 * @param {Element} element The element that was clicked on.
997 * @return {Element} Returns the parent button or null if not found.
998 * @private
999 */
1000 goog.ui.Dialog.prototype.findParentButton_ = function(element) {
1001 var el = element;
1002 while (el != null && el != this.buttonEl_) {
1003 if (el.tagName == goog.dom.TagName.BUTTON) {
1004 return /** @type {Element} */ (el);
1005 }
1006 el = el.parentNode;
1007 }
1008 return null;
1009 };
1010
1011
1012 /**
1013 * Handles keydown and keypress events, and dismisses the popup if cancel is
1014 * pressed. If there is a cancel action in the ButtonSet, than that will be
1015 * fired. Also prevents tabbing out of the dialog.
1016 * @param {goog.events.BrowserEvent} e Browser's event object.
1017 * @private
1018 */
1019 goog.ui.Dialog.prototype.onKey_ = function(e) {
1020 var close = false;
1021 var hasHandler = false;
1022 var buttonSet = this.getButtonSet();
1023 var target = e.target;
1024
1025 if (e.type == goog.events.EventType.KEYDOWN) {
1026 // Escape and tab can only properly be handled in keydown handlers.
1027 if (this.escapeToCancel_ && e.keyCode == goog.events.KeyCodes.ESC) {
1028 // Only if there is a valid cancel button is an event dispatched.
1029 var cancel = buttonSet && buttonSet.getCancel();
1030
1031 // Users may expect to hit escape on a SELECT element.
1032 var isSpecialFormElement =
1033 target.tagName == goog.dom.TagName.SELECT && !target.disabled;
1034
1035 if (cancel && !isSpecialFormElement) {
1036 hasHandler = true;
1037
1038 var caption = buttonSet.get(cancel);
1039 close = this.dispatchEvent(
1040 new goog.ui.Dialog.Event(
1041 cancel,
1042 /** @type {Element|null|string} */ (caption)));
1043 } else if (!isSpecialFormElement) {
1044 close = true;
1045 }
1046 } else if (
1047 e.keyCode == goog.events.KeyCodes.TAB && e.shiftKey &&
1048 target == this.getElement()) {
1049 // Prevent the user from shift-tabbing backwards out of the dialog box.
1050 // Instead, set up a wrap in focus backward to the end of the dialog.
1051 this.setupBackwardTabWrap();
1052 }
1053 } else if (e.keyCode == goog.events.KeyCodes.ENTER) {
1054 // Only handle ENTER in keypress events, in case the action opens a
1055 // popup window.
1056 var key;
1057 if (target.tagName == goog.dom.TagName.BUTTON && !target.disabled) {
1058 // If the target is a button and it's enabled, we can fire that button's
1059 // handler.
1060 key = target.name;
1061 } else if (target == this.titleCloseEl_) {
1062 // if the title 'close' button is in focus, close the dialog
1063 this.handleTitleClose_();
1064 } else if (buttonSet) {
1065 // Try to fire the default button's handler (if one exists), but only if
1066 // the button is enabled.
1067 var defaultKey = buttonSet.getDefault();
1068 var defaultButton = defaultKey && buttonSet.getButton(defaultKey);
1069
1070 // Users may expect to hit enter on a TEXTAREA, SELECT or an A element.
1071 var isSpecialFormElement = (target.tagName == goog.dom.TagName.TEXTAREA ||
1072 target.tagName == goog.dom.TagName.SELECT ||
1073 target.tagName == goog.dom.TagName.A) &&
1074 !target.disabled;
1075
1076 if (defaultButton && !defaultButton.disabled && !isSpecialFormElement) {
1077 key = defaultKey;
1078 }
1079 }
1080 if (key && buttonSet) {
1081 hasHandler = true;
1082 close = this.dispatchEvent(
1083 new goog.ui.Dialog.Event(key, String(buttonSet.get(key))));
1084 }
1085 } else if (
1086 target == this.titleCloseEl_ && e.keyCode == goog.events.KeyCodes.SPACE) {
1087 // if the title 'close' button is in focus on 'SPACE,' close the dialog
1088 this.handleTitleClose_();
1089 }
1090
1091 if (close || hasHandler) {
1092 e.stopPropagation();
1093 e.preventDefault();
1094 }
1095
1096 if (close) {
1097 this.setVisible(false);
1098 }
1099 };
1100
1101
1102
1103 /**
1104 * Dialog event class.
1105 * @param {string} key Key identifier for the button.
1106 * @param {string|Element} caption Caption on the button (might be i18nlized).
1107 * @constructor
1108 * @extends {goog.events.Event}
1109 */
1110 goog.ui.Dialog.Event = function(key, caption) {
1111 this.type = goog.ui.Dialog.EventType.SELECT;
1112 this.key = key;
1113 this.caption = caption;
1114 };
1115 goog.inherits(goog.ui.Dialog.Event, goog.events.Event);
1116
1117
1118 /**
1119 * Event type constant for dialog events.
1120 * TODO(attila): Change this to goog.ui.Dialog.EventType.SELECT.
1121 * @type {string}
1122 * @deprecated Use goog.ui.Dialog.EventType.SELECT.
1123 */
1124 goog.ui.Dialog.SELECT_EVENT = 'dialogselect';
1125
1126
1127 /**
1128 * Events dispatched by dialogs.
1129 * @enum {string}
1130 */
1131 goog.ui.Dialog.EventType = {
1132 /**
1133 * Dispatched when the user closes the dialog.
1134 * The dispatched event will always be of type {@link goog.ui.Dialog.Event}.
1135 * Canceling the event will prevent the dialog from closing.
1136 */
1137 SELECT: 'dialogselect',
1138
1139 /**
1140 * Dispatched after the dialog is closed. Not cancelable.
1141 * @deprecated Use goog.ui.PopupBase.EventType.HIDE.
1142 */
1143 AFTER_HIDE: 'afterhide',
1144
1145 /**
1146 * Dispatched after the dialog is shown. Not cancelable.
1147 * @deprecated Use goog.ui.PopupBase.EventType.SHOW.
1148 */
1149 AFTER_SHOW: 'aftershow'
1150 };
1151
1152
1153
1154 /**
1155 * A button set defines the behaviour of a set of buttons that the dialog can
1156 * show. Uses the {@link goog.structs.Map} interface.
1157 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link
1158 * goog.ui.Component} for semantics.
1159 * @constructor
1160 * @extends {goog.structs.Map}
1161 */
1162 goog.ui.Dialog.ButtonSet = function(opt_domHelper) {
1163 // TODO(attila): Refactor ButtonSet to extend goog.ui.Component?
1164 this.dom_ = opt_domHelper || goog.dom.getDomHelper();
1165 goog.structs.Map.call(this);
1166 };
1167 goog.inherits(goog.ui.Dialog.ButtonSet, goog.structs.Map);
1168 goog.tagUnsealableClass(goog.ui.Dialog.ButtonSet);
1169
1170
1171 /**
1172 * A CSS className for this component.
1173 * @type {string}
1174 * @private
1175 */
1176 goog.ui.Dialog.ButtonSet.prototype.class_ = goog.getCssName('goog-buttonset');
1177
1178
1179 /**
1180 * The button that has default focus (references key in buttons_ map).
1181 * @type {?string}
1182 * @private
1183 */
1184 goog.ui.Dialog.ButtonSet.prototype.defaultButton_ = null;
1185
1186
1187 /**
1188 * Optional container the button set should be rendered into.
1189 * @type {Element}
1190 * @private
1191 */
1192 goog.ui.Dialog.ButtonSet.prototype.element_ = null;
1193
1194
1195 /**
1196 * The button whose action is associated with the escape key and the X button
1197 * on the dialog.
1198 * @type {?string}
1199 * @private
1200 */
1201 goog.ui.Dialog.ButtonSet.prototype.cancelButton_ = null;
1202
1203
1204 /** @override */
1205 goog.ui.Dialog.ButtonSet.prototype.clear = function() {
1206 goog.structs.Map.prototype.clear.call(this);
1207 this.defaultButton_ = this.cancelButton_ = null;
1208 };
1209
1210
1211 /**
1212 * Adds a button to the button set. Buttons will be displayed in the order they
1213 * are added.
1214 *
1215 * @param {*} key Key used to identify the button in events.
1216 * @param {*} caption A string caption or a DOM node that can be
1217 * appended to a button element.
1218 * @param {boolean=} opt_isDefault Whether this button is the default button,
1219 * Dialog will dispatch for this button if enter is pressed.
1220 * @param {boolean=} opt_isCancel Whether this button has the same behaviour as
1221 * cancel. If escape is pressed this button will fire.
1222 * @return {!goog.ui.Dialog.ButtonSet} The button set, to make it easy to chain
1223 * "set" calls and build new ButtonSets.
1224 * @override
1225 */
1226 goog.ui.Dialog.ButtonSet.prototype.set = function(
1227 key, caption, opt_isDefault, opt_isCancel) {
1228 goog.structs.Map.prototype.set.call(this, key, caption);
1229
1230 if (opt_isDefault) {
1231 this.defaultButton_ = /** @type {?string} */ (key);
1232 }
1233 if (opt_isCancel) {
1234 this.cancelButton_ = /** @type {?string} */ (key);
1235 }
1236
1237 return this;
1238 };
1239
1240
1241 /**
1242 * Adds a button (an object with a key and caption) to this button set. Buttons
1243 * will be displayed in the order they are added.
1244 * @see goog.ui.Dialog.DefaultButtons
1245 * @param {{key: string, caption: string}} button The button key and caption.
1246 * @param {boolean=} opt_isDefault Whether this button is the default button.
1247 * Dialog will dispatch for this button if enter is pressed.
1248 * @param {boolean=} opt_isCancel Whether this button has the same behavior as
1249 * cancel. If escape is pressed this button will fire.
1250 * @return {!goog.ui.Dialog.ButtonSet} The button set, to make it easy to chain
1251 * "addButton" calls and build new ButtonSets.
1252 */
1253 goog.ui.Dialog.ButtonSet.prototype.addButton = function(
1254 button, opt_isDefault, opt_isCancel) {
1255 return this.set(button.key, button.caption, opt_isDefault, opt_isCancel);
1256 };
1257
1258
1259 /**
1260 * Attaches the button set to an element, rendering it inside.
1261 * @param {Element} el Container.
1262 */
1263 goog.ui.Dialog.ButtonSet.prototype.attachToElement = function(el) {
1264 this.element_ = el;
1265 this.render();
1266 };
1267
1268
1269 /**
1270 * Renders the button set inside its container element.
1271 */
1272 goog.ui.Dialog.ButtonSet.prototype.render = function() {
1273 if (this.element_) {
1274 goog.dom.safe.setInnerHtml(this.element_, goog.html.SafeHtml.EMPTY);
1275 var domHelper = goog.dom.getDomHelper(this.element_);
1276 this.forEach(function(caption, key) {
1277 var button =
1278 domHelper.createDom(goog.dom.TagName.BUTTON, {'name': key}, caption);
1279 if (key == this.defaultButton_) {
1280 button.className = goog.getCssName(this.class_, 'default');
1281 }
1282 this.element_.appendChild(button);
1283 }, this);
1284 }
1285 };
1286
1287
1288 /**
1289 * Decorates the given element by adding any {@code button} elements found
1290 * among its descendants to the button set. The first button found is assumed
1291 * to be the default and will receive focus when the button set is rendered.
1292 * If a button with a name of {@link goog.ui.Dialog.DefaultButtonKeys.CANCEL}
1293 * is found, it is assumed to have "Cancel" semantics.
1294 * TODO(attila): ButtonSet should be a goog.ui.Component. Really.
1295 * @param {Element} element The element to decorate; should contain buttons.
1296 */
1297 goog.ui.Dialog.ButtonSet.prototype.decorate = function(element) {
1298 if (!element || element.nodeType != goog.dom.NodeType.ELEMENT) {
1299 return;
1300 }
1301
1302 this.element_ = element;
1303 var buttons =
1304 goog.dom.getElementsByTagName(goog.dom.TagName.BUTTON, this.element_);
1305 for (var i = 0, button, key, caption; button = buttons[i]; i++) {
1306 // Buttons should have a "name" attribute and have their caption defined by
1307 // their innerHTML, but not everyone knows this, and we should play nice.
1308 key = button.name || button.id;
1309 caption = goog.dom.getTextContent(button) || button.value;
1310 if (key) {
1311 var isDefault = i == 0;
1312 var isCancel = button.name == goog.ui.Dialog.DefaultButtonKeys.CANCEL;
1313 this.set(key, caption, isDefault, isCancel);
1314 if (isDefault) {
1315 goog.dom.classlist.add(button, goog.getCssName(this.class_, 'default'));
1316 }
1317 }
1318 }
1319 };
1320
1321
1322 /**
1323 * Gets the component's element.
1324 * @return {Element} The element for the component.
1325 * TODO(user): Remove after refactoring to goog.ui.Component.
1326 */
1327 goog.ui.Dialog.ButtonSet.prototype.getElement = function() {
1328 return this.element_;
1329 };
1330
1331
1332 /**
1333 * Returns the dom helper that is being used on this component.
1334 * @return {!goog.dom.DomHelper} The dom helper used on this component.
1335 * TODO(user): Remove after refactoring to goog.ui.Component.
1336 */
1337 goog.ui.Dialog.ButtonSet.prototype.getDomHelper = function() {
1338 return this.dom_;
1339 };
1340
1341
1342 /**
1343 * Sets the default button.
1344 * @param {?string} key The default button.
1345 */
1346 goog.ui.Dialog.ButtonSet.prototype.setDefault = function(key) {
1347 this.defaultButton_ = key;
1348 };
1349
1350
1351 /**
1352 * Returns the default button.
1353 * @return {?string} The default button.
1354 */
1355 goog.ui.Dialog.ButtonSet.prototype.getDefault = function() {
1356 return this.defaultButton_;
1357 };
1358
1359
1360 /**
1361 * Sets the cancel button.
1362 * @param {?string} key The cancel button.
1363 */
1364 goog.ui.Dialog.ButtonSet.prototype.setCancel = function(key) {
1365 this.cancelButton_ = key;
1366 };
1367
1368
1369 /**
1370 * Returns the cancel button.
1371 * @return {?string} The cancel button.
1372 */
1373 goog.ui.Dialog.ButtonSet.prototype.getCancel = function() {
1374 return this.cancelButton_;
1375 };
1376
1377
1378 /**
1379 * Returns the HTML Button element.
1380 * @param {string} key The button to return.
1381 * @return {Element} The button, if found else null.
1382 */
1383 goog.ui.Dialog.ButtonSet.prototype.getButton = function(key) {
1384 var buttons = this.getAllButtons();
1385 for (var i = 0, nextButton; nextButton = buttons[i]; i++) {
1386 if (nextButton.name == key || nextButton.id == key) {
1387 return nextButton;
1388 }
1389 }
1390 return null;
1391 };
1392
1393
1394 /**
1395 * Returns all the HTML Button elements in the button set container.
1396 * @return {!IArrayLike<!Element>} A live NodeList of the buttons.
1397 */
1398 goog.ui.Dialog.ButtonSet.prototype.getAllButtons = function() {
1399 return goog.dom.getElementsByTagName(
1400 goog.dom.TagName.BUTTON, goog.asserts.assert(this.element_));
1401 };
1402
1403
1404 /**
1405 * Enables or disables a button in this set by key. If the button is not found,
1406 * does nothing.
1407 * @param {string} key The button to enable or disable.
1408 * @param {boolean} enabled True to enable; false to disable.
1409 */
1410 goog.ui.Dialog.ButtonSet.prototype.setButtonEnabled = function(key, enabled) {
1411 var button = this.getButton(key);
1412 if (button) {
1413 button.disabled = !enabled;
1414 }
1415 };
1416
1417
1418 /**
1419 * Enables or disables all of the buttons in this set.
1420 * @param {boolean} enabled True to enable; false to disable.
1421 */
1422 goog.ui.Dialog.ButtonSet.prototype.setAllButtonsEnabled = function(enabled) {
1423 var allButtons = this.getAllButtons();
1424 for (var i = 0, button; button = allButtons[i]; i++) {
1425 button.disabled = !enabled;
1426 }
1427 };
1428
1429
1430 /**
1431 * The keys used to identify standard buttons in events.
1432 * @enum {string}
1433 */
1434 goog.ui.Dialog.DefaultButtonKeys = {
1435 OK: 'ok',
1436 CANCEL: 'cancel',
1437 YES: 'yes',
1438 NO: 'no',
1439 SAVE: 'save',
1440 CONTINUE: 'continue'
1441 };
1442
1443
1444 /**
1445 * @desc Standard caption for the dialog 'OK' button.
1446 * @private
1447 */
1448 goog.ui.Dialog.MSG_DIALOG_OK_ = goog.getMsg('OK');
1449
1450
1451 /**
1452 * @desc Standard caption for the dialog 'Cancel' button.
1453 * @private
1454 */
1455 goog.ui.Dialog.MSG_DIALOG_CANCEL_ = goog.getMsg('Cancel');
1456
1457
1458 /**
1459 * @desc Standard caption for the dialog 'Yes' button.
1460 * @private
1461 */
1462 goog.ui.Dialog.MSG_DIALOG_YES_ = goog.getMsg('Yes');
1463
1464
1465 /**
1466 * @desc Standard caption for the dialog 'No' button.
1467 * @private
1468 */
1469 goog.ui.Dialog.MSG_DIALOG_NO_ = goog.getMsg('No');
1470
1471
1472 /**
1473 * @desc Standard caption for the dialog 'Save' button.
1474 * @private
1475 */
1476 goog.ui.Dialog.MSG_DIALOG_SAVE_ = goog.getMsg('Save');
1477
1478
1479 /**
1480 * @desc Standard caption for the dialog 'Continue' button.
1481 * @private
1482 */
1483 goog.ui.Dialog.MSG_DIALOG_CONTINUE_ = goog.getMsg('Continue');
1484
1485
1486 /**
1487 * @desc Standard label for the dialog 'X' (close) button.
1488 * @private
1489 */
1490 goog.ui.Dialog.MSG_GOOG_UI_DIALOG_CLOSE_ = goog.getMsg('Close');
1491
1492
1493 /**
1494 * The default captions for the default buttons.
1495 * @enum {string}
1496 */
1497 goog.ui.Dialog.DefaultButtonCaptions = {
1498 OK: goog.ui.Dialog.MSG_DIALOG_OK_,
1499 CANCEL: goog.ui.Dialog.MSG_DIALOG_CANCEL_,
1500 YES: goog.ui.Dialog.MSG_DIALOG_YES_,
1501 NO: goog.ui.Dialog.MSG_DIALOG_NO_,
1502 SAVE: goog.ui.Dialog.MSG_DIALOG_SAVE_,
1503 CONTINUE: goog.ui.Dialog.MSG_DIALOG_CONTINUE_
1504 };
1505
1506
1507 /**
1508 * The standard buttons (keys associated with captions).
1509 * @enum {{key: string, caption: string}}
1510 */
1511 goog.ui.Dialog.ButtonSet.DefaultButtons = {
1512 OK: {
1513 key: goog.ui.Dialog.DefaultButtonKeys.OK,
1514 caption: goog.ui.Dialog.DefaultButtonCaptions.OK
1515 },
1516 CANCEL: {
1517 key: goog.ui.Dialog.DefaultButtonKeys.CANCEL,
1518 caption: goog.ui.Dialog.DefaultButtonCaptions.CANCEL
1519 },
1520 YES: {
1521 key: goog.ui.Dialog.DefaultButtonKeys.YES,
1522 caption: goog.ui.Dialog.DefaultButtonCaptions.YES
1523 },
1524 NO: {
1525 key: goog.ui.Dialog.DefaultButtonKeys.NO,
1526 caption: goog.ui.Dialog.DefaultButtonCaptions.NO
1527 },
1528 SAVE: {
1529 key: goog.ui.Dialog.DefaultButtonKeys.SAVE,
1530 caption: goog.ui.Dialog.DefaultButtonCaptions.SAVE
1531 },
1532 CONTINUE: {
1533 key: goog.ui.Dialog.DefaultButtonKeys.CONTINUE,
1534 caption: goog.ui.Dialog.DefaultButtonCaptions.CONTINUE
1535 }
1536 };
1537
1538
1539 /**
1540 * Creates a new ButtonSet with a single 'OK' button, which is also set with
1541 * cancel button semantics so that pressing escape will close the dialog.
1542 * @return {!goog.ui.Dialog.ButtonSet} The created ButtonSet.
1543 */
1544 goog.ui.Dialog.ButtonSet.createOk = function() {
1545 return new goog.ui.Dialog.ButtonSet().addButton(
1546 goog.ui.Dialog.ButtonSet.DefaultButtons.OK, true, true);
1547 };
1548
1549
1550 /**
1551 * Creates a new ButtonSet with 'OK' (default) and 'Cancel' buttons.
1552 * @return {!goog.ui.Dialog.ButtonSet} The created ButtonSet.
1553 */
1554 goog.ui.Dialog.ButtonSet.createOkCancel = function() {
1555 return new goog.ui.Dialog.ButtonSet()
1556 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.OK, true)
1557 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.CANCEL, false, true);
1558 };
1559
1560
1561 /**
1562 * Creates a new ButtonSet with 'Yes' (default) and 'No' buttons.
1563 * @return {!goog.ui.Dialog.ButtonSet} The created ButtonSet.
1564 */
1565 goog.ui.Dialog.ButtonSet.createYesNo = function() {
1566 return new goog.ui.Dialog.ButtonSet()
1567 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.YES, true)
1568 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.NO, false, true);
1569 };
1570
1571
1572 /**
1573 * Creates a new ButtonSet with 'Yes', 'No' (default), and 'Cancel' buttons.
1574 * @return {!goog.ui.Dialog.ButtonSet} The created ButtonSet.
1575 */
1576 goog.ui.Dialog.ButtonSet.createYesNoCancel = function() {
1577 return new goog.ui.Dialog.ButtonSet()
1578 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.YES)
1579 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.NO, true)
1580 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.CANCEL, false, true);
1581 };
1582
1583
1584 /**
1585 * Creates a new ButtonSet with 'Continue', 'Save', and 'Cancel' (default)
1586 * buttons.
1587 * @return {!goog.ui.Dialog.ButtonSet} The created ButtonSet.
1588 */
1589 goog.ui.Dialog.ButtonSet.createContinueSaveCancel = function() {
1590 return new goog.ui.Dialog.ButtonSet()
1591 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.CONTINUE)
1592 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.SAVE)
1593 .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.CANCEL, true, true);
1594 };
1595
1596
1597 // TODO(user): These shared instances should be phased out.
1598 (function() {
1599 if (typeof document != 'undefined') {
1600 /** @deprecated Use goog.ui.Dialog.ButtonSet#createOk. */
1601 goog.ui.Dialog.ButtonSet.OK = goog.ui.Dialog.ButtonSet.createOk();
1602
1603 /** @deprecated Use goog.ui.Dialog.ButtonSet#createOkCancel. */
1604 goog.ui.Dialog.ButtonSet.OK_CANCEL =
1605 goog.ui.Dialog.ButtonSet.createOkCancel();
1606
1607 /** @deprecated Use goog.ui.Dialog.ButtonSet#createYesNo. */
1608 goog.ui.Dialog.ButtonSet.YES_NO = goog.ui.Dialog.ButtonSet.createYesNo();
1609
1610 /** @deprecated Use goog.ui.Dialog.ButtonSet#createYesNoCancel. */
1611 goog.ui.Dialog.ButtonSet.YES_NO_CANCEL =
1612 goog.ui.Dialog.ButtonSet.createYesNoCancel();
1613
1614 /** @deprecated Use goog.ui.Dialog.ButtonSet#createContinueSaveCancel. */
1615 goog.ui.Dialog.ButtonSet.CONTINUE_SAVE_CANCEL =
1616 goog.ui.Dialog.ButtonSet.createContinueSaveCancel();
1617 }
1618 })();