dom7.esm.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /**
  2. * Dom7 4.0.6
  3. * Minimalistic JavaScript library for DOM manipulation, with a jQuery-compatible API
  4. * https://framework7.io/docs/dom7.html
  5. *
  6. * Copyright 2023, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: February 2, 2023
  11. */
  12. import { getWindow, getDocument } from 'ssr-window';
  13. /* eslint-disable no-proto */
  14. function makeReactive(obj) {
  15. const proto = obj.__proto__;
  16. Object.defineProperty(obj, '__proto__', {
  17. get() {
  18. return proto;
  19. },
  20. set(value) {
  21. proto.__proto__ = value;
  22. }
  23. });
  24. }
  25. class Dom7 extends Array {
  26. constructor(items) {
  27. if (typeof items === 'number') {
  28. super(items);
  29. } else {
  30. super(...(items || []));
  31. makeReactive(this);
  32. }
  33. }
  34. }
  35. function arrayFlat(arr = []) {
  36. const res = [];
  37. arr.forEach(el => {
  38. if (Array.isArray(el)) {
  39. res.push(...arrayFlat(el));
  40. } else {
  41. res.push(el);
  42. }
  43. });
  44. return res;
  45. }
  46. function arrayFilter(arr, callback) {
  47. return Array.prototype.filter.call(arr, callback);
  48. }
  49. function arrayUnique(arr) {
  50. const uniqueArray = [];
  51. for (let i = 0; i < arr.length; i += 1) {
  52. if (uniqueArray.indexOf(arr[i]) === -1) uniqueArray.push(arr[i]);
  53. }
  54. return uniqueArray;
  55. }
  56. function toCamelCase(string) {
  57. return string.toLowerCase().replace(/-(.)/g, (match, group) => group.toUpperCase());
  58. }
  59. // eslint-disable-next-line
  60. function qsa(selector, context) {
  61. if (typeof selector !== 'string') {
  62. return [selector];
  63. }
  64. const a = [];
  65. const res = context.querySelectorAll(selector);
  66. for (let i = 0; i < res.length; i += 1) {
  67. a.push(res[i]);
  68. }
  69. return a;
  70. }
  71. function $(selector, context) {
  72. const window = getWindow();
  73. const document = getDocument();
  74. let arr = [];
  75. if (!context && selector instanceof Dom7) {
  76. return selector;
  77. }
  78. if (!selector) {
  79. return new Dom7(arr);
  80. }
  81. if (typeof selector === 'string') {
  82. const html = selector.trim();
  83. if (html.indexOf('<') >= 0 && html.indexOf('>') >= 0) {
  84. let toCreate = 'div';
  85. if (html.indexOf('<li') === 0) toCreate = 'ul';
  86. if (html.indexOf('<tr') === 0) toCreate = 'tbody';
  87. if (html.indexOf('<td') === 0 || html.indexOf('<th') === 0) toCreate = 'tr';
  88. if (html.indexOf('<tbody') === 0) toCreate = 'table';
  89. if (html.indexOf('<option') === 0) toCreate = 'select';
  90. const tempParent = document.createElement(toCreate);
  91. tempParent.innerHTML = html;
  92. for (let i = 0; i < tempParent.childNodes.length; i += 1) {
  93. arr.push(tempParent.childNodes[i]);
  94. }
  95. } else {
  96. arr = qsa(selector.trim(), context || document);
  97. } // arr = qsa(selector, document);
  98. } else if (selector.nodeType || selector === window || selector === document) {
  99. arr.push(selector);
  100. } else if (Array.isArray(selector)) {
  101. if (selector instanceof Dom7) return selector;
  102. arr = selector;
  103. }
  104. return new Dom7(arrayUnique(arr));
  105. }
  106. $.fn = Dom7.prototype;
  107. // eslint-disable-next-line
  108. function addClass(...classes) {
  109. const classNames = arrayFlat(classes.map(c => c.split(' ')));
  110. this.forEach(el => {
  111. el.classList.add(...classNames);
  112. });
  113. return this;
  114. }
  115. function removeClass(...classes) {
  116. const classNames = arrayFlat(classes.map(c => c.split(' ')));
  117. this.forEach(el => {
  118. el.classList.remove(...classNames);
  119. });
  120. return this;
  121. }
  122. function toggleClass(...classes) {
  123. const classNames = arrayFlat(classes.map(c => c.split(' ')));
  124. this.forEach(el => {
  125. classNames.forEach(className => {
  126. el.classList.toggle(className);
  127. });
  128. });
  129. }
  130. function hasClass(...classes) {
  131. const classNames = arrayFlat(classes.map(c => c.split(' ')));
  132. return arrayFilter(this, el => {
  133. return classNames.filter(className => el.classList.contains(className)).length > 0;
  134. }).length > 0;
  135. }
  136. function attr(attrs, value) {
  137. if (arguments.length === 1 && typeof attrs === 'string') {
  138. // Get attr
  139. if (this[0]) return this[0].getAttribute(attrs);
  140. return undefined;
  141. } // Set attrs
  142. for (let i = 0; i < this.length; i += 1) {
  143. if (arguments.length === 2) {
  144. // String
  145. this[i].setAttribute(attrs, value);
  146. } else {
  147. // Object
  148. for (const attrName in attrs) {
  149. this[i][attrName] = attrs[attrName];
  150. this[i].setAttribute(attrName, attrs[attrName]);
  151. }
  152. }
  153. }
  154. return this;
  155. }
  156. function removeAttr(attr) {
  157. for (let i = 0; i < this.length; i += 1) {
  158. this[i].removeAttribute(attr);
  159. }
  160. return this;
  161. }
  162. function prop(props, value) {
  163. if (arguments.length === 1 && typeof props === 'string') {
  164. // Get prop
  165. if (this[0]) return this[0][props];
  166. } else {
  167. // Set props
  168. for (let i = 0; i < this.length; i += 1) {
  169. if (arguments.length === 2) {
  170. // String
  171. this[i][props] = value;
  172. } else {
  173. // Object
  174. for (const propName in props) {
  175. this[i][propName] = props[propName];
  176. }
  177. }
  178. }
  179. return this;
  180. }
  181. return this;
  182. }
  183. function data(key, value) {
  184. let el;
  185. if (typeof value === 'undefined') {
  186. el = this[0];
  187. if (!el) return undefined; // Get value
  188. if (el.dom7ElementDataStorage && key in el.dom7ElementDataStorage) {
  189. return el.dom7ElementDataStorage[key];
  190. }
  191. const dataKey = el.getAttribute(`data-${key}`);
  192. if (dataKey) {
  193. return dataKey;
  194. }
  195. return undefined;
  196. } // Set value
  197. for (let i = 0; i < this.length; i += 1) {
  198. el = this[i];
  199. if (!el.dom7ElementDataStorage) el.dom7ElementDataStorage = {};
  200. el.dom7ElementDataStorage[key] = value;
  201. }
  202. return this;
  203. }
  204. function removeData(key) {
  205. for (let i = 0; i < this.length; i += 1) {
  206. const el = this[i];
  207. if (el.dom7ElementDataStorage && el.dom7ElementDataStorage[key]) {
  208. el.dom7ElementDataStorage[key] = null;
  209. delete el.dom7ElementDataStorage[key];
  210. }
  211. }
  212. }
  213. function dataset() {
  214. const el = this[0];
  215. if (!el) return undefined;
  216. const dataset = {}; // eslint-disable-line
  217. if (el.dataset) {
  218. for (const dataKey in el.dataset) {
  219. dataset[dataKey] = el.dataset[dataKey];
  220. }
  221. } else {
  222. for (let i = 0; i < el.attributes.length; i += 1) {
  223. const attr = el.attributes[i];
  224. if (attr.name.indexOf('data-') >= 0) {
  225. dataset[toCamelCase(attr.name.split('data-')[1])] = attr.value;
  226. }
  227. }
  228. }
  229. for (const key in dataset) {
  230. if (dataset[key] === 'false') dataset[key] = false;else if (dataset[key] === 'true') dataset[key] = true;else if (parseFloat(dataset[key]) === dataset[key] * 1) dataset[key] *= 1;
  231. }
  232. return dataset;
  233. }
  234. function val(value) {
  235. if (typeof value === 'undefined') {
  236. // get value
  237. const el = this[0];
  238. if (!el) return undefined;
  239. if (el.multiple && el.nodeName.toLowerCase() === 'select') {
  240. const values = [];
  241. for (let i = 0; i < el.selectedOptions.length; i += 1) {
  242. values.push(el.selectedOptions[i].value);
  243. }
  244. return values;
  245. }
  246. return el.value;
  247. } // set value
  248. for (let i = 0; i < this.length; i += 1) {
  249. const el = this[i];
  250. if (Array.isArray(value) && el.multiple && el.nodeName.toLowerCase() === 'select') {
  251. for (let j = 0; j < el.options.length; j += 1) {
  252. el.options[j].selected = value.indexOf(el.options[j].value) >= 0;
  253. }
  254. } else {
  255. el.value = value;
  256. }
  257. }
  258. return this;
  259. }
  260. function value(value) {
  261. return this.val(value);
  262. }
  263. function transform(transform) {
  264. for (let i = 0; i < this.length; i += 1) {
  265. this[i].style.transform = transform;
  266. }
  267. return this;
  268. }
  269. function transition(duration) {
  270. for (let i = 0; i < this.length; i += 1) {
  271. this[i].style.transitionDuration = typeof duration !== 'string' ? `${duration}ms` : duration;
  272. }
  273. return this;
  274. }
  275. function on(...args) {
  276. let [eventType, targetSelector, listener, capture] = args;
  277. if (typeof args[1] === 'function') {
  278. [eventType, listener, capture] = args;
  279. targetSelector = undefined;
  280. }
  281. if (!capture) capture = false;
  282. function handleLiveEvent(e) {
  283. const target = e.target;
  284. if (!target) return;
  285. const eventData = e.target.dom7EventData || [];
  286. if (eventData.indexOf(e) < 0) {
  287. eventData.unshift(e);
  288. }
  289. if ($(target).is(targetSelector)) listener.apply(target, eventData);else {
  290. const parents = $(target).parents(); // eslint-disable-line
  291. for (let k = 0; k < parents.length; k += 1) {
  292. if ($(parents[k]).is(targetSelector)) listener.apply(parents[k], eventData);
  293. }
  294. }
  295. }
  296. function handleEvent(e) {
  297. const eventData = e && e.target ? e.target.dom7EventData || [] : [];
  298. if (eventData.indexOf(e) < 0) {
  299. eventData.unshift(e);
  300. }
  301. listener.apply(this, eventData);
  302. }
  303. const events = eventType.split(' ');
  304. let j;
  305. for (let i = 0; i < this.length; i += 1) {
  306. const el = this[i];
  307. if (!targetSelector) {
  308. for (j = 0; j < events.length; j += 1) {
  309. const event = events[j];
  310. if (!el.dom7Listeners) el.dom7Listeners = {};
  311. if (!el.dom7Listeners[event]) el.dom7Listeners[event] = [];
  312. el.dom7Listeners[event].push({
  313. listener,
  314. proxyListener: handleEvent
  315. });
  316. el.addEventListener(event, handleEvent, capture);
  317. }
  318. } else {
  319. // Live events
  320. for (j = 0; j < events.length; j += 1) {
  321. const event = events[j];
  322. if (!el.dom7LiveListeners) el.dom7LiveListeners = {};
  323. if (!el.dom7LiveListeners[event]) el.dom7LiveListeners[event] = [];
  324. el.dom7LiveListeners[event].push({
  325. listener,
  326. proxyListener: handleLiveEvent
  327. });
  328. el.addEventListener(event, handleLiveEvent, capture);
  329. }
  330. }
  331. }
  332. return this;
  333. }
  334. function off(...args) {
  335. let [eventType, targetSelector, listener, capture] = args;
  336. if (typeof args[1] === 'function') {
  337. [eventType, listener, capture] = args;
  338. targetSelector = undefined;
  339. }
  340. if (!capture) capture = false;
  341. const events = eventType.split(' ');
  342. for (let i = 0; i < events.length; i += 1) {
  343. const event = events[i];
  344. for (let j = 0; j < this.length; j += 1) {
  345. const el = this[j];
  346. let handlers;
  347. if (!targetSelector && el.dom7Listeners) {
  348. handlers = el.dom7Listeners[event];
  349. } else if (targetSelector && el.dom7LiveListeners) {
  350. handlers = el.dom7LiveListeners[event];
  351. }
  352. if (handlers && handlers.length) {
  353. for (let k = handlers.length - 1; k >= 0; k -= 1) {
  354. const handler = handlers[k];
  355. if (listener && handler.listener === listener) {
  356. el.removeEventListener(event, handler.proxyListener, capture);
  357. handlers.splice(k, 1);
  358. } else if (listener && handler.listener && handler.listener.dom7proxy && handler.listener.dom7proxy === listener) {
  359. el.removeEventListener(event, handler.proxyListener, capture);
  360. handlers.splice(k, 1);
  361. } else if (!listener) {
  362. el.removeEventListener(event, handler.proxyListener, capture);
  363. handlers.splice(k, 1);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. return this;
  370. }
  371. function once(...args) {
  372. const dom = this;
  373. let [eventName, targetSelector, listener, capture] = args;
  374. if (typeof args[1] === 'function') {
  375. [eventName, listener, capture] = args;
  376. targetSelector = undefined;
  377. }
  378. function onceHandler(...eventArgs) {
  379. listener.apply(this, eventArgs);
  380. dom.off(eventName, targetSelector, onceHandler, capture);
  381. if (onceHandler.dom7proxy) {
  382. delete onceHandler.dom7proxy;
  383. }
  384. }
  385. onceHandler.dom7proxy = listener;
  386. return dom.on(eventName, targetSelector, onceHandler, capture);
  387. }
  388. function trigger(...args) {
  389. const window = getWindow();
  390. const events = args[0].split(' ');
  391. const eventData = args[1];
  392. for (let i = 0; i < events.length; i += 1) {
  393. const event = events[i];
  394. for (let j = 0; j < this.length; j += 1) {
  395. const el = this[j];
  396. if (window.CustomEvent) {
  397. const evt = new window.CustomEvent(event, {
  398. detail: eventData,
  399. bubbles: true,
  400. cancelable: true
  401. });
  402. el.dom7EventData = args.filter((data, dataIndex) => dataIndex > 0);
  403. el.dispatchEvent(evt);
  404. el.dom7EventData = [];
  405. delete el.dom7EventData;
  406. }
  407. }
  408. }
  409. return this;
  410. }
  411. function transitionStart(callback) {
  412. const dom = this;
  413. function fireCallBack(e) {
  414. if (e.target !== this) return;
  415. callback.call(this, e);
  416. dom.off('transitionstart', fireCallBack);
  417. }
  418. if (callback) {
  419. dom.on('transitionstart', fireCallBack);
  420. }
  421. return this;
  422. }
  423. function transitionEnd(callback) {
  424. const dom = this;
  425. function fireCallBack(e) {
  426. if (e.target !== this) return;
  427. callback.call(this, e);
  428. dom.off('transitionend', fireCallBack);
  429. }
  430. if (callback) {
  431. dom.on('transitionend', fireCallBack);
  432. }
  433. return this;
  434. }
  435. function animationEnd(callback) {
  436. const dom = this;
  437. function fireCallBack(e) {
  438. if (e.target !== this) return;
  439. callback.call(this, e);
  440. dom.off('animationend', fireCallBack);
  441. }
  442. if (callback) {
  443. dom.on('animationend', fireCallBack);
  444. }
  445. return this;
  446. }
  447. function width() {
  448. const window = getWindow();
  449. if (this[0] === window) {
  450. return window.innerWidth;
  451. }
  452. if (this.length > 0) {
  453. return parseFloat(this.css('width'));
  454. }
  455. return null;
  456. }
  457. function outerWidth(includeMargins) {
  458. if (this.length > 0) {
  459. if (includeMargins) {
  460. const styles = this.styles();
  461. return this[0].offsetWidth + parseFloat(styles.getPropertyValue('margin-right')) + parseFloat(styles.getPropertyValue('margin-left'));
  462. }
  463. return this[0].offsetWidth;
  464. }
  465. return null;
  466. }
  467. function height() {
  468. const window = getWindow();
  469. if (this[0] === window) {
  470. return window.innerHeight;
  471. }
  472. if (this.length > 0) {
  473. return parseFloat(this.css('height'));
  474. }
  475. return null;
  476. }
  477. function outerHeight(includeMargins) {
  478. if (this.length > 0) {
  479. if (includeMargins) {
  480. const styles = this.styles();
  481. return this[0].offsetHeight + parseFloat(styles.getPropertyValue('margin-top')) + parseFloat(styles.getPropertyValue('margin-bottom'));
  482. }
  483. return this[0].offsetHeight;
  484. }
  485. return null;
  486. }
  487. function offset() {
  488. if (this.length > 0) {
  489. const window = getWindow();
  490. const document = getDocument();
  491. const el = this[0];
  492. const box = el.getBoundingClientRect();
  493. const body = document.body;
  494. const clientTop = el.clientTop || body.clientTop || 0;
  495. const clientLeft = el.clientLeft || body.clientLeft || 0;
  496. const scrollTop = el === window ? window.scrollY : el.scrollTop;
  497. const scrollLeft = el === window ? window.scrollX : el.scrollLeft;
  498. return {
  499. top: box.top + scrollTop - clientTop,
  500. left: box.left + scrollLeft - clientLeft
  501. };
  502. }
  503. return null;
  504. }
  505. function hide() {
  506. for (let i = 0; i < this.length; i += 1) {
  507. this[i].style.display = 'none';
  508. }
  509. return this;
  510. }
  511. function show() {
  512. const window = getWindow();
  513. for (let i = 0; i < this.length; i += 1) {
  514. const el = this[i];
  515. if (el.style.display === 'none') {
  516. el.style.display = '';
  517. }
  518. if (window.getComputedStyle(el, null).getPropertyValue('display') === 'none') {
  519. // Still not visible
  520. el.style.display = 'block';
  521. }
  522. }
  523. return this;
  524. }
  525. function styles() {
  526. const window = getWindow();
  527. if (this[0]) return window.getComputedStyle(this[0], null);
  528. return {};
  529. }
  530. function css(props, value) {
  531. const window = getWindow();
  532. let i;
  533. if (arguments.length === 1) {
  534. if (typeof props === 'string') {
  535. // .css('width')
  536. if (this[0]) return window.getComputedStyle(this[0], null).getPropertyValue(props);
  537. } else {
  538. // .css({ width: '100px' })
  539. for (i = 0; i < this.length; i += 1) {
  540. for (const prop in props) {
  541. this[i].style[prop] = props[prop];
  542. }
  543. }
  544. return this;
  545. }
  546. }
  547. if (arguments.length === 2 && typeof props === 'string') {
  548. // .css('width', '100px')
  549. for (i = 0; i < this.length; i += 1) {
  550. this[i].style[props] = value;
  551. }
  552. return this;
  553. }
  554. return this;
  555. }
  556. function each(callback) {
  557. if (!callback) return this;
  558. this.forEach((el, index) => {
  559. callback.apply(el, [el, index]);
  560. });
  561. return this;
  562. }
  563. function filter(callback) {
  564. const result = arrayFilter(this, callback);
  565. return $(result);
  566. }
  567. function html(html) {
  568. if (typeof html === 'undefined') {
  569. return this[0] ? this[0].innerHTML : null;
  570. }
  571. for (let i = 0; i < this.length; i += 1) {
  572. this[i].innerHTML = html;
  573. }
  574. return this;
  575. }
  576. function text(text) {
  577. if (typeof text === 'undefined') {
  578. return this[0] ? this[0].textContent.trim() : null;
  579. }
  580. for (let i = 0; i < this.length; i += 1) {
  581. this[i].textContent = text;
  582. }
  583. return this;
  584. }
  585. function is(selector) {
  586. const window = getWindow();
  587. const document = getDocument();
  588. const el = this[0];
  589. let compareWith;
  590. let i;
  591. if (!el || typeof selector === 'undefined') return false;
  592. if (typeof selector === 'string') {
  593. if (el.matches) return el.matches(selector);
  594. if (el.webkitMatchesSelector) return el.webkitMatchesSelector(selector);
  595. if (el.msMatchesSelector) return el.msMatchesSelector(selector);
  596. compareWith = $(selector);
  597. for (i = 0; i < compareWith.length; i += 1) {
  598. if (compareWith[i] === el) return true;
  599. }
  600. return false;
  601. }
  602. if (selector === document) {
  603. return el === document;
  604. }
  605. if (selector === window) {
  606. return el === window;
  607. }
  608. if (selector.nodeType || selector instanceof Dom7) {
  609. compareWith = selector.nodeType ? [selector] : selector;
  610. for (i = 0; i < compareWith.length; i += 1) {
  611. if (compareWith[i] === el) return true;
  612. }
  613. return false;
  614. }
  615. return false;
  616. }
  617. function index() {
  618. let child = this[0];
  619. let i;
  620. if (child) {
  621. i = 0; // eslint-disable-next-line
  622. while ((child = child.previousSibling) !== null) {
  623. if (child.nodeType === 1) i += 1;
  624. }
  625. return i;
  626. }
  627. return undefined;
  628. }
  629. function eq(index) {
  630. if (typeof index === 'undefined') return this;
  631. const length = this.length;
  632. if (index > length - 1) {
  633. return $([]);
  634. }
  635. if (index < 0) {
  636. const returnIndex = length + index;
  637. if (returnIndex < 0) return $([]);
  638. return $([this[returnIndex]]);
  639. }
  640. return $([this[index]]);
  641. }
  642. function append(...els) {
  643. let newChild;
  644. const document = getDocument();
  645. for (let k = 0; k < els.length; k += 1) {
  646. newChild = els[k];
  647. for (let i = 0; i < this.length; i += 1) {
  648. if (typeof newChild === 'string') {
  649. const tempDiv = document.createElement('div');
  650. tempDiv.innerHTML = newChild;
  651. while (tempDiv.firstChild) {
  652. this[i].appendChild(tempDiv.firstChild);
  653. }
  654. } else if (newChild instanceof Dom7) {
  655. for (let j = 0; j < newChild.length; j += 1) {
  656. this[i].appendChild(newChild[j]);
  657. }
  658. } else {
  659. this[i].appendChild(newChild);
  660. }
  661. }
  662. }
  663. return this;
  664. }
  665. function appendTo(parent) {
  666. $(parent).append(this);
  667. return this;
  668. }
  669. function prepend(newChild) {
  670. const document = getDocument();
  671. let i;
  672. let j;
  673. for (i = 0; i < this.length; i += 1) {
  674. if (typeof newChild === 'string') {
  675. const tempDiv = document.createElement('div');
  676. tempDiv.innerHTML = newChild;
  677. for (j = tempDiv.childNodes.length - 1; j >= 0; j -= 1) {
  678. this[i].insertBefore(tempDiv.childNodes[j], this[i].childNodes[0]);
  679. }
  680. } else if (newChild instanceof Dom7) {
  681. for (j = 0; j < newChild.length; j += 1) {
  682. this[i].insertBefore(newChild[j], this[i].childNodes[0]);
  683. }
  684. } else {
  685. this[i].insertBefore(newChild, this[i].childNodes[0]);
  686. }
  687. }
  688. return this;
  689. }
  690. function prependTo(parent) {
  691. $(parent).prepend(this);
  692. return this;
  693. }
  694. function insertBefore(selector) {
  695. const before = $(selector);
  696. for (let i = 0; i < this.length; i += 1) {
  697. if (before.length === 1) {
  698. before[0].parentNode.insertBefore(this[i], before[0]);
  699. } else if (before.length > 1) {
  700. for (let j = 0; j < before.length; j += 1) {
  701. before[j].parentNode.insertBefore(this[i].cloneNode(true), before[j]);
  702. }
  703. }
  704. }
  705. }
  706. function insertAfter(selector) {
  707. const after = $(selector);
  708. for (let i = 0; i < this.length; i += 1) {
  709. if (after.length === 1) {
  710. after[0].parentNode.insertBefore(this[i], after[0].nextSibling);
  711. } else if (after.length > 1) {
  712. for (let j = 0; j < after.length; j += 1) {
  713. after[j].parentNode.insertBefore(this[i].cloneNode(true), after[j].nextSibling);
  714. }
  715. }
  716. }
  717. }
  718. function next(selector) {
  719. if (this.length > 0) {
  720. if (selector) {
  721. if (this[0].nextElementSibling && $(this[0].nextElementSibling).is(selector)) {
  722. return $([this[0].nextElementSibling]);
  723. }
  724. return $([]);
  725. }
  726. if (this[0].nextElementSibling) return $([this[0].nextElementSibling]);
  727. return $([]);
  728. }
  729. return $([]);
  730. }
  731. function nextAll(selector) {
  732. const nextEls = [];
  733. let el = this[0];
  734. if (!el) return $([]);
  735. while (el.nextElementSibling) {
  736. const next = el.nextElementSibling; // eslint-disable-line
  737. if (selector) {
  738. if ($(next).is(selector)) nextEls.push(next);
  739. } else nextEls.push(next);
  740. el = next;
  741. }
  742. return $(nextEls);
  743. }
  744. function prev(selector) {
  745. if (this.length > 0) {
  746. const el = this[0];
  747. if (selector) {
  748. if (el.previousElementSibling && $(el.previousElementSibling).is(selector)) {
  749. return $([el.previousElementSibling]);
  750. }
  751. return $([]);
  752. }
  753. if (el.previousElementSibling) return $([el.previousElementSibling]);
  754. return $([]);
  755. }
  756. return $([]);
  757. }
  758. function prevAll(selector) {
  759. const prevEls = [];
  760. let el = this[0];
  761. if (!el) return $([]);
  762. while (el.previousElementSibling) {
  763. const prev = el.previousElementSibling; // eslint-disable-line
  764. if (selector) {
  765. if ($(prev).is(selector)) prevEls.push(prev);
  766. } else prevEls.push(prev);
  767. el = prev;
  768. }
  769. return $(prevEls);
  770. }
  771. function siblings(selector) {
  772. return this.nextAll(selector).add(this.prevAll(selector));
  773. }
  774. function parent(selector) {
  775. const parents = []; // eslint-disable-line
  776. for (let i = 0; i < this.length; i += 1) {
  777. if (this[i].parentNode !== null) {
  778. if (selector) {
  779. if ($(this[i].parentNode).is(selector)) parents.push(this[i].parentNode);
  780. } else {
  781. parents.push(this[i].parentNode);
  782. }
  783. }
  784. }
  785. return $(parents);
  786. }
  787. function parents(selector) {
  788. const parents = []; // eslint-disable-line
  789. for (let i = 0; i < this.length; i += 1) {
  790. let parent = this[i].parentNode; // eslint-disable-line
  791. while (parent) {
  792. if (selector) {
  793. if ($(parent).is(selector)) parents.push(parent);
  794. } else {
  795. parents.push(parent);
  796. }
  797. parent = parent.parentNode;
  798. }
  799. }
  800. return $(parents);
  801. }
  802. function closest(selector) {
  803. let closest = this; // eslint-disable-line
  804. if (typeof selector === 'undefined') {
  805. return $([]);
  806. }
  807. if (!closest.is(selector)) {
  808. closest = closest.parents(selector).eq(0);
  809. }
  810. return closest;
  811. }
  812. function find(selector) {
  813. const foundElements = [];
  814. for (let i = 0; i < this.length; i += 1) {
  815. const found = this[i].querySelectorAll(selector);
  816. for (let j = 0; j < found.length; j += 1) {
  817. foundElements.push(found[j]);
  818. }
  819. }
  820. return $(foundElements);
  821. }
  822. function children(selector) {
  823. const children = []; // eslint-disable-line
  824. for (let i = 0; i < this.length; i += 1) {
  825. const childNodes = this[i].children;
  826. for (let j = 0; j < childNodes.length; j += 1) {
  827. if (!selector || $(childNodes[j]).is(selector)) {
  828. children.push(childNodes[j]);
  829. }
  830. }
  831. }
  832. return $(children);
  833. }
  834. function remove() {
  835. for (let i = 0; i < this.length; i += 1) {
  836. if (this[i].parentNode) this[i].parentNode.removeChild(this[i]);
  837. }
  838. return this;
  839. }
  840. function detach() {
  841. return this.remove();
  842. }
  843. function add(...els) {
  844. const dom = this;
  845. let i;
  846. let j;
  847. for (i = 0; i < els.length; i += 1) {
  848. const toAdd = $(els[i]);
  849. for (j = 0; j < toAdd.length; j += 1) {
  850. dom.push(toAdd[j]);
  851. }
  852. }
  853. return dom;
  854. }
  855. function empty() {
  856. for (let i = 0; i < this.length; i += 1) {
  857. const el = this[i];
  858. if (el.nodeType === 1) {
  859. for (let j = 0; j < el.childNodes.length; j += 1) {
  860. if (el.childNodes[j].parentNode) {
  861. el.childNodes[j].parentNode.removeChild(el.childNodes[j]);
  862. }
  863. }
  864. el.textContent = '';
  865. }
  866. }
  867. return this;
  868. }
  869. // eslint-disable-next-line
  870. function scrollTo(...args) {
  871. const window = getWindow();
  872. let [left, top, duration, easing, callback] = args;
  873. if (args.length === 4 && typeof easing === 'function') {
  874. callback = easing;
  875. [left, top, duration, callback, easing] = args;
  876. }
  877. if (typeof easing === 'undefined') easing = 'swing';
  878. return this.each(function animate() {
  879. const el = this;
  880. let currentTop;
  881. let currentLeft;
  882. let maxTop;
  883. let maxLeft;
  884. let newTop;
  885. let newLeft;
  886. let scrollTop; // eslint-disable-line
  887. let scrollLeft; // eslint-disable-line
  888. let animateTop = top > 0 || top === 0;
  889. let animateLeft = left > 0 || left === 0;
  890. if (typeof easing === 'undefined') {
  891. easing = 'swing';
  892. }
  893. if (animateTop) {
  894. currentTop = el.scrollTop;
  895. if (!duration) {
  896. el.scrollTop = top;
  897. }
  898. }
  899. if (animateLeft) {
  900. currentLeft = el.scrollLeft;
  901. if (!duration) {
  902. el.scrollLeft = left;
  903. }
  904. }
  905. if (!duration) return;
  906. if (animateTop) {
  907. maxTop = el.scrollHeight - el.offsetHeight;
  908. newTop = Math.max(Math.min(top, maxTop), 0);
  909. }
  910. if (animateLeft) {
  911. maxLeft = el.scrollWidth - el.offsetWidth;
  912. newLeft = Math.max(Math.min(left, maxLeft), 0);
  913. }
  914. let startTime = null;
  915. if (animateTop && newTop === currentTop) animateTop = false;
  916. if (animateLeft && newLeft === currentLeft) animateLeft = false;
  917. function render(time = new Date().getTime()) {
  918. if (startTime === null) {
  919. startTime = time;
  920. }
  921. const progress = Math.max(Math.min((time - startTime) / duration, 1), 0);
  922. const easeProgress = easing === 'linear' ? progress : 0.5 - Math.cos(progress * Math.PI) / 2;
  923. let done;
  924. if (animateTop) scrollTop = currentTop + easeProgress * (newTop - currentTop);
  925. if (animateLeft) scrollLeft = currentLeft + easeProgress * (newLeft - currentLeft);
  926. if (animateTop && newTop > currentTop && scrollTop >= newTop) {
  927. el.scrollTop = newTop;
  928. done = true;
  929. }
  930. if (animateTop && newTop < currentTop && scrollTop <= newTop) {
  931. el.scrollTop = newTop;
  932. done = true;
  933. }
  934. if (animateLeft && newLeft > currentLeft && scrollLeft >= newLeft) {
  935. el.scrollLeft = newLeft;
  936. done = true;
  937. }
  938. if (animateLeft && newLeft < currentLeft && scrollLeft <= newLeft) {
  939. el.scrollLeft = newLeft;
  940. done = true;
  941. }
  942. if (done) {
  943. if (callback) callback();
  944. return;
  945. }
  946. if (animateTop) el.scrollTop = scrollTop;
  947. if (animateLeft) el.scrollLeft = scrollLeft;
  948. window.requestAnimationFrame(render);
  949. }
  950. window.requestAnimationFrame(render);
  951. });
  952. } // scrollTop(top, duration, easing, callback) {
  953. function scrollTop(...args) {
  954. let [top, duration, easing, callback] = args;
  955. if (args.length === 3 && typeof easing === 'function') {
  956. [top, duration, callback, easing] = args;
  957. }
  958. const dom = this;
  959. if (typeof top === 'undefined') {
  960. if (dom.length > 0) return dom[0].scrollTop;
  961. return null;
  962. }
  963. return dom.scrollTo(undefined, top, duration, easing, callback);
  964. }
  965. function scrollLeft(...args) {
  966. let [left, duration, easing, callback] = args;
  967. if (args.length === 3 && typeof easing === 'function') {
  968. [left, duration, callback, easing] = args;
  969. }
  970. const dom = this;
  971. if (typeof left === 'undefined') {
  972. if (dom.length > 0) return dom[0].scrollLeft;
  973. return null;
  974. }
  975. return dom.scrollTo(left, undefined, duration, easing, callback);
  976. }
  977. // eslint-disable-next-line
  978. function animate(initialProps, initialParams) {
  979. const window = getWindow();
  980. const els = this;
  981. const a = {
  982. props: Object.assign({}, initialProps),
  983. params: Object.assign({
  984. duration: 300,
  985. easing: 'swing' // or 'linear'
  986. /* Callbacks
  987. begin(elements)
  988. complete(elements)
  989. progress(elements, complete, remaining, start, tweenValue)
  990. */
  991. }, initialParams),
  992. elements: els,
  993. animating: false,
  994. que: [],
  995. easingProgress(easing, progress) {
  996. if (easing === 'swing') {
  997. return 0.5 - Math.cos(progress * Math.PI) / 2;
  998. }
  999. if (typeof easing === 'function') {
  1000. return easing(progress);
  1001. }
  1002. return progress;
  1003. },
  1004. stop() {
  1005. if (a.frameId) {
  1006. window.cancelAnimationFrame(a.frameId);
  1007. }
  1008. a.animating = false;
  1009. a.elements.each(el => {
  1010. const element = el;
  1011. delete element.dom7AnimateInstance;
  1012. });
  1013. a.que = [];
  1014. },
  1015. done(complete) {
  1016. a.animating = false;
  1017. a.elements.each(el => {
  1018. const element = el;
  1019. delete element.dom7AnimateInstance;
  1020. });
  1021. if (complete) complete(els);
  1022. if (a.que.length > 0) {
  1023. const que = a.que.shift();
  1024. a.animate(que[0], que[1]);
  1025. }
  1026. },
  1027. animate(props, params) {
  1028. if (a.animating) {
  1029. a.que.push([props, params]);
  1030. return a;
  1031. }
  1032. const elements = []; // Define & Cache Initials & Units
  1033. a.elements.each((el, index) => {
  1034. let initialFullValue;
  1035. let initialValue;
  1036. let unit;
  1037. let finalValue;
  1038. let finalFullValue;
  1039. if (!el.dom7AnimateInstance) a.elements[index].dom7AnimateInstance = a;
  1040. elements[index] = {
  1041. container: el
  1042. };
  1043. Object.keys(props).forEach(prop => {
  1044. initialFullValue = window.getComputedStyle(el, null).getPropertyValue(prop).replace(',', '.');
  1045. initialValue = parseFloat(initialFullValue);
  1046. unit = initialFullValue.replace(initialValue, '');
  1047. finalValue = parseFloat(props[prop]);
  1048. finalFullValue = props[prop] + unit;
  1049. elements[index][prop] = {
  1050. initialFullValue,
  1051. initialValue,
  1052. unit,
  1053. finalValue,
  1054. finalFullValue,
  1055. currentValue: initialValue
  1056. };
  1057. });
  1058. });
  1059. let startTime = null;
  1060. let time;
  1061. let elementsDone = 0;
  1062. let propsDone = 0;
  1063. let done;
  1064. let began = false;
  1065. a.animating = true;
  1066. function render() {
  1067. time = new Date().getTime();
  1068. let progress;
  1069. let easeProgress; // let el;
  1070. if (!began) {
  1071. began = true;
  1072. if (params.begin) params.begin(els);
  1073. }
  1074. if (startTime === null) {
  1075. startTime = time;
  1076. }
  1077. if (params.progress) {
  1078. // eslint-disable-next-line
  1079. params.progress(els, Math.max(Math.min((time - startTime) / params.duration, 1), 0), startTime + params.duration - time < 0 ? 0 : startTime + params.duration - time, startTime);
  1080. }
  1081. elements.forEach(element => {
  1082. const el = element;
  1083. if (done || el.done) return;
  1084. Object.keys(props).forEach(prop => {
  1085. if (done || el.done) return;
  1086. progress = Math.max(Math.min((time - startTime) / params.duration, 1), 0);
  1087. easeProgress = a.easingProgress(params.easing, progress);
  1088. const {
  1089. initialValue,
  1090. finalValue,
  1091. unit
  1092. } = el[prop];
  1093. el[prop].currentValue = initialValue + easeProgress * (finalValue - initialValue);
  1094. const currentValue = el[prop].currentValue;
  1095. if (finalValue > initialValue && currentValue >= finalValue || finalValue < initialValue && currentValue <= finalValue) {
  1096. el.container.style[prop] = finalValue + unit;
  1097. propsDone += 1;
  1098. if (propsDone === Object.keys(props).length) {
  1099. el.done = true;
  1100. elementsDone += 1;
  1101. }
  1102. if (elementsDone === elements.length) {
  1103. done = true;
  1104. }
  1105. }
  1106. if (done) {
  1107. a.done(params.complete);
  1108. return;
  1109. }
  1110. el.container.style[prop] = currentValue + unit;
  1111. });
  1112. });
  1113. if (done) return; // Then call
  1114. a.frameId = window.requestAnimationFrame(render);
  1115. }
  1116. a.frameId = window.requestAnimationFrame(render);
  1117. return a;
  1118. }
  1119. };
  1120. if (a.elements.length === 0) {
  1121. return els;
  1122. }
  1123. let animateInstance;
  1124. for (let i = 0; i < a.elements.length; i += 1) {
  1125. if (a.elements[i].dom7AnimateInstance) {
  1126. animateInstance = a.elements[i].dom7AnimateInstance;
  1127. } else a.elements[i].dom7AnimateInstance = a;
  1128. }
  1129. if (!animateInstance) {
  1130. animateInstance = a;
  1131. }
  1132. if (initialProps === 'stop') {
  1133. animateInstance.stop();
  1134. } else {
  1135. animateInstance.animate(a.props, a.params);
  1136. }
  1137. return els;
  1138. }
  1139. function stop() {
  1140. const els = this;
  1141. for (let i = 0; i < els.length; i += 1) {
  1142. if (els[i].dom7AnimateInstance) {
  1143. els[i].dom7AnimateInstance.stop();
  1144. }
  1145. }
  1146. }
  1147. const noTrigger = 'resize scroll'.split(' ');
  1148. function shortcut(name) {
  1149. function eventHandler(...args) {
  1150. if (typeof args[0] === 'undefined') {
  1151. for (let i = 0; i < this.length; i += 1) {
  1152. if (noTrigger.indexOf(name) < 0) {
  1153. if (name in this[i]) this[i][name]();else {
  1154. $(this[i]).trigger(name);
  1155. }
  1156. }
  1157. }
  1158. return this;
  1159. }
  1160. return this.on(name, ...args);
  1161. }
  1162. return eventHandler;
  1163. }
  1164. const click = shortcut('click');
  1165. const blur = shortcut('blur');
  1166. const focus = shortcut('focus');
  1167. const focusin = shortcut('focusin');
  1168. const focusout = shortcut('focusout');
  1169. const keyup = shortcut('keyup');
  1170. const keydown = shortcut('keydown');
  1171. const keypress = shortcut('keypress');
  1172. const submit = shortcut('submit');
  1173. const change = shortcut('change');
  1174. const mousedown = shortcut('mousedown');
  1175. const mousemove = shortcut('mousemove');
  1176. const mouseup = shortcut('mouseup');
  1177. const mouseenter = shortcut('mouseenter');
  1178. const mouseleave = shortcut('mouseleave');
  1179. const mouseout = shortcut('mouseout');
  1180. const mouseover = shortcut('mouseover');
  1181. const touchstart = shortcut('touchstart');
  1182. const touchend = shortcut('touchend');
  1183. const touchmove = shortcut('touchmove');
  1184. const resize = shortcut('resize');
  1185. const scroll = shortcut('scroll');
  1186. export default $;
  1187. export { $, add, addClass, animate, animationEnd, append, appendTo, attr, blur, change, children, click, closest, css, data, dataset, detach, each, empty, eq, filter, find, focus, focusin, focusout, hasClass, height, hide, html, index, insertAfter, insertBefore, is, keydown, keypress, keyup, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, mouseup, next, nextAll, off, offset, on, once, outerHeight, outerWidth, parent, parents, prepend, prependTo, prev, prevAll, prop, remove, removeAttr, removeClass, removeData, resize, scroll, scrollLeft, scrollTo, scrollTop, show, siblings, stop, styles, submit, text, toggleClass, touchend, touchmove, touchstart, transform, transition, transitionEnd, transitionStart, trigger, val, value, width };