UNPKG

3.98 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 */
23
24import MDCComponent from '@material/base/component';
25import MDCIconToggleFoundation from './foundation';
26import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
27
28/**
29 * @extends {MDCComponent<!MDCIconToggleFoundation>}
30 */
31class MDCIconToggle extends MDCComponent {
32 static attachTo(root) {
33 return new MDCIconToggle(root);
34 }
35
36 constructor(...args) {
37 super(...args);
38
39 /** @private {!MDCRipple} */
40 this.ripple_ = this.initRipple_();
41 }
42
43 /** @return {!Element} */
44 get iconEl_() {
45 const {'iconInnerSelector': sel} = this.root_.dataset;
46 return sel ?
47 /** @type {!Element} */ (this.root_.querySelector(sel)) : this.root_;
48 }
49
50 /**
51 * @return {!MDCRipple}
52 * @private
53 */
54 initRipple_() {
55 const adapter = Object.assign(MDCRipple.createAdapter(this), {
56 isUnbounded: () => true,
57 isSurfaceActive: () => this.foundation_.isKeyboardActivated(),
58 });
59 const foundation = new MDCRippleFoundation(adapter);
60 return new MDCRipple(this.root_, foundation);
61 }
62
63 destroy() {
64 this.ripple_.destroy();
65 super.destroy();
66 }
67
68 /** @return {!MDCIconToggleFoundation} */
69 getDefaultFoundation() {
70 return new MDCIconToggleFoundation({
71 addClass: (className) => this.iconEl_.classList.add(className),
72 removeClass: (className) => this.iconEl_.classList.remove(className),
73 registerInteractionHandler: (type, handler) => this.root_.addEventListener(type, handler),
74 deregisterInteractionHandler: (type, handler) => this.root_.removeEventListener(type, handler),
75 setText: (text) => this.iconEl_.textContent = text,
76 getTabIndex: () => /* number */ this.root_.tabIndex,
77 setTabIndex: (tabIndex) => this.root_.tabIndex = tabIndex,
78 getAttr: (name, value) => this.root_.getAttribute(name, value),
79 setAttr: (name, value) => this.root_.setAttribute(name, value),
80 rmAttr: (name) => this.root_.removeAttribute(name),
81 notifyChange: (evtData) => this.emit(MDCIconToggleFoundation.strings.CHANGE_EVENT, evtData),
82 });
83 }
84
85 initialSyncWithDOM() {
86 this.on = this.root_.getAttribute(MDCIconToggleFoundation.strings.ARIA_PRESSED) === 'true';
87 this.disabled = this.root_.getAttribute(MDCIconToggleFoundation.strings.ARIA_DISABLED) === 'true';
88 }
89
90 /** @return {!MDCRipple} */
91 get ripple() {
92 return this.ripple_;
93 }
94
95 /** @return {boolean} */
96 get on() {
97 return this.foundation_.isOn();
98 }
99
100 /** @param {boolean} isOn */
101 set on(isOn) {
102 this.foundation_.toggle(isOn);
103 }
104
105 /** @return {boolean} */
106 get disabled() {
107 return this.foundation_.isDisabled();
108 }
109
110 /** @param {boolean} isDisabled */
111 set disabled(isDisabled) {
112 this.foundation_.setDisabled(isDisabled);
113 }
114
115 refreshToggleData() {
116 this.foundation_.refreshToggleData();
117 }
118}
119
120export {MDCIconToggle, MDCIconToggleFoundation};