index.js 12 KB

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