methods.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. import { window, document } from 'ssr-window';
  2. import Dom7 from './dom7-class';
  3. import $ from './$';
  4. import { toCamelCase, unique } from './utils';
  5. // Classes and attributes
  6. function addClass(className) {
  7. if (typeof className === 'undefined') {
  8. return this;
  9. }
  10. const classes = className.split(' ');
  11. for (let i = 0; i < classes.length; i += 1) {
  12. for (let j = 0; j < this.length; j += 1) {
  13. if (typeof this[j] !== 'undefined' && typeof this[j].classList !== 'undefined') this[j].classList.add(classes[i]);
  14. }
  15. }
  16. return this;
  17. }
  18. function removeClass(className) {
  19. const classes = className.split(' ');
  20. for (let i = 0; i < classes.length; i += 1) {
  21. for (let j = 0; j < this.length; j += 1) {
  22. if (typeof this[j] !== 'undefined' && typeof this[j].classList !== 'undefined') this[j].classList.remove(classes[i]);
  23. }
  24. }
  25. return this;
  26. }
  27. function hasClass(className) {
  28. if (!this[0]) return false;
  29. return this[0].classList.contains(className);
  30. }
  31. function toggleClass(className) {
  32. const classes = className.split(' ');
  33. for (let i = 0; i < classes.length; i += 1) {
  34. for (let j = 0; j < this.length; j += 1) {
  35. if (typeof this[j] !== 'undefined' && typeof this[j].classList !== 'undefined') this[j].classList.toggle(classes[i]);
  36. }
  37. }
  38. return this;
  39. }
  40. function attr(attrs, value) {
  41. if (arguments.length === 1 && typeof attrs === 'string') {
  42. // Get attr
  43. if (this[0]) return this[0].getAttribute(attrs);
  44. return undefined;
  45. }
  46. // Set attrs
  47. for (let i = 0; i < this.length; i += 1) {
  48. if (arguments.length === 2) {
  49. // String
  50. this[i].setAttribute(attrs, value);
  51. } else {
  52. // Object
  53. // eslint-disable-next-line
  54. for (const attrName in attrs) {
  55. this[i][attrName] = attrs[attrName];
  56. this[i].setAttribute(attrName, attrs[attrName]);
  57. }
  58. }
  59. }
  60. return this;
  61. }
  62. // eslint-disable-next-line
  63. function removeAttr(attr) {
  64. for (let i = 0; i < this.length; i += 1) {
  65. this[i].removeAttribute(attr);
  66. }
  67. return this;
  68. }
  69. // eslint-disable-next-line
  70. function prop(props, value) {
  71. if (arguments.length === 1 && typeof props === 'string') {
  72. // Get prop
  73. if (this[0]) return this[0][props];
  74. } else {
  75. // Set props
  76. for (let i = 0; i < this.length; i += 1) {
  77. if (arguments.length === 2) {
  78. // String
  79. this[i][props] = value;
  80. } else {
  81. // Object
  82. // eslint-disable-next-line
  83. for (const propName in props) {
  84. this[i][propName] = props[propName];
  85. }
  86. }
  87. }
  88. return this;
  89. }
  90. }
  91. function data(key, value) {
  92. let el;
  93. if (typeof value === 'undefined') {
  94. el = this[0];
  95. // Get value
  96. if (el) {
  97. if (el.dom7ElementDataStorage && (key in el.dom7ElementDataStorage)) {
  98. return el.dom7ElementDataStorage[key];
  99. }
  100. const dataKey = el.getAttribute(`data-${key}`);
  101. if (dataKey) {
  102. return dataKey;
  103. }
  104. return undefined;
  105. }
  106. return undefined;
  107. }
  108. // Set value
  109. for (let i = 0; i < this.length; i += 1) {
  110. el = this[i];
  111. if (!el.dom7ElementDataStorage) el.dom7ElementDataStorage = {};
  112. el.dom7ElementDataStorage[key] = value;
  113. }
  114. return this;
  115. }
  116. function removeData(key) {
  117. for (let i = 0; i < this.length; i += 1) {
  118. const el = this[i];
  119. if (el.dom7ElementDataStorage && el.dom7ElementDataStorage[key]) {
  120. el.dom7ElementDataStorage[key] = null;
  121. delete el.dom7ElementDataStorage[key];
  122. }
  123. }
  124. }
  125. function dataset() {
  126. const el = this[0];
  127. if (!el) return undefined;
  128. const dataset = {}; // eslint-disable-line
  129. if (el.dataset) {
  130. // eslint-disable-next-line
  131. for (const dataKey in el.dataset) {
  132. dataset[dataKey] = el.dataset[dataKey];
  133. }
  134. } else {
  135. for (let i = 0; i < el.attributes.length; i += 1) {
  136. // eslint-disable-next-line
  137. const attr = el.attributes[i];
  138. if (attr.name.indexOf('data-') >= 0) {
  139. dataset[toCamelCase(attr.name.split('data-')[1])] = attr.value;
  140. }
  141. }
  142. }
  143. // eslint-disable-next-line
  144. for (const key in dataset) {
  145. if (dataset[key] === 'false') dataset[key] = false;
  146. else if (dataset[key] === 'true') dataset[key] = true;
  147. else if (parseFloat(dataset[key]) === dataset[key] * 1) dataset[key] *= 1;
  148. }
  149. return dataset;
  150. }
  151. function val(value) {
  152. const dom = this;
  153. if (typeof value === 'undefined') {
  154. if (dom[0]) {
  155. if (dom[0].multiple && dom[0].nodeName.toLowerCase() === 'select') {
  156. const values = [];
  157. for (let i = 0; i < dom[0].selectedOptions.length; i += 1) {
  158. values.push(dom[0].selectedOptions[i].value);
  159. }
  160. return values;
  161. }
  162. return dom[0].value;
  163. }
  164. return undefined;
  165. }
  166. for (let i = 0; i < dom.length; i += 1) {
  167. const el = dom[i];
  168. if (Array.isArray(value) && el.multiple && el.nodeName.toLowerCase() === 'select') {
  169. for (let j = 0; j < el.options.length; j += 1) {
  170. el.options[j].selected = value.indexOf(el.options[j].value) >= 0;
  171. }
  172. } else {
  173. el.value = value;
  174. }
  175. }
  176. return dom;
  177. }
  178. // Transforms
  179. // eslint-disable-next-line
  180. function transform(transform) {
  181. for (let i = 0; i < this.length; i += 1) {
  182. const elStyle = this[i].style;
  183. elStyle.webkitTransform = transform;
  184. elStyle.transform = transform;
  185. }
  186. return this;
  187. }
  188. function transition(duration) {
  189. if (typeof duration !== 'string') {
  190. duration = `${duration}ms`; // eslint-disable-line
  191. }
  192. for (let i = 0; i < this.length; i += 1) {
  193. const elStyle = this[i].style;
  194. elStyle.webkitTransitionDuration = duration;
  195. elStyle.transitionDuration = duration;
  196. }
  197. return this;
  198. }
  199. // Events
  200. function on(...args) {
  201. let [eventType, targetSelector, listener, capture] = args;
  202. if (typeof args[1] === 'function') {
  203. [eventType, listener, capture] = args;
  204. targetSelector = undefined;
  205. }
  206. if (!capture) capture = false;
  207. function handleLiveEvent(e) {
  208. const target = e.target;
  209. if (!target) return;
  210. const eventData = e.target.dom7EventData || [];
  211. if (eventData.indexOf(e) < 0) {
  212. eventData.unshift(e);
  213. }
  214. if ($(target).is(targetSelector)) listener.apply(target, eventData);
  215. else {
  216. const parents = $(target).parents(); // eslint-disable-line
  217. for (let k = 0; k < parents.length; k += 1) {
  218. if ($(parents[k]).is(targetSelector)) listener.apply(parents[k], eventData);
  219. }
  220. }
  221. }
  222. function handleEvent(e) {
  223. const eventData = e && e.target ? e.target.dom7EventData || [] : [];
  224. if (eventData.indexOf(e) < 0) {
  225. eventData.unshift(e);
  226. }
  227. listener.apply(this, eventData);
  228. }
  229. const events = eventType.split(' ');
  230. let j;
  231. for (let i = 0; i < this.length; i += 1) {
  232. const el = this[i];
  233. if (!targetSelector) {
  234. for (j = 0; j < events.length; j += 1) {
  235. const event = events[j];
  236. if (!el.dom7Listeners) el.dom7Listeners = {};
  237. if (!el.dom7Listeners[event]) el.dom7Listeners[event] = [];
  238. el.dom7Listeners[event].push({
  239. listener,
  240. proxyListener: handleEvent,
  241. });
  242. el.addEventListener(event, handleEvent, capture);
  243. }
  244. } else {
  245. // Live events
  246. for (j = 0; j < events.length; j += 1) {
  247. const event = events[j];
  248. if (!el.dom7LiveListeners) el.dom7LiveListeners = {};
  249. if (!el.dom7LiveListeners[event]) el.dom7LiveListeners[event] = [];
  250. el.dom7LiveListeners[event].push({
  251. listener,
  252. proxyListener: handleLiveEvent,
  253. });
  254. el.addEventListener(event, handleLiveEvent, capture);
  255. }
  256. }
  257. }
  258. return this;
  259. }
  260. function off(...args) {
  261. let [eventType, targetSelector, listener, capture] = args;
  262. if (typeof args[1] === 'function') {
  263. [eventType, listener, capture] = args;
  264. targetSelector = undefined;
  265. }
  266. if (!capture) capture = false;
  267. const events = eventType.split(' ');
  268. for (let i = 0; i < events.length; i += 1) {
  269. const event = events[i];
  270. for (let j = 0; j < this.length; j += 1) {
  271. const el = this[j];
  272. let handlers;
  273. if (!targetSelector && el.dom7Listeners) {
  274. handlers = el.dom7Listeners[event];
  275. } else if (targetSelector && el.dom7LiveListeners) {
  276. handlers = el.dom7LiveListeners[event];
  277. }
  278. if (handlers && handlers.length) {
  279. for (let k = handlers.length - 1; k >= 0; k -= 1) {
  280. const handler = handlers[k];
  281. if (listener && handler.listener === listener) {
  282. el.removeEventListener(event, handler.proxyListener, capture);
  283. handlers.splice(k, 1);
  284. } else if (listener && handler.listener && handler.listener.dom7proxy && handler.listener.dom7proxy === listener) {
  285. el.removeEventListener(event, handler.proxyListener, capture);
  286. handlers.splice(k, 1);
  287. } else if (!listener) {
  288. el.removeEventListener(event, handler.proxyListener, capture);
  289. handlers.splice(k, 1);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. return this;
  296. }
  297. function once(...args) {
  298. const dom = this;
  299. let [eventName, targetSelector, listener, capture] = args;
  300. if (typeof args[1] === 'function') {
  301. [eventName, listener, capture] = args;
  302. targetSelector = undefined;
  303. }
  304. function onceHandler(...eventArgs) {
  305. listener.apply(this, eventArgs);
  306. dom.off(eventName, targetSelector, onceHandler, capture);
  307. if (onceHandler.dom7proxy) {
  308. delete onceHandler.dom7proxy;
  309. }
  310. }
  311. onceHandler.dom7proxy = listener;
  312. return dom.on(eventName, targetSelector, onceHandler, capture);
  313. }
  314. function trigger(...args) {
  315. const events = args[0].split(' ');
  316. const eventData = args[1];
  317. for (let i = 0; i < events.length; i += 1) {
  318. const event = events[i];
  319. for (let j = 0; j < this.length; j += 1) {
  320. const el = this[j];
  321. let evt;
  322. try {
  323. evt = new window.CustomEvent(event, {
  324. detail: eventData,
  325. bubbles: true,
  326. cancelable: true,
  327. });
  328. } catch (e) {
  329. evt = document.createEvent('Event');
  330. evt.initEvent(event, true, true);
  331. evt.detail = eventData;
  332. }
  333. // eslint-disable-next-line
  334. el.dom7EventData = args.filter((data, dataIndex) => dataIndex > 0);
  335. el.dispatchEvent(evt);
  336. el.dom7EventData = [];
  337. delete el.dom7EventData;
  338. }
  339. }
  340. return this;
  341. }
  342. function transitionEnd(callback) {
  343. const events = ['webkitTransitionEnd', 'transitionend'];
  344. const dom = this;
  345. let i;
  346. function fireCallBack(e) {
  347. /* jshint validthis:true */
  348. if (e.target !== this) return;
  349. callback.call(this, e);
  350. for (i = 0; i < events.length; i += 1) {
  351. dom.off(events[i], fireCallBack);
  352. }
  353. }
  354. if (callback) {
  355. for (i = 0; i < events.length; i += 1) {
  356. dom.on(events[i], fireCallBack);
  357. }
  358. }
  359. return this;
  360. }
  361. function animationEnd(callback) {
  362. const events = ['webkitAnimationEnd', 'animationend'];
  363. const dom = this;
  364. let i;
  365. function fireCallBack(e) {
  366. if (e.target !== this) return;
  367. callback.call(this, e);
  368. for (i = 0; i < events.length; i += 1) {
  369. dom.off(events[i], fireCallBack);
  370. }
  371. }
  372. if (callback) {
  373. for (i = 0; i < events.length; i += 1) {
  374. dom.on(events[i], fireCallBack);
  375. }
  376. }
  377. return this;
  378. }
  379. // Sizing/Styles
  380. function width() {
  381. if (this[0] === window) {
  382. return window.innerWidth;
  383. }
  384. if (this.length > 0) {
  385. return parseFloat(this.css('width'));
  386. }
  387. return null;
  388. }
  389. function outerWidth(includeMargins) {
  390. if (this.length > 0) {
  391. if (includeMargins) {
  392. // eslint-disable-next-line
  393. const styles = this.styles();
  394. return this[0].offsetWidth + parseFloat(styles.getPropertyValue('margin-right')) + parseFloat(styles.getPropertyValue('margin-left'));
  395. }
  396. return this[0].offsetWidth;
  397. }
  398. return null;
  399. }
  400. function height() {
  401. if (this[0] === window) {
  402. return window.innerHeight;
  403. }
  404. if (this.length > 0) {
  405. return parseFloat(this.css('height'));
  406. }
  407. return null;
  408. }
  409. function outerHeight(includeMargins) {
  410. if (this.length > 0) {
  411. if (includeMargins) {
  412. // eslint-disable-next-line
  413. const styles = this.styles();
  414. return this[0].offsetHeight + parseFloat(styles.getPropertyValue('margin-top')) + parseFloat(styles.getPropertyValue('margin-bottom'));
  415. }
  416. return this[0].offsetHeight;
  417. }
  418. return null;
  419. }
  420. function offset() {
  421. if (this.length > 0) {
  422. const el = this[0];
  423. const box = el.getBoundingClientRect();
  424. const body = document.body;
  425. const clientTop = el.clientTop || body.clientTop || 0;
  426. const clientLeft = el.clientLeft || body.clientLeft || 0;
  427. const scrollTop = el === window ? window.scrollY : el.scrollTop;
  428. const scrollLeft = el === window ? window.scrollX : el.scrollLeft;
  429. return {
  430. top: (box.top + scrollTop) - clientTop,
  431. left: (box.left + scrollLeft) - clientLeft,
  432. };
  433. }
  434. return null;
  435. }
  436. function hide() {
  437. for (let i = 0; i < this.length; i += 1) {
  438. this[i].style.display = 'none';
  439. }
  440. return this;
  441. }
  442. function show() {
  443. for (let i = 0; i < this.length; i += 1) {
  444. const el = this[i];
  445. if (el.style.display === 'none') {
  446. el.style.display = '';
  447. }
  448. if (window.getComputedStyle(el, null).getPropertyValue('display') === 'none') {
  449. // Still not visible
  450. el.style.display = 'block';
  451. }
  452. }
  453. return this;
  454. }
  455. function styles() {
  456. if (this[0]) return window.getComputedStyle(this[0], null);
  457. return {};
  458. }
  459. function css(props, value) {
  460. let i;
  461. if (arguments.length === 1) {
  462. if (typeof props === 'string') {
  463. if (this[0]) return window.getComputedStyle(this[0], null).getPropertyValue(props);
  464. } else {
  465. for (i = 0; i < this.length; i += 1) {
  466. // eslint-disable-next-line
  467. for (let prop in props) {
  468. this[i].style[prop] = props[prop];
  469. }
  470. }
  471. return this;
  472. }
  473. }
  474. if (arguments.length === 2 && typeof props === 'string') {
  475. for (i = 0; i < this.length; i += 1) {
  476. this[i].style[props] = value;
  477. }
  478. return this;
  479. }
  480. return this;
  481. }
  482. // Dom manipulation
  483. function toArray() {
  484. const arr = [];
  485. for (let i = 0; i < this.length; i += 1) {
  486. arr.push(this[i]);
  487. }
  488. return arr;
  489. }
  490. // Iterate over the collection passing elements to `callback`
  491. function each(callback) {
  492. // Don't bother continuing without a callback
  493. if (!callback) return this;
  494. // Iterate over the current collection
  495. for (let i = 0; i < this.length; i += 1) {
  496. // If the callback returns false
  497. if (callback.call(this[i], i, this[i]) === false) {
  498. // End the loop early
  499. return this;
  500. }
  501. }
  502. // Return `this` to allow chained DOM operations
  503. return this;
  504. }
  505. function forEach(callback) {
  506. // Don't bother continuing without a callback
  507. if (!callback) return this;
  508. // Iterate over the current collection
  509. for (let i = 0; i < this.length; i += 1) {
  510. // If the callback returns false
  511. if (callback.call(this[i], this[i], i) === false) {
  512. // End the loop early
  513. return this;
  514. }
  515. }
  516. // Return `this` to allow chained DOM operations
  517. return this;
  518. }
  519. function filter(callback) {
  520. const matchedItems = [];
  521. const dom = this;
  522. for (let i = 0; i < dom.length; i += 1) {
  523. if (callback.call(dom[i], i, dom[i])) matchedItems.push(dom[i]);
  524. }
  525. return new Dom7(matchedItems);
  526. }
  527. function map(callback) {
  528. const modifiedItems = [];
  529. const dom = this;
  530. for (let i = 0; i < dom.length; i += 1) {
  531. modifiedItems.push(callback.call(dom[i], i, dom[i]));
  532. }
  533. return new Dom7(modifiedItems);
  534. }
  535. // eslint-disable-next-line
  536. function html(html) {
  537. if (typeof html === 'undefined') {
  538. return this[0] ? this[0].innerHTML : undefined;
  539. }
  540. for (let i = 0; i < this.length; i += 1) {
  541. this[i].innerHTML = html;
  542. }
  543. return this;
  544. }
  545. // eslint-disable-next-line
  546. function text(text) {
  547. if (typeof text === 'undefined') {
  548. if (this[0]) {
  549. return this[0].textContent.trim();
  550. }
  551. return null;
  552. }
  553. for (let i = 0; i < this.length; i += 1) {
  554. this[i].textContent = text;
  555. }
  556. return this;
  557. }
  558. function is(selector) {
  559. const el = this[0];
  560. let compareWith;
  561. let i;
  562. if (!el || typeof selector === 'undefined') return false;
  563. if (typeof selector === 'string') {
  564. if (el.matches) return el.matches(selector);
  565. else if (el.webkitMatchesSelector) return el.webkitMatchesSelector(selector);
  566. else if (el.msMatchesSelector) return el.msMatchesSelector(selector);
  567. compareWith = $(selector);
  568. for (i = 0; i < compareWith.length; i += 1) {
  569. if (compareWith[i] === el) return true;
  570. }
  571. return false;
  572. } else if (selector === document) return el === document;
  573. else if (selector === window) return el === window;
  574. if (selector.nodeType || selector instanceof Dom7) {
  575. compareWith = selector.nodeType ? [selector] : selector;
  576. for (i = 0; i < compareWith.length; i += 1) {
  577. if (compareWith[i] === el) return true;
  578. }
  579. return false;
  580. }
  581. return false;
  582. }
  583. function indexOf(el) {
  584. for (let i = 0; i < this.length; i += 1) {
  585. if (this[i] === el) return i;
  586. }
  587. return -1;
  588. }
  589. function index() {
  590. let child = this[0];
  591. let i;
  592. if (child) {
  593. i = 0;
  594. // eslint-disable-next-line
  595. while ((child = child.previousSibling) !== null) {
  596. if (child.nodeType === 1) i += 1;
  597. }
  598. return i;
  599. }
  600. return undefined;
  601. }
  602. // eslint-disable-next-line
  603. function eq(index) {
  604. if (typeof index === 'undefined') return this;
  605. const length = this.length;
  606. let returnIndex;
  607. if (index > length - 1) {
  608. return new Dom7([]);
  609. }
  610. if (index < 0) {
  611. returnIndex = length + index;
  612. if (returnIndex < 0) return new Dom7([]);
  613. return new Dom7([this[returnIndex]]);
  614. }
  615. return new Dom7([this[index]]);
  616. }
  617. function append(...args) {
  618. let newChild;
  619. for (let k = 0; k < args.length; k += 1) {
  620. newChild = args[k];
  621. for (let i = 0; i < this.length; i += 1) {
  622. if (typeof newChild === 'string') {
  623. const tempDiv = document.createElement('div');
  624. tempDiv.innerHTML = newChild;
  625. while (tempDiv.firstChild) {
  626. this[i].appendChild(tempDiv.firstChild);
  627. }
  628. } else if (newChild instanceof Dom7) {
  629. for (let j = 0; j < newChild.length; j += 1) {
  630. this[i].appendChild(newChild[j]);
  631. }
  632. } else {
  633. this[i].appendChild(newChild);
  634. }
  635. }
  636. }
  637. return this;
  638. }
  639. // eslint-disable-next-line
  640. function appendTo(parent) {
  641. $(parent).append(this);
  642. return this;
  643. }
  644. function prepend(newChild) {
  645. let i;
  646. let j;
  647. for (i = 0; i < this.length; i += 1) {
  648. if (typeof newChild === 'string') {
  649. const tempDiv = document.createElement('div');
  650. tempDiv.innerHTML = newChild;
  651. for (j = tempDiv.childNodes.length - 1; j >= 0; j -= 1) {
  652. this[i].insertBefore(tempDiv.childNodes[j], this[i].childNodes[0]);
  653. }
  654. } else if (newChild instanceof Dom7) {
  655. for (j = 0; j < newChild.length; j += 1) {
  656. this[i].insertBefore(newChild[j], this[i].childNodes[0]);
  657. }
  658. } else {
  659. this[i].insertBefore(newChild, this[i].childNodes[0]);
  660. }
  661. }
  662. return this;
  663. }
  664. // eslint-disable-next-line
  665. function prependTo(parent) {
  666. $(parent).prepend(this);
  667. return this;
  668. }
  669. function insertBefore(selector) {
  670. const before = $(selector);
  671. for (let i = 0; i < this.length; i += 1) {
  672. if (before.length === 1) {
  673. before[0].parentNode.insertBefore(this[i], before[0]);
  674. } else if (before.length > 1) {
  675. for (let j = 0; j < before.length; j += 1) {
  676. before[j].parentNode.insertBefore(this[i].cloneNode(true), before[j]);
  677. }
  678. }
  679. }
  680. }
  681. function insertAfter(selector) {
  682. const after = $(selector);
  683. for (let i = 0; i < this.length; i += 1) {
  684. if (after.length === 1) {
  685. after[0].parentNode.insertBefore(this[i], after[0].nextSibling);
  686. } else if (after.length > 1) {
  687. for (let j = 0; j < after.length; j += 1) {
  688. after[j].parentNode.insertBefore(this[i].cloneNode(true), after[j].nextSibling);
  689. }
  690. }
  691. }
  692. }
  693. function next(selector) {
  694. if (this.length > 0) {
  695. if (selector) {
  696. if (this[0].nextElementSibling && $(this[0].nextElementSibling).is(selector)) {
  697. return new Dom7([this[0].nextElementSibling]);
  698. }
  699. return new Dom7([]);
  700. }
  701. if (this[0].nextElementSibling) return new Dom7([this[0].nextElementSibling]);
  702. return new Dom7([]);
  703. }
  704. return new Dom7([]);
  705. }
  706. function nextAll(selector) {
  707. const nextEls = [];
  708. let el = this[0];
  709. if (!el) return new Dom7([]);
  710. while (el.nextElementSibling) {
  711. const next = el.nextElementSibling; // eslint-disable-line
  712. if (selector) {
  713. if ($(next).is(selector)) nextEls.push(next);
  714. } else nextEls.push(next);
  715. el = next;
  716. }
  717. return new Dom7(nextEls);
  718. }
  719. function prev(selector) {
  720. if (this.length > 0) {
  721. const el = this[0];
  722. if (selector) {
  723. if (el.previousElementSibling && $(el.previousElementSibling).is(selector)) {
  724. return new Dom7([el.previousElementSibling]);
  725. }
  726. return new Dom7([]);
  727. }
  728. if (el.previousElementSibling) return new Dom7([el.previousElementSibling]);
  729. return new Dom7([]);
  730. }
  731. return new Dom7([]);
  732. }
  733. function prevAll(selector) {
  734. const prevEls = [];
  735. let el = this[0];
  736. if (!el) return new Dom7([]);
  737. while (el.previousElementSibling) {
  738. const prev = el.previousElementSibling; // eslint-disable-line
  739. if (selector) {
  740. if ($(prev).is(selector)) prevEls.push(prev);
  741. } else prevEls.push(prev);
  742. el = prev;
  743. }
  744. return new Dom7(prevEls);
  745. }
  746. function siblings(selector) {
  747. return this.nextAll(selector).add(this.prevAll(selector));
  748. }
  749. function parent(selector) {
  750. const parents = []; // eslint-disable-line
  751. for (let i = 0; i < this.length; i += 1) {
  752. if (this[i].parentNode !== null) {
  753. if (selector) {
  754. if ($(this[i].parentNode).is(selector)) parents.push(this[i].parentNode);
  755. } else {
  756. parents.push(this[i].parentNode);
  757. }
  758. }
  759. }
  760. return $(unique(parents));
  761. }
  762. function parents(selector) {
  763. const parents = []; // eslint-disable-line
  764. for (let i = 0; i < this.length; i += 1) {
  765. let parent = this[i].parentNode; // eslint-disable-line
  766. while (parent) {
  767. if (selector) {
  768. if ($(parent).is(selector)) parents.push(parent);
  769. } else {
  770. parents.push(parent);
  771. }
  772. parent = parent.parentNode;
  773. }
  774. }
  775. return $(unique(parents));
  776. }
  777. function closest(selector) {
  778. let closest = this; // eslint-disable-line
  779. if (typeof selector === 'undefined') {
  780. return new Dom7([]);
  781. }
  782. if (!closest.is(selector)) {
  783. closest = closest.parents(selector).eq(0);
  784. }
  785. return closest;
  786. }
  787. function find(selector) {
  788. const foundElements = [];
  789. for (let i = 0; i < this.length; i += 1) {
  790. const found = this[i].querySelectorAll(selector);
  791. for (let j = 0; j < found.length; j += 1) {
  792. foundElements.push(found[j]);
  793. }
  794. }
  795. return new Dom7(foundElements);
  796. }
  797. function children(selector) {
  798. const children = []; // eslint-disable-line
  799. for (let i = 0; i < this.length; i += 1) {
  800. const childNodes = this[i].childNodes;
  801. for (let j = 0; j < childNodes.length; j += 1) {
  802. if (!selector) {
  803. if (childNodes[j].nodeType === 1) children.push(childNodes[j]);
  804. } else if (childNodes[j].nodeType === 1 && $(childNodes[j]).is(selector)) {
  805. children.push(childNodes[j]);
  806. }
  807. }
  808. }
  809. return new Dom7(unique(children));
  810. }
  811. function remove() {
  812. for (let i = 0; i < this.length; i += 1) {
  813. if (this[i].parentNode) this[i].parentNode.removeChild(this[i]);
  814. }
  815. return this;
  816. }
  817. function detach() {
  818. return this.remove();
  819. }
  820. function add(...args) {
  821. const dom = this;
  822. let i;
  823. let j;
  824. for (i = 0; i < args.length; i += 1) {
  825. const toAdd = $(args[i]);
  826. for (j = 0; j < toAdd.length; j += 1) {
  827. dom[dom.length] = toAdd[j];
  828. dom.length += 1;
  829. }
  830. }
  831. return dom;
  832. }
  833. function empty() {
  834. for (let i = 0; i < this.length; i += 1) {
  835. const el = this[i];
  836. if (el.nodeType === 1) {
  837. for (let j = 0; j < el.childNodes.length; j += 1) {
  838. if (el.childNodes[j].parentNode) {
  839. el.childNodes[j].parentNode.removeChild(el.childNodes[j]);
  840. }
  841. }
  842. el.textContent = '';
  843. }
  844. }
  845. return this;
  846. }
  847. export {
  848. addClass,
  849. removeClass,
  850. hasClass,
  851. toggleClass,
  852. attr,
  853. removeAttr,
  854. prop,
  855. data,
  856. removeData,
  857. dataset,
  858. val,
  859. transform,
  860. transition,
  861. on,
  862. off,
  863. once,
  864. trigger,
  865. transitionEnd,
  866. animationEnd,
  867. width,
  868. outerWidth,
  869. height,
  870. outerHeight,
  871. offset,
  872. hide,
  873. show,
  874. styles,
  875. css,
  876. toArray,
  877. each,
  878. forEach,
  879. filter,
  880. map,
  881. html,
  882. text,
  883. is,
  884. indexOf,
  885. index,
  886. eq,
  887. append,
  888. appendTo,
  889. prepend,
  890. prependTo,
  891. insertBefore,
  892. insertAfter,
  893. next,
  894. nextAll,
  895. prev,
  896. prevAll,
  897. siblings,
  898. parent,
  899. parents,
  900. closest,
  901. find,
  902. children,
  903. remove,
  904. detach,
  905. add,
  906. empty,
  907. };