Skip to content

Instantly share code, notes, and snippets.

@primalmotion
Forked from jlebrech/gist:769588
Created January 7, 2011 15:31
Show Gist options
  • Save primalmotion/769592 to your computer and use it in GitHub Desktop.
Save primalmotion/769592 to your computer and use it in GitHub Desktop.
@import <Foundation/CPObject.j>
theSupaNotificationName = @"theSupaNotificationName"
@implementation ProjectsView : CPView
{
id _delegate @accessors(property=delegate);
CPScrollView scrollView;
CPTableView projectsTable;
CPArray projects;
}
- (id)initWithFrame:(CPRect)frame
{
self = [super initWithFrame:frame];
if (self){
var projects = [CPArray alloc];
// create the CPTableView
projectsTable = [[CPTableView alloc] initWithFrame:[self bounds]];
[projectsTable setAutoresizingMask:CPViewHeightSizable ];
scrollView = [[CPScrollView alloc] initWithFrame:[self bounds]];
[scrollView setAutoresizingMask: CPViewWidthSizable | CPViewHeightSizable];
[scrollView setAutohidesScrollers:YES];
[scrollView setDocumentView:projectsTable];
[projectsTable setUsesAlternatingRowBackgroundColors:YES];
//[projectsTable setAlternatingRowBackgroundColors:[CPArray arrayWithObjects:[CPColor whiteColor],[CPColor redColor]]];
//var headerColor = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"button-bezel-center.png"]]];
//[[projectsTable cornerView] setBackgroundColor:headerColor];
// add the first column
var column = [[CPTableColumn alloc] initWithIdentifier:@"Name"];
[[column headerView] setStringValue:"Project Name"];
//[[column headerView] setBackgroundColor:headerColor];
[column setWidth:200.0];
[projectsTable addTableColumn:column];
/*
// commented out for now as requires extra httprequests
var column2 = [[CPTableColumn alloc] initWithIdentifier:@"Complete"];
[[column2 headerView] setStringValue:"Complete"];
//[[column2 headerView] setBackgroundColor:headerColor];
[column2 setWidth:50.0];
[projectsTable addTableColumn:column2];
var column3 = [[CPTableColumn alloc] initWithIdentifier:@"Deadline"];
[[column3 headerView] setStringValue:"Deadline"];
//[[column3 headerView] setBackgroundColor:headerColor];
[column3 setWidth:80.0];
[projectsTable addTableColumn:column3];
*/
[self getProjects];
[projectsTable setDelegate:self];
[projectsTable setDataSource:self];
[self addSubview:scrollView];
}
return self;
}
- (void)getProjects
{
CPLog.trace("Getting Projects");
var request = [CPURLRequest requestWithURL:"/listprojects"] ;
var projectConnection = [CPURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data
{
projects = JSON.parse(data);
[projectsTable reloadData];
}
-(void)connection:(CPURLConnection)connection didReceiveResponse:(CPHTTPURLResponse)response
{
//console.log(response);
}
- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error
{
//This method is called if the request fails for any reason.
}
- (id)tableView:(CPTableView)tableView objectValueForTableColumn:(CPTableColumn)tableColumn row:(int)row
{
//console.log(projects[row]["due_date"]);
switch ([tableColumn identifier]){
case @"Name":
return projects[row]["name"];
case @"Complete":
return (projects[row]["complete"]==1)?"Completed":"";
case @"Deadline":
return @"today"; //projects[row]["due_date"]["_content"];
default:
return @"foo";
}
}
// CPTableView datasource methods
- (int)numberOfRowsInTableView:(CPTableView)tableView
{
return projects.length;
}
- (void)tableViewSelectionDidChange:(CPTableView)aTableView
{
row = [projectsTable selectedRow];
CPLog.trace(projects[row]['id']);
//if ([_delegate respondsToSelector:@selector(doWhateverWithThisId:)])
// [_delegate doWhateverWithThisId:projects[row]['id']];
[[CPNotificationCenter defaultCenter] postNotificationName:theSupaNotificationName object:self userInfo:projects[row]['id']];
}
// then in wherever you need to be notified you do
//[CPNotificationCenter defaultCenter] addObserver:self selector:selector(didRecieveSupaNotification:) name:theSupaNotificationName object:nil]
// and implement
//- (void)didRecieveSupaNotification:(CPNotification)aNotification
//{
//}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment