|
| 1 | +define("test", function() { "use strict"; |
| 2 | + |
| 3 | + function create_main_fragment(component, ctx) { |
| 4 | + var p; |
| 5 | + |
| 6 | + return { |
| 7 | + c() { |
| 8 | + p = createElement("p"); |
| 9 | + p.textContent = "Hello world!"; |
| 10 | + }, |
| 11 | + |
| 12 | + m(target, anchor) { |
| 13 | + insert(target, p, anchor); |
| 14 | + }, |
| 15 | + |
| 16 | + p: noop, |
| 17 | + |
| 18 | + d(detach) { |
| 19 | + if (detach) { |
| 20 | + detachNode(p); |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + function Main(options) { |
| 27 | + init(this, options); |
| 28 | + this._state = assign({}, options.data); |
| 29 | + this._intro = true; |
| 30 | + |
| 31 | + this._fragment = create_main_fragment(this, this._state); |
| 32 | + |
| 33 | + if (options.target) { |
| 34 | + this._fragment.c(); |
| 35 | + this._mount(options.target, options.anchor); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + assign(Main.prototype, { |
| 40 | + destroy: destroy, |
| 41 | + get: get, |
| 42 | + fire: fire, |
| 43 | + on: on, |
| 44 | + set: set, |
| 45 | + _set: _set, |
| 46 | + _mount: _mount, |
| 47 | + _differs: _differs |
| 48 | + }); |
| 49 | + |
| 50 | + Main.prototype._recompute = noop; |
| 51 | + |
| 52 | + function createElement(name) { |
| 53 | + return document.createElement(name); |
| 54 | + } |
| 55 | + |
| 56 | + function insert(target, node, anchor) { |
| 57 | + target.insertBefore(node, anchor); |
| 58 | + } |
| 59 | + |
| 60 | + function noop() {} |
| 61 | + |
| 62 | + function detachNode(node) { |
| 63 | + node.parentNode.removeChild(node); |
| 64 | + } |
| 65 | + |
| 66 | + function init(component, options) { |
| 67 | + component._handlers = blankObject(); |
| 68 | + component._bind = options._bind; |
| 69 | + |
| 70 | + component.options = options; |
| 71 | + component.root = options.root || component; |
| 72 | + component.store = options.store || component.root.store; |
| 73 | + } |
| 74 | + |
| 75 | + function assign(tar, src) { |
| 76 | + for (var k in src) tar[k] = src[k]; |
| 77 | + return tar; |
| 78 | + } |
| 79 | + |
| 80 | + function destroy(detach) { |
| 81 | + this.destroy = noop; |
| 82 | + this.fire('destroy'); |
| 83 | + this.set = noop; |
| 84 | + |
| 85 | + this._fragment.d(detach !== false); |
| 86 | + this._fragment = null; |
| 87 | + this._state = {}; |
| 88 | + } |
| 89 | + |
| 90 | + function get() { |
| 91 | + return this._state; |
| 92 | + } |
| 93 | + |
| 94 | + function fire(eventName, data) { |
| 95 | + var handlers = |
| 96 | + eventName in this._handlers && this._handlers[eventName].slice(); |
| 97 | + if (!handlers) return; |
| 98 | + |
| 99 | + for (var i = 0; i < handlers.length; i += 1) { |
| 100 | + var handler = handlers[i]; |
| 101 | + |
| 102 | + if (!handler.__calling) { |
| 103 | + try { |
| 104 | + handler.__calling = true; |
| 105 | + handler.call(this, data); |
| 106 | + } finally { |
| 107 | + handler.__calling = false; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + function on(eventName, handler) { |
| 114 | + var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); |
| 115 | + handlers.push(handler); |
| 116 | + |
| 117 | + return { |
| 118 | + cancel: function() { |
| 119 | + var index = handlers.indexOf(handler); |
| 120 | + if (~index) handlers.splice(index, 1); |
| 121 | + } |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + function set(newState) { |
| 126 | + this._set(assign({}, newState)); |
| 127 | + if (this.root._lock) return; |
| 128 | + this.root._lock = true; |
| 129 | + callAll(this.root._beforecreate); |
| 130 | + callAll(this.root._oncreate); |
| 131 | + callAll(this.root._aftercreate); |
| 132 | + this.root._lock = false; |
| 133 | + } |
| 134 | + |
| 135 | + function _set(newState) { |
| 136 | + var oldState = this._state, |
| 137 | + changed = {}, |
| 138 | + dirty = false; |
| 139 | + |
| 140 | + for (var key in newState) { |
| 141 | + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; |
| 142 | + } |
| 143 | + if (!dirty) return; |
| 144 | + |
| 145 | + this._state = assign(assign({}, oldState), newState); |
| 146 | + this._recompute(changed, this._state); |
| 147 | + if (this._bind) this._bind(changed, this._state); |
| 148 | + |
| 149 | + if (this._fragment) { |
| 150 | + this.fire("state", { changed: changed, current: this._state, previous: oldState }); |
| 151 | + this._fragment.p(changed, this._state); |
| 152 | + this.fire("update", { changed: changed, current: this._state, previous: oldState }); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + function _mount(target, anchor) { |
| 157 | + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); |
| 158 | + } |
| 159 | + |
| 160 | + function _differs(a, b) { |
| 161 | + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); |
| 162 | + } |
| 163 | + |
| 164 | + function blankObject() { |
| 165 | + return Object.create(null); |
| 166 | + } |
| 167 | + |
| 168 | + function callAll(fns) { |
| 169 | + while (fns && fns.length) fns.shift()(); |
| 170 | + } |
| 171 | + return Main; |
| 172 | +}); |
0 commit comments