UNPKG

6.18 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2017 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, __values } from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, strings } from './constants';
26var INTERACTION_EVENTS = ['click', 'keydown'];
27var MDCTextFieldIconFoundation = /** @class */ (function (_super) {
28 __extends(MDCTextFieldIconFoundation, _super);
29 function MDCTextFieldIconFoundation(adapter) {
30 var _this = _super.call(this, __assign(__assign({}, MDCTextFieldIconFoundation.defaultAdapter), adapter)) || this;
31 _this.savedTabIndex = null;
32 _this.interactionHandler = function (evt) {
33 _this.handleInteraction(evt);
34 };
35 return _this;
36 }
37 Object.defineProperty(MDCTextFieldIconFoundation, "strings", {
38 get: function () {
39 return strings;
40 },
41 enumerable: false,
42 configurable: true
43 });
44 Object.defineProperty(MDCTextFieldIconFoundation, "cssClasses", {
45 get: function () {
46 return cssClasses;
47 },
48 enumerable: false,
49 configurable: true
50 });
51 Object.defineProperty(MDCTextFieldIconFoundation, "defaultAdapter", {
52 /**
53 * See {@link MDCTextFieldIconAdapter} for typing information on parameters and return types.
54 */
55 get: function () {
56 // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
57 return {
58 getAttr: function () { return null; },
59 setAttr: function () { return undefined; },
60 removeAttr: function () { return undefined; },
61 setContent: function () { return undefined; },
62 registerInteractionHandler: function () { return undefined; },
63 deregisterInteractionHandler: function () { return undefined; },
64 notifyIconAction: function () { return undefined; },
65 };
66 // tslint:enable:object-literal-sort-keys
67 },
68 enumerable: false,
69 configurable: true
70 });
71 MDCTextFieldIconFoundation.prototype.init = function () {
72 var e_1, _a;
73 this.savedTabIndex = this.adapter.getAttr('tabindex');
74 try {
75 for (var INTERACTION_EVENTS_1 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next(); !INTERACTION_EVENTS_1_1.done; INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next()) {
76 var evtType = INTERACTION_EVENTS_1_1.value;
77 this.adapter.registerInteractionHandler(evtType, this.interactionHandler);
78 }
79 }
80 catch (e_1_1) { e_1 = { error: e_1_1 }; }
81 finally {
82 try {
83 if (INTERACTION_EVENTS_1_1 && !INTERACTION_EVENTS_1_1.done && (_a = INTERACTION_EVENTS_1.return)) _a.call(INTERACTION_EVENTS_1);
84 }
85 finally { if (e_1) throw e_1.error; }
86 }
87 };
88 MDCTextFieldIconFoundation.prototype.destroy = function () {
89 var e_2, _a;
90 try {
91 for (var INTERACTION_EVENTS_2 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next(); !INTERACTION_EVENTS_2_1.done; INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next()) {
92 var evtType = INTERACTION_EVENTS_2_1.value;
93 this.adapter.deregisterInteractionHandler(evtType, this.interactionHandler);
94 }
95 }
96 catch (e_2_1) { e_2 = { error: e_2_1 }; }
97 finally {
98 try {
99 if (INTERACTION_EVENTS_2_1 && !INTERACTION_EVENTS_2_1.done && (_a = INTERACTION_EVENTS_2.return)) _a.call(INTERACTION_EVENTS_2);
100 }
101 finally { if (e_2) throw e_2.error; }
102 }
103 };
104 MDCTextFieldIconFoundation.prototype.setDisabled = function (disabled) {
105 if (!this.savedTabIndex) {
106 return;
107 }
108 if (disabled) {
109 this.adapter.setAttr('tabindex', '-1');
110 this.adapter.removeAttr('role');
111 }
112 else {
113 this.adapter.setAttr('tabindex', this.savedTabIndex);
114 this.adapter.setAttr('role', strings.ICON_ROLE);
115 }
116 };
117 MDCTextFieldIconFoundation.prototype.setAriaLabel = function (label) {
118 this.adapter.setAttr('aria-label', label);
119 };
120 MDCTextFieldIconFoundation.prototype.setContent = function (content) {
121 this.adapter.setContent(content);
122 };
123 MDCTextFieldIconFoundation.prototype.handleInteraction = function (evt) {
124 var isEnterKey = evt.key === 'Enter' || evt.keyCode === 13;
125 if (evt.type === 'click' || isEnterKey) {
126 evt.preventDefault(); // stop click from causing host label to focus
127 // input
128 this.adapter.notifyIconAction();
129 }
130 };
131 return MDCTextFieldIconFoundation;
132}(MDCFoundation));
133export { MDCTextFieldIconFoundation };
134// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
135export default MDCTextFieldIconFoundation;
136//# sourceMappingURL=foundation.js.map
\No newline at end of file