UNPKG

4.82 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 { __assign, __extends } from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, strings } from './constants';
26var MDCIconButtonToggleFoundation = /** @class */ (function (_super) {
27 __extends(MDCIconButtonToggleFoundation, _super);
28 function MDCIconButtonToggleFoundation(adapter) {
29 var _this = _super.call(this, __assign(__assign({}, MDCIconButtonToggleFoundation.defaultAdapter), adapter)) || this;
30 /**
31 * Whether the icon button has an aria label that changes depending on
32 * toggled state.
33 */
34 _this.hasToggledAriaLabel = false;
35 return _this;
36 }
37 Object.defineProperty(MDCIconButtonToggleFoundation, "cssClasses", {
38 get: function () {
39 return cssClasses;
40 },
41 enumerable: false,
42 configurable: true
43 });
44 Object.defineProperty(MDCIconButtonToggleFoundation, "strings", {
45 get: function () {
46 return strings;
47 },
48 enumerable: false,
49 configurable: true
50 });
51 Object.defineProperty(MDCIconButtonToggleFoundation, "defaultAdapter", {
52 get: function () {
53 return {
54 addClass: function () { return undefined; },
55 hasClass: function () { return false; },
56 notifyChange: function () { return undefined; },
57 removeClass: function () { return undefined; },
58 getAttr: function () { return null; },
59 setAttr: function () { return undefined; },
60 };
61 },
62 enumerable: false,
63 configurable: true
64 });
65 MDCIconButtonToggleFoundation.prototype.init = function () {
66 var ariaLabelOn = this.adapter.getAttr(strings.DATA_ARIA_LABEL_ON);
67 var ariaLabelOff = this.adapter.getAttr(strings.DATA_ARIA_LABEL_OFF);
68 if (ariaLabelOn && ariaLabelOff) {
69 if (this.adapter.getAttr(strings.ARIA_PRESSED) !== null) {
70 throw new Error('MDCIconButtonToggleFoundation: Button should not set ' +
71 '`aria-pressed` if it has a toggled aria label.');
72 }
73 this.hasToggledAriaLabel = true;
74 }
75 else {
76 this.adapter.setAttr(strings.ARIA_PRESSED, String(this.isOn()));
77 }
78 };
79 MDCIconButtonToggleFoundation.prototype.handleClick = function () {
80 this.toggle();
81 this.adapter.notifyChange({ isOn: this.isOn() });
82 };
83 MDCIconButtonToggleFoundation.prototype.isOn = function () {
84 return this.adapter.hasClass(cssClasses.ICON_BUTTON_ON);
85 };
86 MDCIconButtonToggleFoundation.prototype.toggle = function (isOn) {
87 if (isOn === void 0) { isOn = !this.isOn(); }
88 // Toggle UI based on state.
89 if (isOn) {
90 this.adapter.addClass(cssClasses.ICON_BUTTON_ON);
91 }
92 else {
93 this.adapter.removeClass(cssClasses.ICON_BUTTON_ON);
94 }
95 // Toggle aria attributes based on state.
96 if (this.hasToggledAriaLabel) {
97 var ariaLabel = isOn ?
98 this.adapter.getAttr(strings.DATA_ARIA_LABEL_ON) :
99 this.adapter.getAttr(strings.DATA_ARIA_LABEL_OFF);
100 this.adapter.setAttr(strings.ARIA_LABEL, ariaLabel || '');
101 }
102 else {
103 this.adapter.setAttr(strings.ARIA_PRESSED, "" + isOn);
104 }
105 };
106 return MDCIconButtonToggleFoundation;
107}(MDCFoundation));
108export { MDCIconButtonToggleFoundation };
109// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
110export default MDCIconButtonToggleFoundation;
111//# sourceMappingURL=foundation.js.map
\No newline at end of file