UNPKG

7.52 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23import { __extends } from "tslib";
24import { MDCComponent } from '@material/base/component';
25import { closest } from '@material/dom/ponyfill';
26import { strings } from './constants';
27import { MDCSnackbarFoundation } from './foundation';
28import * as util from './util';
29var SURFACE_SELECTOR = strings.SURFACE_SELECTOR, LABEL_SELECTOR = strings.LABEL_SELECTOR, ACTION_SELECTOR = strings.ACTION_SELECTOR, DISMISS_SELECTOR = strings.DISMISS_SELECTOR, OPENING_EVENT = strings.OPENING_EVENT, OPENED_EVENT = strings.OPENED_EVENT, CLOSING_EVENT = strings.CLOSING_EVENT, CLOSED_EVENT = strings.CLOSED_EVENT;
30var MDCSnackbar = /** @class */ (function (_super) {
31 __extends(MDCSnackbar, _super);
32 function MDCSnackbar() {
33 return _super !== null && _super.apply(this, arguments) || this;
34 }
35 MDCSnackbar.attachTo = function (root) {
36 return new MDCSnackbar(root);
37 };
38 MDCSnackbar.prototype.initialize = function (announcerFactory) {
39 if (announcerFactory === void 0) { announcerFactory = function () { return util.announce; }; }
40 this.announce = announcerFactory();
41 };
42 MDCSnackbar.prototype.initialSyncWithDOM = function () {
43 var _this = this;
44 this.surfaceEl = this.root.querySelector(SURFACE_SELECTOR);
45 this.labelEl = this.root.querySelector(LABEL_SELECTOR);
46 this.actionEl = this.root.querySelector(ACTION_SELECTOR);
47 this.handleKeyDown = function (evt) {
48 _this.foundation.handleKeyDown(evt);
49 };
50 this.handleSurfaceClick = function (evt) {
51 var target = evt.target;
52 if (_this.isActionButton(target)) {
53 _this.foundation.handleActionButtonClick(evt);
54 }
55 else if (_this.isActionIcon(target)) {
56 _this.foundation.handleActionIconClick(evt);
57 }
58 };
59 this.registerKeyDownHandler(this.handleKeyDown);
60 this.registerSurfaceClickHandler(this.handleSurfaceClick);
61 };
62 MDCSnackbar.prototype.destroy = function () {
63 _super.prototype.destroy.call(this);
64 this.deregisterKeyDownHandler(this.handleKeyDown);
65 this.deregisterSurfaceClickHandler(this.handleSurfaceClick);
66 };
67 MDCSnackbar.prototype.open = function () {
68 this.foundation.open();
69 };
70 /**
71 * @param reason Why the snackbar was closed. Value will be passed to CLOSING_EVENT and CLOSED_EVENT via the
72 * `event.detail.reason` property. Standard values are REASON_ACTION and REASON_DISMISS, but custom
73 * client-specific values may also be used if desired.
74 */
75 MDCSnackbar.prototype.close = function (reason) {
76 if (reason === void 0) { reason = ''; }
77 this.foundation.close(reason);
78 };
79 MDCSnackbar.prototype.getDefaultFoundation = function () {
80 var _this = this;
81 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
82 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
83 var adapter = {
84 addClass: function (className) {
85 _this.root.classList.add(className);
86 },
87 announce: function () {
88 _this.announce(_this.labelEl);
89 },
90 notifyClosed: function (reason) { return _this.emit(CLOSED_EVENT, reason ? { reason: reason } : {}); },
91 notifyClosing: function (reason) { return _this.emit(CLOSING_EVENT, reason ? { reason: reason } : {}); },
92 notifyOpened: function () { return _this.emit(OPENED_EVENT, {}); },
93 notifyOpening: function () { return _this.emit(OPENING_EVENT, {}); },
94 removeClass: function (className) { return _this.root.classList.remove(className); },
95 };
96 return new MDCSnackbarFoundation(adapter);
97 };
98 Object.defineProperty(MDCSnackbar.prototype, "timeoutMs", {
99 get: function () {
100 return this.foundation.getTimeoutMs();
101 },
102 set: function (timeoutMs) {
103 this.foundation.setTimeoutMs(timeoutMs);
104 },
105 enumerable: false,
106 configurable: true
107 });
108 Object.defineProperty(MDCSnackbar.prototype, "closeOnEscape", {
109 get: function () {
110 return this.foundation.getCloseOnEscape();
111 },
112 set: function (closeOnEscape) {
113 this.foundation.setCloseOnEscape(closeOnEscape);
114 },
115 enumerable: false,
116 configurable: true
117 });
118 Object.defineProperty(MDCSnackbar.prototype, "isOpen", {
119 get: function () {
120 return this.foundation.isOpen();
121 },
122 enumerable: false,
123 configurable: true
124 });
125 Object.defineProperty(MDCSnackbar.prototype, "labelText", {
126 get: function () {
127 // This property only returns null if the node is a document, DOCTYPE,
128 // or notation. On Element nodes, it always returns a string.
129 return this.labelEl.textContent;
130 },
131 set: function (labelText) {
132 this.labelEl.textContent = labelText;
133 },
134 enumerable: false,
135 configurable: true
136 });
137 Object.defineProperty(MDCSnackbar.prototype, "actionButtonText", {
138 get: function () {
139 return this.actionEl.textContent;
140 },
141 set: function (actionButtonText) {
142 this.actionEl.textContent = actionButtonText;
143 },
144 enumerable: false,
145 configurable: true
146 });
147 MDCSnackbar.prototype.registerKeyDownHandler = function (handler) {
148 this.listen('keydown', handler);
149 };
150 MDCSnackbar.prototype.deregisterKeyDownHandler = function (handler) {
151 this.unlisten('keydown', handler);
152 };
153 MDCSnackbar.prototype.registerSurfaceClickHandler = function (handler) {
154 this.surfaceEl.addEventListener('click', handler);
155 };
156 MDCSnackbar.prototype.deregisterSurfaceClickHandler = function (handler) {
157 this.surfaceEl.removeEventListener('click', handler);
158 };
159 MDCSnackbar.prototype.isActionButton = function (target) {
160 return Boolean(closest(target, ACTION_SELECTOR));
161 };
162 MDCSnackbar.prototype.isActionIcon = function (target) {
163 return Boolean(closest(target, DISMISS_SELECTOR));
164 };
165 return MDCSnackbar;
166}(MDCComponent));
167export { MDCSnackbar };
168//# sourceMappingURL=component.js.map
\No newline at end of file