1 | import _Promise from 'babel-runtime/core-js/promise';
|
2 | import _typeof from 'babel-runtime/helpers/typeof';
|
3 | import _Object$keys from 'babel-runtime/core-js/object/keys';
|
4 | import _Array$from from 'babel-runtime/core-js/array/from';
|
5 | import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | import onsElements from './elements';
|
24 | import styler from './styler';
|
25 | import internal from './internal';
|
26 | import autoStyle from './autostyle';
|
27 | import ModifierUtil from './internal/modifier-util';
|
28 | import animationOptionsParse from './animation-options-parser';
|
29 | import platform from './platform';
|
30 |
|
31 | var util = {};
|
32 | var errorPrefix = '[Onsen UI]';
|
33 |
|
34 | util.globals = {
|
35 | fabOffset: 0,
|
36 | errorPrefix: errorPrefix,
|
37 | supportsPassive: false
|
38 | };
|
39 |
|
40 | platform._runOnActualPlatform(function () {
|
41 | util.globals.actualMobileOS = platform.getMobileOS();
|
42 | util.globals.isUIWebView = platform.isUIWebView();
|
43 | util.globals.isWKWebView = platform.isWKWebView();
|
44 | });
|
45 |
|
46 | try {
|
47 | var opts = Object.defineProperty({}, 'passive', {
|
48 | get: function get() {
|
49 | util.globals.supportsPassive = true;
|
50 | }
|
51 | });
|
52 | window.addEventListener('testPassive', null, opts);
|
53 | window.removeEventListener('testPassive', null, opts);
|
54 | } catch (e) {
|
55 | null;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | util.addEventListener = function (el, name, handler, opt, isGD) {
|
66 | el.addEventListener(name, handler, util.globals.supportsPassive ? opt : (opt || {}).capture);
|
67 | };
|
68 | util.removeEventListener = function (el, name, handler, opt, isGD) {
|
69 | el.removeEventListener(name, handler, util.globals.supportsPassive ? opt : (opt || {}).capture);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | util.prepareQuery = function (query) {
|
77 | return query instanceof Function ? query : function (element) {
|
78 | return util.match(element, query);
|
79 | };
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | util.match = function (e, s) {
|
88 | return (e.matches || e.webkitMatchesSelector || e.mozMatchesSelector || e.msMatchesSelector).call(e, s);
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | util.findChild = function (element, query) {
|
97 | var match = util.prepareQuery(query);
|
98 |
|
99 |
|
100 | for (var i = 0; i < element.childNodes.length; i++) {
|
101 | var node = element.childNodes[i];
|
102 | if (node.nodeType !== Node.ELEMENT_NODE) {
|
103 |
|
104 | continue;
|
105 | }
|
106 | if (match(node)) {
|
107 | return node;
|
108 | }
|
109 | }
|
110 | return null;
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | util.findParent = function (element, query, until) {
|
119 | var match = util.prepareQuery(query);
|
120 |
|
121 | var parent = element.parentNode;
|
122 | for (;;) {
|
123 | if (!parent || parent === document || parent instanceof DocumentFragment || until && until(parent)) {
|
124 | return null;
|
125 | } else if (match(parent)) {
|
126 | return parent;
|
127 | }
|
128 | parent = parent.parentNode;
|
129 | }
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | util.isAttached = function (element) {
|
137 | return document.body.contains(element);
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | util.hasAnyComponentAsParent = function (element) {
|
145 | while (element && document.documentElement !== element) {
|
146 | element = element.parentNode;
|
147 | if (element && element.nodeName.toLowerCase().match(/(ons-navigator|ons-tabbar|ons-modal)/)) {
|
148 | return true;
|
149 | }
|
150 | }
|
151 | return false;
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | util.getAllChildNodes = function (element) {
|
159 | var _ref;
|
160 |
|
161 | return (_ref = [element]).concat.apply(_ref, _toConsumableArray(_Array$from(element.children).map(function (childEl) {
|
162 | return util.getAllChildNodes(childEl);
|
163 | })));
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | util.isPageControl = function (element) {
|
171 | return element.nodeName.match(/^ons-(navigator|splitter|tabbar|page)$/i);
|
172 | };
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | util.propagateAction = function (element, action) {
|
179 | for (var i = 0; i < element.childNodes.length; i++) {
|
180 | var child = element.childNodes[i];
|
181 | if (child[action] instanceof Function) {
|
182 | child[action]();
|
183 | } else {
|
184 | util.propagateAction(child, action);
|
185 | }
|
186 | }
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | util.camelize = function (string) {
|
194 | return string.toLowerCase().replace(/-([a-z])/g, function (m, l) {
|
195 | return l.toUpperCase();
|
196 | });
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | util.hyphenate = function (string) {
|
204 | return string.replace(/([a-zA-Z])([A-Z])/g, '$1-$2').toLowerCase();
|
205 | };
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | util.create = function () {
|
213 | var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
214 | var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
215 |
|
216 | var classList = selector.split('.');
|
217 | var element = document.createElement(classList.shift() || 'div');
|
218 |
|
219 | if (classList.length) {
|
220 | element.className = classList.join(' ');
|
221 | }
|
222 |
|
223 | styler(element, style);
|
224 |
|
225 | return element;
|
226 | };
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | util.createElement = function (html) {
|
233 | var wrapper = document.createElement('div');
|
234 |
|
235 | if (html instanceof DocumentFragment) {
|
236 | wrapper.appendChild(document.importNode(html, true));
|
237 | } else {
|
238 | wrapper.innerHTML = html.trim();
|
239 | }
|
240 |
|
241 | if (wrapper.children.length > 1) {
|
242 | util.throw('HTML template must contain a single root element');
|
243 | }
|
244 |
|
245 | var element = wrapper.children[0];
|
246 | wrapper.children[0].remove();
|
247 | return element;
|
248 | };
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | util.createFragment = function (html) {
|
255 | var template = document.createElement('template');
|
256 | template.innerHTML = html;
|
257 | return document.importNode(template.content, true);
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | util.extend = function (dst) {
|
266 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
267 | args[_key - 1] = arguments[_key];
|
268 | }
|
269 |
|
270 | for (var i = 0; i < args.length; i++) {
|
271 | if (args[i]) {
|
272 | var keys = _Object$keys(args[i]);
|
273 | for (var j = 0; j < keys.length; j++) {
|
274 | var key = keys[j];
|
275 | dst[key] = args[i][key];
|
276 | }
|
277 | }
|
278 | }
|
279 |
|
280 | return dst;
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | util.arrayFrom = function (arrayLike) {
|
288 | return Array.prototype.slice.apply(arrayLike);
|
289 | };
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | util.parseJSONObjectSafely = function (jsonString) {
|
297 | var failSafe = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
298 |
|
299 | try {
|
300 | var result = JSON.parse('' + jsonString);
|
301 | if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) === 'object' && result !== null) {
|
302 | return result;
|
303 | }
|
304 | } catch (e) {
|
305 | return failSafe;
|
306 | }
|
307 | return failSafe;
|
308 | };
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | util.findFromPath = function (path) {
|
315 | path = path.split('.');
|
316 | var el = window,
|
317 | key;
|
318 | while (key = path.shift()) {
|
319 |
|
320 | el = el[key];
|
321 | }
|
322 | return el;
|
323 | };
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | util.getTopPage = function (container) {
|
330 | return container && (container.tagName.toLowerCase() === 'ons-page' ? container : container.topPage) || null;
|
331 | };
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | util.findToolbarPage = function (container) {
|
338 | var page = util.getTopPage(container);
|
339 |
|
340 | if (page) {
|
341 | if (page._canAnimateToolbar()) {
|
342 | return page;
|
343 | }
|
344 |
|
345 | for (var i = 0; i < page._contentElement.children.length; i++) {
|
346 | var nextPage = util.getTopPage(page._contentElement.children[i]);
|
347 | if (nextPage && !/ons-tabbar/i.test(page._contentElement.children[i].tagName)) {
|
348 | return util.findToolbarPage(nextPage);
|
349 | }
|
350 | }
|
351 | }
|
352 |
|
353 | return null;
|
354 | };
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | util.triggerElementEvent = function (target, eventName) {
|
363 | var detail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
364 |
|
365 |
|
366 | var event = new CustomEvent(eventName, {
|
367 | bubbles: true,
|
368 | cancelable: true,
|
369 | detail: detail
|
370 | });
|
371 |
|
372 | _Object$keys(detail).forEach(function (key) {
|
373 | event[key] = detail[key];
|
374 | });
|
375 |
|
376 | target.dispatchEvent(event);
|
377 |
|
378 | return event;
|
379 | };
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | util.hasModifier = function (target, modifierName) {
|
387 | if (!target.hasAttribute('modifier')) {
|
388 | return false;
|
389 | }
|
390 |
|
391 | return RegExp('(^|\\s+)' + modifierName + '($|\\s+)', 'i').test(target.getAttribute('modifier'));
|
392 | };
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | util.addModifier = function (target, modifierName) {
|
402 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
403 |
|
404 | if (options.autoStyle) {
|
405 | modifierName = autoStyle.mapModifier(modifierName, target, options.forceAutoStyle);
|
406 | }
|
407 |
|
408 | if (util.hasModifier(target, modifierName)) {
|
409 | return false;
|
410 | }
|
411 |
|
412 | target.setAttribute('modifier', ((target.getAttribute('modifier') || '') + ' ' + modifierName).trim());
|
413 | return true;
|
414 | };
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | util.removeModifier = function (target, modifierName) {
|
424 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
425 |
|
426 | if (options.autoStyle) {
|
427 | modifierName = autoStyle.mapModifier(modifierName, target, options.forceAutoStyle);
|
428 | }
|
429 |
|
430 | if (!target.getAttribute('modifier') || !util.hasModifier(target, modifierName)) {
|
431 | return false;
|
432 | }
|
433 |
|
434 | var newModifiers = target.getAttribute('modifier').split(/\s+/).filter(function (m) {
|
435 | return m && m !== modifierName;
|
436 | });
|
437 | newModifiers.length ? target.setAttribute('modifier', newModifiers.join(' ')) : target.removeAttribute('modifier');
|
438 | return true;
|
439 | };
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | util.toggleModifier = function () {
|
450 | var options = arguments.length > 2 ? arguments.length <= 2 ? undefined : arguments[2] : {};
|
451 | var force = typeof options === 'boolean' ? options : options.force;
|
452 |
|
453 | var toggle = typeof force === 'boolean' ? force : !util.hasModifier.apply(util, arguments);
|
454 | toggle ? util.addModifier.apply(util, arguments) : util.removeModifier.apply(util, arguments);
|
455 | };
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 | util.restoreClass = function (el, defaultClass, scheme) {
|
463 | defaultClass.split(/\s+/).forEach(function (c) {
|
464 | return c !== '' && !el.classList.contains(c) && el.classList.add(c);
|
465 | });
|
466 | el.hasAttribute('modifier') && ModifierUtil.refresh(el, scheme);
|
467 | };
|
468 |
|
469 |
|
470 | util.updateParentPosition = function (el) {
|
471 | if (!el._parentUpdated && el.parentElement) {
|
472 | if (window.getComputedStyle(el.parentElement).getPropertyValue('position') === 'static') {
|
473 | el.parentElement.style.position = 'relative';
|
474 | }
|
475 | el._parentUpdated = true;
|
476 | }
|
477 | };
|
478 |
|
479 | util.toggleAttribute = function (element, name, value) {
|
480 | if (value) {
|
481 | element.setAttribute(name, typeof value === 'boolean' ? '' : value);
|
482 | } else {
|
483 | element.removeAttribute(name);
|
484 | }
|
485 | };
|
486 |
|
487 | util.bindListeners = function (element, listenerNames) {
|
488 | listenerNames.forEach(function (name) {
|
489 | var boundName = name.replace(/^_[a-z]/, '_bound' + name[1].toUpperCase());
|
490 | element[boundName] = element[boundName] || element[name].bind(element);
|
491 | });
|
492 | };
|
493 |
|
494 | util.each = function (obj, f) {
|
495 | return _Object$keys(obj).forEach(function (key) {
|
496 | return f(key, obj[key]);
|
497 | });
|
498 | };
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 | util.updateRipple = function (target, hasRipple) {
|
506 | var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
507 |
|
508 | if (hasRipple === undefined) {
|
509 | hasRipple = target.hasAttribute('ripple');
|
510 | }
|
511 |
|
512 | var rippleElement = util.findChild(target, 'ons-ripple');
|
513 |
|
514 | if (hasRipple) {
|
515 | if (!rippleElement) {
|
516 | var element = document.createElement('ons-ripple');
|
517 | _Object$keys(attrs).forEach(function (key) {
|
518 | return element.setAttribute(key, attrs[key]);
|
519 | });
|
520 | target.insertBefore(element, target.firstChild);
|
521 | }
|
522 | } else if (rippleElement) {
|
523 | rippleElement.remove();
|
524 | }
|
525 | };
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 | util.animationOptionsParse = animationOptionsParse;
|
532 |
|
533 |
|
534 |
|
535 |
|
536 | util.isInteger = function (value) {
|
537 | return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
|
538 | };
|
539 |
|
540 |
|
541 |
|
542 |
|
543 | util.defer = function () {
|
544 | var deferred = {};
|
545 | deferred.promise = new _Promise(function (resolve, reject) {
|
546 | deferred.resolve = resolve;
|
547 | deferred.reject = reject;
|
548 | });
|
549 | return deferred;
|
550 | };
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 | util.warn = function () {
|
558 | for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
559 | args[_key2] = arguments[_key2];
|
560 | }
|
561 |
|
562 | if (!internal.config.warningsDisabled) {
|
563 | var _console;
|
564 |
|
565 | (_console = console).warn.apply(_console, [errorPrefix].concat(args));
|
566 | }
|
567 | };
|
568 |
|
569 | util.throw = function (message) {
|
570 | throw new Error(errorPrefix + ' ' + message);
|
571 | };
|
572 |
|
573 | util.throwAbstract = function () {
|
574 | return util.throw('Cannot instantiate abstract class');
|
575 | };
|
576 | util.throwMember = function () {
|
577 | return util.throw('Class member must be implemented');
|
578 | };
|
579 | util.throwPageLoader = function () {
|
580 | return util.throw('First parameter should be an instance of PageLoader');
|
581 | };
|
582 | util.throwAnimator = function (el) {
|
583 | return util.throw('"Animator" param must inherit ' + el + 'Animator');
|
584 | };
|
585 |
|
586 | var prevent = function prevent(e) {
|
587 | return e.cancelable && e.preventDefault();
|
588 | };
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | util.iosPreventScroll = function (gd) {
|
596 | if (util.globals.actualMobileOS === 'ios') {
|
597 | var clean = function clean(e) {
|
598 | gd.off('touchmove', prevent);
|
599 | gd.off('dragend', clean);
|
600 | };
|
601 |
|
602 | gd.on('touchmove', prevent);
|
603 | gd.on('dragend', clean);
|
604 | }
|
605 | };
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 | util.iosPageScrollFix = function (add) {
|
614 |
|
615 | if (util.globals.actualMobileOS === 'ios') {
|
616 | document.body.classList.toggle('ons-ios-scroll', add);
|
617 | if (!util.globals.isUIWebView || internal.config.forceUIWebViewScrollFix) {
|
618 | document.body.classList.toggle('ons-ios-scroll-fix', add);
|
619 | }
|
620 | }
|
621 | };
|
622 | util.iosMaskScrollFix = function (el, add) {
|
623 |
|
624 | if (util.globals.isUIWebView) {
|
625 | var action = (add ? 'add' : 'remove') + 'EventListener';
|
626 | el[action]('touchmove', prevent, false);
|
627 | }
|
628 | };
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 | util.isValidGesture = function (event) {
|
636 | return event.gesture !== undefined && (event.gesture.distance <= 15 || event.gesture.deltaTime <= 100);
|
637 | };
|
638 |
|
639 | util.checkMissingImport = function () {
|
640 | for (var _len3 = arguments.length, elementNames = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
641 | elementNames[_key3] = arguments[_key3];
|
642 | }
|
643 |
|
644 | elementNames.forEach(function (name) {
|
645 | if (!onsElements[name]) {
|
646 | util.throw('Ons' + name + ' is required but was not imported (Custom Elements)');
|
647 | }
|
648 | });
|
649 | };
|
650 |
|
651 | export default util; |
\ | No newline at end of file |