mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
Tests/Manual/People: Add free-form tags to people, with a token field in Details and an Any Tag smart folder row
This commit is contained in:
1 parent
1d8935e1a1
commit
ebf2356696
6 files changed
+126
-9
No files matched your search
@@ -34,10 +34,11 @@ PeopleDragType = @"PeoplePeopleDragType";
|
||||
SourceItemDragType = @"PeopleSourceItemDragType";
|
||||
|
||||
LibraryDepartments = [@"Engineering", @"Design", @"Marketing", @"Sales", @"Support", @"Finance", @"Legal"];
|
||||
LibraryTags = [@"Remote", @"Mentor", @"Onboarding", @"Speaker", @"Part-time", @"Contractor", @"First Aid", @"Volunteer"];
|
||||
LibraryCities = [@"Berlin", @"Hamburg", @"Munich", @"Zürich", @"Vienna", @"Paris", @"London", @"Lisbon", @"New York", @"San Francisco", @"Tokyo"];
|
||||
|
||||
var LibraryDefaultsKey = @"People.Library.v1",
|
||||
ObservedPersonKeys = [@"firstName", @"lastName", @"email", @"department", @"city", @"birthDate", @"salary", @"isManager", @"notes"],
|
||||
ObservedPersonKeys = [@"firstName", @"lastName", @"email", @"department", @"city", @"birthDate", @"salary", @"isManager", @"notes", @"tags"],
|
||||
SourceItemUIDCounter = 0;
|
||||
|
||||
|
||||
@@ -240,6 +241,34 @@ var LibraryDefaultsKey = @"People.Library.v1",
|
||||
return _people;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns every tag in use, once each, sorted, with the sample tags. The detail pane offers
|
||||
them as completions in its token field.
|
||||
*/
|
||||
- (CPArray)allTags
|
||||
{
|
||||
var seen = {},
|
||||
result = [];
|
||||
|
||||
function add(aTag)
|
||||
{
|
||||
var key = aTag.toLowerCase();
|
||||
|
||||
if (!seen[key])
|
||||
{
|
||||
seen[key] = YES;
|
||||
result.push(aTag);
|
||||
}
|
||||
}
|
||||
|
||||
LibraryTags.forEach(add);
|
||||
|
||||
for (var i = 0, count = [_people count]; i < count; i++)
|
||||
([_people[i] tags] || []).forEach(add);
|
||||
|
||||
return result.sort(function(a, b) { return a.localeCompare(b); });
|
||||
}
|
||||
|
||||
- (CPArray)sections
|
||||
{
|
||||
return _sections;
|
||||
@@ -860,6 +889,19 @@ var LibraryDefaultsKey = @"People.Library.v1",
|
||||
[person setSalary:38000 + Math.round(random() * 240) * 500];
|
||||
[person setIsManager:random() < 0.12];
|
||||
|
||||
var tagCount = Math.floor(random() * 3),
|
||||
personTags = [];
|
||||
|
||||
while (personTags.length < tagCount)
|
||||
{
|
||||
var tag = pick(LibraryTags);
|
||||
|
||||
if (personTags.indexOf(tag) === -1)
|
||||
personTags.push(tag);
|
||||
}
|
||||
|
||||
[person setTags:personTags];
|
||||
|
||||
if (i % 13 === 3)
|
||||
[person setNotes:sampleNote(noteIndex++)];
|
||||
|
||||
|
||||
+18
-3
@@ -8,7 +8,9 @@
|
||||
* - CPPredicateEditor (CPRuleEditor subclass) built entirely from
|
||||
* CPPredicateEditorRowTemplate instances: compound (AND/OR/NOT),
|
||||
* key path + string attribute type with [cd] options, key path + integer
|
||||
* attribute type, key path + constant right expressions (pop-up)
|
||||
* attribute type, key path + constant right expressions (pop-up), and
|
||||
* key path + string attribute type with the ANY modifier, for the tags
|
||||
* array ("ANY tags ==[cd] 'Remote'")
|
||||
* - round trip predicate -> editor (setObjectValue:) -> predicate
|
||||
* - CPRuleEditorRowsDidChangeNotification + target/action for live preview
|
||||
* - resizing a sheet while it is attached
|
||||
@@ -32,7 +34,8 @@ var EditorWidth = 680.0,
|
||||
"age" : "Age",
|
||||
"salary" : "Salary",
|
||||
"notesText" : "Notes",
|
||||
"hasNotes" : "Has Notes"
|
||||
"hasNotes" : "Has Notes",
|
||||
"tags" : "Any Tag"
|
||||
};
|
||||
|
||||
|
||||
@@ -281,7 +284,19 @@ var EditorWidth = 680.0,
|
||||
operators:[CPEqualToPredicateOperatorType]
|
||||
options:0];
|
||||
|
||||
return [compound, strings, numbers, departments, flags];
|
||||
/*
|
||||
Tags is an array, so the comparison uses the ANY modifier: the row matches a person
|
||||
when at least one tag passes it.
|
||||
*/
|
||||
var tags = [[LabeledRowTemplate alloc] initWithLeftExpressions:keyPaths([@"tags"])
|
||||
rightExpressionAttributeType:CPStringAttributeType
|
||||
modifier:CPAnyPredicateModifier
|
||||
operators:[CPEqualToPredicateOperatorType,
|
||||
CPContainsPredicateOperatorType,
|
||||
CPBeginsWithPredicateOperatorType]
|
||||
options:CPCaseInsensitivePredicateOption | CPDiacriticInsensitivePredicateOption];
|
||||
|
||||
return [compound, strings, numbers, departments, flags, tags];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Running the sheet
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
* table's notes column and the Has Notes smart folder use. The notes are saved
|
||||
* as a keyed archive, because an attributed string is not a property list
|
||||
* value.
|
||||
*
|
||||
* "tags" is an array of free-form strings, edited in a CPTokenField in the
|
||||
* detail pane. "tagsText", the tags joined by commas, is a derived key that
|
||||
* search uses.
|
||||
*/
|
||||
|
||||
@import <Foundation/Foundation.j>
|
||||
@@ -38,6 +42,7 @@ var PersonUIDCounter = 0;
|
||||
CPNumber salary @accessors;
|
||||
BOOL isManager @accessors;
|
||||
CPAttributedString notes @accessors;
|
||||
CPArray tags @accessors;
|
||||
}
|
||||
|
||||
+ (CPString)nextUID
|
||||
@@ -65,6 +70,11 @@ var PersonUIDCounter = 0;
|
||||
return [CPSet setWithObject:@"notes"];
|
||||
}
|
||||
|
||||
+ (CPSet)keyPathsForValuesAffectingTagsText
|
||||
{
|
||||
return [CPSet setWithObject:@"tags"];
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
return [self initWithUID:[Person nextUID]];
|
||||
@@ -83,6 +93,7 @@ var PersonUIDCounter = 0;
|
||||
birthDate = new Date(1990, 0, 1);
|
||||
salary = 50000;
|
||||
isManager = NO;
|
||||
tags = [];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -106,6 +117,8 @@ var PersonUIDCounter = 0;
|
||||
|
||||
var archivedNotes = [aDictionary objectForKey:@"notes"];
|
||||
notes = archivedNotes ? [CPKeyedUnarchiver unarchiveObjectWithData:[CPData dataWithRawString:archivedNotes]] : nil;
|
||||
|
||||
tags = [([aDictionary objectForKey:@"tags"] || []) copy];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -124,7 +137,8 @@ var PersonUIDCounter = 0;
|
||||
birthDate ? [birthDate timeIntervalSince1970] : 0, @"birthDate",
|
||||
Number(salary || 0), @"salary",
|
||||
isManager ? 1 : 0, @"isManager",
|
||||
[self hasNotes] ? [[CPKeyedArchiver archivedDataWithRootObject:notes] rawString] : @"", @"notes"];
|
||||
[self hasNotes] ? [[CPKeyedArchiver archivedDataWithRootObject:notes] rawString] : @"", @"notes",
|
||||
[(tags || []) copy], @"tags"];
|
||||
}
|
||||
|
||||
- (int)age
|
||||
@@ -142,6 +156,12 @@ var PersonUIDCounter = 0;
|
||||
return years;
|
||||
}
|
||||
|
||||
// The tags joined by commas, or the empty string when there are none.
|
||||
- (CPString)tagsText
|
||||
{
|
||||
return (tags || []).join(@", ");
|
||||
}
|
||||
|
||||
// The plain text of the notes, or the empty string when there are none.
|
||||
- (CPString)notesText
|
||||
{
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* ("age").
|
||||
*
|
||||
* Controls covered: CPTextField (+formatter), CPComboBox, CPPopUpButton
|
||||
* (selectedValue binding), CPDatePicker, CPCheckBox, CPScrollView, CPTextView (rich text,
|
||||
* (selectedValue binding), CPDatePicker, CPCheckBox, CPTokenField, CPScrollView, CPTextView (rich text,
|
||||
* attributedString binding), and CPBox in its custom
|
||||
* type (an undrawn section container) and its separator type, CPButton in its disclosure
|
||||
* bezel style, and CPButtonBar with a toggle button.
|
||||
@@ -38,6 +38,10 @@
|
||||
* the table's notes icon follows the typing. CPTextView has no editable binding, so the
|
||||
* controller sets its editable state, and the scroll view's border, when the mode changes.
|
||||
*
|
||||
* The Tags row holds a CPTokenField bound to the person's tags, an array of free-form strings.
|
||||
* Typing a comma or Return ends a token. As the controller is the token field's delegate, it
|
||||
* offers completions from the tags already in use.
|
||||
*
|
||||
* The pane shows a person only while exactly one row is selected. With no selection or with
|
||||
* several rows selected, a cover takes the place of the fields and asks for a single row. The
|
||||
* main split view controller binds "selectionCount" to the number of selected people.
|
||||
@@ -157,7 +161,7 @@ PersonDetailMinimumWidth = PersonDetailPreferredWidth - FieldWidth + ShortFie
|
||||
[self _bind:[self _textFieldInRow:row++ label:@"Last Name:" in:content] binding:CPValueBinding key:@"lastName" options:options];
|
||||
[self _bind:[self _textFieldInRow:row++ label:@"Email:" in:content] binding:CPValueBinding key:@"email" options:options];
|
||||
|
||||
content = [self _addSectionWithTitle:@"Details" rowCount:7];
|
||||
content = [self _addSectionWithTitle:@"Details" rowCount:8];
|
||||
row = 0;
|
||||
|
||||
[self _labelInRow:row title:@"Department:" in:content];
|
||||
@@ -210,6 +214,20 @@ PersonDetailMinimumWidth = PersonDetailPreferredWidth - FieldWidth + ShortFie
|
||||
[salary setAutoresizingMask:CPViewNotSizable];
|
||||
[self _bind:salary binding:CPValueBinding key:@"salary" options:options];
|
||||
|
||||
[self _labelInRow:row title:@"Tags:" in:content];
|
||||
var tags = [[CPTokenField alloc] initWithFrame:CGRectMakeZero()];
|
||||
[tags setDelegate:self];
|
||||
[self _place:tags row:row++ x:fieldX width:FieldWidth height:TextFieldHeight in:content];
|
||||
[tags setAutoresizingMask:CPViewWidthSizable];
|
||||
[self _bind:tags binding:CPValueBinding key:@"tags" options:nil];
|
||||
[tags bind:CPEditableBinding toObject:_library withKeyPath:@"editable" options:nil];
|
||||
|
||||
/*
|
||||
The token field takes its bezel from the mode with the text fields, so in view mode the
|
||||
tags show as plain tokens.
|
||||
*/
|
||||
[_textFields addObject:tags];
|
||||
|
||||
[self _labelInRow:row title:@"Record ID:" in:content];
|
||||
var uid = [self _valueLabelInRow:row++ in:content];
|
||||
[uid setSelectable:YES];
|
||||
@@ -507,6 +525,22 @@ PersonDetailMinimumWidth = PersonDetailPreferredWidth - FieldWidth + ShortFie
|
||||
[_documentView setFrameSize:CGSizeMake(CGRectGetWidth([_documentView frame]), y + Margin)];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Token field delegate
|
||||
|
||||
/*
|
||||
Offers the tags in use that start with aSubstring, leaving out those the person already has.
|
||||
*/
|
||||
- (CPArray)tokenField:(CPTokenField)aTokenField completionsForSubstring:(CPString)aSubstring indexOfToken:(CPInteger)anIndex indexOfSelectedItem:(CPInteger)anIndexOfSelectedItem
|
||||
{
|
||||
var prefix = [aSubstring lowercaseString],
|
||||
current = [aTokenField objectValue] || [];
|
||||
|
||||
return [_library allTags].filter(function(aTag)
|
||||
{
|
||||
return [[aTag lowercaseString] hasPrefix:prefix] && [current indexOfObject:aTag] === CPNotFound;
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Layout helpers
|
||||
|
||||
// Centres aView vertically on the midline of row aRow of a section.
|
||||
|
||||
@@ -284,8 +284,8 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
|
||||
if (![searchString length])
|
||||
_searchPredicate = nil;
|
||||
else
|
||||
_searchPredicate = [CPPredicate predicateWithFormat:@"(firstName CONTAINS[cd] %@) OR (lastName CONTAINS[cd] %@) OR (email CONTAINS[cd] %@) OR (city CONTAINS[cd] %@) OR (department CONTAINS[cd] %@) OR (notesText CONTAINS[cd] %@)",
|
||||
searchString, searchString, searchString, searchString, searchString, searchString];
|
||||
_searchPredicate = [CPPredicate predicateWithFormat:@"(firstName CONTAINS[cd] %@) OR (lastName CONTAINS[cd] %@) OR (email CONTAINS[cd] %@) OR (city CONTAINS[cd] %@) OR (department CONTAINS[cd] %@) OR (notesText CONTAINS[cd] %@) OR (tagsText CONTAINS[cd] %@)",
|
||||
searchString, searchString, searchString, searchString, searchString, searchString, searchString];
|
||||
|
||||
[self _refreshPeople];
|
||||
}
|
||||
|
||||
@@ -89,6 +89,12 @@ to build with a web component framework.
|
||||
"Please select a single row to edit or display."
|
||||
3. The contextual menu of the Notes field adds the selection commands and basic style commands.
|
||||
|
||||
Details has a Tags row: a CPTokenField bound to the person's tags, an array of free-form
|
||||
strings. A comma or Return ends a token, and the field offers completions from the tags in use,
|
||||
through its delegate. In view mode the tags show as plain tokens. Search also matches tags.
|
||||
The smart folder editor has an Any Tag row ("is", "contains", "begins with"), which builds an
|
||||
ANY predicate on the tags array, for example `ANY tags ==[cd] "Remote"`.
|
||||
|
||||
### People table
|
||||
|
||||
4. The Notes column is the first column by default.
|
||||
|
||||
Reference in new issue
Block a user