From a70bc7d9532b52a0c8ac596332e60f40e1c244b2 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sun, 14 Oct 2012 18:27:25 +0100 Subject: [PATCH] "Creates Sort Descriptors" `CPTableColumn` value binding option. With this option an array controller driven table view can be sorted by clicking the table column headers without manually setting up sort descriptor prototypes. If `CPCreatesSortDescriptorBindingOption` is enabled on a table column value binding, sort descriptors will be created automatically. The default value for this option is YES. --- AppKit/CPTableColumn.j | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/AppKit/CPTableColumn.j b/AppKit/CPTableColumn.j index b0349993d..6a1765cad 100644 --- a/AppKit/CPTableColumn.j +++ b/AppKit/CPTableColumn.j @@ -494,7 +494,13 @@ CPTableColumnUserResizingMask = 1 << 1; */ - (CPSortDescriptor)sortDescriptorPrototype { - return _sortDescriptorPrototype; + if (_sortDescriptorPrototype) + return _sortDescriptorPrototype; + + var binderClass = [[self class] _binderClassForBinding:CPValueBinding], + binding = [binderClass getBinding:CPValueBinding forObject:self]; + + return [binding _defaultSortDescriptorPrototype]; } /*! @@ -570,6 +576,30 @@ CPTableColumnUserResizingMask = 1 << 1; [tableView reloadDataForRowIndexes:rowIndexes columnIndexes:columnIndexes]; } +- (CPSortDescriptor)_defaultSortDescriptorPrototype +{ + if (![self createsSortDescriptor]) + return nil; + + var keyPath = [_info objectForKey:CPObservedKeyPathKey], + dotIndex = keyPath.lastIndexOf("."); + + if (dotIndex === CPNotFound) + return nil; + + var firstPart = keyPath.substring(0, dotIndex), + key = keyPath.substring(dotIndex + 1); + + return [CPSortDescriptor sortDescriptorWithKey:key ascending:YES]; +} + +- (BOOL)createsSortDescriptor +{ + var options = [_info objectForKey:CPOptionsKey], + optionValue = [options objectForKey:CPCreatesSortDescriptorBindingOption]; + return optionValue === nil ? YES : [optionValue boolValue]; +} + @end @implementation CPTableColumn (Bindings)