Skip to content

Instantly share code, notes, and snippets.

@sr3d
Created October 2, 2010 06:59
Show Gist options
  • Save sr3d/607394 to your computer and use it in GitHub Desktop.
Save sr3d/607394 to your computer and use it in GitHub Desktop.
Ti.include('../../lib/active_record.js');
Ti.include('../../lib/utils.js');
Ti.include('../../lib/models.js');
win.title = 'Guests';
var
eventId = DB.get('event_id'),
guests = [],
guestByIds = [],
contacts = [],
contactByIds = [],
filterStatuses = ['All', 'Pending', 'Accepted', 'Rejected'],
currentFilter = filterStatuses[0],
emptyRows = [],
search = Ti.UI.createSearchBar({ showCancel:false }),
tableView = Ti.UI.createTableView({ search:search, filterAttribute: 'fullName' });
win.addEventListener('focus', function() {
buildTableView();
showToolbar();
});
function buildTableView() {
loadGuests();
if( guests.length == 0 ) {
buildEmptyRows();
return;
};
var rows = [], tableIndex = [], currentHeader = '';
for( var i = 0; i < guests.length; i++ ) {
var contact = contactByIds[ guests[i].contact_id ];
var row = Ti.UI.createTableViewRow({title: guests[i].fullName, fullName: guests[i].fullName, guest: guests[i], contact: contact, hasChild: true, className: 'guestRow' });
var firstLetter = guests[i].fullName.substr(0,1).toUpperCase();
if( currentHeader != firstLetter ) {
row.header = currentHeader = firstLetter;
tableIndex.push( { title: firstLetter, index: i });
};
rows.push(row);
};
tableView.data = rows;
tableView.index = tableIndex;
};
function loadGuests() {
guests = Model.Guest.findAllMainGuestsForEvent(eventId, currentFilter); //({ all: true, where:{ event_id : eventId } }) || [];
guestByIds = [];
contacts = Model.Contact.findAllContactsForMainGuestsForEvent(eventId, currentFilter);
contactByIds = [];
/* build the lookup */
for(var i = 0; i < contacts.length; i++ ) { contactByIds[ contacts[i].id ] = contacts[i]; };
for(var i = 0; i < guests.length; i++ ) {
guestByIds[ guests[i].id ] = guests[i];
guests[i].fullName = contactByIds[ guests[i].contact_id ].fullName();
};
guests.sort(function(a,b){ return a.fullName > b.fullName ? 1 : ( a.fullName < b.fullName ? -1 : 0 ) });
};
function buildEmptyRows() {
if( emptyRows.length == 0 ) {
for( var i = 0; i < 3; i++ ) {
emptyRows.push(Ti.UI.createTableViewRow({selectionStyle: NO_SELECTION}));
};
emptyRows.push( Ti.UI.createTableViewRow({title: ' No Guests Found', textAlign: 'center', color: '#DCDCDC', selectionStyle: NO_SELECTION}));
};
tableView.data = emptyRows;
};
win.add(tableView);
/* TOOLBAR */
var toolbar;
function showToolbar() {
/* tool bar */
if( !toolbar ) {
var filter = Titanium.UI.createTabbedBar({ labels: filterStatuses, index: 0} );
// var label = Ti.UI.createLabel({text: 'RSVP:', width:'auto'} );
var flexSpace = Ti.UI.createButton({ systemButton:Ti.UI.iPhone.SystemButton.FLEXIBLE_SPACE });
filter.addEventListener('click', function(e) {
currentFilter = filterStatuses[ e.index ];
buildTableView();
});
toolbar = ([flexSpace,filter,flexSpace]);
};
win.setToolbar(toolbar);
};
function hideToolbar() {
win.setToolbar([]);
};
var addBtn = Ti.UI.createButton({ systemButton:Titanium.UI.iPhone.SystemButton.ADD });
addBtn.addEventListener('click', function() {
var newWin = Ti.UI.createWindow( { url: 'new.js', title: "New Guest" } );
tab.open(newWin,{animated:true});
});
win.rightNavButton = addBtn;
/* PULL TO REFRESH */
tableView.addEventListener('beginReloading', function(e) {
Model.Guest.refreshLocal( {
async: false,
onData: function() {
buildTableView();
}
} );
tableView.fireEvent('endReloading'); // do this to close the thing
});
Ti.include('../shared/_pull_to_refresh.js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment