contacts/js/components/contactList/contactList_controller.js

251 lines
6.9 KiB
JavaScript
Raw Normal View History

angular.module('contactsApp')
.controller('contactlistCtrl', function($scope, $filter, $route, $routeParams, $timeout, AddressBookService, ContactService, SortByService, vCardPropertiesService, SearchService) {
2015-10-27 17:52:09 +00:00
var ctrl = this;
2015-12-08 11:31:43 +00:00
ctrl.routeParams = $routeParams;
2016-02-17 21:34:36 +00:00
ctrl.contactList = [];
2016-03-17 01:09:33 +00:00
ctrl.searchTerm = '';
ctrl.show = true;
ctrl.invalid = false;
ctrl.limitTo = 25;
2016-03-07 23:40:11 +00:00
ctrl.sortBy = SortByService.getSortBy();
2016-03-17 12:42:06 +00:00
ctrl.t = {
emptySearch : t('contacts', 'No search result for {query}', {query: ctrl.searchTerm})
};
ctrl.resetLimitTo = function () {
ctrl.limitTo = 25;
clearInterval(ctrl.intervalId);
ctrl.intervalId = setInterval(
function () {
if (!ctrl.loading && ctrl.contacts && ctrl.contacts.length > ctrl.limitTo) {
ctrl.limitTo += 25;
$scope.$apply();
}
}, 300);
};
2016-03-07 23:40:11 +00:00
$scope.query = function(contact) {
2016-03-14 23:58:51 +00:00
return contact.matches(SearchService.getSearchTerm());
2016-03-07 23:40:11 +00:00
};
SortByService.subscribe(function(newValue) {
ctrl.sortBy = newValue;
});
2016-03-14 23:58:51 +00:00
SearchService.registerObserverCallback(function(ev) {
2016-03-16 19:15:14 +00:00
if (ev.event === 'submitSearch') {
var uid = !_.isEmpty(ctrl.contactList) ? ctrl.contactList[0].uid() : undefined;
2016-03-17 17:07:39 +00:00
ctrl.setSelectedId(uid);
2016-03-16 19:15:14 +00:00
$scope.$apply();
}
if (ev.event === 'changeSearch') {
ctrl.resetLimitTo();
2016-03-17 01:09:33 +00:00
ctrl.searchTerm = ev.searchTerm;
2016-03-17 12:42:06 +00:00
ctrl.t.emptySearch = t('contacts',
'No search result for {query}',
{query: ctrl.searchTerm}
);
2016-03-16 19:15:14 +00:00
$scope.$apply();
}
2016-03-07 23:40:11 +00:00
});
ctrl.loading = true;
ContactService.registerObserverCallback(function(ev) {
$timeout(function() { $scope.$apply(function() {
if (ev.event === 'delete') {
if (ctrl.contactList.length === 1) {
$route.updateParams({
gid: $routeParams.gid,
uid: undefined
});
} else {
for (var i = 0, length = ctrl.contactList.length; i < length; i++) {
if (ctrl.contactList[i].uid() === ev.uid) {
$route.updateParams({
gid: $routeParams.gid,
uid: (ctrl.contactList[i+1]) ? ctrl.contactList[i+1].uid() : ctrl.contactList[i-1].uid()
});
break;
}
}
}
}
else if (ev.event === 'create') {
$route.updateParams({
gid: $routeParams.gid,
uid: ev.uid
});
}
else if (ev.event === 'importend') {
$route.updateParams({
gid: t('contacts', 'All contacts')
});
}
if(ev.contacts.length !== 0) {
ctrl.contacts = ev.contacts;
}
}); });
2016-02-11 14:30:52 +00:00
});
AddressBookService.registerObserverCallback(function(ev) {
$timeout(function() { $scope.$apply(function() {
if (ev.event === 'delete') {
// Get contacts
ctrl.loading = true;
ContactService.updateDeletedAddressbook(function() {
ctrl.loading = false;
});
}
}); });
});
// Get contacts
ContactService.getAll().then(function(contacts) {
if(contacts.length>0) {
$scope.$apply(function() {
ctrl.contacts = contacts;
});
} else {
ctrl.loading = false;
}
});
var getVisibleNames = function getVisibleNames() {
function isScrolledIntoView(el) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var bothAboveViewport = elemBottom < 0;
var bothBelowViewPort = elemTop > window.innerHeight;
var isVisible = !bothAboveViewport && !bothBelowViewPort;
return isVisible;
}
var elements = Array.prototype.slice.call(document.querySelectorAll('.contact__icon:not(.ng-hide)'));
var names = elements
.filter(isScrolledIntoView)
.map(function (el) {
var siblings = Array.prototype.slice.call(el.parentElement.children);
var nameElement = siblings.find(function (sibling) {
return sibling.getAttribute('class').indexOf('content-list-item-line-one') !== -1;
});
return nameElement.innerText;
});
return names;
};
var timeoutId = null;
document.querySelector('.app-content-list').addEventListener('scroll', function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(function () {
var names = getVisibleNames();
ContactService.getFullContacts(names);
}, 250);
});
// Wait for ctrl.contactList to be updated, load the contact requested in the URL if any, and
// load full details for the probably initially visible contacts.
// Then kill the watch.
var unbindListWatch = $scope.$watch('ctrl.contactList', function() {
if(ctrl.contactList && ctrl.contactList.length > 0) {
// Check if a specific uid is requested
if($routeParams.uid && $routeParams.gid) {
ctrl.contactList.forEach(function(contact) {
if(contact.uid() === $routeParams.uid) {
ctrl.setSelectedId($routeParams.uid);
ctrl.loading = false;
}
});
}
// No contact previously loaded, let's load the first of the list if not in mobile mode
if(ctrl.loading && $(window).width() > 768) {
ctrl.setSelectedId(ctrl.contactList[0].uid());
}
var firstNames = ctrl.contactList.slice(0, 20).map(function (c) { return c.displayName(); });
ContactService.getFullContacts(firstNames);
ctrl.loading = false;
unbindListWatch();
}
});
$scope.$watch('ctrl.routeParams.uid', function(newValue, oldValue) {
// Used for mobile view to clear the url
if(typeof oldValue != 'undefined' && typeof newValue == 'undefined' && $(window).width() <= 768) {
// no contact selected
ctrl.show = true;
return;
}
if(newValue === undefined) {
// we might have to wait until ng-repeat filled the contactList
if(ctrl.contactList && ctrl.contactList.length > 0) {
$route.updateParams({
gid: $routeParams.gid,
uid: ctrl.contactList[0].uid()
});
} else {
// watch for next contactList update
var unbindWatch = $scope.$watch('ctrl.contactList', function() {
if(ctrl.contactList && ctrl.contactList.length > 0) {
$route.updateParams({
gid: $routeParams.gid,
uid: ctrl.contactList[0].uid()
});
}
2016-02-29 12:00:28 +00:00
unbindWatch(); // unbind as we only want one update
});
}
} else {
// displaying contact details
ctrl.show = false;
}
});
$scope.$watch('ctrl.routeParams.gid', function() {
// we might have to wait until ng-repeat filled the contactList
ctrl.contactList = [];
ctrl.resetLimitTo();
// not in mobile mode
if($(window).width() > 768) {
// watch for next contactList update
var unbindWatch = $scope.$watch('ctrl.contactList', function() {
if(ctrl.contactList && ctrl.contactList.length > 0) {
$route.updateParams({
gid: $routeParams.gid,
uid: $routeParams.uid || ctrl.contactList[0].uid()
});
}
unbindWatch(); // unbind as we only want one update
});
}
});
// Watch if we have an invalid contact
$scope.$watch('ctrl.contactList[0].displayName()', function(displayName) {
ctrl.invalid = (displayName === '');
});
2016-02-18 21:07:10 +00:00
ctrl.hasContacts = function () {
if (!ctrl.contacts) {
return false;
}
return ctrl.contacts.length > 0;
};
2016-03-17 17:07:39 +00:00
ctrl.setSelectedId = function (contactId) {
$route.updateParams({
uid: contactId
});
};
ctrl.getSelectedId = function() {
return $routeParams.uid;
2016-02-18 16:46:06 +00:00
};
2016-03-16 19:37:40 +00:00
});