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 | var Component = (function () {
|
42 | function Component(name, defaultProps, props) {
|
43 | var _this = this;
|
44 | this.template = '';
|
45 | this.id = null;
|
46 | this.eventHandlers = [];
|
47 | this.registeredElements = [];
|
48 | this.name = name;
|
49 | var element = typeof props.element === 'string'
|
50 | ? document.querySelector(props.element) : props.element;
|
51 | var config = {};
|
52 | if (element) {
|
53 | var dataConfig = Util.Selector.attrConfig(element);
|
54 | if (dataConfig) {
|
55 | config = dataConfig;
|
56 | }
|
57 | }
|
58 | this.defaultProps = defaultProps;
|
59 | this.props = Object.assign(defaultProps, config, props, { element: element });
|
60 | this.id = this.uid();
|
61 | this.elementListener = function (event) { return _this.onBeforeElementEvent(event); };
|
62 | this.setEventsHandler();
|
63 | }
|
64 | Component.prototype.setTemplate = function (template) {
|
65 | this.template = template;
|
66 | };
|
67 | Component.prototype.getTemplate = function () {
|
68 | return this.template;
|
69 | };
|
70 | Component.prototype.getElement = function () {
|
71 | return this.getProp('element') || null;
|
72 | };
|
73 | Component.prototype.setElement = function (element) {
|
74 | this.props.element = element;
|
75 | };
|
76 | Component.prototype.getId = function () {
|
77 | return this.id;
|
78 | };
|
79 | Component.prototype.uid = function () {
|
80 | return Math.random().toString(36).substr(2, 10);
|
81 | };
|
82 | Component.prototype.getName = function () {
|
83 | return this.name;
|
84 | };
|
85 | Component.prototype.getProps = function () {
|
86 | return this.props;
|
87 | };
|
88 | Component.prototype.getProp = function (name) {
|
89 | var defaultValue = this.defaultProps[name];
|
90 | return typeof this.props[name] !== 'undefined' ? this.props[name] : defaultValue;
|
91 | };
|
92 | Component.prototype.setProps = function (props) {
|
93 | var componentProps = Object.assign({}, props);
|
94 | this.props = Object.assign(this.props, componentProps);
|
95 | };
|
96 | Component.prototype.setProp = function (name, value) {
|
97 | if (typeof this.props[name] === 'undefined') {
|
98 | throw new Error('Cannot set an invalid prop');
|
99 | }
|
100 | this.props[name] = value;
|
101 | };
|
102 | Component.prototype.registerElements = function (elements) {
|
103 | var _this = this;
|
104 | elements.forEach(function (element) { return _this.registerElement(element); });
|
105 | };
|
106 | Component.prototype.registerElement = function (element) {
|
107 | element.target.addEventListener(element.event, this.elementListener);
|
108 | this.registeredElements.push(element);
|
109 | };
|
110 | Component.prototype.unregisterElements = function () {
|
111 | var _this = this;
|
112 | this.registeredElements.forEach(function (element) {
|
113 | _this.unregisterElement(element);
|
114 | });
|
115 | };
|
116 | Component.prototype.unregisterElement = function (element) {
|
117 | var registeredElementIndex = this.registeredElements
|
118 | .findIndex(function (el) { return el.target === element.target && el.event === element.event; });
|
119 | if (registeredElementIndex > -1) {
|
120 | element.target.removeEventListener(element.event, this.elementListener);
|
121 | this.registeredElements.splice(registeredElementIndex, 1);
|
122 | }
|
123 | else {
|
124 | console.error('Warning! Could not remove element:'
|
125 | + ' ' + (element.target + " with event: " + element.event + "."));
|
126 | }
|
127 | };
|
128 | Component.prototype.triggerEvent = function (eventName, detail, objectEventOnly) {
|
129 | var _this = this;
|
130 | if (detail === void 0) { detail = {}; }
|
131 | if (objectEventOnly === void 0) { objectEventOnly = false; }
|
132 | var eventNameObject = eventName.split('.').reduce(function (acc, current, index) {
|
133 | if (index === 0) {
|
134 | return current;
|
135 | }
|
136 | return acc + current.charAt(0).toUpperCase() + current.slice(1);
|
137 | });
|
138 | var eventNameAlias = "on" + eventNameObject
|
139 | .charAt(0).toUpperCase() + eventNameObject.slice(1);
|
140 | var props = this.getProps();
|
141 | this.eventHandlers.forEach(function (scope) {
|
142 | if (typeof scope[eventNameObject] === 'function') {
|
143 | scope[eventNameObject].apply(_this, [detail]);
|
144 | }
|
145 | if (typeof scope[eventNameAlias] === 'function') {
|
146 | props[eventNameAlias].apply(_this, [detail]);
|
147 | }
|
148 | });
|
149 | if (objectEventOnly) {
|
150 | return;
|
151 | }
|
152 | var element = this.getElement();
|
153 | if (element) {
|
154 | Util.Dispatch.elementEvent(element, eventName, this.name, detail);
|
155 | }
|
156 | else {
|
157 | Util.Dispatch.winDocEvent(eventName, this.name, detail);
|
158 | }
|
159 | };
|
160 | Component.prototype.preventClosable = function () {
|
161 | return false;
|
162 | };
|
163 | Component.prototype.destroy = function () {
|
164 | this.unregisterElements();
|
165 | };
|
166 | Component.prototype.onElementEvent = function (event) {
|
167 | };
|
168 | Component.prototype.setEventsHandler = function () {
|
169 | var props = this.getProps();
|
170 | var scope = Object.keys(props).reduce(function (cur, key) {
|
171 | if (typeof props[key] === 'function') {
|
172 | cur[key] = props[key];
|
173 | }
|
174 | return cur;
|
175 | }, {});
|
176 | if (Object.keys(scope).length > 0) {
|
177 | this.eventHandlers.push(scope);
|
178 | }
|
179 | };
|
180 | Component.prototype.onBeforeElementEvent = function (event) {
|
181 | if (this.preventClosable()) {
|
182 | return;
|
183 | }
|
184 | this.onElementEvent(event);
|
185 | };
|
186 | return Component;
|
187 | }());
|
188 |
|
189 | var Size;
|
190 | (function (Size) {
|
191 | Size["sm"] = "sm";
|
192 | Size["md"] = "md";
|
193 | Size["lg"] = "lg";
|
194 | Size["xl"] = "xl";
|
195 | })(Size || (Size = {}));
|
196 | var Color;
|
197 | (function (Color) {
|
198 | Color["primary"] = "primary";
|
199 | Color["secondary"] = "secondary";
|
200 | Color["success"] = "success";
|
201 | Color["warning"] = "warning";
|
202 | Color["danger"] = "danger";
|
203 | })(Color || (Color = {}));
|
204 | var Direction;
|
205 | (function (Direction) {
|
206 | Direction["top"] = "top";
|
207 | Direction["right"] = "right";
|
208 | Direction["bottom"] = "bottom";
|
209 | Direction["left"] = "left";
|
210 | })(Direction || (Direction = {}));
|
211 |
|
212 | var Loader = (function (_super) {
|
213 | __extends(Loader, _super);
|
214 | function Loader(props) {
|
215 | if (props === void 0) { props = { color: Color.primary, size: Size.md }; }
|
216 | return _super.call(this, 'loader', { fade: true, size: Size.md, color: Color.primary }, props) || this;
|
217 | }
|
218 | Loader.prototype.show = function () {
|
219 | var element = this.getElement();
|
220 | if (element.classList.contains('hide')) {
|
221 | element.classList.remove('hide');
|
222 | }
|
223 | this.triggerEvent(Util.Event.SHOW);
|
224 | var size = this.getProp('size');
|
225 | Util.Selector.removeClasses(element, Object.values(Size), 'loader');
|
226 | element.classList.add("loader-" + size);
|
227 | var color = this.getProp('color');
|
228 | var spinner = this.getSpinner();
|
229 | Util.Selector.removeClasses(spinner, Object.values(Color), 'loader');
|
230 | spinner.classList.add("loader-" + color);
|
231 | this.triggerEvent(Util.Event.SHOWN);
|
232 | return true;
|
233 | };
|
234 | Loader.prototype.animate = function (startAnimation) {
|
235 | if (startAnimation === void 0) { startAnimation = true; }
|
236 | if (startAnimation) {
|
237 | this.show();
|
238 | }
|
239 | else {
|
240 | this.hide();
|
241 | }
|
242 | var loaderSpinner = this.getSpinner();
|
243 | if (startAnimation
|
244 | && !loaderSpinner.classList.contains('loader-spinner-animated')) {
|
245 | loaderSpinner.classList.add('loader-spinner-animated');
|
246 | return true;
|
247 | }
|
248 | if (!startAnimation
|
249 | && loaderSpinner.classList.contains('loader-spinner-animated')) {
|
250 | loaderSpinner.classList.remove('loader-spinner-animated');
|
251 | }
|
252 | return true;
|
253 | };
|
254 | Loader.prototype.hide = function () {
|
255 | var element = this.getElement();
|
256 | if (!element.classList.contains('hide')) {
|
257 | element.classList.add('hide');
|
258 | }
|
259 | this.triggerEvent(Util.Event.HIDE);
|
260 | this.triggerEvent(Util.Event.HIDDEN);
|
261 | return true;
|
262 | };
|
263 | Loader.prototype.getSpinner = function () {
|
264 | return this.getElement().querySelector('.loader-spinner');
|
265 | };
|
266 | return Loader;
|
267 | }(Component));
|
268 |
|
269 | module.exports = Loader;
|
270 |
|