From 043df711857d850e59a8d61f6296da6a89df5df0 Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Thu, 8 Sep 2011 22:56:52 -0500 Subject: [PATCH 1/4] Enabled tokenField:shouldAddObjects:atIndex delegate method and used on _autocompleteWithDOMEvent method to have some control over tokens. Not sure why wasn't enabled, it was very few code. Maybe no one needed it 'til now --- AppKit/CPTokenField.j | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/AppKit/CPTokenField.j b/AppKit/CPTokenField.j index 61481d970..ac35a1dd6 100755 --- a/AppKit/CPTokenField.j +++ b/AppKit/CPTokenField.j @@ -171,6 +171,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", - (void)_autocompleteWithDOMEvent:(JSObject)DOMEvent { + var index = 0; if (!_cachedCompletions || ![self hasThemeState:CPThemeStateAutoCompleting]) return; @@ -205,8 +206,11 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", // Explicitly remove the last object because the array contains strings and removeObject uses isEqual to compare objects if (shouldRemoveLastObject) [objectValue removeObjectAtIndex:_selectedRange.location]; - - [objectValue insertObject:token atIndex:_selectedRange.location]; + + //confirm with the delegate the token inclusion + if ([[self tokenField:self shouldAddObjects:[CPArray arrayWithObject:token] atIndex:index] count]) + [objectValue insertObject:token atIndex:_selectedRange.location]; + var location = _selectedRange.location; [self setObjectValue:objectValue]; _selectedRange = CPMakeRange(location + 1, 0); @@ -1153,7 +1157,19 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", // // return an array of represented objects you want to add. // // If you want to reject the add, return an empty array. // // returning nil will cause an error. -// - (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index; +- (CPArray)tokenField:(CPTokenField )tokenField shouldAddObjects:(CPArray)tokens atIndex:(NSUInteger)index + { + if ([[self delegate] respondsToSelector:@selector(tokenField:shouldAddObjects:atIndex:)]) + { + var approvedObjects = [[self delegate] tokenField:tokenField shouldAddObjects:tokens atIndex:index]; + if (approvedObjects !== nil) + { + return approvedObjects; + } + } + + return tokens; + } // // // If you return nil or don't implement these delegate methods, we will assume // // editing string = display string = represented object From 662b1ec69684c39af8e6fe1908e6a28224f32da2 Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Fri, 9 Sep 2011 12:48:18 -0500 Subject: [PATCH 2/4] Submitted the right CPTokenField file, was missing the part to use the delegate approved tokens. Still lacks a way to put the cursor after the last token (if the delegate returns more than one token) --- AppKit/CPTokenField.j | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AppKit/CPTokenField.j b/AppKit/CPTokenField.j index ac35a1dd6..481ca298d 100755 --- a/AppKit/CPTokenField.j +++ b/AppKit/CPTokenField.j @@ -208,8 +208,14 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", [objectValue removeObjectAtIndex:_selectedRange.location]; //confirm with the delegate the token inclusion - if ([[self tokenField:self shouldAddObjects:[CPArray arrayWithObject:token] atIndex:index] count]) - [objectValue insertObject:token atIndex:_selectedRange.location]; + delegateApprovedObjects = [self tokenField:self shouldAddObjects:[CPArray arrayWithObject:token] atIndex:index]; + if (delegateApprovedObjects) + { + for(var i = 0; i < [delegateApprovedObjects count]; i++) + { + [objectValue insertObject:[delegateApprovedObjects objectAtIndex:i] atIndex:_selectedRange.location + i]; + } + } var location = _selectedRange.location; [self setObjectValue:objectValue]; From c6403baa04efb2e6f7906097101d01ada3698047 Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Fri, 9 Sep 2011 13:28:37 -0500 Subject: [PATCH 3/4] Fixed cursor position issue in a little escapade...guess that's all for now folks --- AppKit/CPTokenField.j | 1 + 1 file changed, 1 insertion(+) diff --git a/AppKit/CPTokenField.j b/AppKit/CPTokenField.j index 481ca298d..c5eed96ed 100755 --- a/AppKit/CPTokenField.j +++ b/AppKit/CPTokenField.j @@ -215,6 +215,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", { [objectValue insertObject:[delegateApprovedObjects objectAtIndex:i] atIndex:_selectedRange.location + i]; } + _selectedRange.location += [delegateApprovedObjects count]; } var location = _selectedRange.location; From e5284e9b43a32838016c94fd29ffb7a06b921d96 Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Fri, 9 Sep 2011 22:31:17 -0500 Subject: [PATCH 4/4] Fixed some possible error of my part where selectedRange is moved beyond bounds --- AppKit/CPTokenField.j | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/AppKit/CPTokenField.j b/AppKit/CPTokenField.j index c5eed96ed..bd4222449 100755 --- a/AppKit/CPTokenField.j +++ b/AppKit/CPTokenField.j @@ -214,13 +214,17 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", for(var i = 0; i < [delegateApprovedObjects count]; i++) { [objectValue insertObject:[delegateApprovedObjects objectAtIndex:i] atIndex:_selectedRange.location + i]; - } - _selectedRange.location += [delegateApprovedObjects count]; + } } var location = _selectedRange.location; + var delegateApprovedObjectsCount = [delegateApprovedObjects count]; [self setObjectValue:objectValue]; - _selectedRange = CPMakeRange(location + 1, 0); + //this part puts the cursor after the last token + if(delegateApprovedObjectsCount > 1) + _selectedRange = CPMakeRange(location + delegateApprovedObjectsCount - 1, 0); + else + _selectedRange = CPMakeRange(location + 1, 0); [self _inputElement].value = @""; [self setNeedsLayout]; @@ -1148,6 +1152,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", // // 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]; @@ -1164,7 +1169,7 @@ var CPThemeStateAutoCompleting = @"CPThemeStateAutoCompleting", // // return an array of represented objects you want to add. // // If you want to reject the add, return an empty array. // // returning nil will cause an error. -- (CPArray)tokenField:(CPTokenField )tokenField shouldAddObjects:(CPArray)tokens atIndex:(NSUInteger)index +- (CPArray)tokenField:(CPTokenField )tokenField shouldAddObjects:(CPArray)tokens atIndex:(int)index { if ([[self delegate] respondsToSelector:@selector(tokenField:shouldAddObjects:atIndex:)]) {