Skip to content

Commit

Permalink
rebuilt
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 16, 2013
1 parent e8a4c29 commit a08906e
Show file tree
Hide file tree
Showing 9 changed files with 880 additions and 718 deletions.
496 changes: 279 additions & 217 deletions build/Ractive-legacy.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions build/Ractive-legacy.min.js

Large diffs are not rendered by default.

291 changes: 155 additions & 136 deletions build/Ractive-legacy.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ var cssTransitionsEnabled, transition, transitionend;
FileListBinding,
GenericBinding;

bindAttribute = function ( lazy ) {
var node = this.parentNode, interpolator, i, binding;
bindAttribute = function () {
var node = this.parentNode, interpolator, binding;

if ( !this.fragment ) {
return false; // report failure
Expand Down Expand Up @@ -510,30 +510,6 @@ var cssTransitionsEnabled, transition, transitionend;
node._ractive.binding = binding;
this.twoway = true;

this.boundEvents = [ 'change' ];

if ( !lazy ) {
this.boundEvents.push( 'input' );

// this is a hack to see if we're in IE - if so, we probably need to add
// a keyup listener as well, since in IE8 the input event doesn't fire,
// and in IE9 it doesn't fire when text is deleted
if ( node.attachEvent ) {
this.boundEvents.push( 'keyup' );
}
}

// Another IE fix, this time with checkboxes that don't fire change events
// until they blur
if ( node.attachEvent && node.type === 'checkbox' ) {
this.boundEvents.push( 'click' );
}

i = this.boundEvents.length;
while ( i-- ) {
node.addEventListener( this.boundEvents[i], updateModel, false );
}

return true;
};

Expand Down Expand Up @@ -582,7 +558,7 @@ var cssTransitionsEnabled, transition, transitionend;
}
}

if ( attribute.propertyName === 'check' ) {
if ( attribute.propertyName === 'checked' ) {
return new CheckedBinding( attribute, node );
}

Expand Down Expand Up @@ -1037,123 +1013,170 @@ addEventProxies = function ( element, proxies ) {
}
}
};
addEventProxy = function ( element, triggerEventName, proxyDescriptor, contextStack ) {
var root = element.root, proxyName, proxyArgs, dynamicArgs, definition, listener, handler, comboKey;
(function () {

element.ractify();
var MasterEventHandler,
ProxyEvent,
firePlainEvent,
fireEventWithArgs,
fireEventWithDynamicArgs,
customHandlers,
genericHandler,
getCustomHandler;

if ( typeof proxyDescriptor === 'string' ) {
proxyName = proxyDescriptor;
} else {
proxyName = proxyDescriptor.n;
}
addEventProxy = function ( element, triggerEventName, proxyDescriptor, contextStack, indexRefs ) {
var events, master;

// This key uniquely identifies this trigger+proxy name combo on this element
comboKey = triggerEventName + '=' + proxyName;

if ( proxyDescriptor.a ) {
proxyArgs = proxyDescriptor.a;
}
events = element.ractify().events;
master = events[ triggerEventName ] || ( events[ triggerEventName ] = new MasterEventHandler( element, triggerEventName, contextStack, indexRefs ) );

else if ( proxyDescriptor.d ) {
dynamicArgs = true;
master.add( proxyDescriptor );
};

proxyArgs = new StringFragment({
descriptor: proxyDescriptor.d,
root: element.root,
owner: element,
contextStack: contextStack
});
MasterEventHandler = function ( element, eventName, contextStack ) {
var definition;

if ( !element.proxyFrags ) {
element.proxyFrags = [];
}
element.proxyFrags[ element.proxyFrags.length ] = proxyArgs;
}
this.element = element;
this.root = element.root;
this.node = element.node;
this.name = eventName;
this.contextStack = contextStack; // TODO do we need to pass contextStack down everywhere? Doesn't it belong to the parentFragment?
this.proxies = [];

if ( proxyArgs !== undefined ) {
// store arguments on the element, so we can reuse the same handler
// with multiple elements
if ( element.node._ractive[ comboKey ] ) {
throw new Error( 'You cannot have two proxy events with the same trigger event (' + comboKey + ')' );
if ( definition = ( this.root.eventDefinitions[ eventName ] || Ractive.eventDefinitions[ eventName ] ) ) {
this.custom = definition( this.node, getCustomHandler( eventName ) );
} else {
this.node.addEventListener( eventName, genericHandler, false );
}
};

element.node._ractive[ comboKey ] = {
dynamic: dynamicArgs,
payload: proxyArgs
};
}
MasterEventHandler.prototype = {
add: function ( proxy ) {
this.proxies[ this.proxies.length ] = new ProxyEvent( this.element, this.root, proxy, this.contextStack );
},

// Is this a custom event?
if ( definition = ( root.eventDefinitions[ triggerEventName ] || Ractive.eventDefinitions[ triggerEventName ] ) ) {
// If the proxy is a string (e.g. <a proxy-click='select'>{{item}}</a>) then
// we can reuse the handler. This eliminates the need for event delegation
if ( !root._customProxies[ comboKey ] ) {
root._customProxies[ comboKey ] = function ( proxyEvent ) {
var args, payload;
// TODO teardown when element torn down
teardown: function () {
var i;

if ( !proxyEvent.node ) {
throw new Error( 'Proxy event definitions must fire events with a `node` property' );
}
if ( this.custom ) {
this.custom.teardown();
} else {
this.node.removeEventListener( this.name, genericHandler, false );
}

proxyEvent.keypath = proxyEvent.node._ractive.keypath;
proxyEvent.context = root.get( proxyEvent.keypath );
proxyEvent.index = proxyEvent.node._ractive.index;
i = this.proxies.length;
while ( i-- ) {
this.proxies[i].teardown();
}
},

if ( proxyEvent.node._ractive[ comboKey ] ) {
args = proxyEvent.node._ractive[ comboKey ];
payload = args.dynamic ? args.payload.toJSON() : args.payload;
}
fire: function ( event ) {
var i = this.proxies.length;

root.fire( proxyName, proxyEvent, payload );
};
while ( i-- ) {
this.proxies[i].fire( event );
}
}
};

handler = root._customProxies[ comboKey ];
ProxyEvent = function ( element, ractive, descriptor, contextStack ) {
var name;

// Use custom event. Apply definition to this node
listener = definition( element.node, handler );
element.customEventListeners[ element.customEventListeners.length ] = listener;
this.root = ractive;

return;
}
name = descriptor.n || descriptor;

// If not, we just need to check it is a valid event for this element
// warn about invalid event handlers, if we're in debug mode
if ( element.node[ 'on' + triggerEventName ] !== undefined && root.debug ) {
if ( console && console.warn ) {
console.warn( 'Invalid event handler (' + triggerEventName + ')' );
if ( typeof name === 'string' ) {
this.n = name;
} else {
this.n = new StringFragment({
descriptor: descriptor.n,
root: this.root,
owner: element,
contextStack: contextStack
});
}
}

if ( !root._proxies[ comboKey ] ) {
root._proxies[ comboKey ] = function ( event ) {
var args, payload, proxyEvent = {
node: element,
original: event,
keypath: element.node._ractive.keypath,
context: root.get( element.node._ractive.keypath ),
index: element.node._ractive.index
};
if ( descriptor.a ) {
this.a = descriptor.a;
this.fire = fireEventWithArgs;
return;
}

if ( descriptor.d ) {
this.d = new StringFragment({
descriptor: descriptor.d,
root: this.root,
owner: element,
contextStack: contextStack
});
this.fire = fireEventWithDynamicArgs;
return;
}

this.fire = firePlainEvent;
};

if ( element.node._ractive && element.node._ractive[ comboKey ] ) {
args = element.node._ractive[ comboKey ];
payload = args.dynamic ? args.payload.toJSON() : args.payload;
ProxyEvent.prototype = {
teardown: function () {
if ( this.n.teardown) {
this.n.teardown();
}

root.fire( proxyName, proxyEvent, payload );
};
}
if ( this.d ) {
this.d.teardown();
}
},

handler = root._proxies[ comboKey ];
bubble: noop // TODO can we get rid of this?
};

element.eventListeners[ element.eventListeners.length ] = {
n: triggerEventName,
h: handler
// the ProxyEvent instance fire method could be any of these
firePlainEvent = function ( event ) {
this.root.fire( this.n.toString(), event );
};

element.node.addEventListener( triggerEventName, handler, false );
};
fireEventWithArgs = function ( event ) {
this.root.fire( this.n.toString(), event, this.a );
};

fireEventWithDynamicArgs = function ( event ) {
this.root.fire( this.n.toString(), event, this.d.toJSON() );
};

// all native DOM events dealt with by Ractive share a single handler
genericHandler = function ( event ) {
var storage = this._ractive;

storage.events[ event.type ].fire({
node: this,
original: event,
index: storage.index,
keypath: storage.keypath,
context: storage.root.get( storage.keypath )
});
};

customHandlers = {};

getCustomHandler = function ( eventName ) {
if ( customHandlers[ eventName ] ) {
return customHandlers[ eventName ];
}

return customHandlers[ eventName ] = function ( event ) {
var storage = event.node._ractive;

event.index = storage.index;
event.keypath = storage.keypath;
event.context = storage.root.get( storage.keypath );

storage.events[ eventName ].fire( event );
};
};

}());
appendElementChildren = function ( element, node, descriptor, docFrag ) {
if ( typeof descriptor.f === 'string' && ( !node || ( !node.namespaceURI || node.namespaceURI === namespaces.html ) ) ) {
// great! we can use innerHTML
Expand Down Expand Up @@ -4964,6 +4987,7 @@ animationCollection = {
// Attribute
DomAttribute = function ( options ) {

this.element = options.element;
determineNameAndNamespace( this, options.name );

// if it's an empty attribute, or just a straight key-value pair, with no
Expand All @@ -4975,7 +4999,6 @@ animationCollection = {

// otherwise we need to do some work
this.root = options.root;
this.element = options.element;
this.parentNode = options.parentNode;

// share parentFragment with parent element
Expand Down Expand Up @@ -5136,7 +5159,7 @@ animationCollection = {
}

if ( attribute.name === 'value' ) {
options.parentNode._ractive.value = options.value;
attribute.element.ractify().value = options.value;
}
}

Expand Down Expand Up @@ -5464,30 +5487,21 @@ DomElement = function ( options, docFrag ) {

DomElement.prototype = {
teardown: function ( detach ) {
var self = this, listener;
var eventName;

// Children first. that way, any transitions on child elements will be
// handled by the current transitionManager
if ( self.fragment ) {
self.fragment.teardown( false );
if ( this.fragment ) {
this.fragment.teardown( false );
}

while ( self.attributes.length ) {
self.attributes.pop().teardown();
while ( this.attributes.length ) {
this.attributes.pop().teardown();
}

while ( self.eventListeners.length ) {
listener = self.eventListeners.pop();
self.node.removeEventListener( listener.n, listener.h, false );
}

while ( self.customEventListeners.length ) {
self.customEventListeners.pop().teardown();
}

if ( this.proxyFrags ) {
while ( this.proxyFrags.length ) {
this.proxyFrags.pop().teardown();
if ( this.node._ractive ) {
for ( eventName in this.node._ractive.events ) {
this.node._ractive.events[ eventName ].teardown();
}
}

Expand All @@ -5508,6 +5522,7 @@ DomElement.prototype = {
return null;
},

// TODO can we get rid of this?
bubble: noop, // just so event proxy and transition fragments have something to call!

toString: function () {
Expand Down Expand Up @@ -5542,10 +5557,14 @@ DomElement.prototype = {
defineProperty( this.node, '_ractive', {
value: {
keypath: ( contextStack.length ? contextStack[ contextStack.length - 1 ] : '' ),
index: this.parentFragment.indexRefs
index: this.parentFragment.indexRefs,
events: createFromNull(),
root: this.root
}
});
}

return this.node._ractive;
}
};
DomFragment = function ( options ) {
Expand Down
4 changes: 2 additions & 2 deletions build/Ractive-legacy.runtime.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit a08906e

Please sign in to comment.