forked from canjs/canjs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to modifier so it works with processors.
- Loading branch information
Ralph Holzmann
committed
Aug 21, 2012
1 parent
44c1204
commit b4f0067
Showing
3 changed files
with
84 additions
and
31 deletions.
There are no files selected for viewing
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,43 +1,81 @@ | ||
steal("can/control", "can/util/function", function() { | ||
|
||
// Hang on to original action | ||
var originalAction = can.Control._action, | ||
originalShifter = can.Control._shifter; | ||
var originalSetup = can.Control.setup, | ||
processors = can.Control.processors, | ||
modifier = { | ||
delim: ":", | ||
hasModifier: function( name ) { | ||
return name.indexOf( modifier.delim ) !== -1; | ||
}, | ||
modify: function( name, fn, options ) { | ||
var parts = name.match(/([\w]+)\((.+)\)/), | ||
mod, args; | ||
|
||
// Redefine _isAction to handle new syntax | ||
can.extend( can.Control, { | ||
if ( parts ) { | ||
mod = can.getObject( parts[1], [options || {}, can, window] ); | ||
args = can.sub( parts[2], [options || {}, can, window] ).split(","); | ||
if ( mod ) { | ||
args.unshift( fn ); | ||
fn = mod.apply( null, args ); | ||
} | ||
} | ||
|
||
_action: function( methodName, options ) { | ||
return fn; | ||
}, | ||
addProcessor : function( event, mod ) { | ||
|
||
var processorName = [event, mod].join( modifier.delim ); | ||
|
||
processors[ processorName ] = function( el, nil, selector, methodName, control ) { | ||
|
||
var callback = modifier.modify( mod, can.Control._shifter(control, methodName), control.options ); | ||
control[event] = callback; | ||
|
||
if ( selector ) { | ||
can.bind( el, event, callback ); | ||
return function() { | ||
can.unbind( el, event, callback ); | ||
}; | ||
} else { | ||
selector = can.trim( selector ); | ||
can.delegate.call(el, selector, event, callback); | ||
return function() { | ||
can.undelegate.call(el, selector, event, callback); | ||
}; | ||
} | ||
|
||
var parts = methodName.split(":"), | ||
name = parts.shift(); | ||
|
||
return originalAction.apply( this, [ name, options ] ); | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
_shifter: function( context, name ) { | ||
var fn = originalShifter.apply( this, arguments ), | ||
parts = name.split(":"), | ||
fnName, args, modifier; | ||
// Redefine _isAction to handle new syntax | ||
can.extend( can.Control, { | ||
|
||
// If there's still a part left, this means we have a modifier | ||
if ( parts[1] ) { | ||
parts = parts.pop().match(/([\w]+)\((.+)\)/); | ||
modifier: modifier, | ||
|
||
if(parts){ | ||
fnName = parts[1]; | ||
args = parts[2] ? parts[2].split(","): []; | ||
setup: function( el, options ) { | ||
|
||
modifier = can.getObject( fnName, [ context.options, can, window ]); | ||
can.each( this.prototype, function( fn, key, prototype ) { | ||
var parts, event, mod; | ||
if ( modifier.hasModifier( key )) { | ||
// Figure out parts | ||
parts = key.split( modifier.delim ); | ||
event = parts.shift().split(" ").pop(); | ||
mod = parts.join(""); | ||
|
||
if ( modifier ) { | ||
args.unshift( fn ); | ||
fn = modifier.apply( null, args ); | ||
if ( ! ( key in processors )) { | ||
modifier.addProcessor( event, mod ); | ||
} | ||
|
||
} | ||
} | ||
return fn; | ||
}); | ||
|
||
originalSetup.apply( this, arguments ); | ||
|
||
} | ||
|
||
}); | ||
|
||
}); |
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