1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global.isLoading = factory());
|
5 | }(this, (function () { 'use strict';
|
6 |
|
7 | function unwrapExports (x) {
|
8 | return x && x.__esModule ? x['default'] : x;
|
9 | }
|
10 |
|
11 | function createCommonjsModule(fn, module) {
|
12 | return module = { exports: {} }, fn(module, module.exports), module.exports;
|
13 | }
|
14 |
|
15 | var index$1 = createCommonjsModule(function (module, exports) {
|
16 | 'use strict';
|
17 |
|
18 | exports.__esModule = true;
|
19 | exports.default = createElement;
|
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 | function createElement() {
|
45 | var querySelector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'div';
|
46 |
|
47 | var nodeType = querySelector.match(/^[a-z]+/i);
|
48 | var id = querySelector.match(/#([a-z]+[a-z0-9-]*)/gi);
|
49 | var classes = querySelector.match(/\.([a-z]+[a-z0-9-]*)/gi);
|
50 | var attributes = querySelector.match(/\[([a-z][a-z-]+)(=['|"]?([^\]]*)['|"]?)?\]/gi);
|
51 | var node = nodeType ? nodeType[0] : 'div';
|
52 |
|
53 | if (id && id.length > 1) {
|
54 | throw CreateElementException('only 1 ID is allowed');
|
55 | }
|
56 |
|
57 | var elt = document.createElement(node);
|
58 |
|
59 | if (id) {
|
60 | elt.id = id[0].replace('#', '');
|
61 | }
|
62 |
|
63 | if (classes) {
|
64 | var attrClasses = classes.join(' ').replace(/\./g, '');
|
65 | elt.setAttribute('class', attrClasses);
|
66 | }
|
67 |
|
68 | if (attributes) {
|
69 | attributes.forEach(function (item) {
|
70 | item = item.slice(0, -1).slice(1);
|
71 |
|
72 | var _item$split = item.split('='),
|
73 | label = _item$split[0],
|
74 | value = _item$split[1];
|
75 |
|
76 | if (value) {
|
77 | value = value.replace(/^['"](.*)['"]$/, '$1');
|
78 | }
|
79 | elt.setAttribute(label, value || '');
|
80 | });
|
81 | }
|
82 |
|
83 | for (var _len = arguments.length, content = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
84 | content[_key - 1] = arguments[_key];
|
85 | }
|
86 |
|
87 | content.forEach(function (item) {
|
88 | if (typeof item === 'string' || typeof item === 'number') {
|
89 | elt.appendChild(document.createTextNode(item));
|
90 | } else if (item.nodeType === document.ELEMENT_NODE) {
|
91 | elt.appendChild(item);
|
92 | }
|
93 | });
|
94 |
|
95 | return elt;
|
96 | }
|
97 |
|
98 | function CreateElementException(message) {
|
99 | this.message = message;
|
100 | this.name = 'CreateElementException';
|
101 | }
|
102 | module.exports = exports['default'];
|
103 | });
|
104 |
|
105 | var createElement = unwrapExports(index$1);
|
106 |
|
107 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
108 |
|
109 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
110 |
|
111 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
112 |
|
113 | var index = (function () {
|
114 | for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
|
115 | params[_key] = arguments[_key];
|
116 | }
|
117 |
|
118 | return new IsLoading(params);
|
119 | });
|
120 |
|
121 | var formElements = ['form', 'input', 'textarea', 'label', 'fieldset', 'select', 'button'];
|
122 |
|
123 | var optionsDefault = {
|
124 | 'type': 'switch',
|
125 | 'text': 'loading',
|
126 | 'disableSource': true,
|
127 | 'disableList': []
|
128 | };
|
129 |
|
130 | var IsLoading = function () {
|
131 | function IsLoading(params) {
|
132 | _classCallCheck(this, IsLoading);
|
133 |
|
134 | var options = {};
|
135 | if (params.length === 0 || params.length === 1 && !params[0].nodeType) {
|
136 | this._target = null;
|
137 | options = _extends({}, params[0], { type: 'full-overlay' });
|
138 | } else {
|
139 | this._target = params[0];
|
140 | options = params[1];
|
141 | }
|
142 | this._options = _extends({}, optionsDefault, options);
|
143 | this._fullOverlayId = 'is-loading-full-overlay';
|
144 | }
|
145 |
|
146 | IsLoading.prototype.loading = function loading() {
|
147 | switch (this._options.type) {
|
148 | case 'replace':
|
149 | this._onReplaceType();break;
|
150 | case 'full-overlay':
|
151 | this._onFullOverlayType();break;
|
152 | case 'overlay':
|
153 | this._onElementOverlayType();break;
|
154 | default:
|
155 | this._onSwitchType();break;
|
156 | }
|
157 | };
|
158 |
|
159 | IsLoading.prototype.restoreContent = function restoreContent() {
|
160 | var content = this._target.getAttribute('data-is-loading-content');
|
161 | if (this.isTargetValue) {
|
162 | this._target.value = content;
|
163 | } else {
|
164 | this._target.textContent = content;
|
165 | }
|
166 | };
|
167 |
|
168 | IsLoading.prototype._onSwitchType = function _onSwitchType() {
|
169 | this._toggleElements(false);
|
170 | this._target.setAttribute('data-is-loading-content', this.targetContent);
|
171 | this.targetContent = this._options.text;
|
172 | };
|
173 |
|
174 | IsLoading.prototype._onReplaceType = function _onReplaceType() {
|
175 | this._toggleElements(false);
|
176 | this._target.setAttribute('data-is-loading-content', this.targetContent);
|
177 | this._target.innerHTML = '';
|
178 | this._target.appendChild(createElement('span.is-loading.is-loading-target', this._options.text));
|
179 | };
|
180 |
|
181 | IsLoading.prototype._onElementOverlayType = function _onElementOverlayType() {
|
182 | this._toggleElements(false);
|
183 | var overlayWrapperClass = '.is-loading-element-overlay';
|
184 |
|
185 | if (this._prop('position') === 'static') {
|
186 | this._target.setAttribute('data-is-loading-position', 'static');
|
187 | this._target.classList.add('is-loading-element-overlay-target');
|
188 | }
|
189 |
|
190 | if (!this._target.querySelector(overlayWrapperClass)) {
|
191 | var overlay = createElement(overlayWrapperClass, createElement('.is-loading-text-wrapper', this._options.text));
|
192 | overlay.style.borderRadius = this._prop('border-radius');
|
193 | this._target.appendChild(overlay);
|
194 | }
|
195 | };
|
196 |
|
197 | IsLoading.prototype._onFullOverlayType = function _onFullOverlayType() {
|
198 | this._toggleElements(false);
|
199 | this._showFullOverlay();
|
200 | };
|
201 |
|
202 | IsLoading.prototype._showFullOverlay = function _showFullOverlay() {
|
203 | var overlay = document.querySelector(this._fullOverlayId);
|
204 |
|
205 | if (!overlay) {
|
206 | overlay = createElement('#' + this._fullOverlayId, createElement('.is-loading-text-wrapper', this._options.text));
|
207 | document.querySelector('body').appendChild(overlay);
|
208 | }
|
209 | };
|
210 |
|
211 | IsLoading.prototype._prop = function _prop(prop) {
|
212 | return window.getComputedStyle(this._target).getPropertyValue(prop);
|
213 | };
|
214 |
|
215 | IsLoading.prototype._toggleElements = function _toggleElements() {
|
216 | var status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
217 |
|
218 | var list = [].concat(this._options.disableList);
|
219 | if (this._target && this._options.disableSource === true) {
|
220 | list.unshift(this._target);
|
221 | }
|
222 | list.forEach(function (item) {
|
223 | if (formElements.includes(item.tagName.toLowerCase())) {
|
224 | if (status === true) {
|
225 | item.removeAttribute('disabled');
|
226 | } else {
|
227 | item.setAttribute('disabled', 'disabled');
|
228 | }
|
229 | }
|
230 | if (status === true) {
|
231 | item.classList.remove('disabled');
|
232 | } else {
|
233 | item.classList.add('disabled');
|
234 | }
|
235 | });
|
236 | };
|
237 |
|
238 | IsLoading.prototype.remove = function remove() {
|
239 | this._toggleElements(true);
|
240 | if (this._options.type === 'switch') {
|
241 | this.restoreContent();
|
242 | }
|
243 | if (this._target) {
|
244 | this._target.removeAttribute('data-is-loading-content');
|
245 | }
|
246 | if (this._options.type === 'full-overlay') {
|
247 | var overlay = document.getElementById(this._fullOverlayId);
|
248 | if (overlay) {
|
249 | document.querySelector('body').removeChild(overlay);
|
250 | }
|
251 | }
|
252 | if (this._target && this._target.getAttribute('data-is-loading-position')) {
|
253 | this._target.classList.remove('is-loading-element-overlay-target');
|
254 | }
|
255 | };
|
256 |
|
257 | _createClass(IsLoading, [{
|
258 | key: 'targetContent',
|
259 | get: function get() {
|
260 | if (this.isTargetValue) {
|
261 | return this._target.value;
|
262 | } else {
|
263 | return this._target.textContent;
|
264 | }
|
265 | },
|
266 | set: function set(val) {
|
267 | if (this.isTargetValue) {
|
268 | this._target.value = val;
|
269 | } else {
|
270 | this._target.textContent = val;
|
271 | }
|
272 | }
|
273 | }, {
|
274 | key: 'isTargetValue',
|
275 | get: function get() {
|
276 | var node = this._target.nodeName.toLowerCase();
|
277 | var type = this._target.attributes.type;
|
278 |
|
279 | return node === 'input' && type && (type.value.toLowerCase() === 'button' || type.value.toLowerCase() === 'submit');
|
280 | }
|
281 | }]);
|
282 |
|
283 | return IsLoading;
|
284 | }();
|
285 |
|
286 | return index;
|
287 |
|
288 | })));
|