This repository has been archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Strip byte order mark
- Loading branch information
Showing
27 changed files
with
3,418 additions
and
1,598 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,198 @@ | ||
function render_main_fragment ( root, component ) { | ||
var p = createElement( 'p' ); | ||
|
||
appendNode( createText( "Hello world!" ), p ); | ||
function create_main_fragment ( state, component ) { | ||
var p, text; | ||
|
||
return { | ||
create: function () { | ||
p = createElement( 'p' ); | ||
text = createText( "Hello world!" ); | ||
}, | ||
|
||
mount: function ( target, anchor ) { | ||
insertNode( p, target, anchor ); | ||
appendNode( text, p ); | ||
}, | ||
|
||
update: noop, | ||
|
||
teardown: function ( detach ) { | ||
if ( detach ) { | ||
detachNode( p ); | ||
} | ||
} | ||
|
||
unmount: function () { | ||
detachNode( p ); | ||
}, | ||
|
||
destroy: noop | ||
}; | ||
} | ||
|
||
function Main ( options ) { | ||
options = options || {}; | ||
this._state = options.data || {}; | ||
|
||
this._observers = { | ||
pre: Object.create( null ), | ||
post: Object.create( null ) | ||
}; | ||
|
||
this._handlers = Object.create( null ); | ||
this._root = options._root; | ||
|
||
this._root = options._root || this; | ||
this._yield = options._yield; | ||
|
||
this._torndown = false; | ||
|
||
this._fragment = render_main_fragment( this._state, this ); | ||
if ( options.target ) this._fragment.mount( options.target, null ); | ||
|
||
this._fragment = create_main_fragment( this._state, this ); | ||
|
||
if ( options.target ) { | ||
this._fragment.create(); | ||
this._fragment.mount( options.target, null ); | ||
} | ||
} | ||
|
||
assign( Main.prototype, { | ||
get: get, | ||
fire: fire, | ||
observe: observe, | ||
on: on, | ||
set: set, | ||
_flush: _flush | ||
}); | ||
get: get, | ||
fire: fire, | ||
observe: observe, | ||
on: on, | ||
set: set, | ||
_flush: _flush | ||
}); | ||
|
||
Main.prototype._set = function _set ( newState ) { | ||
var oldState = this._state; | ||
this._state = assign( {}, oldState, newState ); | ||
|
||
dispatchObservers( this, this._observers.pre, newState, oldState ); | ||
if ( this._fragment ) this._fragment.update( newState, this._state ); | ||
dispatchObservers( this, this._observers.post, newState, oldState ); | ||
}; | ||
|
||
Main.prototype.teardown = Main.prototype.destroy = function destroy ( detach ) { | ||
this.fire( 'destroy' ); | ||
|
||
this._fragment.teardown( detach !== false ); | ||
if ( detach !== false ) this._fragment.unmount(); | ||
this._fragment.destroy(); | ||
this._fragment = null; | ||
|
||
this._state = {}; | ||
this._torndown = true; | ||
}; | ||
|
||
function createElement( name ) { | ||
return document.createElement( name ); | ||
function createElement(name) { | ||
return document.createElement(name); | ||
} | ||
|
||
function detachNode( node ) { | ||
node.parentNode.removeChild( node ); | ||
function insertNode(node, target, anchor) { | ||
target.insertBefore(node, anchor); | ||
} | ||
|
||
function insertNode( node, target, anchor ) { | ||
target.insertBefore( node, anchor ); | ||
function detachNode(node) { | ||
node.parentNode.removeChild(node); | ||
} | ||
|
||
function createText( data ) { | ||
return document.createTextNode( data ); | ||
function createText(data) { | ||
return document.createTextNode(data); | ||
} | ||
|
||
function appendNode( node, target ) { | ||
target.appendChild( node ); | ||
function appendNode(node, target) { | ||
target.appendChild(node); | ||
} | ||
|
||
function noop() {} | ||
|
||
function assign( target ) { | ||
for ( var i = 1; i < arguments.length; i += 1 ) { | ||
var source = arguments[i]; | ||
for ( var k in source ) target[k] = source[k]; | ||
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 dispatchObservers( component, group, newState, oldState ) { | ||
for ( var key in group ) { | ||
if ( !( key in newState ) ) continue; | ||
|
||
var newValue = newState[ key ]; | ||
var oldValue = oldState[ key ]; | ||
function dispatchObservers(component, group, newState, oldState) { | ||
for (var key in group) { | ||
if (!(key in newState)) continue; | ||
|
||
if ( newValue === oldValue && typeof newValue !== 'object' ) continue; | ||
var newValue = newState[key]; | ||
var oldValue = oldState[key]; | ||
|
||
var callbacks = group[ key ]; | ||
if ( !callbacks ) continue; | ||
if (differs(newValue, oldValue)) { | ||
var callbacks = group[key]; | ||
if (!callbacks) continue; | ||
|
||
for ( var i = 0; i < callbacks.length; i += 1 ) { | ||
var callback = callbacks[i]; | ||
if ( callback.__calling ) 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; | ||
callback.__calling = true; | ||
callback.call(component, newValue, oldValue); | ||
callback.__calling = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function get( key ) { | ||
return key ? this._state[ key ] : this._state; | ||
function noop() {} | ||
|
||
function get(key) { | ||
return key ? this._state[key] : this._state; | ||
} | ||
|
||
function fire( eventName, data ) { | ||
var handlers = eventName in this._handlers && this._handlers[ eventName ].slice(); | ||
if ( !handlers ) return; | ||
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 ); | ||
for (var i = 0; i < handlers.length; i += 1) { | ||
handlers[i].call(this, data); | ||
} | ||
} | ||
|
||
function observe( key, callback, options ) { | ||
var group = ( options && options.defer ) ? this._observers.pre : this._observers.post; | ||
function observe(key, callback, options) { | ||
var group = options && options.defer | ||
? this._observers.post | ||
: this._observers.pre; | ||
|
||
( group[ key ] || ( group[ key ] = [] ) ).push( callback ); | ||
(group[key] || (group[key] = [])).push(callback); | ||
|
||
if ( !options || options.init !== false ) { | ||
if (!options || options.init !== false) { | ||
callback.__calling = true; | ||
callback.call( this, this._state[ key ] ); | ||
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 ); | ||
cancel: function() { | ||
var index = group[key].indexOf(callback); | ||
if (~index) group[key].splice(index, 1); | ||
} | ||
}; | ||
} | ||
|
||
function on( eventName, handler ) { | ||
if ( eventName === 'teardown' ) return this.on( 'destroy', handler ); | ||
function on(eventName, handler) { | ||
if (eventName === 'teardown') return this.on('destroy', handler); | ||
|
||
var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] ); | ||
handlers.push( 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 ); | ||
cancel: function() { | ||
var index = handlers.indexOf(handler); | ||
if (~index) handlers.splice(index, 1); | ||
} | ||
}; | ||
} | ||
|
||
function set( newState ) { | ||
this._set( newState ); | ||
( this._root || this )._flush(); | ||
function set(newState) { | ||
this._set(assign({}, newState)); | ||
this._root._flush(); | ||
} | ||
|
||
function _flush() { | ||
if ( !this._renderHooks ) return; | ||
if (!this._renderHooks) return; | ||
|
||
while ( this._renderHooks.length ) { | ||
var hook = this._renderHooks.pop(); | ||
hook.fn.call( hook.context ); | ||
while (this._renderHooks.length) { | ||
this._renderHooks.pop()(); | ||
} | ||
} | ||
|
||
function differs(a, b) { | ||
return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); | ||
} | ||
|
||
export default Main; |
Oops, something went wrong.