diff --git a/AppKit/CPTokenField.j b/AppKit/CPTokenField.j index 61481d970..bd4222449 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,11 +206,25 @@ 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 + 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; + 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]; @@ -1137,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]; @@ -1153,7 +1169,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:(int)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