dom7.esm.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /**
  2. * Dom7 4.0.4
  3. * Minimalistic JavaScript library for DOM manipulation, with a jQuery-compatible API
  4. * https://framework7.io/docs/dom7.html
  5. *
  6. * Copyright 2022, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: January 11, 2022
  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 transitionEnd(callback) {
  412. const dom = this;
  413. function fireCallBack(e) {
  414. if (e.target !== this) return;
  415. callback.call(this, e);
  416. dom.off('transitionend', fireCallBack);
  417. }
  418. if (callback) {
  419. dom.on('transitionend', fireCallBack);
  420. }
  421. return this;
  422. }
  423. function animationEnd(callback) {
  424. const dom = this;
  425. function fireCallBack(e) {
  426. if (e.target !== this) return;
  427. callback.call(this, e);
  428. dom.off('animationend', fireCallBack);
  429. }
  430. if (callback) {
  431. dom.on('animationend', fireCallBack);
  432. }
  433. return this;
  434. }
  435. function width() {
  436. const window = getWindow();
  437. if (this[0] === window) {
  438. return window.innerWidth;
  439. }
  440. if (this.length > 0) {
  441. return parseFloat(this.css('width'));
  442. }
  443. return null;
  444. }
  445. function outerWidth(includeMargins) {
  446. if (this.length > 0) {
  447. if (includeMargins) {
  448. const styles = this.styles();
  449. return this[0].offsetWidth + parseFloat(styles.getPropertyValue('margin-right')) + parseFloat(styles.getPropertyValue('margin-left'));
  450. }
  451. return this[0].offsetWidth;
  452. }
  453. return null;
  454. }
  455. function height() {
  456. const window = getWindow();
  457. if (this[0] === window) {
  458. return window.innerHeight;
  459. }
  460. if (this.length > 0) {
  461. return parseFloat(this.css('height'));
  462. }
  463. return null;
  464. }
  465. function outerHeight(includeMargins) {
  466. if (this.length > 0) {
  467. if (includeMargins) {
  468. const styles = this.styles();
  469. return this[0].offsetHeight + parseFloat(styles.getPropertyValue('margin-top')) + parseFloat(styles.getPropertyValue('margin-bottom'));
  470. }
  471. return this[0].offsetHeight;
  472. }
  473. return null;
  474. }
  475. function offset() {
  476. if (this.length > 0) {
  477. const window = getWindow();
  478. const document = getDocument();
  479. const el = this[0];
  480. const box = el.getBoundingClientRect();
  481. const body = document.body;
  482. const clientTop = el.clientTop || body.clientTop || 0;
  483. const clientLeft = el.clientLeft || body.clientLeft || 0;
  484. const scrollTop = el === window ? window.scrollY : el.scrollTop;
  485. const scrollLeft = el === window ? window.scrollX : el.scrollLeft;
  486. return {
  487. top: box.top + scrollTop - clientTop,
  488. left: box.left + scrollLeft - clientLeft
  489. };
  490. }
  491. return null;
  492. }
  493. function hide() {
  494. for (let i = 0; i < this.length; i += 1) {
  495. this[i].style.display = 'none';
  496. }
  497. return this;
  498. }
  499. function show() {
  500. const window = getWindow();
  501. for (let i = 0; i < this.length; i += 1) {
  502. const el = this[i];
  503. if (el.style.display === 'none') {
  504. el.style.display = '';
  505. }
  506. if (window.getComputedStyle(el, null).getPropertyValue('display') === 'none') {
  507. // Still not visible
  508. el.style.display = 'block';
  509. }
  510. }
  511. return this;
  512. }
  513. function styles() {
  514. const window = getWindow();
  515. if (this[0]) return window.getComputedStyle(this[0], null);
  516. return {};
  517. }
  518. function css(props, value) {
  519. const window = getWindow();
  520. let i;
  521. if (arguments.length === 1) {
  522. if (typeof props === 'string') {
  523. // .css('width')
  524. if (this[0]) return window.getComputedStyle(this[0], null).getPropertyValue(props);
  525. } else {
  526. // .css({ width: '100px' })
  527. for (i = 0; i < this.length; i += 1) {
  528. for (const prop in props) {
  529. this[i].style[prop] = props[prop];
  530. }
  531. }
  532. return this;
  533. }
  534. }
  535. if (arguments.length === 2 && typeof props === 'string') {
  536. // .css('width', '100px')
  537. for (i = 0; i < this.length; i += 1) {
  538. this[i].style[props] = value;
  539. }
  540. return this;
  541. }
  542. return this;
  543. }
  544. function each(callback) {
  545. if (!callback) return this;
  546. this.forEach((el, index) => {
  547. callback.apply(el, [el, index]);
  548. });
  549. return this;
  550. }
  551. function filter(callback) {
  552. const result = arrayFilter(this, callback);
  553. return $(result);
  554. }
  555. function html(html) {
  556. if (typeof html === 'undefined') {
  557. return this[0] ? this[0].innerHTML : null;
  558. }
  559. for (let i = 0; i < this.length; i += 1) {
  560. this[i].innerHTML = html;
  561. }
  562. return this;
  563. }
  564. function text(text) {
  565. if (typeof text === 'undefined') {
  566. return this[0] ? this[0].textContent.trim() : null;
  567. }
  568. for (let i = 0; i < this.length; i += 1) {
  569. this[i].textContent = text;
  570. }
  571. return this;
  572. }
  573. function is(selector) {
  574. const window = getWindow();
  575. const document = getDocument();
  576. const el = this[0];
  577. let compareWith;
  578. let i;
  579. if (!el || typeof selector === 'undefined') return false;
  580. if (typeof selector === 'string') {
  581. if (el.matches) return el.matches(selector);
  582. if (el.webkitMatchesSelector) return el.webkitMatchesSelector(selector);
  583. if (el.msMatchesSelector) return el.msMatchesSelector(selector);
  584. compareWith = $(selector);
  585. for (i = 0; i < compareWith.length; i += 1) {
  586. if (compareWith[i] === el) return true;
  587. }
  588. return false;
  589. }
  590. if (selector === document) {
  591. return el === document;
  592. }
  593. if (selector === window) {
  594. return el === window;
  595. }
  596. if (selector.nodeType || selector instanceof Dom7) {
  597. compareWith = selector.nodeType ? [selector] : selector;
  598. for (i = 0; i < compareWith.length; i += 1) {
  599. if (compareWith[i] === el) return true;
  600. }
  601. return false;
  602. }
  603. return false;
  604. }
  605. function index() {
  606. let child = this[0];
  607. let i;
  608. if (child) {
  609. i = 0; // eslint-disable-next-line
  610. while ((child = child.previousSibling) !== null) {
  611. if (child.nodeType === 1) i += 1;
  612. }
  613. return i;
  614. }
  615. return undefined;
  616. }
  617. function eq(index) {
  618. if (typeof index === 'undefined') return this;
  619. const length = this.length;
  620. if (index > length - 1) {
  621. return $([]);
  622. }
  623. if (index < 0) {
  624. const returnIndex = length + index;
  625. if (returnIndex < 0) return $([]);
  626. return $([this[returnIndex]]);
  627. }
  628. return $([this[index]]);
  629. }
  630. function append(...els) {
  631. let newChild;
  632. const document = getDocument();
  633. for (let k = 0; k < els.length; k += 1) {
  634. newChild = els[k];
  635. for (let i = 0; i < this.length; i += 1) {
  636. if (typeof newChild === 'string') {
  637. const tempDiv = document.createElement('div');
  638. tempDiv.innerHTML = newChild;
  639. while (tempDiv.firstChild) {
  640. this[i].appendChild(tempDiv.firstChild);
  641. }
  642. } else if (newChild instanceof Dom7) {
  643. for (let j = 0; j < newChild.length; j += 1) {
  644. this[i].appendChild(newChild[j]);
  645. }
  646. } else {
  647. this[i].appendChild(newChild);
  648. }
  649. }
  650. }
  651. return this;
  652. }
  653. function appendTo(parent) {
  654. $(parent).append(this);
  655. return this;
  656. }
  657. function prepend(newChild) {
  658. const document = getDocument();
  659. let i;
  660. let j;
  661. for (i = 0; i < this.length; i += 1) {
  662. if (typeof newChild === 'string') {
  663. const tempDiv = document.createElement('div');
  664. tempDiv.innerHTML = newChild;
  665. for (j = tempDiv.childNodes.length - 1; j >= 0; j -= 1) {
  666. this[i].insertBefore(tempDiv.childNodes[j], this[i].childNodes[0]);
  667. }
  668. } else if (newChild instanceof Dom7) {
  669. for (j = 0; j < newChild.length; j += 1) {
  670. this[i].insertBefore(newChild[j], this[i].childNodes[0]);
  671. }
  672. } else {
  673. this[i].insertBefore(newChild, this[i].childNodes[0]);
  674. }
  675. }
  676. return this;
  677. }
  678. function prependTo(parent) {
  679. $(parent).prepend(this);
  680. return this;
  681. }
  682. function insertBefore(selector) {
  683. const before = $(selector);
  684. for (let i = 0; i < this.length; i += 1) {
  685. if (before.length === 1) {
  686. before[0].parentNode.insertBefore(this[i], before[0]);
  687. } else if (before.length > 1) {
  688. for (let j = 0; j < before.length; j += 1) {
  689. before[j].parentNode.insertBefore(this[i].cloneNode(true), before[j]);
  690. }
  691. }
  692. }
  693. }
  694. function insertAfter(selector) {
  695. const after = $(selector);
  696. for (let i = 0; i < this.length; i += 1) {
  697. if (after.length === 1) {
  698. after[0].parentNode.insertBefore(this[i], after[0].nextSibling);
  699. } else if (after.length > 1) {
  700. for (let j = 0; j < after.length; j += 1) {
  701. after[j].parentNode.insertBefore(this[i].cloneNode(true), after[j].nextSibling);
  702. }
  703. }
  704. }
  705. }
  706. function next(selector) {
  707. if (this.length > 0) {
  708. if (selector) {
  709. if (this[0].nextElementSibling && $(this[0].nextElementSibling).is(selector)) {
  710. return $([this[0].nextElementSibling]);
  711. }
  712. return $([]);
  713. }
  714. if (this[0].nextElementSibling) return $([this[0].nextElementSibling]);
  715. return $([]);
  716. }
  717. return $([]);
  718. }
  719. function nextAll(selector) {
  720. const nextEls = [];
  721. let el = this[0];
  722. if (!el) return $([]);
  723. while (el.nextElementSibling) {
  724. const next = el.nextElementSibling; // eslint-disable-line
  725. if (selector) {
  726. if ($(next).is(selector)) nextEls.push(next);
  727. } else nextEls.push(next);
  728. el = next;
  729. }
  730. return $(nextEls);
  731. }
  732. function prev(selector) {
  733. if (this.length > 0) {
  734. const el = this[0];
  735. if (selector) {
  736. if (el.previousElementSibling && $(el.previousElementSibling).is(selector)) {
  737. return $([el.previousElementSibling]);
  738. }
  739. return $([]);
  740. }
  741. if (el.previousElementSibling) return $([el.previousElementSibling]);
  742. return $([]);
  743. }
  744. return $([]);
  745. }
  746. function prevAll(selector) {
  747. const prevEls = [];
  748. let el = this[0];
  749. if (!el) return $([]);
  750. while (el.previousElementSibling) {
  751. const prev = el.previousElementSibling; // eslint-disable-line
  752. if (selector) {
  753. if ($(prev).is(selector)) prevEls.push(prev);
  754. } else prevEls.push(prev);
  755. el = prev;
  756. }
  757. return $(prevEls);
  758. }
  759. function siblings(selector) {
  760. return this.nextAll(selector).add(this.prevAll(selector));
  761. }
  762. function parent(selector) {
  763. const parents = []; // eslint-disable-line
  764. for (let i = 0; i < this.length; i += 1) {
  765. if (this[i].parentNode !== null) {
  766. if (selector) {
  767. if ($(this[i].parentNode).is(selector)) parents.push(this[i].parentNode);
  768. } else {
  769. parents.push(this[i].parentNode);
  770. }
  771. }
  772. }
  773. return $(parents);
  774. }
  775. function parents(selector) {
  776. const parents = []; // eslint-disable-line
  777. for (let i = 0; i < this.length; i += 1) {
  778. let parent = this[i].parentNode; // eslint-disable-line
  779. while (parent) {
  780. if (selector) {
  781. if ($(parent).is(selector)) parents.push(parent);
  782. } else {
  783. parents.push(parent);
  784. }
  785. parent = parent.parentNode;
  786. }
  787. }
  788. return $(parents);
  789. }
  790. function closest(selector) {
  791. let closest = this; // eslint-disable-line
  792. if (typeof selector === 'undefined') {
  793. return $([]);
  794. }
  795. if (!closest.is(selector)) {
  796. closest = closest.parents(selector).eq(0);
  797. }
  798. return closest;
  799. }
  800. function find(selector) {
  801. const foundElements = [];
  802. for (let i = 0; i < this.length; i += 1) {
  803. const found = this[i].querySelectorAll(selector);
  804. for (let j = 0; j < found.length; j += 1) {
  805. foundElements.push(found[j]);
  806. }
  807. }
  808. return $(foundElements);
  809. }
  810. function children(selector) {
  811. const children = []; // eslint-disable-line
  812. for (let i = 0; i < this.length; i += 1) {
  813. const childNodes = this[i].children;
  814. for (let j = 0; j < childNodes.length; j += 1) {
  815. if (!selector || $(childNodes[j]).is(selector)) {
  816. children.push(childNodes[j]);
  817. }
  818. }
  819. }
  820. return $(children);
  821. }
  822. function remove() {
  823. for (let i = 0; i < this.length; i += 1) {
  824. if (this[i].parentNode) this[i].parentNode.removeChild(this[i]);
  825. }
  826. return this;
  827. }
  828. function detach() {
  829. return this.remove();
  830. }
  831. function add(...els) {
  832. const dom = this;
  833. let i;
  834. let j;
  835. for (i = 0; i < els.length; i += 1) {
  836. const toAdd = $(els[i]);
  837. for (j = 0; j < toAdd.length; j += 1) {
  838. dom.push(toAdd[j]);
  839. }
  840. }
  841. return dom;
  842. }
  843. function empty() {
  844. for (let i = 0; i < this.length; i += 1) {
  845. const el = this[i];
  846. if (el.nodeType === 1) {
  847. for (let j = 0; j < el.childNodes.length; j += 1) {
  848. if (el.childNodes[j].parentNode) {
  849. el.childNodes[j].parentNode.removeChild(el.childNodes[j]);
  850. }
  851. }
  852. el.textContent = '';
  853. }
  854. }
  855. return this;
  856. }
  857. // eslint-disable-next-line
  858. function scrollTo(...args) {
  859. const window = getWindow();
  860. let [left, top, duration, easing, callback] = args;
  861. if (args.length === 4 && typeof easing === 'function') {
  862. callback = easing;
  863. [left, top, duration, callback, easing] = args;
  864. }
  865. if (typeof easing === 'undefined') easing = 'swing';
  866. return this.each(function animate() {
  867. const el = this;
  868. let currentTop;
  869. let currentLeft;
  870. let maxTop;
  871. let maxLeft;
  872. let newTop;
  873. let newLeft;
  874. let scrollTop; // eslint-disable-line
  875. let scrollLeft; // eslint-disable-line
  876. let animateTop = top > 0 || top === 0;
  877. let animateLeft = left > 0 || left === 0;
  878. if (typeof easing === 'undefined') {
  879. easing = 'swing';
  880. }
  881. if (animateTop) {
  882. currentTop = el.scrollTop;
  883. if (!duration) {
  884. el.scrollTop = top;
  885. }
  886. }
  887. if (animateLeft) {
  888. currentLeft = el.scrollLeft;
  889. if (!duration) {
  890. el.scrollLeft = left;
  891. }
  892. }
  893. if (!duration) return;
  894. if (animateTop) {
  895. maxTop = el.scrollHeight - el.offsetHeight;
  896. newTop = Math.max(Math.min(top, maxTop), 0);
  897. }
  898. if (animateLeft) {
  899. maxLeft = el.scrollWidth - el.offsetWidth;
  900. newLeft = Math.max(Math.min(left, maxLeft), 0);
  901. }
  902. let startTime = null;
  903. if (animateTop && newTop === currentTop) animateTop = false;
  904. if (animateLeft && newLeft === currentLeft) animateLeft = false;
  905. function render(time = new Date().getTime()) {
  906. if (startTime === null) {
  907. startTime = time;
  908. }
  909. const progress = Math.max(Math.min((time - startTime) / duration, 1), 0);
  910. const easeProgress = easing === 'linear' ? progress : 0.5 - Math.cos(progress * Math.PI) / 2;
  911. let done;
  912. if (animateTop) scrollTop = currentTop + easeProgress * (newTop - currentTop);
  913. if (animateLeft) scrollLeft = currentLeft + easeProgress * (newLeft - currentLeft);
  914. if (animateTop && newTop > currentTop && scrollTop >= newTop) {
  915. el.scrollTop = newTop;
  916. done = true;
  917. }
  918. if (animateTop && newTop < currentTop && scrollTop <= newTop) {
  919. el.scrollTop = newTop;
  920. done = true;
  921. }
  922. if (animateLeft && newLeft > currentLeft && scrollLeft >= newLeft) {
  923. el.scrollLeft = newLeft;
  924. done = true;
  925. }
  926. if (animateLeft && newLeft < currentLeft && scrollLeft <= newLeft) {
  927. el.scrollLeft = newLeft;
  928. done = true;
  929. }
  930. if (done) {
  931. if (callback) callback();
  932. return;
  933. }
  934. if (animateTop) el.scrollTop = scrollTop;
  935. if (animateLeft) el.scrollLeft = scrollLeft;
  936. window.requestAnimationFrame(render);
  937. }
  938. window.requestAnimationFrame(render);
  939. });
  940. } // scrollTop(top, duration, easing, callback) {
  941. function scrollTop(...args) {
  942. let [top, duration, easing, callback] = args;
  943. if (args.length === 3 && typeof easing === 'function') {
  944. [top, duration, callback, easing] = args;
  945. }
  946. const dom = this;
  947. if (typeof top === 'undefined') {
  948. if (dom.length > 0) return dom[0].scrollTop;
  949. return null;
  950. }
  951. return dom.scrollTo(undefined, top, duration, easing, callback);
  952. }
  953. function scrollLeft(...args) {
  954. let [left, duration, easing, callback] = args;
  955. if (args.length === 3 && typeof easing === 'function') {
  956. [left, duration, callback, easing] = args;
  957. }
  958. const dom = this;
  959. if (typeof left === 'undefined') {
  960. if (dom.length > 0) return dom[0].scrollLeft;
  961. return null;
  962. }
  963. return dom.scrollTo(left, undefined, duration, easing, callback);
  964. }
  965. // eslint-disable-next-line
  966. function animate(initialProps, initialParams) {
  967. const window = getWindow();
  968. const els = this;
  969. const a = {
  970. props: Object.assign({}, initialProps),
  971. params: Object.assign({
  972. duration: 300,
  973. easing: 'swing' // or 'linear'
  974. /* Callbacks
  975. begin(elements)
  976. complete(elements)
  977. progress(elements, complete, remaining, start, tweenValue)
  978. */
  979. }, initialParams),
  980. elements: els,
  981. animating: false,
  982. que: [],
  983. easingProgress(easing, progress) {
  984. if (easing === 'swing') {
  985. return 0.5 - Math.cos(progress * Math.PI) / 2;
  986. }
  987. if (typeof easing === 'function') {
  988. return easing(progress);
  989. }
  990. return progress;
  991. },
  992. stop() {
  993. if (a.frameId) {
  994. window.cancelAnimationFrame(a.frameId);
  995. }
  996. a.animating = false;
  997. a.elements.each(el => {
  998. const element = el;
  999. delete element.dom7AnimateInstance;
  1000. });
  1001. a.que = [];
  1002. },
  1003. done(complete) {
  1004. a.animating = false;
  1005. a.elements.each(el => {
  1006. const element = el;
  1007. delete element.dom7AnimateInstance;
  1008. });
  1009. if (complete) complete(els);
  1010. if (a.que.length > 0) {
  1011. const que = a.que.shift();
  1012. a.animate(que[0], que[1]);
  1013. }
  1014. },
  1015. animate(props, params) {
  1016. if (a.animating) {
  1017. a.que.push([props, params]);
  1018. return a;
  1019. }
  1020. const elements = []; // Define & Cache Initials & Units
  1021. a.elements.each((el, index) => {
  1022. let initialFullValue;
  1023. let initialValue;
  1024. let unit;
  1025. let finalValue;
  1026. let finalFullValue;
  1027. if (!el.dom7AnimateInstance) a.elements[index].dom7AnimateInstance = a;
  1028. elements[index] = {
  1029. container: el
  1030. };
  1031. Object.keys(props).forEach(prop => {
  1032. initialFullValue = window.getComputedStyle(el, null).getPropertyValue(prop).replace(',', '.');
  1033. initialValue = parseFloat(initialFullValue);
  1034. unit = initialFullValue.replace(initialValue, '');
  1035. finalValue = parseFloat(props[prop]);
  1036. finalFullValue = props[prop] + unit;
  1037. elements[index][prop] = {
  1038. initialFullValue,
  1039. initialValue,
  1040. unit,
  1041. finalValue,
  1042. finalFullValue,
  1043. currentValue: initialValue
  1044. };
  1045. });
  1046. });
  1047. let startTime = null;
  1048. let time;
  1049. let elementsDone = 0;
  1050. let propsDone = 0;
  1051. let done;
  1052. let began = false;
  1053. a.animating = true;
  1054. function render() {
  1055. time = new Date().getTime();
  1056. let progress;
  1057. let easeProgress; // let el;
  1058. if (!began) {
  1059. began = true;
  1060. if (params.begin) params.begin(els);
  1061. }
  1062. if (startTime === null) {
  1063. startTime = time;
  1064. }
  1065. if (params.progress) {
  1066. // eslint-disable-next-line
  1067. params.progress(els, Math.max(Math.min((time - startTime) / params.duration, 1), 0), startTime + params.duration - time < 0 ? 0 : startTime + params.duration - time, startTime);
  1068. }
  1069. elements.forEach(element => {
  1070. const el = element;
  1071. if (done || el.done) return;
  1072. Object.keys(props).forEach(prop => {
  1073. if (done || el.done) return;
  1074. progress = Math.max(Math.min((time - startTime) / params.duration, 1), 0);
  1075. easeProgress = a.easingProgress(params.easing, progress);
  1076. const {
  1077. initialValue,
  1078. finalValue,
  1079. unit
  1080. } = el[prop];
  1081. el[prop].currentValue = initialValue + easeProgress * (finalValue - initialValue);
  1082. const currentValue = el[prop].currentValue;
  1083. if (finalValue > initialValue && currentValue >= finalValue || finalValue < initialValue && currentValue <= finalValue) {
  1084. el.container.style[prop] = finalValue + unit;
  1085. propsDone += 1;
  1086. if (propsDone === Object.keys(props).length) {
  1087. el.done = true;
  1088. elementsDone += 1;
  1089. }
  1090. if (elementsDone === elements.length) {
  1091. done = true;
  1092. }
  1093. }
  1094. if (done) {
  1095. a.done(params.complete);
  1096. return;
  1097. }
  1098. el.container.style[prop] = currentValue + unit;
  1099. });
  1100. });
  1101. if (done) return; // Then call
  1102. a.frameId = window.requestAnimationFrame(render);
  1103. }
  1104. a.frameId = window.requestAnimationFrame(render);
  1105. return a;
  1106. }
  1107. };
  1108. if (a.elements.length === 0) {
  1109. return els;
  1110. }
  1111. let animateInstance;
  1112. for (let i = 0; i < a.elements.length; i += 1) {
  1113. if (a.elements[i].dom7AnimateInstance) {
  1114. animateInstance = a.elements[i].dom7AnimateInstance;
  1115. } else a.elements[i].dom7AnimateInstance = a;
  1116. }
  1117. if (!animateInstance) {
  1118. animateInstance = a;
  1119. }
  1120. if (initialProps === 'stop') {
  1121. animateInstance.stop();
  1122. } else {
  1123. animateInstance.animate(a.props, a.params);
  1124. }
  1125. return els;
  1126. }
  1127. function stop() {
  1128. const els = this;
  1129. for (let i = 0; i < els.length; i += 1) {
  1130. if (els[i].dom7AnimateInstance) {
  1131. els[i].dom7AnimateInstance.stop();
  1132. }
  1133. }
  1134. }
  1135. const noTrigger = 'resize scroll'.split(' ');
  1136. function shortcut(name) {
  1137. function eventHandler(...args) {
  1138. if (typeof args[0] === 'undefined') {
  1139. for (let i = 0; i < this.length; i += 1) {
  1140. if (noTrigger.indexOf(name) < 0) {
  1141. if (name in this[i]) this[i][name]();else {
  1142. $(this[i]).trigger(name);
  1143. }
  1144. }
  1145. }
  1146. return this;
  1147. }
  1148. return this.on(name, ...args);
  1149. }
  1150. return eventHandler;
  1151. }
  1152. const click = shortcut('click');
  1153. const blur = shortcut('blur');
  1154. const focus = shortcut('focus');
  1155. const focusin = shortcut('focusin');
  1156. const focusout = shortcut('focusout');
  1157. const keyup = shortcut('keyup');
  1158. const keydown = shortcut('keydown');
  1159. const keypress = shortcut('keypress');
  1160. const submit = shortcut('submit');
  1161. const change = shortcut('change');
  1162. const mousedown = shortcut('mousedown');
  1163. const mousemove = shortcut('mousemove');
  1164. const mouseup = shortcut('mouseup');
  1165. const mouseenter = shortcut('mouseenter');
  1166. const mouseleave = shortcut('mouseleave');
  1167. const mouseout = shortcut('mouseout');
  1168. const mouseover = shortcut('mouseover');
  1169. const touchstart = shortcut('touchstart');
  1170. const touchend = shortcut('touchend');
  1171. const touchmove = shortcut('touchmove');
  1172. const resize = shortcut('resize');
  1173. const scroll = shortcut('scroll');
  1174. export default $;
  1175. 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, trigger, val, value, width };