UNPKG

@curi/svelte

Version:

Curi integration with Svelte

322 lines (265 loc) 6.97 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var store = require('svelte/store'); function noop() {} function assign(tar, src) { for (var k in src) tar[k] = src[k]; return tar; } function appendNode(node, target) { target.appendChild(node); } function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } function detachNode(node) { node.parentNode.removeChild(node); } function reinsertChildren(parent, target) { while (parent.firstChild) target.appendChild(parent.firstChild); } function createElement(name) { return document.createElement(name); } function addListener(node, event, handler) { node.addEventListener(event, handler, false); } function removeListener(node, event, handler) { node.removeEventListener(event, handler, false); } function blankObject() { return Object.create(null); } function destroy(detach) { this.destroy = noop; this.fire('destroy'); this.set = noop; if (detach !== false) this._fragment.u(); this._fragment.d(); this._fragment = null; this._state = {}; } function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { var handler = handlers[i]; if (!handler.__calling) { handler.__calling = true; handler.call(this, data); handler.__calling = false; } } } function get() { return this._state; } function init(component, options) { component._handlers = blankObject(); component._bind = options._bind; component.options = options; component.root = options.root || component; component.store = component.root.store || options.store; } function on(eventName, handler) { var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); handlers.push(handler); return { cancel: function() { var index = handlers.indexOf(handler); if (~index) handlers.splice(index, 1); } }; } function set(newState) { this._set(assign({}, newState)); if (this.root._lock) return; this.root._lock = true; callAll(this.root._beforecreate); callAll(this.root._oncreate); callAll(this.root._aftercreate); this.root._lock = false; } function _set(newState) { var oldState = this._state, changed = {}, dirty = false; for (var key in newState) { if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; this._state = assign(assign({}, oldState), newState); this._recompute(changed, this._state); if (this._bind) this._bind(changed, this._state); if (this._fragment) { this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } function callAll(fns) { while (fns && fns.length) fns.shift()(); } function _mount(target, anchor) { this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); } function _unmount() { if (this._fragment) this._fragment.u(); } function removeFromStore() { this.store._remove(this); } var proto = { destroy, get, fire, on, set, _recompute: noop, _set, _mount, _unmount, _differs }; /* src/Link.html generated by Svelte v2.4.4 */ var canNavigate = function canNavigate(event) { return !event.defaultPrevented && event.button === 0 && !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); }; function location(_ref) { var to = _ref.to, params = _ref.params, hash$$1 = _ref.hash, query = _ref.query, state = _ref.state, $router = _ref.$router, $curi = _ref.$curi; var pathname = to ? $router.route.pathname(to, params) : $curi.response.location.pathname; return { hash: hash$$1, query: query, state: state, pathname: pathname }; } function href(_ref2) { var location = _ref2.location, $router = _ref2.$router; return $router.history.toHref(location); } function data() { return { to: '', params: {}, hash: '', query: '', state: null }; } var methods = { handleClick: function handleClick(event, location, $router) { if (canNavigate(event)) { event.preventDefault(); $router.history.navigate(location); } } }; function create_main_fragment(component, ctx) { var a, slot_content_default = component._slotted.default; function click_handler(event) { component.handleClick(event, ctx.location, ctx.$router); } return { c: function c() { a = createElement("a"); addListener(a, "click", click_handler); a.href = ctx.href; }, m: function m(target, anchor) { insertNode(a, target, anchor); if (slot_content_default) { appendNode(slot_content_default, a); } }, p: function p(changed, _ctx) { ctx = _ctx; if (changed.href) { a.href = ctx.href; } }, u: function u() { detachNode(a); if (slot_content_default) { reinsertChildren(a, slot_content_default); } }, d: function d() { removeListener(a, "click", click_handler); } }; } function Link(options) { init(this, options); this._state = assign(assign(this.store._init(["router", "curi"]), data()), options.data); this.store._add(this, ["router", "curi"]); this._recompute({ to: 1, params: 1, hash: 1, query: 1, state: 1, $router: 1, $curi: 1, location: 1 }, this._state); this._handlers.destroy = [removeFromStore]; this._slotted = options.slots || {}; this.slots = {}; this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); this._mount(options.target, options.anchor); } } assign(Link.prototype, proto); assign(Link.prototype, methods); Link.prototype._recompute = function _recompute(changed, state) { if (changed.to || changed.params || changed.hash || changed.query || changed.state || changed.$router || changed.$curi) { if (this._differs(state.location, state.location = location(state))) changed.location = true; } if (changed.location || changed.$router) { if (this._differs(state.href, state.href = href(state))) changed.href = true; } }; function curiStore(router, store$$1) { if (!store$$1) { store$$1 = new store.Store({ router: router, curi: router.current() }); } else { store$$1.set({ router: router, curi: router.current() }); } router.respond(function (_ref) { var response = _ref.response, navigation = _ref.navigation; store$$1.set({ curi: { response: response, navigation: navigation } }); }, { observe: true }); return store$$1; } exports.Link = Link; exports.curiStore = curiStore;