shared.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. function noop() {}
  2. function assign(tar, src) {
  3. for (var k in src) tar[k] = src[k];
  4. return tar;
  5. }
  6. function assignTrue(tar, src) {
  7. for (var k in src) tar[k] = 1;
  8. return tar;
  9. }
  10. function isPromise(value) {
  11. return value && typeof value.then === 'function';
  12. }
  13. function callAfter(fn, i) {
  14. return () => {
  15. if (!--i) fn();
  16. };
  17. }
  18. function appendNode(node, target) {
  19. target.appendChild(node);
  20. }
  21. function insertNode(node, target, anchor) {
  22. target.insertBefore(node, anchor);
  23. }
  24. function detachNode(node) {
  25. node.parentNode.removeChild(node);
  26. }
  27. function detachBetween(before, after) {
  28. while (before.nextSibling && before.nextSibling !== after) {
  29. before.parentNode.removeChild(before.nextSibling);
  30. }
  31. }
  32. function detachBefore(after) {
  33. while (after.previousSibling) {
  34. after.parentNode.removeChild(after.previousSibling);
  35. }
  36. }
  37. function detachAfter(before) {
  38. while (before.nextSibling) {
  39. before.parentNode.removeChild(before.nextSibling);
  40. }
  41. }
  42. function reinsertBetween(before, after, target) {
  43. while (before.nextSibling && before.nextSibling !== after) {
  44. target.appendChild(before.parentNode.removeChild(before.nextSibling));
  45. }
  46. }
  47. function reinsertChildren(parent, target) {
  48. while (parent.firstChild) target.appendChild(parent.firstChild);
  49. }
  50. function reinsertAfter(before, target) {
  51. while (before.nextSibling) target.appendChild(before.nextSibling);
  52. }
  53. function reinsertBefore(after, target) {
  54. var parent = after.parentNode;
  55. while (parent.firstChild !== after) target.appendChild(parent.firstChild);
  56. }
  57. function destroyEach(iterations, detach) {
  58. for (var i = 0; i < iterations.length; i += 1) {
  59. if (iterations[i]) iterations[i].d(detach);
  60. }
  61. }
  62. function createFragment() {
  63. return document.createDocumentFragment();
  64. }
  65. function createElement(name) {
  66. return document.createElement(name);
  67. }
  68. function createSvgElement(name) {
  69. return document.createElementNS('http://www.w3.org/2000/svg', name);
  70. }
  71. function createText(data) {
  72. return document.createTextNode(data);
  73. }
  74. function createComment() {
  75. return document.createComment('');
  76. }
  77. function addListener(node, event, handler) {
  78. node.addEventListener(event, handler, false);
  79. }
  80. function removeListener(node, event, handler) {
  81. node.removeEventListener(event, handler, false);
  82. }
  83. function setAttribute(node, attribute, value) {
  84. node.setAttribute(attribute, value);
  85. }
  86. function setAttributes(node, attributes) {
  87. for (var key in attributes) {
  88. if (key in node) {
  89. node[key] = attributes[key];
  90. } else {
  91. if (attributes[key] === undefined) removeAttribute(node, key);
  92. else setAttribute(node, key, attributes[key]);
  93. }
  94. }
  95. }
  96. function removeAttribute(node, attribute) {
  97. node.removeAttribute(attribute);
  98. }
  99. function setXlinkAttribute(node, attribute, value) {
  100. node.setAttributeNS('http://www.w3.org/1999/xlink', attribute, value);
  101. }
  102. function getBindingGroupValue(group) {
  103. var value = [];
  104. for (var i = 0; i < group.length; i += 1) {
  105. if (group[i].checked) value.push(group[i].__value);
  106. }
  107. return value;
  108. }
  109. function toNumber(value) {
  110. return value === '' ? undefined : +value;
  111. }
  112. function timeRangesToArray(ranges) {
  113. var array = [];
  114. for (var i = 0; i < ranges.length; i += 1) {
  115. array.push({ start: ranges.start(i), end: ranges.end(i) });
  116. }
  117. return array;
  118. }
  119. function children (element) {
  120. return Array.from(element.childNodes);
  121. }
  122. function claimElement (nodes, name, attributes, svg) {
  123. for (var i = 0; i < nodes.length; i += 1) {
  124. var node = nodes[i];
  125. if (node.nodeName === name) {
  126. for (var j = 0; j < node.attributes.length; j += 1) {
  127. var attribute = node.attributes[j];
  128. if (!attributes[attribute.name]) node.removeAttribute(attribute.name);
  129. }
  130. return nodes.splice(i, 1)[0]; // TODO strip unwanted attributes
  131. }
  132. }
  133. return svg ? createSvgElement(name) : createElement(name);
  134. }
  135. function claimText (nodes, data) {
  136. for (var i = 0; i < nodes.length; i += 1) {
  137. var node = nodes[i];
  138. if (node.nodeType === 3) {
  139. node.data = data;
  140. return nodes.splice(i, 1)[0];
  141. }
  142. }
  143. return createText(data);
  144. }
  145. function setInputType(input, type) {
  146. try {
  147. input.type = type;
  148. } catch (e) {}
  149. }
  150. function setStyle(node, key, value) {
  151. node.style.setProperty(key, value);
  152. }
  153. function selectOption(select, value) {
  154. for (var i = 0; i < select.options.length; i += 1) {
  155. var option = select.options[i];
  156. if (option.__value === value) {
  157. option.selected = true;
  158. return;
  159. }
  160. }
  161. }
  162. function selectOptions(select, value) {
  163. for (var i = 0; i < select.options.length; i += 1) {
  164. var option = select.options[i];
  165. option.selected = ~value.indexOf(option.__value);
  166. }
  167. }
  168. function selectValue(select) {
  169. var selectedOption = select.querySelector(':checked') || select.options[0];
  170. return selectedOption && selectedOption.__value;
  171. }
  172. function selectMultipleValue(select) {
  173. return [].map.call(select.querySelectorAll(':checked'), function(option) {
  174. return option.__value;
  175. });
  176. }
  177. function addResizeListener(element, fn) {
  178. if (getComputedStyle(element).position === 'static') {
  179. element.style.position = 'relative';
  180. }
  181. const object = document.createElement('object');
  182. object.setAttribute('style', 'display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; pointer-events: none; z-index: -1;');
  183. object.type = 'text/html';
  184. let win;
  185. object.onload = () => {
  186. win = object.contentDocument.defaultView;
  187. win.addEventListener('resize', fn);
  188. };
  189. if (/Trident/.test(navigator.userAgent)) {
  190. element.appendChild(object);
  191. object.data = 'about:blank';
  192. } else {
  193. object.data = 'about:blank';
  194. element.appendChild(object);
  195. }
  196. return {
  197. cancel: () => {
  198. win.removeEventListener('resize', fn);
  199. element.removeChild(object);
  200. }
  201. };
  202. }
  203. function linear(t) {
  204. return t;
  205. }
  206. function generateRule({ a, b, delta, duration }, ease, fn) {
  207. const step = 16.666 / duration;
  208. let keyframes = '{\n';
  209. for (let p = 0; p <= 1; p += step) {
  210. const t = a + delta * ease(p);
  211. keyframes += p * 100 + `%{${fn(t, 1 - t)}}\n`;
  212. }
  213. return keyframes + `100% {${fn(b, 1 - b)}}\n}`;
  214. }
  215. // https://github.com/darkskyapp/string-hash/blob/master/index.js
  216. function hash(str) {
  217. let hash = 5381;
  218. let i = str.length;
  219. while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
  220. return hash >>> 0;
  221. }
  222. function wrapTransition(component, node, fn, params, intro) {
  223. let obj = fn(node, params);
  224. let duration;
  225. let ease;
  226. let cssText;
  227. let initialised = false;
  228. return {
  229. t: intro ? 0 : 1,
  230. running: false,
  231. program: null,
  232. pending: null,
  233. run(b, callback) {
  234. if (typeof obj === 'function') {
  235. transitionManager.wait().then(() => {
  236. obj = obj();
  237. this._run(b, callback);
  238. });
  239. } else {
  240. this._run(b, callback);
  241. }
  242. },
  243. _run(b, callback) {
  244. duration = obj.duration || 300;
  245. ease = obj.easing || linear;
  246. const program = {
  247. start: window.performance.now() + (obj.delay || 0),
  248. b,
  249. callback: callback || noop
  250. };
  251. if (intro && !initialised) {
  252. if (obj.css && obj.delay) {
  253. cssText = node.style.cssText;
  254. node.style.cssText += obj.css(0, 1);
  255. }
  256. if (obj.tick) obj.tick(0, 1);
  257. initialised = true;
  258. }
  259. if (!b) {
  260. program.group = transitionManager.outros;
  261. transitionManager.outros.remaining += 1;
  262. }
  263. if (obj.delay) {
  264. this.pending = program;
  265. } else {
  266. this.start(program);
  267. }
  268. if (!this.running) {
  269. this.running = true;
  270. transitionManager.add(this);
  271. }
  272. },
  273. start(program) {
  274. component.fire(`${program.b ? 'intro' : 'outro'}.start`, { node });
  275. program.a = this.t;
  276. program.delta = program.b - program.a;
  277. program.duration = duration * Math.abs(program.b - program.a);
  278. program.end = program.start + program.duration;
  279. if (obj.css) {
  280. if (obj.delay) node.style.cssText = cssText;
  281. const rule = generateRule(program, ease, obj.css);
  282. transitionManager.addRule(rule, program.name = '__svelte_' + hash(rule));
  283. node.style.animation = (node.style.animation || '')
  284. .split(', ')
  285. .filter(anim => anim && (program.delta < 0 || !/__svelte/.test(anim)))
  286. .concat(`${program.name} ${program.duration}ms linear 1 forwards`)
  287. .join(', ');
  288. }
  289. this.program = program;
  290. this.pending = null;
  291. },
  292. update(now) {
  293. const program = this.program;
  294. if (!program) return;
  295. const p = now - program.start;
  296. this.t = program.a + program.delta * ease(p / program.duration);
  297. if (obj.tick) obj.tick(this.t, 1 - this.t);
  298. },
  299. done() {
  300. const program = this.program;
  301. this.t = program.b;
  302. if (obj.tick) obj.tick(this.t, 1 - this.t);
  303. component.fire(`${program.b ? 'intro' : 'outro'}.end`, { node });
  304. if (!program.b && !program.invalidated) {
  305. program.group.callbacks.push(() => {
  306. program.callback();
  307. if (obj.css) transitionManager.deleteRule(node, program.name);
  308. });
  309. if (--program.group.remaining === 0) {
  310. program.group.callbacks.forEach(fn => {
  311. fn();
  312. });
  313. }
  314. } else {
  315. if (obj.css) transitionManager.deleteRule(node, program.name);
  316. }
  317. this.running = !!this.pending;
  318. },
  319. abort() {
  320. if (this.program) {
  321. if (obj.tick) obj.tick(1, 0);
  322. if (obj.css) transitionManager.deleteRule(node, this.program.name);
  323. this.program = this.pending = null;
  324. this.running = false;
  325. }
  326. },
  327. invalidate() {
  328. if (this.program) {
  329. this.program.invalidated = true;
  330. }
  331. }
  332. };
  333. }
  334. var transitionManager = {
  335. running: false,
  336. transitions: [],
  337. bound: null,
  338. stylesheet: null,
  339. activeRules: {},
  340. promise: null,
  341. add(transition) {
  342. this.transitions.push(transition);
  343. if (!this.running) {
  344. this.running = true;
  345. requestAnimationFrame(this.bound || (this.bound = this.next.bind(this)));
  346. }
  347. },
  348. addRule(rule, name) {
  349. if (!this.stylesheet) {
  350. const style = createElement('style');
  351. document.head.appendChild(style);
  352. transitionManager.stylesheet = style.sheet;
  353. }
  354. if (!this.activeRules[name]) {
  355. this.activeRules[name] = true;
  356. this.stylesheet.insertRule(`@keyframes ${name} ${rule}`, this.stylesheet.cssRules.length);
  357. }
  358. },
  359. next() {
  360. this.running = false;
  361. const now = window.performance.now();
  362. let i = this.transitions.length;
  363. while (i--) {
  364. const transition = this.transitions[i];
  365. if (transition.program && now >= transition.program.end) {
  366. transition.done();
  367. }
  368. if (transition.pending && now >= transition.pending.start) {
  369. transition.start(transition.pending);
  370. }
  371. if (transition.running) {
  372. transition.update(now);
  373. this.running = true;
  374. } else if (!transition.pending) {
  375. this.transitions.splice(i, 1);
  376. }
  377. }
  378. if (this.running) {
  379. requestAnimationFrame(this.bound);
  380. } else if (this.stylesheet) {
  381. let i = this.stylesheet.cssRules.length;
  382. while (i--) this.stylesheet.deleteRule(i);
  383. this.activeRules = {};
  384. }
  385. },
  386. deleteRule(node, name) {
  387. node.style.animation = node.style.animation
  388. .split(', ')
  389. .filter(anim => anim && anim.indexOf(name) === -1)
  390. .join(', ');
  391. },
  392. groupOutros() {
  393. this.outros = {
  394. remaining: 0,
  395. callbacks: []
  396. };
  397. },
  398. wait() {
  399. if (!transitionManager.promise) {
  400. transitionManager.promise = Promise.resolve();
  401. transitionManager.promise.then(() => {
  402. transitionManager.promise = null;
  403. });
  404. }
  405. return transitionManager.promise;
  406. }
  407. };
  408. function wrapAnimation(node, from, fn, params) {
  409. if (!from) return;
  410. const to = node.getBoundingClientRect();
  411. if (from.left === to.left && from.right === to.right && from.top === to.top && from.bottom === to.bottom) return;
  412. const info = fn(node, { from, to }, params);
  413. const duration = 'duration' in info ? info.duration : 300;
  414. const delay = 'delay' in info ? info.delay : 0;
  415. const ease = info.easing || linear;
  416. const start = window.performance.now() + delay;
  417. const end = start + duration;
  418. const program = {
  419. a: 0,
  420. t: 0,
  421. b: 1,
  422. delta: 1,
  423. duration,
  424. start,
  425. end
  426. };
  427. const cssText = node.style.cssText;
  428. const animation = {
  429. pending: delay ? program : null,
  430. program: delay ? null : program,
  431. running: true,
  432. start() {
  433. if (info.css) {
  434. if (delay) node.style.cssText = cssText;
  435. const rule = generateRule(program, ease, info.css);
  436. program.name = `__svelte_${hash(rule)}`;
  437. transitionManager.addRule(rule, program.name);
  438. node.style.animation = (node.style.animation || '')
  439. .split(', ')
  440. .filter(anim => anim && (program.delta < 0 || !/__svelte/.test(anim)))
  441. .concat(`${program.name} ${program.duration}ms linear 1 forwards`)
  442. .join(', ');
  443. }
  444. animation.program = program;
  445. animation.pending = null;
  446. },
  447. update: now => {
  448. const p = now - program.start;
  449. const t = program.a + program.delta * ease(p / program.duration);
  450. if (info.tick) info.tick(t, 1 - t);
  451. },
  452. done() {
  453. if (info.tick) info.tick(1, 0);
  454. animation.stop();
  455. },
  456. stop() {
  457. if (info.css) transitionManager.deleteRule(node, program.name);
  458. animation.running = false;
  459. }
  460. };
  461. transitionManager.add(animation);
  462. if (info.tick) info.tick(0, 1);
  463. if (delay) {
  464. if (info.css) node.style.cssText += info.css(0, 1);
  465. } else {
  466. animation.start();
  467. }
  468. return animation;
  469. }
  470. function fixPosition(node) {
  471. const style = getComputedStyle(node);
  472. if (style.position !== 'absolute' && style.position !== 'fixed') {
  473. const { width, height } = style;
  474. const a = node.getBoundingClientRect();
  475. node.style.position = 'absolute';
  476. node.style.width = width;
  477. node.style.height = height;
  478. const b = node.getBoundingClientRect();
  479. if (a.left !== b.left || a.top !== b.top) {
  480. const style = getComputedStyle(node);
  481. const transform = style.transform === 'none' ? '' : style.transform;
  482. node.style.transform = `${transform} translate(${a.left - b.left}px, ${a.top - b.top}px)`;
  483. }
  484. }
  485. }
  486. function handlePromise(promise, info) {
  487. var token = info.token = {};
  488. function update(type, index, key, value) {
  489. if (info.token !== token) return;
  490. info.resolved = key && { [key]: value };
  491. const child_ctx = assign(assign({}, info.ctx), info.resolved);
  492. const block = type && (info.current = type)(info.component, child_ctx);
  493. if (info.block) {
  494. if (info.blocks) {
  495. info.blocks.forEach((block, i) => {
  496. if (i !== index && block) {
  497. transitionManager.groupOutros();
  498. block.o(() => {
  499. block.d(1);
  500. info.blocks[i] = null;
  501. });
  502. }
  503. });
  504. } else {
  505. info.block.d(1);
  506. }
  507. block.c();
  508. block[block.i ? 'i' : 'm'](info.mount(), info.anchor);
  509. info.component.root.set({}); // flush any handlers that were created
  510. }
  511. info.block = block;
  512. if (info.blocks) info.blocks[index] = block;
  513. }
  514. if (isPromise(promise)) {
  515. promise.then(value => {
  516. update(info.then, 1, info.value, value);
  517. }, error => {
  518. update(info.catch, 2, info.error, error);
  519. });
  520. // if we previously had a then/catch block, destroy it
  521. if (info.current !== info.pending) {
  522. update(info.pending, 0);
  523. return true;
  524. }
  525. } else {
  526. if (info.current !== info.then) {
  527. update(info.then, 1, info.value, promise);
  528. return true;
  529. }
  530. info.resolved = { [info.value]: promise };
  531. }
  532. }
  533. function destroyBlock(block, lookup) {
  534. block.d(1);
  535. lookup[block.key] = null;
  536. }
  537. function outroAndDestroyBlock(block, lookup) {
  538. block.o(function() {
  539. destroyBlock(block, lookup);
  540. });
  541. }
  542. function fixAndOutroAndDestroyBlock(block, lookup) {
  543. block.f();
  544. outroAndDestroyBlock(block, lookup);
  545. }
  546. function updateKeyedEach(old_blocks, component, changed, get_key, dynamic, ctx, list, lookup, node, destroy, create_each_block, intro_method, next, get_context) {
  547. var o = old_blocks.length;
  548. var n = list.length;
  549. var i = o;
  550. var old_indexes = {};
  551. while (i--) old_indexes[old_blocks[i].key] = i;
  552. var new_blocks = [];
  553. var new_lookup = {};
  554. var deltas = {};
  555. var i = n;
  556. while (i--) {
  557. var child_ctx = get_context(ctx, list, i);
  558. var key = get_key(child_ctx);
  559. var block = lookup[key];
  560. if (!block) {
  561. block = create_each_block(component, key, child_ctx);
  562. block.c();
  563. } else if (dynamic) {
  564. block.p(changed, child_ctx);
  565. }
  566. new_blocks[i] = new_lookup[key] = block;
  567. if (key in old_indexes) deltas[key] = Math.abs(i - old_indexes[key]);
  568. }
  569. var will_move = {};
  570. var did_move = {};
  571. function insert(block) {
  572. block[intro_method](node, next);
  573. lookup[block.key] = block;
  574. next = block.first;
  575. n--;
  576. }
  577. while (o && n) {
  578. var new_block = new_blocks[n - 1];
  579. var old_block = old_blocks[o - 1];
  580. var new_key = new_block.key;
  581. var old_key = old_block.key;
  582. if (new_block === old_block) {
  583. // do nothing
  584. next = new_block.first;
  585. o--;
  586. n--;
  587. }
  588. else if (!new_lookup[old_key]) {
  589. // remove old block
  590. destroy(old_block, lookup);
  591. o--;
  592. }
  593. else if (!lookup[new_key] || will_move[new_key]) {
  594. insert(new_block);
  595. }
  596. else if (did_move[old_key]) {
  597. o--;
  598. } else if (deltas[new_key] > deltas[old_key]) {
  599. did_move[new_key] = true;
  600. insert(new_block);
  601. } else {
  602. will_move[old_key] = true;
  603. o--;
  604. }
  605. }
  606. while (o--) {
  607. var old_block = old_blocks[o];
  608. if (!new_lookup[old_block.key]) destroy(old_block, lookup);
  609. }
  610. while (n) insert(new_blocks[n - 1]);
  611. return new_blocks;
  612. }
  613. function measure(blocks) {
  614. const rects = {};
  615. let i = blocks.length;
  616. while (i--) rects[blocks[i].key] = blocks[i].node.getBoundingClientRect();
  617. return rects;
  618. }
  619. function animate(blocks, rects, fn, params) {
  620. let i = blocks.length;
  621. while (i--) {
  622. const block = blocks[i];
  623. const from = rects[block.key];
  624. if (!from) continue;
  625. const to = block.node.getBoundingClientRect();
  626. if (from.left === to.left && from.right === to.right && from.top === to.top && from.bottom === to.bottom) continue;
  627. }
  628. }
  629. function getSpreadUpdate(levels, updates) {
  630. var update = {};
  631. var to_null_out = {};
  632. var accounted_for = {};
  633. var i = levels.length;
  634. while (i--) {
  635. var o = levels[i];
  636. var n = updates[i];
  637. if (n) {
  638. for (var key in o) {
  639. if (!(key in n)) to_null_out[key] = 1;
  640. }
  641. for (var key in n) {
  642. if (!accounted_for[key]) {
  643. update[key] = n[key];
  644. accounted_for[key] = 1;
  645. }
  646. }
  647. levels[i] = n;
  648. } else {
  649. for (var key in o) {
  650. accounted_for[key] = 1;
  651. }
  652. }
  653. }
  654. for (var key in to_null_out) {
  655. if (!(key in update)) update[key] = undefined;
  656. }
  657. return update;
  658. }
  659. function spread(args) {
  660. const attributes = Object.assign({}, ...args);
  661. let str = '';
  662. Object.keys(attributes).forEach(name => {
  663. const value = attributes[name];
  664. if (value === undefined) return;
  665. if (value === true) str += " " + name;
  666. str += " " + name + "=" + JSON.stringify(value);
  667. });
  668. return str;
  669. }
  670. const escaped = {
  671. '"': '&quot;',
  672. "'": '&#39;',
  673. '&': '&amp;',
  674. '<': '&lt;',
  675. '>': '&gt;'
  676. };
  677. function escape(html) {
  678. return String(html).replace(/["'&<>]/g, match => escaped[match]);
  679. }
  680. function each(items, assign, fn) {
  681. let str = '';
  682. for (let i = 0; i < items.length; i += 1) {
  683. str += fn(assign(items[i], i));
  684. }
  685. return str;
  686. }
  687. const missingComponent = {
  688. _render: () => ''
  689. };
  690. function blankObject() {
  691. return Object.create(null);
  692. }
  693. function destroy(detach) {
  694. this.destroy = noop;
  695. this.fire('destroy');
  696. this.set = noop;
  697. this._fragment.d(detach !== false);
  698. this._fragment = null;
  699. this._state = {};
  700. }
  701. function destroyDev(detach) {
  702. destroy.call(this, detach);
  703. this.destroy = function() {
  704. console.warn('Component was already destroyed');
  705. };
  706. }
  707. function _differs(a, b) {
  708. return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
  709. }
  710. function _differsImmutable(a, b) {
  711. return a != a ? b == b : a !== b;
  712. }
  713. function fire(eventName, data) {
  714. var handlers =
  715. eventName in this._handlers && this._handlers[eventName].slice();
  716. if (!handlers) return;
  717. for (var i = 0; i < handlers.length; i += 1) {
  718. var handler = handlers[i];
  719. if (!handler.__calling) {
  720. handler.__calling = true;
  721. handler.call(this, data);
  722. handler.__calling = false;
  723. }
  724. }
  725. }
  726. function get() {
  727. return this._state;
  728. }
  729. function init(component, options) {
  730. component._handlers = blankObject();
  731. component._bind = options._bind;
  732. component.options = options;
  733. component.root = options.root || component;
  734. component.store = component.root.store || options.store;
  735. }
  736. function on(eventName, handler) {
  737. var handlers = this._handlers[eventName] || (this._handlers[eventName] = []);
  738. handlers.push(handler);
  739. return {
  740. cancel: function() {
  741. var index = handlers.indexOf(handler);
  742. if (~index) handlers.splice(index, 1);
  743. }
  744. };
  745. }
  746. function run(fn) {
  747. fn();
  748. }
  749. function set(newState) {
  750. this._set(assign({}, newState));
  751. if (this.root._lock) return;
  752. this.root._lock = true;
  753. callAll(this.root._beforecreate);
  754. callAll(this.root._oncreate);
  755. callAll(this.root._aftercreate);
  756. this.root._lock = false;
  757. }
  758. function _set(newState) {
  759. var oldState = this._state,
  760. changed = {},
  761. dirty = false;
  762. for (var key in newState) {
  763. if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
  764. }
  765. if (!dirty) return;
  766. this._state = assign(assign({}, oldState), newState);
  767. this._recompute(changed, this._state);
  768. if (this._bind) this._bind(changed, this._state);
  769. if (this._fragment) {
  770. this.fire("state", { changed: changed, current: this._state, previous: oldState });
  771. this._fragment.p(changed, this._state);
  772. this.fire("update", { changed: changed, current: this._state, previous: oldState });
  773. }
  774. }
  775. function setDev(newState) {
  776. if (typeof newState !== 'object') {
  777. throw new Error(
  778. this._debugName + '.set was called without an object of data key-values to update.'
  779. );
  780. }
  781. this._checkReadOnly(newState);
  782. set.call(this, newState);
  783. }
  784. function callAll(fns) {
  785. while (fns && fns.length) fns.shift()();
  786. }
  787. function _mount(target, anchor) {
  788. this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
  789. }
  790. var PENDING = {};
  791. var SUCCESS = {};
  792. var FAILURE = {};
  793. function removeFromStore() {
  794. this.store._remove(this);
  795. }
  796. var proto = {
  797. destroy,
  798. get,
  799. fire,
  800. on,
  801. set,
  802. _recompute: noop,
  803. _set,
  804. _mount,
  805. _differs
  806. };
  807. var protoDev = {
  808. destroy: destroyDev,
  809. get,
  810. fire,
  811. on,
  812. set: setDev,
  813. _recompute: noop,
  814. _set,
  815. _mount,
  816. _differs
  817. };
  818. export { blankObject, destroy, destroyDev, _differs, _differsImmutable, fire, get, init, on, run, set, _set, setDev, callAll, _mount, PENDING, SUCCESS, FAILURE, removeFromStore, proto, protoDev, wrapAnimation, fixPosition, handlePromise, appendNode, insertNode, detachNode, detachBetween, detachBefore, detachAfter, reinsertBetween, reinsertChildren, reinsertAfter, reinsertBefore, destroyEach, createFragment, createElement, createSvgElement, createText, createComment, addListener, removeListener, setAttribute, setAttributes, removeAttribute, setXlinkAttribute, getBindingGroupValue, toNumber, timeRangesToArray, children, claimElement, claimText, setInputType, setStyle, selectOption, selectOptions, selectValue, selectMultipleValue, addResizeListener, destroyBlock, outroAndDestroyBlock, fixAndOutroAndDestroyBlock, updateKeyedEach, measure, animate, getSpreadUpdate, spread, escaped, escape, each, missingComponent, linear, generateRule, hash, wrapTransition, transitionManager, noop, assign, assignTrue, isPromise, callAfter };