dom7.esm.js 34 KB

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