index.esm.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. function _mergeNamespaces(n, m) {
  2. m.forEach(function (e) {
  3. e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) {
  4. if (k !== 'default' && !(k in n)) {
  5. var d = Object.getOwnPropertyDescriptor(e, k);
  6. Object.defineProperty(n, k, d.get ? d : {
  7. enumerable: true,
  8. get: function () { return e[k]; }
  9. });
  10. }
  11. });
  12. });
  13. return Object.freeze(n);
  14. }
  15. /**
  16. * Flatten array, one level deep.
  17. *
  18. * @param {Array<?>} arr
  19. *
  20. * @return {Array<?>}
  21. */
  22. const nativeToString = Object.prototype.toString;
  23. const nativeHasOwnProperty = Object.prototype.hasOwnProperty;
  24. function isUndefined(obj) {
  25. return obj === undefined;
  26. }
  27. function isArray(obj) {
  28. return nativeToString.call(obj) === '[object Array]';
  29. }
  30. /**
  31. * Return true, if target owns a property with the given key.
  32. *
  33. * @param {Object} target
  34. * @param {String} key
  35. *
  36. * @return {Boolean}
  37. */
  38. function has(target, key) {
  39. return nativeHasOwnProperty.call(target, key);
  40. }
  41. /**
  42. * Iterate over collection; returning something
  43. * (non-undefined) will stop iteration.
  44. *
  45. * @param {Array|Object} collection
  46. * @param {Function} iterator
  47. *
  48. * @return {Object} return result that stopped the iteration
  49. */
  50. function forEach(collection, iterator) {
  51. let val,
  52. result;
  53. if (isUndefined(collection)) {
  54. return;
  55. }
  56. const convertKey = isArray(collection) ? toNum : identity;
  57. for (let key in collection) {
  58. if (has(collection, key)) {
  59. val = collection[key];
  60. result = iterator(val, convertKey(key));
  61. if (result === false) {
  62. return val;
  63. }
  64. }
  65. }
  66. }
  67. function identity(arg) {
  68. return arg;
  69. }
  70. function toNum(arg) {
  71. return Number(arg);
  72. }
  73. /**
  74. * Assigns style attributes in a style-src compliant way.
  75. *
  76. * @param {Element} element
  77. * @param {...Object} styleSources
  78. *
  79. * @return {Element} the element
  80. */
  81. function assign(element, ...styleSources) {
  82. const target = element.style;
  83. forEach(styleSources, function(style) {
  84. if (!style) {
  85. return;
  86. }
  87. forEach(style, function(value, key) {
  88. target[key] = value;
  89. });
  90. });
  91. return element;
  92. }
  93. /**
  94. * Set attribute `name` to `val`, or get attr `name`.
  95. *
  96. * @param {Element} el
  97. * @param {String} name
  98. * @param {String} [val]
  99. * @api public
  100. */
  101. function attr(el, name, val) {
  102. // get
  103. if (arguments.length == 2) {
  104. return el.getAttribute(name);
  105. }
  106. // remove
  107. if (val === null) {
  108. return el.removeAttribute(name);
  109. }
  110. // set
  111. el.setAttribute(name, val);
  112. return el;
  113. }
  114. /**
  115. * Taken from https://github.com/component/classes
  116. *
  117. * Without the component bits.
  118. */
  119. /**
  120. * toString reference.
  121. */
  122. const toString = Object.prototype.toString;
  123. /**
  124. * Wrap `el` in a `ClassList`.
  125. *
  126. * @param {Element} el
  127. * @return {ClassList}
  128. * @api public
  129. */
  130. function classes(el) {
  131. return new ClassList(el);
  132. }
  133. /**
  134. * Initialize a new ClassList for `el`.
  135. *
  136. * @param {Element} el
  137. * @api private
  138. */
  139. function ClassList(el) {
  140. if (!el || !el.nodeType) {
  141. throw new Error('A DOM element reference is required');
  142. }
  143. this.el = el;
  144. this.list = el.classList;
  145. }
  146. /**
  147. * Add class `name` if not already present.
  148. *
  149. * @param {String} name
  150. * @return {ClassList}
  151. * @api public
  152. */
  153. ClassList.prototype.add = function(name) {
  154. this.list.add(name);
  155. return this;
  156. };
  157. /**
  158. * Remove class `name` when present, or
  159. * pass a regular expression to remove
  160. * any which match.
  161. *
  162. * @param {String|RegExp} name
  163. * @return {ClassList}
  164. * @api public
  165. */
  166. ClassList.prototype.remove = function(name) {
  167. if ('[object RegExp]' == toString.call(name)) {
  168. return this.removeMatching(name);
  169. }
  170. this.list.remove(name);
  171. return this;
  172. };
  173. /**
  174. * Remove all classes matching `re`.
  175. *
  176. * @param {RegExp} re
  177. * @return {ClassList}
  178. * @api private
  179. */
  180. ClassList.prototype.removeMatching = function(re) {
  181. const arr = this.array();
  182. for (let i = 0; i < arr.length; i++) {
  183. if (re.test(arr[i])) {
  184. this.remove(arr[i]);
  185. }
  186. }
  187. return this;
  188. };
  189. /**
  190. * Toggle class `name`, can force state via `force`.
  191. *
  192. * For browsers that support classList, but do not support `force` yet,
  193. * the mistake will be detected and corrected.
  194. *
  195. * @param {String} name
  196. * @param {Boolean} force
  197. * @return {ClassList}
  198. * @api public
  199. */
  200. ClassList.prototype.toggle = function(name, force) {
  201. if ('undefined' !== typeof force) {
  202. if (force !== this.list.toggle(name, force)) {
  203. this.list.toggle(name); // toggle again to correct
  204. }
  205. } else {
  206. this.list.toggle(name);
  207. }
  208. return this;
  209. };
  210. /**
  211. * Return an array of classes.
  212. *
  213. * @return {Array}
  214. * @api public
  215. */
  216. ClassList.prototype.array = function() {
  217. return Array.from(this.list);
  218. };
  219. /**
  220. * Check if class `name` is present.
  221. *
  222. * @param {String} name
  223. * @return {ClassList}
  224. * @api public
  225. */
  226. ClassList.prototype.has =
  227. ClassList.prototype.contains = function(name) {
  228. return this.list.contains(name);
  229. };
  230. /**
  231. * Remove all children from the given element.
  232. */
  233. function clear(el) {
  234. var c;
  235. while (el.childNodes.length) {
  236. c = el.childNodes[0];
  237. el.removeChild(c);
  238. }
  239. return el;
  240. }
  241. /**
  242. * @param { HTMLElement } element
  243. * @param { String } selector
  244. *
  245. * @return { boolean }
  246. */
  247. function matches(element, selector) {
  248. return element && typeof element.matches === 'function' && element.matches(selector);
  249. }
  250. /**
  251. * Closest
  252. *
  253. * @param {Element} el
  254. * @param {String} selector
  255. * @param {Boolean} checkYourSelf (optional)
  256. */
  257. function closest(element, selector, checkYourSelf) {
  258. var currentElem = checkYourSelf ? element : element.parentNode;
  259. while (currentElem && currentElem.nodeType !== document.DOCUMENT_NODE &&
  260. currentElem.nodeType !== document.DOCUMENT_FRAGMENT_NODE) {
  261. if (matches(currentElem, selector)) {
  262. return currentElem;
  263. }
  264. currentElem = currentElem.parentNode;
  265. }
  266. return matches(currentElem, selector) ? currentElem : null;
  267. }
  268. var componentEvent = {};
  269. var bind$1, unbind$1, prefix;
  270. function detect () {
  271. bind$1 = window.addEventListener ? 'addEventListener' : 'attachEvent';
  272. unbind$1 = window.removeEventListener ? 'removeEventListener' : 'detachEvent';
  273. prefix = bind$1 !== 'addEventListener' ? 'on' : '';
  274. }
  275. /**
  276. * Bind `el` event `type` to `fn`.
  277. *
  278. * @param {Element} el
  279. * @param {String} type
  280. * @param {Function} fn
  281. * @param {Boolean} capture
  282. * @return {Function}
  283. * @api public
  284. */
  285. var bind_1 = componentEvent.bind = function(el, type, fn, capture){
  286. if (!bind$1) detect();
  287. el[bind$1](prefix + type, fn, capture || false);
  288. return fn;
  289. };
  290. /**
  291. * Unbind `el` event `type`'s callback `fn`.
  292. *
  293. * @param {Element} el
  294. * @param {String} type
  295. * @param {Function} fn
  296. * @param {Boolean} capture
  297. * @return {Function}
  298. * @api public
  299. */
  300. var unbind_1 = componentEvent.unbind = function(el, type, fn, capture){
  301. if (!unbind$1) detect();
  302. el[unbind$1](prefix + type, fn, capture || false);
  303. return fn;
  304. };
  305. var event = /*#__PURE__*/_mergeNamespaces({
  306. __proto__: null,
  307. bind: bind_1,
  308. unbind: unbind_1,
  309. 'default': componentEvent
  310. }, [componentEvent]);
  311. /**
  312. * Module dependencies.
  313. */
  314. /**
  315. * Delegate event `type` to `selector`
  316. * and invoke `fn(e)`. A callback function
  317. * is returned which may be passed to `.unbind()`.
  318. *
  319. * @param {Element} el
  320. * @param {String} selector
  321. * @param {String} type
  322. * @param {Function} fn
  323. * @param {Boolean} capture
  324. * @return {Function}
  325. * @api public
  326. */
  327. // Some events don't bubble, so we want to bind to the capture phase instead
  328. // when delegating.
  329. var forceCaptureEvents = [ 'focus', 'blur' ];
  330. function bind(el, selector, type, fn, capture) {
  331. if (forceCaptureEvents.indexOf(type) !== -1) {
  332. capture = true;
  333. }
  334. return event.bind(el, type, function(e) {
  335. var target = e.target || e.srcElement;
  336. e.delegateTarget = closest(target, selector, true);
  337. if (e.delegateTarget) {
  338. fn.call(el, e);
  339. }
  340. }, capture);
  341. }
  342. /**
  343. * Unbind event `type`'s callback `fn`.
  344. *
  345. * @param {Element} el
  346. * @param {String} type
  347. * @param {Function} fn
  348. * @param {Boolean} capture
  349. * @api public
  350. */
  351. function unbind(el, type, fn, capture) {
  352. if (forceCaptureEvents.indexOf(type) !== -1) {
  353. capture = true;
  354. }
  355. return event.unbind(el, type, fn, capture);
  356. }
  357. var delegate = {
  358. bind,
  359. unbind
  360. };
  361. /**
  362. * Expose `parse`.
  363. */
  364. var domify = parse;
  365. /**
  366. * Tests for browser support.
  367. */
  368. var innerHTMLBug = false;
  369. var bugTestDiv;
  370. if (typeof document !== 'undefined') {
  371. bugTestDiv = document.createElement('div');
  372. // Setup
  373. bugTestDiv.innerHTML = ' <link/><table></table><a href="/a">a</a><input type="checkbox"/>';
  374. // Make sure that link elements get serialized correctly by innerHTML
  375. // This requires a wrapper element in IE
  376. innerHTMLBug = !bugTestDiv.getElementsByTagName('link').length;
  377. bugTestDiv = undefined;
  378. }
  379. /**
  380. * Wrap map from jquery.
  381. */
  382. var map = {
  383. legend: [1, '<fieldset>', '</fieldset>'],
  384. tr: [2, '<table><tbody>', '</tbody></table>'],
  385. col: [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
  386. // for script/link/style tags to work in IE6-8, you have to wrap
  387. // in a div with a non-whitespace character in front, ha!
  388. _default: innerHTMLBug ? [1, 'X<div>', '</div>'] : [0, '', '']
  389. };
  390. map.td =
  391. map.th = [3, '<table><tbody><tr>', '</tr></tbody></table>'];
  392. map.option =
  393. map.optgroup = [1, '<select multiple="multiple">', '</select>'];
  394. map.thead =
  395. map.tbody =
  396. map.colgroup =
  397. map.caption =
  398. map.tfoot = [1, '<table>', '</table>'];
  399. map.polyline =
  400. map.ellipse =
  401. map.polygon =
  402. map.circle =
  403. map.text =
  404. map.line =
  405. map.path =
  406. map.rect =
  407. map.g = [1, '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">','</svg>'];
  408. /**
  409. * Parse `html` and return a DOM Node instance, which could be a TextNode,
  410. * HTML DOM Node of some kind (<div> for example), or a DocumentFragment
  411. * instance, depending on the contents of the `html` string.
  412. *
  413. * @param {String} html - HTML string to "domify"
  414. * @param {Document} doc - The `document` instance to create the Node for
  415. * @return {DOMNode} the TextNode, DOM Node, or DocumentFragment instance
  416. * @api private
  417. */
  418. function parse(html, doc) {
  419. if ('string' != typeof html) throw new TypeError('String expected');
  420. // default to the global `document` object
  421. if (!doc) doc = document;
  422. // tag name
  423. var m = /<([\w:]+)/.exec(html);
  424. if (!m) return doc.createTextNode(html);
  425. html = html.replace(/^\s+|\s+$/g, ''); // Remove leading/trailing whitespace
  426. var tag = m[1];
  427. // body support
  428. if (tag == 'body') {
  429. var el = doc.createElement('html');
  430. el.innerHTML = html;
  431. return el.removeChild(el.lastChild);
  432. }
  433. // wrap map
  434. var wrap = Object.prototype.hasOwnProperty.call(map, tag) ? map[tag] : map._default;
  435. var depth = wrap[0];
  436. var prefix = wrap[1];
  437. var suffix = wrap[2];
  438. var el = doc.createElement('div');
  439. el.innerHTML = prefix + html + suffix;
  440. while (depth--) el = el.lastChild;
  441. // one element
  442. if (el.firstChild == el.lastChild) {
  443. return el.removeChild(el.firstChild);
  444. }
  445. // several elements
  446. var fragment = doc.createDocumentFragment();
  447. while (el.firstChild) {
  448. fragment.appendChild(el.removeChild(el.firstChild));
  449. }
  450. return fragment;
  451. }
  452. var domify$1 = domify;
  453. function query(selector, el) {
  454. el = el || document;
  455. return el.querySelector(selector);
  456. }
  457. function all(selector, el) {
  458. el = el || document;
  459. return el.querySelectorAll(selector);
  460. }
  461. function remove(el) {
  462. el.parentNode && el.parentNode.removeChild(el);
  463. }
  464. export { assign as assignStyle, attr, classes, clear, closest, delegate, domify$1 as domify, event, matches, query, all as queryAll, remove };
  465. //# sourceMappingURL=index.esm.js.map