1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import onsElements from '../ons/elements.js';
|
19 | import util from '../ons/util.js';
|
20 | import autoStyle from '../ons/autostyle.js';
|
21 | import ModifierUtil from '../ons/internal/modifier-util.js';
|
22 | import BaseElement from './base/base-element.js';
|
23 | import contentReady from '../ons/content-ready.js';
|
24 |
|
25 | const defaultClassName = 'back-button';
|
26 |
|
27 | const scheme = {
|
28 | '': 'back-button--*',
|
29 | '.back-button__icon': 'back-button--*__icon',
|
30 | '.back-button__label': 'back-button--*__label'
|
31 | };
|
32 |
|
33 |
|
34 | const iosBackButtonIcon = `
|
35 | <?xml version="1.0" encoding="UTF-8"?>
|
36 | <svg width="13px" height="21px" viewBox="0 0 13 21" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
37 | <title>ios-back-button-icon</title>
|
38 | <desc>Created with Sketch.</desc>
|
39 | <defs></defs>
|
40 | <g id="toolbar-back-button" stroke="none" stroke-width="1" fill-rule="evenodd">
|
41 | <g id="ios" transform="translate(-34.000000, -30.000000)">
|
42 | <polygon id="ios-back-button-icon" points="34 40.5 44.5 30 46.5 32 38 40.5 46.5 49 44.5 51"></polygon>
|
43 | </g>
|
44 | </g>
|
45 | </svg>
|
46 | `;
|
47 |
|
48 |
|
49 | const mdBackButtonIcon = `
|
50 | <?xml version="1.0" encoding="UTF-8"?>
|
51 | <svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
52 | <title>md-back-button-icon</title>
|
53 | <desc>Created with Sketch.</desc>
|
54 | <defs></defs>
|
55 | <g id="toolbar-back-button" stroke="none" stroke-width="1" fill-rule="evenodd">
|
56 | <g id="android" transform="translate(-32.000000, -32.000000)" fill-rule="nonzero">
|
57 | <polygon id="md-back-button-icon" points="48 39 35.83 39 41.42 33.41 40 32 32 40 40 48 41.41 46.59 35.83 41 48 41"></polygon>
|
58 | </g>
|
59 | </g>
|
60 | </svg>
|
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 | export default class BackButtonElement extends BaseElement {
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | constructor() {
|
105 | super();
|
106 |
|
107 | contentReady(this, () => {
|
108 | this._compile();
|
109 | });
|
110 |
|
111 | this._options = {};
|
112 | this._boundOnClick = this._onClick.bind(this);
|
113 |
|
114 | const {onConnected, onDisconnected} = util.defineListenerProperty(this, 'click');
|
115 | this._connectOnClick = onConnected;
|
116 | this._disconnectOnClick = onDisconnected;
|
117 | }
|
118 |
|
119 | _updateIcon(icon = util.findChild(this, '.back-button__icon')) {
|
120 | icon.innerHTML = autoStyle.getPlatform(this) === 'android' || util.hasModifier(this, 'material') ? mdBackButtonIcon : iosBackButtonIcon;
|
121 | }
|
122 |
|
123 | _compile() {
|
124 | autoStyle.prepare(this);
|
125 |
|
126 | this.classList.add(defaultClassName);
|
127 |
|
128 | if (!util.findChild(this, '.back-button__label')) {
|
129 | const label = util.create('span.back-button__label');
|
130 |
|
131 | while (this.childNodes[0]) {
|
132 | label.appendChild(this.childNodes[0]);
|
133 | }
|
134 | this.appendChild(label);
|
135 | }
|
136 |
|
137 | if (!util.findChild(this, '.back-button__icon')) {
|
138 | const icon = util.create('span.back-button__icon');
|
139 | this._updateIcon(icon);
|
140 |
|
141 | this.insertBefore(icon, this.children[0]);
|
142 | }
|
143 |
|
144 | util.updateRipple(this, undefined, {center: '', 'size': 'contain', 'background': 'transparent'});
|
145 |
|
146 | ModifierUtil.initModifier(this, scheme);
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | get options() {
|
185 | return this._options;
|
186 | }
|
187 |
|
188 | set options(object) {
|
189 | this._options = object;
|
190 | }
|
191 |
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | _onClick(event) {
|
201 | setTimeout(() => {
|
202 | if (!event.defaultPrevented) {
|
203 | const navigator = util.findParent(this, 'ons-navigator');
|
204 | if (navigator) {
|
205 | navigator.popPage({...this.options, onsBackButton: true});
|
206 | }
|
207 | }
|
208 | });
|
209 | }
|
210 |
|
211 | connectedCallback() {
|
212 | this.addEventListener('click', this._boundOnClick, false);
|
213 | this._connectOnClick();
|
214 | }
|
215 |
|
216 | static get observedAttributes() {
|
217 | return ['modifier', 'class'];
|
218 | }
|
219 |
|
220 | attributeChangedCallback(name, last, current) {
|
221 | switch (name) {
|
222 | case 'class':
|
223 | util.restoreClass(this, defaultClassName, scheme);
|
224 | break;
|
225 |
|
226 | case 'modifier': {
|
227 | ModifierUtil.onModifierChanged(last, current, this, scheme) && this._updateIcon();
|
228 | break;
|
229 | }
|
230 | }
|
231 | }
|
232 |
|
233 | disconnectedCallback() {
|
234 | this.removeEventListener('click', this._boundOnClick, false);
|
235 | this._disconnectOnClick();
|
236 | }
|
237 |
|
238 | show() {
|
239 | this.style.display = 'inline-block';
|
240 | }
|
241 |
|
242 | hide() {
|
243 | this.style.display = 'none';
|
244 | }
|
245 | }
|
246 |
|
247 | onsElements.BackButton = BackButtonElement;
|
248 | customElements.define('ons-back-button', BackButtonElement);
|