@shortfuse/materialdesignweb 0.2.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.browserslistrc +2 -1
- package/.eslintrc.json +188 -30
- package/.stylelintrc.json +643 -2
- package/.vscode/launch.json +20 -5
- package/.vscode/settings.json +3 -0
- package/CHANGELOG.md +36 -0
- package/README.md +82 -2
- package/adapters/datatable/column.js +176 -0
- package/adapters/datatable/index.js +253 -437
- package/adapters/dom/index.js +586 -0
- package/adapters/list/index.js +36 -113
- package/adapters/search/index.js +153 -180
- package/components/appbar/_spec.scss +165 -0
- package/components/appbar/_theme.scss +0 -0
- package/components/appbar/index.scss +2 -0
- package/components/banner/_spec.scss +83 -0
- package/components/banner/_theme.scss +0 -0
- package/components/banner/index.scss +2 -0
- package/components/bottomnav/README.md +4 -4
- package/components/bottomnav/_spec.scss +149 -0
- package/components/bottomnav/_theme.scss +0 -0
- package/components/bottomnav/index.js +100 -120
- package/components/bottomnav/index.scss +2 -0
- package/components/bottomnav/item.js +88 -0
- package/components/button/README.md +16 -22
- package/components/button/_spec.scss +162 -0
- package/components/button/_theme.scss +42 -0
- package/components/button/index.eta +32 -0
- package/components/button/index.js +37 -48
- package/components/button/index.pug +18 -0
- package/components/button/index.scss +2 -0
- package/components/card/_spec.scss +241 -0
- package/components/card/_theme.scss +0 -0
- package/components/card/index.scss +2 -0
- package/components/chip/_spec.scss +111 -0
- package/components/chip/_theme.scss +105 -0
- package/components/chip/index.js +23 -0
- package/components/chip/index.scss +2 -0
- package/components/chip/item.js +20 -0
- package/components/datatable/_spec.scss +225 -0
- package/components/datatable/_theme.scss +128 -0
- package/components/datatable/cell.js +44 -0
- package/components/datatable/columnheader.js +46 -0
- package/components/datatable/index.js +339 -443
- package/components/datatable/index.scss +2 -0
- package/components/datatable/row.js +48 -0
- package/components/datatable/rowheader.js +18 -0
- package/components/dialog/_spec.scss +203 -0
- package/components/dialog/_theme.scss +7 -0
- package/components/dialog/index.js +512 -437
- package/components/dialog/index.scss +2 -0
- package/components/divider/_spec.scss +11 -0
- package/components/divider/_theme.scss +0 -0
- package/components/divider/index.scss +2 -0
- package/components/elevation/_spec.scss +9 -0
- package/components/elevation/_theme.scss +0 -0
- package/components/elevation/index.scss +2 -0
- package/components/fab/{style.scss → _spec.scss} +104 -79
- package/components/fab/_theme.scss +0 -0
- package/components/fab/index.js +85 -79
- package/components/fab/index.scss +2 -0
- package/components/grid/_spec.scss +169 -0
- package/components/grid/_theme.scss +0 -0
- package/components/grid/index.scss +2 -0
- package/components/layout/_mixins.scss +11 -0
- package/components/layout/_spec.scss +916 -0
- package/components/layout/_theme.scss +19 -0
- package/components/layout/index.js +454 -0
- package/components/layout/index.scss +2 -0
- package/components/list/_spec.scss +363 -0
- package/components/list/_theme.scss +102 -0
- package/components/list/content.js +106 -0
- package/components/list/index.js +234 -79
- package/components/list/index.scss +2 -0
- package/components/list/item.js +167 -0
- package/components/list/secondary.js +45 -0
- package/components/menu/_spec.scss +329 -0
- package/components/menu/_theme.scss +0 -0
- package/components/menu/index.js +636 -651
- package/components/menu/index.scss +2 -0
- package/components/menu/item.js +231 -0
- package/components/progress/_spec.scss +156 -0
- package/components/progress/_theme.scss +0 -0
- package/components/progress/index.js +29 -13
- package/components/progress/index.scss +2 -0
- package/components/selection/_spec.scss +376 -0
- package/components/selection/_theme.scss +134 -0
- package/components/selection/index.eta +60 -0
- package/components/selection/index.js +70 -0
- package/components/selection/index.pug +30 -0
- package/components/selection/index.scss +2 -0
- package/components/selection/input.js +54 -0
- package/components/selection/radiogroup.js +40 -0
- package/components/slider/{style.scss → _spec.scss} +31 -34
- package/components/slider/_theme.scss +0 -0
- package/components/slider/index.scss +2 -0
- package/components/snackbar/_spec.scss +150 -0
- package/components/snackbar/_theme.scss +0 -0
- package/components/snackbar/index.js +293 -206
- package/components/snackbar/index.scss +2 -0
- package/components/tab/_spec.scss +220 -0
- package/components/tab/_theme.scss +0 -0
- package/components/tab/content.js +210 -0
- package/components/tab/index.js +229 -213
- package/components/tab/index.scss +2 -0
- package/components/tab/item.js +88 -0
- package/components/tab/list.js +196 -0
- package/components/tab/panel.js +54 -0
- package/components/textfield/README.md +4 -4
- package/components/textfield/_spec.scss +763 -0
- package/components/textfield/_theme.scss +264 -0
- package/components/textfield/index.eta +74 -0
- package/components/textfield/index.js +132 -138
- package/components/textfield/index.pug +30 -0
- package/components/textfield/index.scss +2 -0
- package/components/tooltip/_spec.scss +185 -0
- package/components/tooltip/_theme.scss +0 -0
- package/components/tooltip/index.scss +2 -0
- package/components/type/_spec.scss +227 -0
- package/components/type/_theme.scss +0 -0
- package/components/type/index.scss +2 -0
- package/core/_breakpoint.scss +189 -0
- package/core/_elevation.scss +78 -0
- package/core/_length.scss +8 -0
- package/core/_motion.scss +31 -0
- package/core/_platform.scss +12 -0
- package/core/_type.scss +128 -0
- package/core/aria/attributes.js +141 -0
- package/core/aria/button.js +49 -0
- package/core/aria/keyboard.js +92 -0
- package/core/aria/rovingtabindex.js +175 -0
- package/core/aria/tab.js +59 -0
- package/core/document/index.js +39 -0
- package/core/dom.js +180 -0
- package/core/overlay/_spec.scss +28 -0
- package/core/overlay/_theme.scss +147 -0
- package/core/overlay/index.js +95 -0
- package/core/overlay/index.scss +2 -0
- package/core/ripple/_spec.scss +196 -0
- package/core/ripple/_theme.scss +20 -0
- package/core/ripple/index.js +286 -0
- package/core/ripple/index.scss +2 -0
- package/core/theme/_aliases.scss +15 -0
- package/core/theme/_config.scss +8 -0
- package/core/theme/_functions.scss +22 -0
- package/{components/theming/palettes.scss → core/theme/_palettes.scss} +173 -151
- package/core/theme/_spec.scss +0 -0
- package/core/theme/_theme.scss +268 -0
- package/core/theme/index.js +50 -0
- package/core/theme/index.scss +4 -0
- package/core/throttler.js +42 -0
- package/core/transition/index.js +465 -0
- package/docs/_flex.scss +28 -0
- package/docs/_menuoptions.js +183 -0
- package/docs/_partials/_androidnavbar.eta +5 -0
- package/docs/_partials/_androidstatusbar.eta +13 -0
- package/docs/_partials/_appbar.eta +27 -0
- package/docs/_partials/_buttontest.eta +31 -0
- package/docs/_partials/_header.eta +146 -0
- package/docs/_partials/_navlistitem.eta +16 -0
- package/docs/_partials/_target.eta +1 -0
- package/docs/_sample-utils.js +88 -0
- package/docs/{src/storage.js → _storage.js} +0 -0
- package/docs/docs.scss +331 -0
- package/docs/framework.scss +26 -0
- package/docs/index.eta +12 -0
- package/docs/index.js +7 -0
- package/docs/pages/appbar.eta +108 -0
- package/docs/pages/appbar.js +0 -0
- package/docs/pages/bottomnav.eta +188 -0
- package/docs/pages/bottomnav.js +118 -0
- package/docs/pages/button.eta +124 -0
- package/docs/pages/button.js +224 -0
- package/docs/pages/card.eta +90 -0
- package/docs/pages/card.js +175 -0
- package/docs/pages/chip.eta +122 -0
- package/docs/pages/chip.js +80 -0
- package/docs/pages/color.eta +143 -0
- package/docs/pages/color.js +261 -0
- package/docs/pages/datatable.eta +323 -0
- package/docs/pages/datatable.js +160 -0
- package/docs/pages/dialog.eta +184 -0
- package/docs/{src/components → pages}/dialog.js +35 -48
- package/docs/pages/dom.eta +26 -0
- package/docs/pages/dom.js +140 -0
- package/docs/pages/elevation.eta +35 -0
- package/docs/pages/elevation.js +0 -0
- package/docs/pages/fab.eta +99 -0
- package/docs/{src/components → pages}/fab.js +6 -13
- package/docs/pages/grid.eta +135 -0
- package/docs/pages/grid.js +128 -0
- package/docs/pages/layout.eta +8 -0
- package/docs/pages/layout.js +0 -0
- package/docs/pages/list.eta +465 -0
- package/docs/pages/list.js +8 -0
- package/docs/pages/menu.eta +274 -0
- package/docs/{src/components → pages}/menu.js +26 -42
- package/docs/pages/overlay.eta +69 -0
- package/docs/pages/overlay.js +3 -0
- package/docs/pages/progress.eta +23 -0
- package/docs/{src/components → pages}/progress.js +2 -4
- package/docs/pages/ripple.eta +27 -0
- package/docs/pages/ripple.js +3 -0
- package/docs/pages/search.eta +242 -0
- package/docs/pages/search.js +226 -0
- package/docs/pages/selection.eta +107 -0
- package/docs/pages/selection.js +12 -0
- package/docs/pages/slider.eta +23 -0
- package/docs/pages/slider.js +0 -0
- package/docs/pages/snackbar.eta +83 -0
- package/docs/{src/components → pages}/snackbar.js +31 -36
- package/docs/pages/tab.eta +407 -0
- package/docs/pages/tab.js +152 -0
- package/docs/pages/textfield.eta +487 -0
- package/docs/{src/components → pages}/textfield.js +41 -45
- package/docs/pages/tooltip.eta +92 -0
- package/docs/pages/tooltip.js +0 -0
- package/docs/pages/transition.eta +117 -0
- package/docs/pages/transition.js +52 -0
- package/docs/pages/type.eta +31 -0
- package/docs/pages/type.js +0 -0
- package/docs/postrender.js +41 -0
- package/docs/prerender.js +16 -0
- package/docs/pwa/_dialogs.eta +143 -0
- package/docs/pwa/_menus.eta +16 -0
- package/docs/pwa/pwa-prerender.js +3 -0
- package/docs/pwa/pwa.eta +478 -0
- package/docs/pwa/pwa.js +298 -0
- package/docs/pwa/pwa.scss +31 -0
- package/docs/themes/theme-colored.scss +15 -0
- package/docs/themes/theme-default.scss +3 -0
- package/index.scss +27 -0
- package/jsconfig.json +8 -2
- package/package.json +54 -27
- package/scripts/deploy-docs.sh +9 -0
- package/templates/index.eta +2 -0
- package/templates/index.pug +3 -0
- package/tsconfig.json +16 -0
- package/utils/function.js +3 -0
- package/webpack.config.js +224 -68
- package/_index.scss +0 -4
- package/components/all-components.scss +0 -21
- package/components/bottomnav/style.scss +0 -190
- package/components/bottomnav/theming.scss +0 -76
- package/components/button/style.scss +0 -315
- package/components/button/theming.scss +0 -134
- package/components/card/style.scss +0 -175
- package/components/card/theming.scss +0 -43
- package/components/common/dom.js +0 -51
- package/components/common/functions.scss +0 -174
- package/components/common/mixins.scss +0 -122
- package/components/common/motion.scss +0 -36
- package/components/common/type.scss +0 -104
- package/components/common/variables.scss +0 -46
- package/components/datatable/style.scss +0 -257
- package/components/datatable/theming.scss +0 -119
- package/components/dialog/style.scss +0 -159
- package/components/dialog/theming.scss +0 -29
- package/components/divider/style.scss +0 -7
- package/components/divider/theming.scss +0 -20
- package/components/elevation/style.scss +0 -32
- package/components/layout/style.scss +0 -223
- package/components/list/style.scss +0 -358
- package/components/list/theming.scss +0 -83
- package/components/menu/style.scss +0 -280
- package/components/menu/theming.scss +0 -80
- package/components/navdrawer/index.js +0 -200
- package/components/navdrawer/style.scss +0 -595
- package/components/navdrawer/theming.scss +0 -62
- package/components/progress/style.scss +0 -136
- package/components/ripple/index.js +0 -63
- package/components/ripple/ripple.scss +0 -122
- package/components/selection/style.scss +0 -320
- package/components/selection/theming.scss +0 -98
- package/components/snackbar/style.scss +0 -212
- package/components/switch/style.scss +0 -3
- package/components/tab/style.scss +0 -275
- package/components/tab/theming.scss +0 -34
- package/components/template/theming.scss +0 -31
- package/components/textfield/style.scss +0 -795
- package/components/textfield/theming.scss +0 -256
- package/components/theming/globals.scss +0 -25
- package/components/theming/theming.scss +0 -559
- package/components/toolbar/style.scss +0 -190
- package/components/toolbar/theming.scss +0 -32
- package/components/tooltip/style.scss +0 -135
- package/components/type/style.scss +0 -167
- package/components/type/theming.scss +0 -25
- package/docs/bottomnav.html +0 -1
- package/docs/bottomnav.min.js +0 -2
- package/docs/bottomnav.min.js.map +0 -1
- package/docs/button.html +0 -1
- package/docs/button.min.js +0 -2
- package/docs/button.min.js.map +0 -1
- package/docs/card.html +0 -1
- package/docs/card.min.js +0 -2
- package/docs/card.min.js.map +0 -1
- package/docs/components.min.css +0 -1
- package/docs/components.min.js +0 -2
- package/docs/components.min.js.map +0 -1
- package/docs/datatable.html +0 -1
- package/docs/datatable.min.js +0 -2
- package/docs/datatable.min.js.map +0 -1
- package/docs/dialog.html +0 -1
- package/docs/dialog.min.js +0 -2
- package/docs/dialog.min.js.map +0 -1
- package/docs/docs.min.css +0 -1
- package/docs/docs.min.js +0 -2
- package/docs/docs.min.js.map +0 -1
- package/docs/elevation.html +0 -1
- package/docs/elevation.min.js +0 -2
- package/docs/elevation.min.js.map +0 -1
- package/docs/fab.html +0 -1
- package/docs/fab.min.js +0 -2
- package/docs/fab.min.js.map +0 -1
- package/docs/index.html +0 -1
- package/docs/index.min.js +0 -2
- package/docs/index.min.js.map +0 -1
- package/docs/layout.html +0 -1
- package/docs/layout.min.js +0 -2
- package/docs/layout.min.js.map +0 -1
- package/docs/list.html +0 -1
- package/docs/list.min.js +0 -2
- package/docs/list.min.js.map +0 -1
- package/docs/menu.html +0 -1
- package/docs/menu.min.js +0 -2
- package/docs/menu.min.js.map +0 -1
- package/docs/navdrawer.html +0 -1
- package/docs/navdrawer.min.js +0 -2
- package/docs/navdrawer.min.js.map +0 -1
- package/docs/prerender.min.js +0 -2
- package/docs/prerender.min.js.map +0 -1
- package/docs/progress.html +0 -1
- package/docs/progress.min.js +0 -2
- package/docs/progress.min.js.map +0 -1
- package/docs/search.html +0 -1
- package/docs/search.min.js +0 -2
- package/docs/search.min.js.map +0 -1
- package/docs/selection.html +0 -1
- package/docs/selection.min.js +0 -2
- package/docs/selection.min.js.map +0 -1
- package/docs/slider.html +0 -1
- package/docs/slider.min.js +0 -2
- package/docs/slider.min.js.map +0 -1
- package/docs/snackbar.html +0 -1
- package/docs/snackbar.min.js +0 -2
- package/docs/snackbar.min.js.map +0 -1
- package/docs/src/components/bottomnav.js +0 -16
- package/docs/src/components/bottomnav.pug +0 -112
- package/docs/src/components/button.js +0 -156
- package/docs/src/components/button.pug +0 -194
- package/docs/src/components/card.js +0 -136
- package/docs/src/components/card.pug +0 -133
- package/docs/src/components/datatable.js +0 -183
- package/docs/src/components/datatable.pug +0 -324
- package/docs/src/components/dialog.pug +0 -138
- package/docs/src/components/elevation.js +0 -3
- package/docs/src/components/elevation.pug +0 -17
- package/docs/src/components/fab.pug +0 -84
- package/docs/src/components/layout.js +0 -116
- package/docs/src/components/layout.pug +0 -104
- package/docs/src/components/list.js +0 -15
- package/docs/src/components/list.pug +0 -293
- package/docs/src/components/menu.pug +0 -292
- package/docs/src/components/navdrawer.js +0 -112
- package/docs/src/components/navdrawer.pug +0 -113
- package/docs/src/components/progress.pug +0 -17
- package/docs/src/components/search.js +0 -206
- package/docs/src/components/search.pug +0 -149
- package/docs/src/components/selection.js +0 -6
- package/docs/src/components/selection.pug +0 -116
- package/docs/src/components/slider.js +0 -3
- package/docs/src/components/slider.pug +0 -19
- package/docs/src/components/snackbar.pug +0 -145
- package/docs/src/components/tab.js +0 -137
- package/docs/src/components/tab.pug +0 -329
- package/docs/src/components/textfield.pug +0 -416
- package/docs/src/components/toolbar.js +0 -6
- package/docs/src/components/toolbar.pug +0 -86
- package/docs/src/components/tooltip.js +0 -6
- package/docs/src/components/tooltip.pug +0 -76
- package/docs/src/components/type.js +0 -6
- package/docs/src/components/type.pug +0 -34
- package/docs/src/components.scss +0 -1
- package/docs/src/docs.scss +0 -284
- package/docs/src/index.js +0 -3
- package/docs/src/index.pug +0 -6
- package/docs/src/menuoptions.js +0 -136
- package/docs/src/mixins.pug +0 -139
- package/docs/src/prerender.js +0 -26
- package/docs/src/sample-utils.js +0 -108
- package/docs/src/targetHandler.js +0 -50
- package/docs/src/theming.ie11.scss +0 -18
- package/docs/src/theming.scss +0 -18
- package/docs/tab.html +0 -1
- package/docs/tab.min.js +0 -2
- package/docs/tab.min.js.map +0 -1
- package/docs/textfield.html +0 -2
- package/docs/textfield.min.js +0 -2
- package/docs/textfield.min.js.map +0 -1
- package/docs/theming.ie11.min.css +0 -1
- package/docs/theming.ie11.min.js +0 -2
- package/docs/theming.ie11.min.js.map +0 -1
- package/docs/theming.min.css +0 -1
- package/docs/theming.min.js +0 -2
- package/docs/theming.min.js.map +0 -1
- package/docs/toolbar.html +0 -1
- package/docs/toolbar.min.js +0 -2
- package/docs/toolbar.min.js.map +0 -1
- package/docs/tooltip.html +0 -1
- package/docs/tooltip.min.js +0 -2
- package/docs/tooltip.min.js.map +0 -1
- package/docs/type.html +0 -1
- package/docs/type.min.js +0 -2
- package/docs/type.min.js.map +0 -1
- package/index.js +0 -16
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
// https://www.w3.org/TR/wai-aria-practices/#dialog_modal
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
dispatchDomEvent,
|
|
5
|
+
getChildElementByClass,
|
|
6
|
+
setTextNode,
|
|
7
|
+
} from '../../core/dom.js';
|
|
8
|
+
import * as Button from '../button/index.js';
|
|
5
9
|
|
|
6
10
|
class DialogStack {
|
|
7
11
|
/**
|
|
8
12
|
* @param {Element} element
|
|
9
|
-
* @param {Element} previousFocus
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {
|
|
13
|
+
* @param {?Element} [previousFocus]
|
|
14
|
+
* @param {{hash:string}} [state]
|
|
15
|
+
* @param {{hash:string}} [previousState]
|
|
12
16
|
*/
|
|
13
17
|
constructor(element, previousFocus, state, previousState) {
|
|
14
18
|
this.element = element;
|
|
@@ -20,507 +24,578 @@ class DialogStack {
|
|
|
20
24
|
|
|
21
25
|
/** @type {DialogStack[]} */
|
|
22
26
|
const OPEN_DIALOGS = [];
|
|
23
|
-
class Dialog {
|
|
24
|
-
static get CANCEL_EVENT() {
|
|
25
|
-
return 'mdw:dialog-cancel';
|
|
26
|
-
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
export const CANCEL_EVENT = 'mdw:dialog-cancel';
|
|
29
|
+
export const CONFIRM_EVENT = 'mdw:dialog-confirm';
|
|
30
|
+
export const CUSTOM_EVENT = 'mdw:dialog-custom';
|
|
31
|
+
export const DISMISS_EVENT = 'mdw:dialog-dismiss';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {TransitionEvent} event
|
|
35
|
+
* @return {void}
|
|
36
|
+
*/
|
|
37
|
+
export function onTransitionEnd(event) {
|
|
38
|
+
if (event.propertyName !== 'opacity') {
|
|
39
|
+
return;
|
|
30
40
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return
|
|
41
|
+
const dialogElement = /** @type {HTMLElement} */ (event.currentTarget);
|
|
42
|
+
if (dialogElement.getAttribute('aria-hidden') !== 'true') {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const popupElement = /** @type {HTMLElement} */ (dialogElement.getElementsByClassName('mdw-dialog__popup')[0]);
|
|
46
|
+
if (!popupElement) {
|
|
47
|
+
return;
|
|
34
48
|
}
|
|
49
|
+
popupElement.style.removeProperty('display');
|
|
50
|
+
}
|
|
35
51
|
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
/**
|
|
53
|
+
* @param {Element} dialogElement
|
|
54
|
+
* @return {void}
|
|
55
|
+
*/
|
|
56
|
+
export function setupARIA(dialogElement) {
|
|
57
|
+
if (dialogElement.hasAttribute('mdw-no-aria')) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
dialogElement.setAttribute('role', 'dialog');
|
|
61
|
+
dialogElement.setAttribute('aria-modal', 'true');
|
|
62
|
+
if (!dialogElement.hasAttribute('aria-hidden')) {
|
|
63
|
+
dialogElement.setAttribute('aria-hidden', 'true');
|
|
64
|
+
}
|
|
65
|
+
const popupElement = dialogElement.getElementsByClassName('mdw-dialog__popup')[0];
|
|
66
|
+
if (!popupElement) {
|
|
67
|
+
return;
|
|
38
68
|
}
|
|
39
69
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
static attach(element) {
|
|
45
|
-
let dialogCloser = getChildElementByClass(element, 'mdw-dialog__close');
|
|
46
|
-
if (!dialogCloser) {
|
|
47
|
-
dialogCloser = document.createElement('div');
|
|
48
|
-
dialogCloser.classList.add('mdw-dialog__close');
|
|
49
|
-
if (element.firstChild) {
|
|
50
|
-
element.insertBefore(dialogCloser, element.firstChild);
|
|
51
|
-
} else {
|
|
52
|
-
element.appendChild(dialogCloser);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
dialogCloser.addEventListener('click', Dialog.onCancelClick);
|
|
56
|
-
const popup = getChildElementByClass(element, 'mdw-dialog__popup');
|
|
57
|
-
popup.addEventListener('keydown', Dialog.onKeyDown);
|
|
58
|
-
const buttons = popup.querySelectorAll('.mdw-dialog__button-area .mdw-button');
|
|
59
|
-
let foundConfirmButton = false;
|
|
60
|
-
let foundCancelButton = false;
|
|
61
|
-
for (let i = 0; i < buttons.length; i += 1) {
|
|
62
|
-
const button = buttons.item(i);
|
|
63
|
-
Button.attach(button);
|
|
64
|
-
if (button.hasAttribute('mdw-custom')) {
|
|
65
|
-
button.addEventListener('click', Dialog.onCustomButtonClick);
|
|
66
|
-
} else if (!foundConfirmButton) {
|
|
67
|
-
button.addEventListener('click', Dialog.onConfirmClick);
|
|
68
|
-
foundConfirmButton = true;
|
|
69
|
-
} else if (!foundCancelButton) {
|
|
70
|
-
button.addEventListener('click', Dialog.onCancelClick);
|
|
71
|
-
foundCancelButton = true;
|
|
72
|
-
} else {
|
|
73
|
-
button.addEventListener('click', Dialog.onCustomButtonClick);
|
|
74
|
-
}
|
|
70
|
+
if (!popupElement.hasAttribute('aria-label') && !popupElement.hasAttribute('aria-labelledby')) {
|
|
71
|
+
const titleElement = dialogElement.getElementsByClassName('mdw-dialog__title')[0];
|
|
72
|
+
if (titleElement) {
|
|
73
|
+
// titleElement
|
|
75
74
|
}
|
|
76
75
|
}
|
|
76
|
+
}
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
78
|
+
/**
|
|
79
|
+
* @param {Element} dialogElement
|
|
80
|
+
* @return {boolean} handled
|
|
81
|
+
*/
|
|
82
|
+
export function hide(dialogElement) {
|
|
83
|
+
if (dialogElement.getAttribute('aria-hidden') === 'true') {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (!dispatchDomEvent(dialogElement, DISMISS_EVENT)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
dialogElement.setAttribute('aria-hidden', 'true');
|
|
90
|
+
// .mdw-dialog__popup hidden by transitionEnd event
|
|
91
|
+
let stackIndex = -1;
|
|
92
|
+
OPEN_DIALOGS.some((stack, index) => {
|
|
93
|
+
if (stack.element === dialogElement) {
|
|
94
|
+
stackIndex = index;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
});
|
|
99
|
+
if (stackIndex !== -1) {
|
|
100
|
+
const stack = OPEN_DIALOGS[stackIndex];
|
|
101
|
+
if (stack.previousFocus && stack.previousFocus instanceof HTMLElement && document.activeElement?.closest('.mdw-dialog') === dialogElement) {
|
|
102
|
+
// Only pop focus back when hiding a dialog with focus within itself.
|
|
103
|
+
try {
|
|
104
|
+
stack.previousFocus.focus();
|
|
105
|
+
} catch {
|
|
106
|
+
// Failed to focus
|
|
92
107
|
}
|
|
93
108
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
button.removeEventListener('click', Dialog.onConfirmClick);
|
|
99
|
-
button.removeEventListener('click', Dialog.onCancelClick);
|
|
100
|
-
button.removeEventListener('click', Dialog.onCustomButtonClick);
|
|
109
|
+
OPEN_DIALOGS.splice(stackIndex, 1);
|
|
110
|
+
if (stack.state && window.history && window.history.state // IE11 returns a cloned state object, not the original
|
|
111
|
+
&& stack.state.hash === window.history.state.hash) {
|
|
112
|
+
window.history.back();
|
|
101
113
|
}
|
|
102
114
|
}
|
|
115
|
+
if (!OPEN_DIALOGS.length) {
|
|
116
|
+
// eslint-disable-next-line no-use-before-define
|
|
117
|
+
window.removeEventListener('popstate', onPopState);
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
103
121
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
122
|
+
/**
|
|
123
|
+
* @param {Element} dialogElement
|
|
124
|
+
* @return {void}
|
|
125
|
+
*/
|
|
126
|
+
export function cancel(dialogElement) {
|
|
127
|
+
if (dispatchDomEvent(dialogElement, CANCEL_EVENT)) {
|
|
128
|
+
hide(dialogElement);
|
|
112
129
|
}
|
|
130
|
+
}
|
|
113
131
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
132
|
+
/**
|
|
133
|
+
* @param {Element} dialogElement
|
|
134
|
+
* @return {void}
|
|
135
|
+
*/
|
|
136
|
+
export function confirm(dialogElement) {
|
|
137
|
+
if (dispatchDomEvent(dialogElement, CONFIRM_EVENT)) {
|
|
138
|
+
hide(dialogElement);
|
|
122
139
|
}
|
|
140
|
+
}
|
|
123
141
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
142
|
+
/**
|
|
143
|
+
* @param {MouseEvent} event
|
|
144
|
+
* @return {void}
|
|
145
|
+
*/
|
|
146
|
+
export function onCancelClick(event) {
|
|
147
|
+
if (event && event.currentTarget instanceof HTMLAnchorElement) {
|
|
148
|
+
event.preventDefault();
|
|
132
149
|
}
|
|
150
|
+
const cancelElement = /** @type {HTMLElement} */ (event.currentTarget);
|
|
151
|
+
const dialogElement = cancelElement.closest('.mdw-dialog');
|
|
152
|
+
cancel(dialogElement);
|
|
153
|
+
}
|
|
133
154
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
155
|
+
/**
|
|
156
|
+
* @param {MouseEvent} event
|
|
157
|
+
* @return {void}
|
|
158
|
+
*/
|
|
159
|
+
export function onConfirmClick(event) {
|
|
160
|
+
if (event && event.currentTarget instanceof HTMLAnchorElement) {
|
|
161
|
+
event.preventDefault();
|
|
142
162
|
}
|
|
163
|
+
const button = /** @type {HTMLElement} */ (event.currentTarget);
|
|
164
|
+
const dialogElement = button.closest('.mdw-dialog');
|
|
165
|
+
confirm(dialogElement);
|
|
166
|
+
}
|
|
143
167
|
|
|
144
|
-
|
|
145
|
-
|
|
168
|
+
/**
|
|
169
|
+
* @param {MouseEvent} event
|
|
170
|
+
* @return {void}
|
|
171
|
+
*/
|
|
172
|
+
export function onCustomButtonClick(event) {
|
|
173
|
+
if (event && event.currentTarget instanceof HTMLAnchorElement) {
|
|
146
174
|
event.preventDefault();
|
|
147
|
-
const dialogElement = findElementParentByClassName(event.currentTarget, 'mdw-dialog');
|
|
148
|
-
Dialog.cancel(dialogElement);
|
|
149
175
|
}
|
|
176
|
+
const button = /** @type {HTMLElement} */ (event.currentTarget);
|
|
177
|
+
if (dispatchDomEvent(button, CUSTOM_EVENT)) {
|
|
178
|
+
const dialogElement = button.closest('.mdw-dialog');
|
|
179
|
+
hide(dialogElement);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
150
182
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
183
|
+
/**
|
|
184
|
+
* @param {KeyboardEvent} event
|
|
185
|
+
* @return {void}
|
|
186
|
+
*/
|
|
187
|
+
export function handleTabKeyPress(event) {
|
|
188
|
+
const popupElement = /** @type {Element} */ (event.currentTarget);
|
|
189
|
+
const focusableElements = popupElement.querySelectorAll([
|
|
190
|
+
'button:not(:disabled):not([tabindex="-1"])',
|
|
191
|
+
'[href]:not(:disabled):not([tabindex="-1"])',
|
|
192
|
+
'input:not(:disabled):not([tabindex="-1"])',
|
|
193
|
+
'select:not(:disabled):not([tabindex="-1"])',
|
|
194
|
+
'textarea:not(:disabled):not([tabindex="-1"])',
|
|
195
|
+
'[tabindex]:not([tabindex="-1"])'].join(', '));
|
|
196
|
+
let foundTarget = false;
|
|
197
|
+
let candidate = null;
|
|
198
|
+
for (const el of focusableElements) {
|
|
199
|
+
if (el === event.target) {
|
|
200
|
+
foundTarget = true;
|
|
201
|
+
if (event.shiftKey) {
|
|
202
|
+
break;
|
|
171
203
|
}
|
|
204
|
+
} else if (event.shiftKey) {
|
|
205
|
+
candidate = el;
|
|
206
|
+
} else if (foundTarget) {
|
|
207
|
+
candidate = el;
|
|
208
|
+
break;
|
|
172
209
|
}
|
|
173
210
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
211
|
+
if (!candidate) {
|
|
212
|
+
candidate = event.shiftKey ? focusableElements[focusableElements.length - 1] : focusableElements[0];
|
|
213
|
+
}
|
|
214
|
+
event.stopPropagation();
|
|
215
|
+
event.preventDefault();
|
|
216
|
+
if (candidate && candidate instanceof HTMLElement) {
|
|
217
|
+
try {
|
|
218
|
+
candidate.focus();
|
|
219
|
+
} catch {
|
|
220
|
+
// Failed to focus
|
|
182
221
|
}
|
|
183
222
|
}
|
|
223
|
+
}
|
|
184
224
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
225
|
+
/**
|
|
226
|
+
* @param {KeyboardEvent} event
|
|
227
|
+
* @return {void}
|
|
228
|
+
*/
|
|
229
|
+
export function onEscapeKeyDown(event) {
|
|
230
|
+
event.stopPropagation();
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
const popupElement = /** @type {HTMLElement} */ (event.currentTarget);
|
|
233
|
+
const dialogElement = popupElement.closest('.mdw-dialog');
|
|
234
|
+
cancel(dialogElement);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param {KeyboardEvent} event
|
|
239
|
+
* @return {void}
|
|
240
|
+
*/
|
|
241
|
+
export function onKeyDown(event) {
|
|
242
|
+
if (event.key === 'Tab') {
|
|
243
|
+
handleTabKeyPress(event);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (event.key === 'Escape' || event.key === 'Esc') {
|
|
247
|
+
onEscapeKeyDown(event);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {Element} dialogElement
|
|
253
|
+
* @return {string}
|
|
254
|
+
*/
|
|
255
|
+
export function getTitleText(dialogElement) {
|
|
256
|
+
let title = 'Dialog';
|
|
257
|
+
const titleElement = dialogElement.getElementsByClassName('mdw-dialog__title')[0];
|
|
258
|
+
if (titleElement) {
|
|
259
|
+
title = titleElement.textContent ?? '';
|
|
260
|
+
} else {
|
|
261
|
+
const bodyElement = dialogElement.getElementsByClassName('mdw-dialog__body')[0];
|
|
262
|
+
if (bodyElement) {
|
|
263
|
+
title = bodyElement.textContent ?? '';
|
|
192
264
|
}
|
|
193
265
|
}
|
|
266
|
+
return title;
|
|
267
|
+
}
|
|
194
268
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
269
|
+
/**
|
|
270
|
+
* @param {PopStateEvent} event
|
|
271
|
+
* @return {void}
|
|
272
|
+
*/
|
|
273
|
+
export function onPopState(event) {
|
|
274
|
+
if (!event.state) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const lastOpenDialog = OPEN_DIALOGS[OPEN_DIALOGS.length - 1];
|
|
278
|
+
if (!lastOpenDialog || !lastOpenDialog.previousState) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if ((lastOpenDialog.previousState === event.state) || Object.keys(event.state)
|
|
282
|
+
.every((key) => event.state[key] === lastOpenDialog.previousState[key])) {
|
|
283
|
+
if (dispatchDomEvent(lastOpenDialog.element, CANCEL_EVENT)) {
|
|
284
|
+
hide(lastOpenDialog.element);
|
|
204
285
|
} else {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
286
|
+
// Revert pop state by pushing state again
|
|
287
|
+
const title = getTitleText(lastOpenDialog.element);
|
|
288
|
+
window.history.pushState(lastOpenDialog.state, title);
|
|
209
289
|
}
|
|
210
|
-
return title;
|
|
211
290
|
}
|
|
291
|
+
}
|
|
212
292
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (!
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
dialogElement.setAttribute('mdw-hide', '');
|
|
225
|
-
let stackIndex = -1;
|
|
226
|
-
OPEN_DIALOGS.some((stack, index) => {
|
|
227
|
-
if (stack.element === dialogElement) {
|
|
228
|
-
stackIndex = index;
|
|
229
|
-
return true;
|
|
230
|
-
}
|
|
231
|
-
return false;
|
|
232
|
-
});
|
|
233
|
-
if (stackIndex !== -1) {
|
|
234
|
-
const stack = OPEN_DIALOGS[stackIndex];
|
|
235
|
-
if (stack.previousFocus) {
|
|
236
|
-
if (findElementParentByClassName(document.activeElement, 'mdw-dialog') === dialogElement) {
|
|
237
|
-
// Only pop focus back when hiding a dialog with focus within itself.
|
|
238
|
-
stack.previousFocus.focus();
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
OPEN_DIALOGS.splice(stackIndex, 1);
|
|
242
|
-
if (stack.state && window.history && window.history.state) {
|
|
243
|
-
// IE11 returns a cloned state object, not the original
|
|
244
|
-
if (stack.state.hash === window.history.state.hash) {
|
|
245
|
-
window.history.back();
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (!OPEN_DIALOGS.length) {
|
|
250
|
-
window.removeEventListener('popstate', Dialog.onPopState);
|
|
293
|
+
/**
|
|
294
|
+
* @param {Element} dialogElement
|
|
295
|
+
* @param {(DocumentFragment|string|null)} [content]
|
|
296
|
+
* @return {void}
|
|
297
|
+
*/
|
|
298
|
+
export function updateTitle(dialogElement, content) {
|
|
299
|
+
let title = dialogElement.getElementsByClassName('mdw-dialog__title')[0];
|
|
300
|
+
if (!content) {
|
|
301
|
+
if (!title) {
|
|
302
|
+
// Nothing to be done
|
|
303
|
+
return;
|
|
251
304
|
}
|
|
252
|
-
|
|
305
|
+
title.remove();
|
|
306
|
+
return;
|
|
253
307
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
event.preventDefault();
|
|
308
|
+
if (!title) {
|
|
309
|
+
title = document.createElement('div');
|
|
310
|
+
title.className = 'mdw-dialog__title';
|
|
311
|
+
const popup = dialogElement.getElementsByClassName('mdw-dialog__popup')[0];
|
|
312
|
+
popup.prepend(title);
|
|
313
|
+
}
|
|
314
|
+
if (content instanceof DocumentFragment) {
|
|
315
|
+
while (title.lastChild) {
|
|
316
|
+
title.removeChild(title.lastChild);
|
|
264
317
|
}
|
|
265
|
-
|
|
318
|
+
title.appendChild(content);
|
|
319
|
+
} else {
|
|
320
|
+
title.textContent = content;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
266
323
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (changed) {
|
|
277
|
-
Dialog.attach(dialogElement);
|
|
278
|
-
const previousFocus = document.activeElement;
|
|
279
|
-
const newState = { hash: Math.random().toString(36).substr(2, 16) };
|
|
280
|
-
let previousState = null;
|
|
281
|
-
if (window.history && window.history.pushState) {
|
|
282
|
-
if (!window.history.state) {
|
|
283
|
-
// Create new previous state
|
|
284
|
-
window.history.replaceState({
|
|
285
|
-
hash: Math.random().toString(36).substr(2, 16),
|
|
286
|
-
}, document.title);
|
|
287
|
-
}
|
|
288
|
-
previousState = window.history.state;
|
|
289
|
-
const title = Dialog.getTitleText(dialogElement);
|
|
290
|
-
window.history.pushState(newState, title);
|
|
291
|
-
window.addEventListener('popstate', Dialog.onPopState);
|
|
292
|
-
}
|
|
293
|
-
const dialogStack = new DialogStack(dialogElement, previousFocus, newState, previousState);
|
|
294
|
-
OPEN_DIALOGS.push(dialogStack);
|
|
295
|
-
const focusElement = dialogElement.querySelector('[mdw-autofocus]');
|
|
296
|
-
if (focusElement) {
|
|
297
|
-
focusElement.focus();
|
|
298
|
-
} else {
|
|
299
|
-
dialogElement.focus();
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return changed;
|
|
324
|
+
/**
|
|
325
|
+
* @param {Element} dialogElement
|
|
326
|
+
* @param {string[]} texts
|
|
327
|
+
* @return {void}
|
|
328
|
+
*/
|
|
329
|
+
export function updateButtonText(dialogElement, texts) {
|
|
330
|
+
const buttons = dialogElement.querySelectorAll('.mdw-dialog__button-area .mdw-button');
|
|
331
|
+
for (let i = 0; i < buttons.length; i++) {
|
|
332
|
+
setTextNode(buttons.item(i), texts[i]);
|
|
303
333
|
}
|
|
334
|
+
}
|
|
304
335
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
'textarea:not(:disabled):not([tabindex="-1"])',
|
|
317
|
-
'[tabindex]:not([tabindex="-1"])'].join(', '));
|
|
318
|
-
let foundTarget = false;
|
|
319
|
-
let candidate = null;
|
|
320
|
-
for (let i = 0; i < focusableElements.length; i += 1) {
|
|
321
|
-
const el = focusableElements.item(i);
|
|
322
|
-
if (el === event.target) {
|
|
323
|
-
foundTarget = true;
|
|
324
|
-
if (event.shiftKey) {
|
|
325
|
-
break;
|
|
326
|
-
}
|
|
327
|
-
} else if (event.shiftKey) {
|
|
328
|
-
candidate = el;
|
|
329
|
-
} else if (foundTarget) {
|
|
330
|
-
candidate = el;
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
336
|
+
/**
|
|
337
|
+
* @param {Element} dialogElement
|
|
338
|
+
* @param {(DocumentFragment|string)} [content]
|
|
339
|
+
* @return {void}
|
|
340
|
+
*/
|
|
341
|
+
export function updateBody(dialogElement, content) {
|
|
342
|
+
let body = dialogElement.getElementsByClassName('mdw-dialog__body')[0];
|
|
343
|
+
if (!content) {
|
|
344
|
+
if (!body) {
|
|
345
|
+
// Nothing to be done
|
|
346
|
+
return;
|
|
333
347
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
348
|
+
body.remove();
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (!body) {
|
|
352
|
+
body = document.createElement('div');
|
|
353
|
+
body.className = 'mdw-dialog__body mdw-theme';
|
|
354
|
+
body.setAttribute('mdw-ink', 'medium');
|
|
355
|
+
const title = dialogElement.getElementsByClassName('mdw-dialog__title')[0];
|
|
356
|
+
if (title) {
|
|
357
|
+
title.after(body);
|
|
358
|
+
} else {
|
|
359
|
+
const popup = dialogElement.getElementsByClassName('mdw-dialog__popup')[0];
|
|
360
|
+
popup.prepend(body);
|
|
342
361
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
362
|
+
}
|
|
363
|
+
if (content instanceof DocumentFragment) {
|
|
364
|
+
while (body.lastChild) {
|
|
365
|
+
body.removeChild(body.lastChild);
|
|
347
366
|
}
|
|
367
|
+
body.appendChild(content);
|
|
368
|
+
} else {
|
|
369
|
+
body.textContent = content;
|
|
348
370
|
}
|
|
371
|
+
}
|
|
349
372
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
373
|
+
/**
|
|
374
|
+
* @param {Element} dialogElement
|
|
375
|
+
* @param {Event|MouseEvent} [event]
|
|
376
|
+
* @return {void}
|
|
377
|
+
*/
|
|
378
|
+
export function updateTransformOrigin(dialogElement, event) {
|
|
379
|
+
const popup = /** @type {HTMLElement} */ (dialogElement.getElementsByClassName('mdw-dialog__popup')[0]);
|
|
380
|
+
popup.style.removeProperty('transform-origin');
|
|
381
|
+
if (!event) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const hadLayer = popup.style.getPropertyValue('display') != null;
|
|
385
|
+
if (!hadLayer) {
|
|
386
|
+
popup.style.setProperty('display', 'flex');
|
|
387
|
+
}
|
|
388
|
+
let pageX;
|
|
389
|
+
let pageY;
|
|
390
|
+
const dialogRect = dialogElement.getBoundingClientRect();
|
|
391
|
+
if ('pageX' in event && 'pageY' in event) {
|
|
392
|
+
pageX = event.pageX - window.pageXOffset;
|
|
393
|
+
pageY = event.pageY - window.pageYOffset;
|
|
394
|
+
} else {
|
|
395
|
+
const target = (event.target || event.currentTarget);
|
|
396
|
+
if (!(target instanceof Element)) {
|
|
363
397
|
return;
|
|
364
398
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
const popup = dialogElement.getElementsByClassName('mdw-dialog__popup')[0];
|
|
369
|
-
popup.insertBefore(title, popup.firstChild);
|
|
370
|
-
}
|
|
371
|
-
if (content instanceof DocumentFragment) {
|
|
372
|
-
while (title.lastChild) {
|
|
373
|
-
title.removeChild(title.lastChild);
|
|
374
|
-
}
|
|
375
|
-
title.appendChild(content);
|
|
376
|
-
} else {
|
|
377
|
-
title.textContent = content;
|
|
399
|
+
const rect = target.getBoundingClientRect();
|
|
400
|
+
if (!rect.width && !rect.width && !rect.left && !rect.top) {
|
|
401
|
+
return;
|
|
378
402
|
}
|
|
403
|
+
pageX = rect.left + (rect.width / 2);
|
|
404
|
+
pageY = rect.top + (rect.height / 2);
|
|
379
405
|
}
|
|
406
|
+
const transformOriginX = pageX - dialogRect.left - popup.offsetLeft;
|
|
407
|
+
const transformOriginY = pageY - dialogRect.top - popup.offsetTop;
|
|
408
|
+
popup.style.setProperty('transform-origin', `${transformOriginX}px ${transformOriginY}px`);
|
|
409
|
+
if (!hadLayer) {
|
|
410
|
+
popup.style.removeProperty('display');
|
|
411
|
+
}
|
|
412
|
+
}
|
|
380
413
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
414
|
+
/**
|
|
415
|
+
* @param {Element} element
|
|
416
|
+
* @return {void}
|
|
417
|
+
*/
|
|
418
|
+
export function attach(element) {
|
|
419
|
+
element.setAttribute('mdw-dialog-js', '');
|
|
420
|
+
element.addEventListener('transitionend', onTransitionEnd);
|
|
421
|
+
|
|
422
|
+
let dialogCloser = getChildElementByClass(element, 'mdw-dialog__close');
|
|
423
|
+
if (!dialogCloser) {
|
|
424
|
+
dialogCloser = document.createElement('div');
|
|
425
|
+
dialogCloser.className = 'mdw-dialog__close';
|
|
426
|
+
if (element.firstChild) {
|
|
427
|
+
element.prepend(dialogCloser);
|
|
428
|
+
} else {
|
|
429
|
+
element.appendChild(dialogCloser);
|
|
392
430
|
}
|
|
393
431
|
}
|
|
432
|
+
dialogCloser.addEventListener('click', onCancelClick);
|
|
433
|
+
const popup = getChildElementByClass(element, 'mdw-dialog__popup');
|
|
434
|
+
popup.addEventListener('keydown', onKeyDown);
|
|
435
|
+
const buttons = popup.querySelectorAll('.mdw-dialog__button-area .mdw-button');
|
|
436
|
+
let foundConfirmButton = false;
|
|
437
|
+
let foundCancelButton = false;
|
|
438
|
+
for (const button of buttons) {
|
|
439
|
+
Button.attach(button);
|
|
440
|
+
if (button.hasAttribute('mdw-custom')) {
|
|
441
|
+
button.addEventListener('click', onCustomButtonClick);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (!foundConfirmButton) {
|
|
445
|
+
button.addEventListener('click', onConfirmClick);
|
|
446
|
+
foundConfirmButton = true;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (!foundCancelButton) {
|
|
450
|
+
button.addEventListener('click', onCancelClick);
|
|
451
|
+
foundCancelButton = true;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
button.addEventListener('click', onCustomButtonClick);
|
|
455
|
+
}
|
|
456
|
+
setupARIA(element);
|
|
457
|
+
}
|
|
394
458
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
459
|
+
/**
|
|
460
|
+
* @param {Object} options
|
|
461
|
+
* @param {(DocumentFragment|string)} [options.title]
|
|
462
|
+
* @param {(DocumentFragment|string)} [options.body]
|
|
463
|
+
* @param {string[]} [options.buttons]
|
|
464
|
+
* @param {boolean} [options.stacked=false]
|
|
465
|
+
* @param {Element} [options.parent]
|
|
466
|
+
* @param {boolean} [options.custom=false] Use custom button events
|
|
467
|
+
* @param {(string|number)} [options.autofocus=0] Autofocus button by index or text
|
|
468
|
+
* @return {Element}
|
|
469
|
+
*/
|
|
470
|
+
export function create(options) {
|
|
471
|
+
const element = document.createElement('div');
|
|
472
|
+
element.className = 'mdw-dialog';
|
|
473
|
+
|
|
474
|
+
const popup = document.createElement('div');
|
|
475
|
+
popup.className = 'mdw-dialog__popup mdw-theme';
|
|
476
|
+
popup.setAttribute('mdw-surface', 'card');
|
|
477
|
+
element.appendChild(popup);
|
|
478
|
+
|
|
479
|
+
updateTitle(element, options.title);
|
|
480
|
+
updateBody(element, options.body);
|
|
481
|
+
|
|
482
|
+
if (options.buttons) {
|
|
483
|
+
const buttonArea = document.createElement('div');
|
|
484
|
+
buttonArea.className = 'mdw-dialog__button-area';
|
|
485
|
+
let index = 0;
|
|
486
|
+
for (const buttonText of options.buttons) {
|
|
487
|
+
const button = document.createElement('div');
|
|
488
|
+
button.className = 'mdw-button mdw-theme';
|
|
489
|
+
button.setAttribute('tabindex', '0');
|
|
490
|
+
button.setAttribute('mdw-ink', 'secondary');
|
|
491
|
+
if (options.custom) {
|
|
492
|
+
button.setAttribute('mdw-custom', '');
|
|
406
493
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
body.classList.add('mdw-dialog__body');
|
|
413
|
-
const title = dialogElement.getElementsByClassName('mdw-dialog__title')[0];
|
|
414
|
-
if (title) {
|
|
415
|
-
title.insertAdjacentElement('afterend', body);
|
|
416
|
-
} else {
|
|
417
|
-
const popup = dialogElement.getElementsByClassName('mdw-dialog__popup')[0];
|
|
418
|
-
popup.insertBefore(body, popup.firstChild);
|
|
494
|
+
button.textContent = buttonText;
|
|
495
|
+
buttonArea.appendChild(button);
|
|
496
|
+
if ((options.autofocus == null && index === 0)
|
|
497
|
+
|| options.autofocus === index || options.autofocus === buttonText) {
|
|
498
|
+
button.setAttribute('mdw-autofocus', '');
|
|
419
499
|
}
|
|
500
|
+
index += 1;
|
|
420
501
|
}
|
|
421
|
-
if (
|
|
422
|
-
|
|
423
|
-
body.removeChild(body.lastChild);
|
|
424
|
-
}
|
|
425
|
-
body.appendChild(content);
|
|
426
|
-
} else {
|
|
427
|
-
body.textContent = content;
|
|
502
|
+
if (options.stacked) {
|
|
503
|
+
buttonArea.setAttribute('mdw-stacked', '');
|
|
428
504
|
}
|
|
505
|
+
popup.appendChild(buttonArea);
|
|
429
506
|
}
|
|
507
|
+
if (options.parent) {
|
|
508
|
+
options.parent.appendChild(element);
|
|
509
|
+
}
|
|
510
|
+
attach(element);
|
|
511
|
+
return element;
|
|
512
|
+
}
|
|
430
513
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
514
|
+
/**
|
|
515
|
+
* @param {Element} dialogElement
|
|
516
|
+
* @param {Event} [event]
|
|
517
|
+
* @return {boolean} handled
|
|
518
|
+
*/
|
|
519
|
+
export function show(dialogElement, event) {
|
|
520
|
+
if (event && event.type === 'click' && event.currentTarget instanceof HTMLAnchorElement) {
|
|
521
|
+
// Prevent anchor link
|
|
522
|
+
event.preventDefault();
|
|
523
|
+
}
|
|
524
|
+
let changed = false;
|
|
525
|
+
|
|
526
|
+
if (dialogElement.getAttribute('aria-hidden') !== 'false') {
|
|
527
|
+
dialogElement.setAttribute('aria-hidden', 'false');
|
|
528
|
+
const popupElement = /** @type {HTMLElement} */ (dialogElement.getElementsByClassName('mdw-dialog__popup')[0]);
|
|
529
|
+
popupElement.style.setProperty('display', 'flex');
|
|
530
|
+
changed = true;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
updateTransformOrigin(dialogElement, event);
|
|
534
|
+
|
|
535
|
+
if (!changed) {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
attach(dialogElement);
|
|
539
|
+
const previousFocus = document.activeElement;
|
|
540
|
+
const newState = { hash: Math.random().toString(36).slice(2, 18) };
|
|
541
|
+
let previousState = null;
|
|
542
|
+
if (window.history && window.history.pushState) {
|
|
543
|
+
if (!window.history.state) {
|
|
544
|
+
// Create new previous state
|
|
545
|
+
window.history.replaceState({
|
|
546
|
+
hash: Math.random().toString(36).slice(2, 18),
|
|
547
|
+
}, document.title);
|
|
548
|
+
}
|
|
549
|
+
previousState = window.history.state;
|
|
550
|
+
const title = getTitleText(dialogElement);
|
|
551
|
+
window.history.pushState(newState, title);
|
|
552
|
+
window.addEventListener('popstate', onPopState);
|
|
553
|
+
}
|
|
554
|
+
const dialogStack = new DialogStack(dialogElement, previousFocus, newState, previousState);
|
|
555
|
+
OPEN_DIALOGS.push(dialogStack);
|
|
556
|
+
const focusElement = dialogElement.querySelector('[mdw-autofocus]');
|
|
557
|
+
try {
|
|
558
|
+
if (focusElement && focusElement instanceof HTMLElement) {
|
|
559
|
+
if (focusElement.scrollIntoView) {
|
|
560
|
+
focusElement.scrollIntoView();
|
|
475
561
|
}
|
|
476
|
-
|
|
562
|
+
focusElement.focus();
|
|
563
|
+
} else if (dialogElement instanceof HTMLElement) {
|
|
564
|
+
dialogElement.focus();
|
|
477
565
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
}
|
|
481
|
-
Dialog.attach(element);
|
|
482
|
-
return element;
|
|
566
|
+
} catch {
|
|
567
|
+
// Failed to focus
|
|
483
568
|
}
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
484
571
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
popupPageY += element.offsetTop - element.scrollTop;
|
|
503
|
-
element = element.offsetParent;
|
|
504
|
-
}
|
|
505
|
-
let { pageX, pageY } = event;
|
|
506
|
-
if (!pageX && !pageY) {
|
|
507
|
-
const target = event.currentTarget || event.target;
|
|
508
|
-
const rect = target.getBoundingClientRect();
|
|
509
|
-
if (!rect.width && !rect.width && !rect.x && !rect.y) {
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
pageX = rect.x + (rect.width / 2);
|
|
513
|
-
pageY = rect.y + (rect.height / 2);
|
|
572
|
+
/**
|
|
573
|
+
* @param {Element} dialogElement
|
|
574
|
+
* @return {void}
|
|
575
|
+
*/
|
|
576
|
+
export function detach(dialogElement) {
|
|
577
|
+
dialogElement.removeEventListener('transitionend', onTransitionEnd);
|
|
578
|
+
dialogElement.removeAttribute('mdw-dialog-js');
|
|
579
|
+
const dialogCloser = getChildElementByClass(dialogElement, 'mdw-dialog__close');
|
|
580
|
+
if (dialogCloser) {
|
|
581
|
+
dialogCloser.removeEventListener('click', onCancelClick);
|
|
582
|
+
}
|
|
583
|
+
const popupElement = /** @type {HTMLElement} */ (dialogElement.getElementsByClassName('mdw-dialog__popup')[0]);
|
|
584
|
+
if (popupElement) {
|
|
585
|
+
popupElement.removeEventListener('keydown', onKeyDown);
|
|
586
|
+
popupElement.style.removeProperty('transform-origin');
|
|
587
|
+
if (popupElement.hasAttribute('style') && !popupElement.getAttribute('style')) {
|
|
588
|
+
popupElement.removeAttribute('style');
|
|
514
589
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
590
|
+
}
|
|
591
|
+
const buttons = popupElement.querySelectorAll('.mdw-dialog__button-area .mdw-button');
|
|
592
|
+
for (const button of buttons) {
|
|
593
|
+
Button.detach(button);
|
|
594
|
+
button.removeEventListener('click', onConfirmClick);
|
|
595
|
+
button.removeEventListener('click', onCancelClick);
|
|
596
|
+
button.removeEventListener('click', onCustomButtonClick);
|
|
518
597
|
}
|
|
519
598
|
}
|
|
520
599
|
|
|
521
600
|
// Aliases
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
export {
|
|
525
|
-
Dialog,
|
|
526
|
-
};
|
|
601
|
+
export const dismiss = hide;
|