UNPKG

12.3 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2016 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 { MDCComponent } from '@material/base/component';
25import { MDCRipple } from '@material/ripple/component';
26import { MDCRippleFoundation } from '@material/ripple/foundation';
27import { MDCChipTrailingAction } from '../trailingaction/component';
28import { strings as trailingActionStrings } from '../trailingaction/constants';
29import { strings } from './constants';
30import { MDCChipFoundation } from './foundation';
31var MDCChip = /** @class */ (function (_super) {
32 __extends(MDCChip, _super);
33 function MDCChip() {
34 return _super !== null && _super.apply(this, arguments) || this;
35 }
36 Object.defineProperty(MDCChip.prototype, "selected", {
37 /**
38 * @return Whether the chip is selected.
39 */
40 get: function () {
41 return this.foundation.isSelected();
42 },
43 /**
44 * Sets selected state on the chip.
45 */
46 set: function (selected) {
47 this.foundation.setSelected(selected);
48 },
49 enumerable: false,
50 configurable: true
51 });
52 Object.defineProperty(MDCChip.prototype, "shouldRemoveOnTrailingIconClick", {
53 /**
54 * @return Whether a trailing icon click should trigger exit/removal of the chip.
55 */
56 get: function () {
57 return this.foundation.getShouldRemoveOnTrailingIconClick();
58 },
59 /**
60 * Sets whether a trailing icon click should trigger exit/removal of the chip.
61 */
62 set: function (shouldRemove) {
63 this.foundation.setShouldRemoveOnTrailingIconClick(shouldRemove);
64 },
65 enumerable: false,
66 configurable: true
67 });
68 Object.defineProperty(MDCChip.prototype, "setShouldFocusPrimaryActionOnClick", {
69 /**
70 * Sets whether a clicking on the chip should focus the primary action.
71 */
72 set: function (shouldFocus) {
73 this.foundation.setShouldFocusPrimaryActionOnClick(shouldFocus);
74 },
75 enumerable: false,
76 configurable: true
77 });
78 Object.defineProperty(MDCChip.prototype, "ripple", {
79 get: function () {
80 return this.rippleSurface;
81 },
82 enumerable: false,
83 configurable: true
84 });
85 Object.defineProperty(MDCChip.prototype, "id", {
86 get: function () {
87 return this.root.id;
88 },
89 enumerable: false,
90 configurable: true
91 });
92 MDCChip.attachTo = function (root) {
93 return new MDCChip(root);
94 };
95 MDCChip.prototype.initialize = function (rippleFactory, trailingActionFactory) {
96 var _this = this;
97 if (rippleFactory === void 0) { rippleFactory = function (el, foundation) { return new MDCRipple(el, foundation); }; }
98 if (trailingActionFactory === void 0) { trailingActionFactory = function (el) { return new MDCChipTrailingAction(el); }; }
99 this.leadingIcon = this.root.querySelector(strings.LEADING_ICON_SELECTOR);
100 this.checkmark = this.root.querySelector(strings.CHECKMARK_SELECTOR);
101 this.primaryAction =
102 this.root.querySelector(strings.PRIMARY_ACTION_SELECTOR);
103 var trailingActionEl = this.root.querySelector(strings.TRAILING_ACTION_SELECTOR);
104 if (trailingActionEl) {
105 this.trailingAction = trailingActionFactory(trailingActionEl);
106 }
107 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
108 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
109 var rippleAdapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { computeBoundingRect: function () { return _this.foundation.getDimensions(); } });
110 this.rippleSurface =
111 rippleFactory(this.root, new MDCRippleFoundation(rippleAdapter));
112 };
113 MDCChip.prototype.initialSyncWithDOM = function () {
114 var _this = this;
115 // Custom events
116 this.handleTrailingActionInteraction = function () {
117 _this.foundation.handleTrailingActionInteraction();
118 };
119 this.handleTrailingActionNavigation =
120 function (evt) {
121 _this.foundation.handleTrailingActionNavigation(evt);
122 };
123 // Native events
124 this.handleClick = function () {
125 _this.foundation.handleClick();
126 };
127 this.handleKeydown = function (evt) {
128 _this.foundation.handleKeydown(evt);
129 };
130 this.handleTransitionEnd = function (evt) {
131 _this.foundation.handleTransitionEnd(evt);
132 };
133 this.handleFocusIn = function (evt) {
134 _this.foundation.handleFocusIn(evt);
135 };
136 this.handleFocusOut = function (evt) {
137 _this.foundation.handleFocusOut(evt);
138 };
139 this.listen('transitionend', this.handleTransitionEnd);
140 this.listen('click', this.handleClick);
141 this.listen('keydown', this.handleKeydown);
142 this.listen('focusin', this.handleFocusIn);
143 this.listen('focusout', this.handleFocusOut);
144 if (this.trailingAction) {
145 this.listen(trailingActionStrings.INTERACTION_EVENT, this.handleTrailingActionInteraction);
146 this.listen(trailingActionStrings.NAVIGATION_EVENT, this.handleTrailingActionNavigation);
147 }
148 };
149 MDCChip.prototype.destroy = function () {
150 this.rippleSurface.destroy();
151 this.unlisten('transitionend', this.handleTransitionEnd);
152 this.unlisten('keydown', this.handleKeydown);
153 this.unlisten('click', this.handleClick);
154 this.unlisten('focusin', this.handleFocusIn);
155 this.unlisten('focusout', this.handleFocusOut);
156 if (this.trailingAction) {
157 this.unlisten(trailingActionStrings.INTERACTION_EVENT, this.handleTrailingActionInteraction);
158 this.unlisten(trailingActionStrings.NAVIGATION_EVENT, this.handleTrailingActionNavigation);
159 }
160 _super.prototype.destroy.call(this);
161 };
162 /**
163 * Begins the exit animation which leads to removal of the chip.
164 */
165 MDCChip.prototype.beginExit = function () {
166 this.foundation.beginExit();
167 };
168 MDCChip.prototype.getDefaultFoundation = function () {
169 var _this = this;
170 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
171 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
172 var adapter = {
173 addClass: function (className) { return _this.root.classList.add(className); },
174 addClassToLeadingIcon: function (className) {
175 if (_this.leadingIcon) {
176 _this.leadingIcon.classList.add(className);
177 }
178 },
179 eventTargetHasClass: function (target, className) {
180 return target ? target.classList.contains(className) : false;
181 },
182 focusPrimaryAction: function () {
183 if (_this.primaryAction) {
184 _this.primaryAction.focus();
185 }
186 },
187 focusTrailingAction: function () {
188 if (_this.trailingAction) {
189 _this.trailingAction.focus();
190 }
191 },
192 getAttribute: function (attr) { return _this.root.getAttribute(attr); },
193 getCheckmarkBoundingClientRect: function () {
194 return _this.checkmark ? _this.checkmark.getBoundingClientRect() : null;
195 },
196 getComputedStyleValue: function (propertyName) {
197 return window.getComputedStyle(_this.root).getPropertyValue(propertyName);
198 },
199 getRootBoundingClientRect: function () { return _this.root.getBoundingClientRect(); },
200 hasClass: function (className) { return _this.root.classList.contains(className); },
201 hasLeadingIcon: function () { return !!_this.leadingIcon; },
202 isRTL: function () { return window.getComputedStyle(_this.root).getPropertyValue('direction') === 'rtl'; },
203 isTrailingActionNavigable: function () {
204 if (_this.trailingAction) {
205 return _this.trailingAction.isNavigable();
206 }
207 return false;
208 },
209 notifyInteraction: function () { return _this.emit(strings.INTERACTION_EVENT, { chipId: _this.id }, true /* shouldBubble */); },
210 notifyNavigation: function (key, source) {
211 return _this.emit(strings.NAVIGATION_EVENT, { chipId: _this.id, key: key, source: source }, true /* shouldBubble */);
212 },
213 notifyRemoval: function (removedAnnouncement) {
214 _this.emit(strings.REMOVAL_EVENT, { chipId: _this.id, removedAnnouncement: removedAnnouncement }, true /* shouldBubble */);
215 },
216 notifySelection: function (selected, shouldIgnore) {
217 return _this.emit(strings.SELECTION_EVENT, { chipId: _this.id, selected: selected, shouldIgnore: shouldIgnore }, true /* shouldBubble */);
218 },
219 notifyTrailingIconInteraction: function () {
220 return _this.emit(strings.TRAILING_ICON_INTERACTION_EVENT, { chipId: _this.id }, true /* shouldBubble */);
221 },
222 notifyEditStart: function () { },
223 notifyEditFinish: function () { },
224 removeClass: function (className) { return _this.root.classList.remove(className); },
225 removeClassFromLeadingIcon: function (className) {
226 if (_this.leadingIcon) {
227 _this.leadingIcon.classList.remove(className);
228 }
229 },
230 removeTrailingActionFocus: function () {
231 if (_this.trailingAction) {
232 _this.trailingAction.removeFocus();
233 }
234 },
235 setPrimaryActionAttr: function (attr, value) {
236 if (_this.primaryAction) {
237 _this.primaryAction.setAttribute(attr, value);
238 }
239 },
240 setStyleProperty: function (propertyName, value) {
241 return _this.root.style.setProperty(propertyName, value);
242 },
243 };
244 return new MDCChipFoundation(adapter);
245 };
246 MDCChip.prototype.setSelectedFromChipSet = function (selected, shouldNotifyClients) {
247 this.foundation.setSelectedFromChipSet(selected, shouldNotifyClients);
248 };
249 MDCChip.prototype.focusPrimaryAction = function () {
250 this.foundation.focusPrimaryAction();
251 };
252 MDCChip.prototype.focusTrailingAction = function () {
253 this.foundation.focusTrailingAction();
254 };
255 MDCChip.prototype.removeFocus = function () {
256 this.foundation.removeFocus();
257 };
258 MDCChip.prototype.remove = function () {
259 var parent = this.root.parentNode;
260 if (parent !== null) {
261 parent.removeChild(this.root);
262 }
263 };
264 return MDCChip;
265}(MDCComponent));
266export { MDCChip };
267//# sourceMappingURL=component.js.map
\No newline at end of file