UNPKG

svelte

Version:

The magical disappearing UI framework

583 lines (479 loc) • 13.8 kB
function noop() {} function assign(target) { var k, source, i = 1, len = arguments.length; for (; i < len; i++) { source = arguments[i]; for (k in source) target[k] = source[k]; } return target; } function appendNode(node, target) { target.appendChild(node); } function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } function detachNode(node) { node.parentNode.removeChild(node); } function detachBetween(before, after) { while (before.nextSibling && before.nextSibling !== after) { before.parentNode.removeChild(before.nextSibling); } } function detachBefore(after) { while (after.previousSibling) { after.parentNode.removeChild(after.previousSibling); } } function detachAfter(before) { while (before.nextSibling) { before.parentNode.removeChild(before.nextSibling); } } function reinsertBetween(before, after, target) { while (before.nextSibling && before.nextSibling !== after) { target.appendChild(before.parentNode.removeChild(before.nextSibling)); } } function reinsertChildren(parent, target) { while (parent.firstChild) target.appendChild(parent.firstChild); } function reinsertAfter(before, target) { while (before.nextSibling) target.appendChild(before.nextSibling); } function reinsertBefore(after, target) { var parent = after.parentNode; while (parent.firstChild !== after) target.appendChild(parent.firstChild); } function destroyEach(iterations) { for (var i = 0; i < iterations.length; i += 1) { if (iterations[i]) iterations[i].d(); } } function createFragment() { return document.createDocumentFragment(); } function createElement(name) { return document.createElement(name); } function createSvgElement(name) { return document.createElementNS('http://www.w3.org/2000/svg', name); } function createText(data) { return document.createTextNode(data); } function createComment() { return document.createComment(''); } function addListener(node, event, handler) { node.addEventListener(event, handler, false); } function removeListener(node, event, handler) { node.removeEventListener(event, handler, false); } function setAttribute(node, attribute, value) { node.setAttribute(attribute, value); } function setXlinkAttribute(node, attribute, value) { node.setAttributeNS('http://www.w3.org/1999/xlink', attribute, value); } function getBindingGroupValue(group) { var value = []; for (var i = 0; i < group.length; i += 1) { if (group[i].checked) value.push(group[i].__value); } return value; } function toNumber(value) { return value === '' ? undefined : +value; } function timeRangesToArray(ranges) { var array = []; for (var i = 0; i < ranges.length; i += 1) { array.push({ start: ranges.start(i), end: ranges.end(i) }); } return array; } function children (element) { return Array.from(element.childNodes); } function claimElement (nodes, name, attributes, svg) { for (var i = 0; i < nodes.length; i += 1) { var node = nodes[i]; if (node.nodeName === name) { for (var j = 0; j < node.attributes.length; j += 1) { var attribute = node.attributes[j]; if (!attributes[attribute.name]) node.removeAttribute(attribute.name); } return nodes.splice(i, 1)[0]; // TODO strip unwanted attributes } } return svg ? createSvgElement(name) : createElement(name); } function claimText (nodes, data) { for (var i = 0; i < nodes.length; i += 1) { var node = nodes[i]; if (node.nodeType === 3) { node.data = data; return nodes.splice(i, 1)[0]; } } return createText(data); } function setInputType(input, type) { try { input.type = type; } catch (e) {} } function setStyle(node, key, value) { node.style.setProperty(key, value); } function linear(t) { return t; } function generateRule( a, b, delta, duration, ease, fn ) { var keyframes = '{\n'; for (var p = 0; p <= 1; p += 16.666 / duration) { var t = a + delta * ease(p); keyframes += p * 100 + '%{' + fn(t) + '}\n'; } return keyframes + '100% {' + fn(b) + '}\n}'; } // https://github.com/darkskyapp/string-hash/blob/master/index.js function hash(str) { var hash = 5381; var i = str.length; while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i); return hash >>> 0; } function wrapTransition(component, node, fn, params, intro, outgroup) { var obj = fn(node, params); var duration = obj.duration || 300; var ease = obj.easing || linear; var cssText; // TODO share <style> tag between all transitions? if (obj.css && !transitionManager.stylesheet) { var style = createElement('style'); document.head.appendChild(style); transitionManager.stylesheet = style.sheet; } if (intro) { if (obj.css && obj.delay) { cssText = node.style.cssText; node.style.cssText += obj.css(0); } if (obj.tick) obj.tick(0); } return { t: intro ? 0 : 1, running: false, program: null, pending: null, run: function(intro, callback) { var program = { start: window.performance.now() + (obj.delay || 0), intro: intro, callback: callback }; if (obj.delay) { this.pending = program; } else { this.start(program); } if (!this.running) { this.running = true; transitionManager.add(this); } }, start: function(program) { component.fire(program.intro ? 'intro.start' : 'outro.start', { node: node }); program.a = this.t; program.b = program.intro ? 1 : 0; program.delta = program.b - program.a; program.duration = duration * Math.abs(program.b - program.a); program.end = program.start + program.duration; if (obj.css) { if (obj.delay) node.style.cssText = cssText; program.rule = generateRule( program.a, program.b, program.delta, program.duration, ease, obj.css ); transitionManager.addRule(program.rule, program.name = '__svelte_' + hash(program.rule)); node.style.animation = (node.style.animation || '') .split(', ') .filter(function(anim) { // when introing, discard old animations if there are any return anim && (program.delta < 0 || !/__svelte/.test(anim)); }) .concat(program.name + ' ' + duration + 'ms linear 1 forwards') .join(', '); } this.program = program; this.pending = null; }, update: function(now) { var program = this.program; if (!program) return; var p = now - program.start; this.t = program.a + program.delta * ease(p / program.duration); if (obj.tick) obj.tick(this.t); }, done: function() { var program = this.program; this.t = program.b; if (obj.tick) obj.tick(this.t); if (obj.css) transitionManager.deleteRule(node, program.name); program.callback(); program = null; this.running = !!this.pending; }, abort: function() { if (obj.tick) obj.tick(1); if (obj.css) transitionManager.deleteRule(node, this.program.name); this.program = this.pending = null; this.running = false; } }; } var transitionManager = { running: false, transitions: [], bound: null, stylesheet: null, activeRules: {}, add: function(transition) { this.transitions.push(transition); if (!this.running) { this.running = true; requestAnimationFrame(this.bound || (this.bound = this.next.bind(this))); } }, addRule: function(rule, name) { if (!this.activeRules[name]) { this.activeRules[name] = true; this.stylesheet.insertRule('@keyframes ' + name + ' ' + rule, this.stylesheet.cssRules.length); } }, next: function() { this.running = false; var now = window.performance.now(); var i = this.transitions.length; while (i--) { var transition = this.transitions[i]; if (transition.program && now >= transition.program.end) { transition.done(); } if (transition.pending && now >= transition.pending.start) { transition.start(transition.pending); } if (transition.running) { transition.update(now); this.running = true; } else if (!transition.pending) { this.transitions.splice(i, 1); } } if (this.running) { requestAnimationFrame(this.bound); } else if (this.stylesheet) { var i = this.stylesheet.cssRules.length; while (i--) this.stylesheet.deleteRule(i); this.activeRules = {}; } }, deleteRule: function(node, name) { node.style.animation = node.style.animation .split(', ') .filter(function(anim) { return anim.slice(0, name.length) !== name; }) .join(', '); } }; function blankObject() { return Object.create(null); } function destroy(detach) { this.destroy = noop; this.fire('destroy'); this.set = this.get = noop; if (detach !== false) this._fragment.u(); this._fragment.d(); this._fragment = this._state = null; } function destroyDev(detach) { destroy.call(this, detach); this.destroy = function() { console.warn('Component was already destroyed'); }; } function differs(a, b) { return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; var callbacks = group[key]; if (!callbacks) continue; for (var i = 0; i < callbacks.length; i += 1) { var callback = callbacks[i]; if (callback.__calling) continue; callback.__calling = true; callback.call(component, newValue, oldValue); callback.__calling = false; } } } 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) { handlers[i].call(this, data); } } function get(key) { return key ? this._state[key] : this._state; } function init(component, options) { component.options = options; component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._root = options._root || component; component._yield = options._yield; component._bind = options._bind; } function observe(key, callback, options) { var group = options && options.defer ? this._observers.post : this._observers.pre; (group[key] || (group[key] = [])).push(callback); if (!options || options.init !== false) { callback.__calling = true; callback.call(this, this._state[key]); callback.__calling = false; } return { cancel: function() { var index = group[key].indexOf(callback); if (~index) group[key].splice(index, 1); } }; } function observeDev(key, callback, options) { var c = (key = '' + key).search(/[^\w]/); if (c > -1) { var message = 'The first argument to component.observe(...) must be the name of a top-level property'; if (c > 0) message += ", i.e. '" + key.slice(0, c) + "' rather than '" + key + "'"; throw new Error(message); } return observe.call(this, key, callback, options); } function on(eventName, handler) { if (eventName === 'teardown') return this.on('destroy', 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 onDev(eventName, handler) { if (eventName === 'teardown') { console.warn( "Use component.on('destroy', ...) instead of component.on('teardown', ...) which has been deprecated and will be unsupported in Svelte 2" ); return this.on('destroy', handler); } return on.call(this, eventName, handler); } 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 (differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; this._state = assign({}, oldState, newState); this._recompute(changed, this._state); if (this._bind) this._bind(changed, this._state); dispatchObservers(this, this._observers.pre, changed, this._state, oldState); this._fragment.p(changed, this._state); dispatchObservers(this, this._observers.post, changed, this._state, oldState); } function _setDev(newState) { if (typeof newState !== 'object') { throw new Error( this._debugName + ' .set was called without an object of data key-values to update.' ); } this._checkReadOnly(newState); _set.call(this, newState); } function callAll(fns) { while (fns && fns.length) fns.pop()(); } function _mount(target, anchor) { this._fragment.m(target, anchor); } function _unmount() { this._fragment.u(); } var proto = { destroy: destroy, get: get, fire: fire, observe: observe, on: on, set: set, teardown: destroy, _recompute: noop, _set: _set, _mount: _mount, _unmount: _unmount }; var protoDev = { destroy: destroyDev, get: get, fire: fire, observe: observeDev, on: onDev, set: set, teardown: destroyDev, _recompute: noop, _set: _setDev, _mount: _mount, _unmount: _unmount }; export { blankObject, destroy, destroyDev, differs, dispatchObservers, fire, get, init, observe, observeDev, on, onDev, set, _set, _setDev, callAll, _mount, _unmount, proto, protoDev, appendNode, insertNode, detachNode, detachBetween, detachBefore, detachAfter, reinsertBetween, reinsertChildren, reinsertAfter, reinsertBefore, destroyEach, createFragment, createElement, createSvgElement, createText, createComment, addListener, removeListener, setAttribute, setXlinkAttribute, getBindingGroupValue, toNumber, timeRangesToArray, children, claimElement, claimText, setInputType, setStyle, linear, generateRule, hash, wrapTransition, transitionManager, noop, assign };