1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
9 |
|
10 | var Util = _interopDefault(require('../util.js'));
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var extendStatics = function(d, b) {
|
29 | extendStatics = Object.setPrototypeOf ||
|
30 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
31 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
32 | return extendStatics(d, b);
|
33 | };
|
34 |
|
35 | function __extends(d, b) {
|
36 | extendStatics(d, b);
|
37 | function __() { this.constructor = d; }
|
38 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
39 | }
|
40 |
|
41 | function __awaiter(thisArg, _arguments, P, generator) {
|
42 | return new (P || (P = Promise))(function (resolve, reject) {
|
43 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
44 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
45 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
46 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
47 | });
|
48 | }
|
49 |
|
50 | function __generator(thisArg, body) {
|
51 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
52 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
53 | function verb(n) { return function (v) { return step([n, v]); }; }
|
54 | function step(op) {
|
55 | if (f) throw new TypeError("Generator is already executing.");
|
56 | while (_) try {
|
57 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
58 | if (y = 0, t) op = [op[0] & 2, t.value];
|
59 | switch (op[0]) {
|
60 | case 0: case 1: t = op; break;
|
61 | case 4: _.label++; return { value: op[1], done: false };
|
62 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
63 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
64 | default:
|
65 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
66 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
67 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
68 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
69 | if (t[2]) _.ops.pop();
|
70 | _.trys.pop(); continue;
|
71 | }
|
72 | op = body.call(thisArg, _);
|
73 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
74 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
75 | }
|
76 | }
|
77 |
|
78 | var Component = (function () {
|
79 | function Component(name, defaultProps, props) {
|
80 | var _this = this;
|
81 | this.template = '';
|
82 | this.id = null;
|
83 | this.eventHandlers = [];
|
84 | this.registeredElements = [];
|
85 | this.name = name;
|
86 | var element = typeof props.element === 'string'
|
87 | ? document.querySelector(props.element) : props.element;
|
88 | var config = {};
|
89 | if (element) {
|
90 | var dataConfig = Util.Selector.attrConfig(element);
|
91 | if (dataConfig) {
|
92 | config = dataConfig;
|
93 | }
|
94 | }
|
95 | this.defaultProps = defaultProps;
|
96 | this.props = Object.assign(defaultProps, config, props, { element: element });
|
97 | this.id = this.uid();
|
98 | this.elementListener = function (event) { return _this.onBeforeElementEvent(event); };
|
99 | this.setEventsHandler();
|
100 | }
|
101 | Component.prototype.setTemplate = function (template) {
|
102 | this.template = template;
|
103 | };
|
104 | Component.prototype.getTemplate = function () {
|
105 | return this.template;
|
106 | };
|
107 | Component.prototype.getElement = function () {
|
108 | return this.getProp('element') || null;
|
109 | };
|
110 | Component.prototype.setElement = function (element) {
|
111 | this.props.element = element;
|
112 | };
|
113 | Component.prototype.getId = function () {
|
114 | return this.id;
|
115 | };
|
116 | Component.prototype.uid = function () {
|
117 | return Math.random().toString(36).substr(2, 10);
|
118 | };
|
119 | Component.prototype.getName = function () {
|
120 | return this.name;
|
121 | };
|
122 | Component.prototype.getProps = function () {
|
123 | return this.props;
|
124 | };
|
125 | Component.prototype.getProp = function (name) {
|
126 | var defaultValue = this.defaultProps[name];
|
127 | return typeof this.props[name] !== 'undefined' ? this.props[name] : defaultValue;
|
128 | };
|
129 | Component.prototype.setProps = function (props) {
|
130 | var componentProps = Object.assign({}, props);
|
131 | this.props = Object.assign(this.props, componentProps);
|
132 | };
|
133 | Component.prototype.setProp = function (name, value) {
|
134 | if (typeof this.props[name] === 'undefined') {
|
135 | throw new Error('Cannot set an invalid prop');
|
136 | }
|
137 | this.props[name] = value;
|
138 | };
|
139 | Component.prototype.registerElements = function (elements) {
|
140 | var _this = this;
|
141 | elements.forEach(function (element) { return _this.registerElement(element); });
|
142 | };
|
143 | Component.prototype.registerElement = function (element) {
|
144 | element.target.addEventListener(element.event, this.elementListener);
|
145 | this.registeredElements.push(element);
|
146 | };
|
147 | Component.prototype.unregisterElements = function () {
|
148 | var _this = this;
|
149 | this.registeredElements.forEach(function (element) {
|
150 | _this.unregisterElement(element);
|
151 | });
|
152 | };
|
153 | Component.prototype.unregisterElement = function (element) {
|
154 | var registeredElementIndex = this.registeredElements
|
155 | .findIndex(function (el) { return el.target === element.target && el.event === element.event; });
|
156 | if (registeredElementIndex > -1) {
|
157 | element.target.removeEventListener(element.event, this.elementListener);
|
158 | this.registeredElements.splice(registeredElementIndex, 1);
|
159 | }
|
160 | else {
|
161 | console.error('Warning! Could not remove element:'
|
162 | + ' ' + (element.target + " with event: " + element.event + "."));
|
163 | }
|
164 | };
|
165 | Component.prototype.triggerEvent = function (eventName, detail, objectEventOnly) {
|
166 | var _this = this;
|
167 | if (detail === void 0) { detail = {}; }
|
168 | if (objectEventOnly === void 0) { objectEventOnly = false; }
|
169 | var eventNameObject = eventName.split('.').reduce(function (acc, current, index) {
|
170 | if (index === 0) {
|
171 | return current;
|
172 | }
|
173 | return acc + current.charAt(0).toUpperCase() + current.slice(1);
|
174 | });
|
175 | var eventNameAlias = "on" + eventNameObject
|
176 | .charAt(0).toUpperCase() + eventNameObject.slice(1);
|
177 | var props = this.getProps();
|
178 | this.eventHandlers.forEach(function (scope) {
|
179 | if (typeof scope[eventNameObject] === 'function') {
|
180 | scope[eventNameObject].apply(_this, [detail]);
|
181 | }
|
182 | if (typeof scope[eventNameAlias] === 'function') {
|
183 | props[eventNameAlias].apply(_this, [detail]);
|
184 | }
|
185 | });
|
186 | if (objectEventOnly) {
|
187 | return;
|
188 | }
|
189 | var element = this.getElement();
|
190 | if (element) {
|
191 | Util.Dispatch.elementEvent(element, eventName, this.name, detail);
|
192 | }
|
193 | else {
|
194 | Util.Dispatch.winDocEvent(eventName, this.name, detail);
|
195 | }
|
196 | };
|
197 | Component.prototype.preventClosable = function () {
|
198 | return false;
|
199 | };
|
200 | Component.prototype.destroy = function () {
|
201 | this.unregisterElements();
|
202 | };
|
203 | Component.prototype.onElementEvent = function (event) {
|
204 | };
|
205 | Component.prototype.setEventsHandler = function () {
|
206 | var props = this.getProps();
|
207 | var scope = Object.keys(props).reduce(function (cur, key) {
|
208 | if (typeof props[key] === 'function') {
|
209 | cur[key] = props[key];
|
210 | }
|
211 | return cur;
|
212 | }, {});
|
213 | if (Object.keys(scope).length > 0) {
|
214 | this.eventHandlers.push(scope);
|
215 | }
|
216 | };
|
217 | Component.prototype.onBeforeElementEvent = function (event) {
|
218 | if (this.preventClosable()) {
|
219 | return;
|
220 | }
|
221 | this.onElementEvent(event);
|
222 | };
|
223 | return Component;
|
224 | }());
|
225 |
|
226 | var Tab = (function (_super) {
|
227 | __extends(Tab, _super);
|
228 | function Tab(props) {
|
229 | var _this = _super.call(this, 'tab', {}, props) || this;
|
230 | _this.tabSelector = 'tab';
|
231 | _this.tabItemSelector = 'tab-item';
|
232 | _this.tabContentSelector = 'tab-pane';
|
233 | _this.onAnimation = false;
|
234 | _this.registerElement({ target: _this.getElement(), event: Util.Event.CLICK });
|
235 | return _this;
|
236 | }
|
237 | Tab.attachDOM = function () {
|
238 | Util.Observer.subscribe({
|
239 | componentClass: 'tabs',
|
240 | onAdded: function (element, create) {
|
241 | create(new Tab({ element: element }));
|
242 | },
|
243 | onRemoved: function (element, remove) {
|
244 | remove('Tab', element);
|
245 | },
|
246 | });
|
247 | };
|
248 | Tab.prototype.onElementEvent = function (event) {
|
249 | var target = event.target;
|
250 | if (!target) {
|
251 | return;
|
252 | }
|
253 | var dataToggleAttr = target.getAttribute('data-toggle');
|
254 | if (dataToggleAttr) {
|
255 | var id = target.getAttribute('href') || target.getAttribute('data-target');
|
256 | if (!id) {
|
257 | return;
|
258 | }
|
259 | event.preventDefault();
|
260 | this.show(target);
|
261 | }
|
262 | };
|
263 | Tab.prototype.show = function (tabLink) {
|
264 | var _this = this;
|
265 | if (this.onAnimation) {
|
266 | return false;
|
267 | }
|
268 | var tabItem = Util.Selector.closest(tabLink, "." + this.tabItemSelector);
|
269 | if (!tabItem || tabItem.classList.contains('active')) {
|
270 | return false;
|
271 | }
|
272 | var id = tabLink.getAttribute('href') || tabLink.getAttribute('data-target');
|
273 | if (!id) {
|
274 | return false;
|
275 | }
|
276 | var tabContainer = this.getElement();
|
277 | var tabItems = Array.from(tabContainer.querySelectorAll('.tab-item') || []);
|
278 | tabItems.forEach(function (tab) {
|
279 | if (tab.classList.contains('active')) {
|
280 | tab.classList.remove('active');
|
281 | }
|
282 | var link = tab.querySelector('.tab-link');
|
283 | if (link) {
|
284 | link.setAttribute('aria-selected', 'false');
|
285 | }
|
286 | });
|
287 | this.onAnimation = true;
|
288 | tabItem.classList.add('active');
|
289 | tabLink.setAttribute('aria-selected', 'true');
|
290 | var tabContent = document.querySelector(id);
|
291 | if (!tabContent) {
|
292 | return false;
|
293 | }
|
294 | var tabContentParent = tabContent.parentNode;
|
295 | if (!tabContentParent) {
|
296 | return false;
|
297 | }
|
298 | var tabContents = Array
|
299 | .from(tabContentParent.querySelectorAll("." + this.tabContentSelector) || []);
|
300 | var prevTabItem = tabContainer.querySelector('.tab-item.active');
|
301 | tabContents.forEach(function (tab) {
|
302 | if (tab.classList.contains('active')) {
|
303 | tab.classList.remove('active');
|
304 | }
|
305 | });
|
306 | tabContent.classList.add('showing');
|
307 | this.triggerEvent(Util.Event.SHOW, this.getTabEvent(tabLink));
|
308 | if (prevTabItem) {
|
309 | this.triggerEvent(Util.Event.HIDE, this.getTabEvent(prevTabItem));
|
310 | }
|
311 | var onShowed = function () {
|
312 | tabContent.classList.remove('animate');
|
313 | tabContent.classList.add('active');
|
314 | tabContent.classList.remove('showing');
|
315 | _this.triggerEvent(Util.Event.SHOWN, _this.getTabEvent(tabLink));
|
316 | if (prevTabItem) {
|
317 | _this.triggerEvent(Util.Event.HIDDEN, _this.getTabEvent(prevTabItem));
|
318 | }
|
319 | _this.onAnimation = false;
|
320 | tabContent.removeEventListener(Util.Event.TRANSITION_END, onShowed);
|
321 | };
|
322 | (function () { return __awaiter(_this, void 0, void 0, function () {
|
323 | return __generator(this, function (_a) {
|
324 | switch (_a.label) {
|
325 | case 0: return [4, Util.sleep(20)];
|
326 | case 1:
|
327 | _a.sent();
|
328 | tabContent.addEventListener(Util.Event.TRANSITION_END, onShowed);
|
329 | tabContent.classList.add('animate');
|
330 | return [2];
|
331 | }
|
332 | });
|
333 | }); })();
|
334 | return true;
|
335 | };
|
336 | Tab.prototype.getTabEvent = function (tabLink) {
|
337 | return {
|
338 | id: tabLink.getAttribute('href') || tabLink.getAttribute('data-target'),
|
339 | target: tabLink,
|
340 | };
|
341 | };
|
342 | Tab.prototype.destroy = function () {
|
343 | this.registerElement({ target: this.getElement(), event: Util.Event.CLICK });
|
344 | };
|
345 | return Tab;
|
346 | }(Component));
|
347 | Tab.attachDOM();
|
348 |
|
349 | module.exports = Tab;
|
350 |
|