mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-17 04:00:42 +00:00
Support object representation in CPTokenField
Add some tests
This commit is contained in:
1 parent
9179a31a93
commit
0f8aa1e644
2 files changed
+107
-4
No files matched your search
+33
-4
@@ -465,7 +465,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
if ([token isKindOfClass:[CPString class]])
|
||||
continue;
|
||||
|
||||
[objectValue addObject:[token stringValue]];
|
||||
[objectValue addObject:[token representedObject]];
|
||||
}
|
||||
|
||||
#if PLATFORM(DOM)
|
||||
@@ -501,13 +501,14 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
for (var i = 0, count = [aValue count]; i < count; i++)
|
||||
{
|
||||
// Do we have this token among the old ones?
|
||||
var tokenValue = aValue[i],
|
||||
var tokenObject = aValue[i],
|
||||
tokenValue = [self tokenField:self displayStringForRepresentedObject:tokenObject],
|
||||
newToken = nil;
|
||||
|
||||
for (var j = 0, oldCount = [oldTokens count]; j < oldCount; j++)
|
||||
{
|
||||
var oldToken = oldTokens[j];
|
||||
if ([oldToken stringValue] == tokenValue)
|
||||
if ([oldToken representedObject] == tokenObject)
|
||||
{
|
||||
// Yep. Reuse it.
|
||||
[oldTokens removeObjectAtIndex:j];
|
||||
@@ -520,6 +521,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
{
|
||||
newToken = [[_CPTokenFieldToken alloc] init];
|
||||
[newToken setTokenField:self];
|
||||
[newToken setRepresentedObject:tokenObject];
|
||||
[newToken setStringValue:tokenValue];
|
||||
[contentView addSubview:newToken];
|
||||
}
|
||||
@@ -1111,6 +1113,23 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
// // Alows the delegate to provide a string to be displayed as a proxy for the given represented object.
|
||||
// // If you return nil or do not implement this method, then representedObject is displayed as the string.
|
||||
- (CPString)tokenField:(CPTokenField)tokenField displayStringForRepresentedObject:(id)representedObject
|
||||
{
|
||||
if ([[self delegate] respondsToSelector:@selector(tokenField:displayStringForRepresentedObject:)])
|
||||
{
|
||||
var stringForRepresentedObject = [[self delegate] tokenField:tokenField displayStringForRepresentedObject:representedObject];
|
||||
if (stringForRepresentedObject !== nil)
|
||||
{
|
||||
return stringForRepresentedObject;
|
||||
}
|
||||
}
|
||||
|
||||
return representedObject;
|
||||
}
|
||||
|
||||
//
|
||||
// // return an array of represented objects you want to add.
|
||||
// // If you want to reject the add, return an empty array.
|
||||
@@ -1119,7 +1138,6 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
//
|
||||
// // If you return nil or don't implement these delegate methods, we will assume
|
||||
// // editing string = display string = represented object
|
||||
// - (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject;
|
||||
// - (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject;
|
||||
// - (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString: (NSString *)editingString;
|
||||
//
|
||||
@@ -1143,6 +1161,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
{
|
||||
_CPTokenFieldTokenCloseButton _deleteButton;
|
||||
CPTokenField _tokenField;
|
||||
id _representedObject;
|
||||
}
|
||||
|
||||
+ (CPString)defaultThemeClass
|
||||
@@ -1175,6 +1194,16 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting",
|
||||
_tokenField = tokenField;
|
||||
}
|
||||
|
||||
- (id)representedObject
|
||||
{
|
||||
return _representedObject;
|
||||
}
|
||||
|
||||
- (void)setRepresentedObject:(id)representedObject
|
||||
{
|
||||
_representedObject = representedObject;
|
||||
}
|
||||
|
||||
- (CGSize)_minimumFrameSize
|
||||
{
|
||||
var size = CGSizeMakeZero(),
|
||||
|
||||
@@ -12,6 +12,7 @@ var STATES = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorad
|
||||
|
||||
@implementation AppController : CPObject
|
||||
{
|
||||
CPTokenField tokenFieldD;
|
||||
}
|
||||
|
||||
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
|
||||
@@ -61,10 +62,43 @@ var STATES = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorad
|
||||
|
||||
[contentView addSubview:tokenFieldC];
|
||||
|
||||
|
||||
tokenFieldD = [[CPTokenField alloc] initWithFrame:CGRectMake(15, 230, 500, 30)],
|
||||
labelD = [[CPTextField alloc] initWithFrame:CGRectMake(15, 210, 500, 24)];
|
||||
|
||||
[labelD setStringValue:"This token field contains represented objects."];
|
||||
[contentView addSubview:labelD];
|
||||
|
||||
[tokenFieldD setEditable:YES];
|
||||
|
||||
// Delegate must be setted before objectValue
|
||||
[tokenFieldD setDelegate:self];
|
||||
|
||||
[tokenFieldD setObjectValue:[
|
||||
[Person personWithFirstName:@"Luc" lastName:@"Vauvillier"],
|
||||
[Person personWithFirstName:@"John" lastName:@"Doe"],
|
||||
[Person personWithFirstName:@"Amélie" lastName:@"Poulain"],
|
||||
[Person personWithFirstName:@"Jean" lastName:@"Valjean"]
|
||||
]];
|
||||
|
||||
[contentView addSubview:tokenFieldD];
|
||||
|
||||
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 270, 0, 0)];
|
||||
[button setTitle:"Get Object Values"];
|
||||
[button sizeToFit];
|
||||
[button setTarget:self];
|
||||
[button setAction:@selector(getObjectValues:)];
|
||||
[contentView addSubview:button];
|
||||
|
||||
[theWindow orderFront:self];
|
||||
|
||||
}
|
||||
|
||||
- (void)getObjectValues:(id)sender
|
||||
{
|
||||
alert([tokenFieldD objectValue]);
|
||||
}
|
||||
|
||||
- (CPArray)tokenField:(CPTokenField)aTokenField completionsForSubstring:(CPString)substring indexOfToken:(int)tokenIndex indexOfSelectedItem:(int)selectedIndex
|
||||
{
|
||||
var r = [];
|
||||
@@ -80,4 +114,44 @@ var STATES = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorad
|
||||
return r;
|
||||
}
|
||||
|
||||
- (CPString)tokenField:(CPTokenField)tokenField displayStringForRepresentedObject:(id)representedObject
|
||||
{
|
||||
if ([representedObject isKindOfClass:Person])
|
||||
{
|
||||
return [representedObject fullname];
|
||||
}
|
||||
|
||||
return representedObject;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// A sample of a custom Object
|
||||
|
||||
@implementation Person : CPObject
|
||||
{
|
||||
CPString _firstName;
|
||||
CPString _lastName;
|
||||
}
|
||||
|
||||
+ (id)personWithFirstName:(CPString)aFirstName lastName:(CPString)aLastName
|
||||
{
|
||||
return [[self alloc] initWithFirstName:aFirstName lastName:aLastName];
|
||||
}
|
||||
|
||||
- (id)initWithFirstName:(CPString)aFirstName lastName:(CPString)aLastName {
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
_firstName = aFirstName;
|
||||
_lastName = aLastName;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (CPString)fullname
|
||||
{
|
||||
return [CPString stringWithFormat:@"%@ %@", _firstName, _lastName];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user