-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
executable file
·163 lines (147 loc) · 5.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @license
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import MDCComponent from '@material/base/component';
import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
import MDCChipAdapter from './adapter';
import MDCChipFoundation from './foundation';
import {strings} from './constants';
/**
* @extends {MDCComponent<!MDCChipFoundation>}
* @final
*/
class MDCChip extends MDCComponent {
/**
* @param {...?} args
*/
constructor(...args) {
super(...args);
/** @private {?Element} */
this.leadingIcon_;
/** @private {!MDCRipple} */
this.ripple_;
}
/**
* @param {!Element} root
* @return {!MDCChip}
*/
static attachTo(root) {
return new MDCChip(root);
}
initialize() {
this.leadingIcon_ = this.root_.querySelector(strings.LEADING_ICON_SELECTOR);
// Adjust ripple size for chips with animated growing width. This applies when filter chips without
// a leading icon are selected, and a leading checkmark will cause the chip width to expand.
const checkmarkEl = this.root_.querySelector(strings.CHECKMARK_SELECTOR);
if (checkmarkEl && !this.leadingIcon_) {
const adapter = Object.assign(MDCRipple.createAdapter(this), {
computeBoundingRect: () => {
const height = this.root_.getBoundingClientRect().height;
// The checkmark's width is initially set to 0, so use the checkmark's height as a proxy since the
// checkmark should always be square.
const width = this.root_.getBoundingClientRect().width + checkmarkEl.getBoundingClientRect().height;
return {height, width};
},
});
this.ripple_ = new MDCRipple(this.root_, new MDCRippleFoundation(adapter));
} else {
this.ripple_ = new MDCRipple(this.root_);
}
}
destroy() {
this.ripple_.destroy();
super.destroy();
}
/**
* Returns true if the chip is selected.
* @return {boolean}
*/
isSelected() {
return this.foundation_.isSelected();
}
/**
* Begins the exit animation which leads to removal of the chip.
*/
beginExit() {
this.foundation_.beginExit();
}
/**
* @return {!MDCChipFoundation}
*/
get foundation() {
return this.foundation_;
}
/**
* Returns whether a trailing icon click should trigger exit/removal of the chip.
* @return {boolean}
*/
get shouldRemoveOnTrailingIconClick() {
return this.foundation_.getShouldRemoveOnTrailingIconClick();
}
/**
* Sets whether a trailing icon click should trigger exit/removal of the chip.
* @param {boolean} shouldRemove
*/
set shouldRemoveOnTrailingIconClick(shouldRemove) {
return this.foundation_.setShouldRemoveOnTrailingIconClick(shouldRemove);
}
/**
* @return {!MDCChipFoundation}
*/
getDefaultFoundation() {
return new MDCChipFoundation(/** @type {!MDCChipAdapter} */ (Object.assign({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
hasClass: (className) => this.root_.classList.contains(className),
addClassToLeadingIcon: (className) => {
if (this.leadingIcon_) {
this.leadingIcon_.classList.add(className);
}
},
removeClassFromLeadingIcon: (className) => {
if (this.leadingIcon_) {
this.leadingIcon_.classList.remove(className);
}
},
eventTargetHasClass: (target, className) => target.classList.contains(className),
registerEventHandler: (evtType, handler) => this.root_.addEventListener(evtType, handler),
deregisterEventHandler: (evtType, handler) => this.root_.removeEventListener(evtType, handler),
registerTrailingIconInteractionHandler: (evtType, handler) => {
const trailingIconEl = this.root_.querySelector(strings.TRAILING_ICON_SELECTOR);
if (trailingIconEl) {
trailingIconEl.addEventListener(evtType, handler);
}
},
deregisterTrailingIconInteractionHandler: (evtType, handler) => {
const trailingIconEl = this.root_.querySelector(strings.TRAILING_ICON_SELECTOR);
if (trailingIconEl) {
trailingIconEl.removeEventListener(evtType, handler);
}
},
notifyInteraction: () => this.emit(strings.INTERACTION_EVENT, {chip: this}, true /* shouldBubble */),
notifyTrailingIconInteraction: () => this.emit(
strings.TRAILING_ICON_INTERACTION_EVENT, {chip: this}, true /* shouldBubble */),
notifyRemoval: () => this.emit(strings.REMOVAL_EVENT, {chip: this, root: this.root_}, true /* shouldBubble */),
getComputedStyleValue: (propertyName) => window.getComputedStyle(this.root_).getPropertyValue(propertyName),
setStyleProperty: (propertyName, value) => this.root_.style.setProperty(propertyName, value),
})));
}
/** @return {!MDCRipple} */
get ripple() {
return this.ripple_;
}
}
export {MDCChip, MDCChipFoundation};