Skip to content

Instantly share code, notes, and snippets.

@fmquaglia
Created March 30, 2016 15:34
Show Gist options
  • Save fmquaglia/25a789ad351bca00a714ca9b0bf70b73 to your computer and use it in GitHub Desktop.
Save fmquaglia/25a789ad351bca00a714ca9b0bf70b73 to your computer and use it in GitHub Desktop.
'use strict';
angular.module('circleBackApp')
.controller('ContactsCtrl',
function($scope,
$routeParams,
$route,
$rootScope,
$location,
$timeout,
$filter,
$log,
$modal,
$q,
CBCore,
AuthService,
Contacts,
Groups,
Mode,
Selection,
Sorting,
Status
) {
Mode.set();
$scope.$watch(
function() {return Mode.isESC()},
function(isESC) {
if (isESC) {
$location.path(Mode.getDefaultUrl());
}
}
);
$scope.Contacts = Contacts;
$scope.Selection = Selection;
$scope.Sorting = Sorting;
$scope.Status = Status;
$scope.page_type = $route.current.page_type;
$scope.view_trash = $route.current.view_trash;
$scope.view_group = $route.current.view_group;
// Start with a blank slate
$scope.contacts = [];
Status.search = '';
$scope.hasActiveSearch = false;
Selection.clear();
if(!AuthService.isLoggedIn()) {
// TODO:: this is a temporary action to clear the cache and force the login screen. This should really be handled by a authentication interceptor.
AuthService.logout();
}
// What Contacts index are we going to use as our source?
var contacts;
if (angular.isString($routeParams.groupId)) {
contacts = {};
// Contacts by group, make sure groups are loaded.
Groups.promise.then(
function() {
// Watch for reference changes in Contacts.byGroupId
$scope.$watch(
function() {
return Contacts.byGroupId[$routeParams.groupId]
},
function(newContacts) {
if (angular.isObject(newContacts)) {
contacts = newContacts;
$scope.currentGroup = Groups.getById($routeParams.groupId);
$scope.listTitle = $scope.currentGroup.name;
}
}
)
},
function() {
// Uh oh, no groups!
$log.log("Failed to load groups, redirecting to all contacts.");
$location.path('/contacts');
});
} else {
$scope.currentGroup = null;
if ($route.current.view_trash) {
contacts = Contacts.trashById;
$scope.listTitle = 'Deleted Contacts';
} else {
contacts = Contacts.byId;
$scope.listTitle = 'All Contacts';
}
}
// Setup the watch
$scope.$watch(
function() {
return {
contactIds: _.keys(contacts),
search: Status.search,
sorting: Sorting.current
};
},
function(watch) {
$scope.contactIds = watch.contactIds;
$scope.contacts = Contacts.getFilteredContactList(
watch.contactIds,
Status.search,
Sorting.current
);
$scope.contacts = $filter('cbSort')($scope.contacts);
$scope.$broadcast('ContactsCtrl.refreshSortedContactList');
},
true
);
// Update contact list on contact update
$scope.$on('Contacts.updateContact', function() {
$scope.contacts = $filter('cbSort')($scope.contacts);
});
$scope.toggleSelectAll = function() {
if ($scope.isAllSelected()) {
Selection.clear();
} else {
Selection.set($scope.contactIds);
}
};
$scope.isAllSelected = function(){
var ret = false;
if (angular.isArray($scope.contactIds)) {
ret = angular.equals(Selection.get().sort(), $scope.contactIds.sort());
}
return ret;
};
$scope.editGroup = function(group) {
$modal.open({
backdrop: 'static',
templateUrl: 'views/groups/edit.html',
controller: 'EditGroupCtrl',
resolve: {
group: function() {
return group;
}
},
windowClass:'group-modal'
});
};
$scope.$on('$viewContentLoad', $scope.clearSearch);
$scope.$on('Groups.update', function(event, newGroup, oldGroup) {
if (angular.isString(newGroup.name) &&
angular.isString($routeParams.groupId) &&
$routeParams.groupId === newGroup.groupId) {
$scope.currentGroup = Groups.getById($routeParams.groupId);
$scope.listTitle = newGroup.name;
}
});
$scope.newContact = function() {
$scope.$broadcast('ContactsCtrl.newContact');
$scope.$broadcast('autofocus', angular.element('#new_contact input.first_name'));
};
$scope.clearSearch = function() {
$log.log('cleared!');
Status.search = '';
};
function getNoResultsMessage() {
var noResultsMessage = 'Oops! You haven\'t added any contacts yet. Click the "Add contacts to your CircleBack" button to start adding new contacts.';
if ($scope.view_group) {
noResultsMessage = 'This group has no contacts yet. Go to "All Contacts" and add some contacts to it.';
} else if ($scope.view_trash) {
noResultsMessage = 'The Trash is empty.'
}
return noResultsMessage;
}
$scope.noResultsMessage = getNoResultsMessage();
$scope.$watch(
function() {
return Status.search;
}, function(newValue, oldValue) {
if (newValue.length > 0) {
Status.safeApply(function() {
$scope.hasActiveSearch = true;
$scope.noResultsMessage = 'Sorry nobody met your search criteria. ' +
'Please try again.';
});
} else {
Status.safeApply(function() {
$scope.hasActiveSearch = false;
$scope.noResultsMessage = getNoResultsMessage();
});
}
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment