2015-06-19 09:36:51 -07:00
|
|
|
<!--
|
|
|
|
@license
|
|
|
|
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
|
|
|
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
|
|
|
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
|
|
|
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
|
|
|
Code distributed by Google as part of the polymer project is also
|
|
|
|
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
|
|
|
-->
|
|
|
|
|
|
|
|
<link rel="import" href="../polymer/polymer.html">
|
|
|
|
<link rel="import" href="../iron-overlay-behavior/iron-overlay-behavior.html">
|
2015-12-14 08:43:03 -07:00
|
|
|
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
|
|
|
|
<link rel="import" href="../paper-styles/default-theme.html">
|
|
|
|
<link rel="import" href="../paper-styles/typography.html">
|
|
|
|
<link rel="import" href="../paper-styles/shadow.html">
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
/**
|
2015-12-14 08:43:03 -07:00
|
|
|
Use `Polymer.PaperDialogBehavior` and `paper-dialog-shared-styles.html` to implement a Material Design
|
2015-06-19 09:36:51 -07:00
|
|
|
dialog.
|
|
|
|
|
|
|
|
For example, if `<paper-dialog-impl>` implements this behavior:
|
|
|
|
|
|
|
|
<paper-dialog-impl>
|
|
|
|
<h2>Header</h2>
|
|
|
|
<div>Dialog body</div>
|
|
|
|
<div class="buttons">
|
|
|
|
<paper-button dialog-dismiss>Cancel</paper-button>
|
|
|
|
<paper-button dialog-confirm>Accept</paper-button>
|
|
|
|
</div>
|
|
|
|
</paper-dialog-impl>
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
`paper-dialog-shared-styles.html` provide styles for a header, content area, and an action area for buttons.
|
2015-06-19 09:36:51 -07:00
|
|
|
Use the `<h2>` tag for the header and the `buttons` class for the action area. You can use the
|
|
|
|
`paper-dialog-scrollable` element (in its own repository) if you need a scrolling content area.
|
|
|
|
|
|
|
|
Use the `dialog-dismiss` and `dialog-confirm` attributes on interactive controls to close the
|
|
|
|
dialog. If the user dismisses the dialog with `dialog-confirm`, the `closingReason` will update
|
|
|
|
to include `confirmed: true`.
|
|
|
|
|
|
|
|
### Styling
|
|
|
|
|
|
|
|
The following custom properties and mixins are available for styling.
|
|
|
|
|
|
|
|
Custom property | Description | Default
|
|
|
|
----------------|-------------|----------
|
|
|
|
`--paper-dialog-background-color` | Dialog background color | `--primary-background-color`
|
|
|
|
`--paper-dialog-color` | Dialog foreground color | `--primary-text-color`
|
|
|
|
`--paper-dialog` | Mixin applied to the dialog | `{}`
|
|
|
|
`--paper-dialog-title` | Mixin applied to the title (`<h2>`) element | `{}`
|
|
|
|
`--paper-dialog-button-color` | Button area foreground color | `--default-primary-color`
|
|
|
|
|
|
|
|
### Accessibility
|
|
|
|
|
|
|
|
This element has `role="dialog"` by default. Depending on the context, it may be more appropriate
|
2015-06-25 18:13:51 -07:00
|
|
|
to override this attribute with `role="alertdialog"`.
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
|
|
If `modal` is set, the element will set `aria-modal` and prevent the focus from exiting the element.
|
|
|
|
It will also ensure that focus remains in the dialog.
|
|
|
|
|
|
|
|
The `aria-labelledby` attribute will be set to the header element, if one exists.
|
|
|
|
|
|
|
|
@hero hero.svg
|
|
|
|
@demo demo/index.html
|
|
|
|
@polymerBehavior Polymer.PaperDialogBehavior
|
|
|
|
*/
|
|
|
|
|
|
|
|
Polymer.PaperDialogBehaviorImpl = {
|
|
|
|
|
|
|
|
hostAttributes: {
|
|
|
|
'role': 'dialog',
|
|
|
|
'tabindex': '-1'
|
|
|
|
},
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
|
|
|
|
/**
|
2016-02-10 12:16:48 -07:00
|
|
|
* If `modal` is true, this implies `no-cancel-on-outside-click`, `no-cancel-on-esc-key` and `with-backdrop`.
|
2015-06-19 09:36:51 -07:00
|
|
|
*/
|
|
|
|
modal: {
|
|
|
|
type: Boolean,
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-02-10 12:16:48 -07:00
|
|
|
observers: [
|
|
|
|
'_modalChanged(modal, _readied)'
|
|
|
|
],
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
listeners: {
|
2016-02-10 12:16:48 -07:00
|
|
|
'tap': '_onDialogClick'
|
|
|
|
},
|
|
|
|
|
|
|
|
ready: function () {
|
|
|
|
// Only now these properties can be read.
|
|
|
|
this.__prevNoCancelOnOutsideClick = this.noCancelOnOutsideClick;
|
|
|
|
this.__prevNoCancelOnEscKey = this.noCancelOnEscKey;
|
|
|
|
this.__prevWithBackdrop = this.withBackdrop;
|
2015-06-19 09:36:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
attached: function() {
|
2016-01-29 19:43:11 -07:00
|
|
|
// this._observer is used by iron-overlay-behavior
|
|
|
|
this._ariaObserver = Polymer.dom(this).observeNodes(this._updateAriaLabelledBy);
|
2015-06-19 09:36:51 -07:00
|
|
|
this._updateAriaLabelledBy();
|
|
|
|
},
|
|
|
|
|
|
|
|
detached: function() {
|
2016-01-29 19:43:11 -07:00
|
|
|
Polymer.dom(this).unobserveNodes(this._ariaObserver);
|
2015-06-19 09:36:51 -07:00
|
|
|
},
|
|
|
|
|
2016-02-10 12:16:48 -07:00
|
|
|
_modalChanged: function(modal, readied) {
|
|
|
|
if (modal) {
|
2015-06-19 09:36:51 -07:00
|
|
|
this.setAttribute('aria-modal', 'true');
|
|
|
|
} else {
|
|
|
|
this.setAttribute('aria-modal', 'false');
|
|
|
|
}
|
2016-02-10 12:16:48 -07:00
|
|
|
|
|
|
|
// modal implies noCancelOnOutsideClick, noCancelOnEscKey and withBackdrop.
|
|
|
|
// We need to wait for the element to be ready before we can read the
|
|
|
|
// properties values.
|
|
|
|
if (!readied) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modal) {
|
|
|
|
this.__prevNoCancelOnOutsideClick = this.noCancelOnOutsideClick;
|
|
|
|
this.__prevNoCancelOnEscKey = this.noCancelOnEscKey;
|
|
|
|
this.__prevWithBackdrop = this.withBackdrop;
|
2015-06-19 09:36:51 -07:00
|
|
|
this.noCancelOnOutsideClick = true;
|
2016-02-10 12:16:48 -07:00
|
|
|
this.noCancelOnEscKey = true;
|
2015-06-19 09:36:51 -07:00
|
|
|
this.withBackdrop = true;
|
2016-02-10 12:16:48 -07:00
|
|
|
} else {
|
|
|
|
// If the value was changed to false, let it false.
|
|
|
|
this.noCancelOnOutsideClick = this.noCancelOnOutsideClick &&
|
|
|
|
this.__prevNoCancelOnOutsideClick;
|
|
|
|
this.noCancelOnEscKey = this.noCancelOnEscKey &&
|
|
|
|
this.__prevNoCancelOnEscKey;
|
|
|
|
this.withBackdrop = this.withBackdrop && this.__prevWithBackdrop;
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateAriaLabelledBy: function() {
|
|
|
|
var header = Polymer.dom(this).querySelector('h2');
|
|
|
|
if (!header) {
|
|
|
|
this.removeAttribute('aria-labelledby');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var headerId = header.getAttribute('id');
|
|
|
|
if (headerId && this.getAttribute('aria-labelledby') === headerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// set aria-describedBy to the header element
|
|
|
|
var labelledById;
|
|
|
|
if (headerId) {
|
|
|
|
labelledById = headerId;
|
|
|
|
} else {
|
|
|
|
labelledById = 'paper-dialog-header-' + new Date().getUTCMilliseconds();
|
|
|
|
header.setAttribute('id', labelledById);
|
|
|
|
}
|
|
|
|
this.setAttribute('aria-labelledby', labelledById);
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateClosingReasonConfirmed: function(confirmed) {
|
|
|
|
this.closingReason = this.closingReason || {};
|
|
|
|
this.closingReason.confirmed = confirmed;
|
|
|
|
},
|
|
|
|
|
2016-02-10 12:16:48 -07:00
|
|
|
/**
|
|
|
|
* Will dismiss the dialog if user clicked on an element with dialog-dismiss
|
|
|
|
* or dialog-confirm attribute.
|
|
|
|
*/
|
2015-06-19 09:36:51 -07:00
|
|
|
_onDialogClick: function(event) {
|
2016-02-10 12:16:48 -07:00
|
|
|
// Search for the element with dialog-confirm or dialog-dismiss,
|
|
|
|
// from the root target until this (excluded).
|
|
|
|
var path = Polymer.dom(event).path;
|
|
|
|
for (var i = 0; i < path.indexOf(this); i++) {
|
|
|
|
var target = path[i];
|
|
|
|
if (target.hasAttribute && (target.hasAttribute('dialog-dismiss') || target.hasAttribute('dialog-confirm'))) {
|
|
|
|
this._updateClosingReasonConfirmed(target.hasAttribute('dialog-confirm'));
|
|
|
|
this.close();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @polymerBehavior */
|
|
|
|
Polymer.PaperDialogBehavior = [Polymer.IronOverlayBehavior, Polymer.PaperDialogBehaviorImpl];
|
|
|
|
|
|
|
|
</script>
|