1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Backgroung load on import and async load on init. Closes #49

This commit is contained in:
Thomas Tanghus 2013-05-08 06:47:36 +02:00
parent d60356baf0
commit e7267209ef
6 changed files with 173 additions and 116 deletions

View File

@ -130,7 +130,7 @@ OC.Contacts = OC.Contacts || {};
*/
AddressBook.prototype.update = function(properties, cb) {
var self = this;
$.when(this.storage.updateAddressBook(this.getBackend(), self.getId(), {properties:properties}))
return $.when(this.storage.updateAddressBook(this.getBackend(), self.getId(), {properties:properties}))
.then(function(response) {
if(response.error) {
$(document).trigger('status.contact.error', {
@ -301,6 +301,10 @@ OC.Contacts = OC.Contacts || {};
console.log('Import done');
self.$importStatusText.text(t('contacts', 'Imported {imported} contacts. {failed} failed.',
{imported:response.data.imported, failed: response.data.failed}));
var addressBook = self.find({id:response.data.addressbookid, backend: response.data.backend});
$(document).trigger('status.addressbook.imported', {
addressbook: addressBook
});
} else {
done = true;
self.$importStatusText.text(response.message);
@ -362,6 +366,7 @@ OC.Contacts = OC.Contacts || {};
* @return AddressBook|null
*/
AddressBookList.prototype.find = function(info) {
console.log('AddressBookList.find', info);
var addressBook = null;
$.each(this.addressBooks, function(idx, book) {
if(book.getId() === info.id && book.getBackend() === info.backend) {
@ -472,8 +477,9 @@ OC.Contacts = OC.Contacts || {};
/**
* Load address books
*/
AddressBookList.prototype.loadAddressBooks = function(cb) {
AddressBookList.prototype.loadAddressBooks = function() {
var self = this;
var defer = $.Deferred();
$.when(this.storage.getAddressBooksForUser()).then(function(response) {
if(!response.error) {
var num = response.data.addressbooks.length;
@ -486,9 +492,9 @@ OC.Contacts = OC.Contacts || {};
} else {
self.$bookList.find('a.action.share').css('display', 'none');
}
cb({error:false, addressbooks: self.addressBooks});
defer.resolve(self.addressBooks);
} else {
cb(response);
defer.reject(response);
$(document).trigger('status.contact.error', {
message: response.message
});
@ -498,10 +504,12 @@ OC.Contacts = OC.Contacts || {};
.fail(function(jqxhr, textStatus, error) {
var err = textStatus + ', ' + error;
console.warn( "Request Failed: " + err);
$(document).trigger('status.contact.error', {
defer.reject({
error: true,
message: t('contacts', 'Failed loading address books: {error}', {error:err})
});
});
return defer.promise();
};
OC.Contacts.AddressBookList = AddressBookList;

115
js/app.js
View File

@ -179,33 +179,54 @@ OC.Contacts = OC.Contacts || {
this.$groupList,
this.$groupListItemTemplate
);
this.addressBooks.loadAddressBooks(function(response) {
if(!response.error) {
var num = response.addressbooks.length;
$.each(response.addressbooks, function(idx, book) {
self.contacts.loadContacts(book.getBackend(), book.getId(), function(response) {
if(response.error) {
console.log(response.message);
$(document).trigger('status.contact.error', {
message: response.message
});
}
num -= 1;
if(num === 0) {
self.contacts.doSort();
$(document).trigger('status.contacts.loaded', {
status: true,
numcontacts: self.contacts.length
});
}
});
self.groups.loadGroups(function() {
self.loading(0, self.$navigation, false);
});
$.when(this.addressBooks.loadAddressBooks()).then(function(addressBooks) {
var num = addressBooks.length;
var deferreds = $(addressBooks).map(function(i, elem) {
return self.contacts.loadContacts(this.getBackend(), this.getId());
});
// This little beauty is from http://stackoverflow.com/a/6162959/373007 ;)
$.when.apply(null, deferreds.get()).then(function(response) {
self.contacts.doSort();
$(document).trigger('status.contacts.loaded', {
status: true,
numcontacts: self.contacts.length
});
} else {
console.log(response.message);
$(document).trigger('status.contact.error', {
message: response.message
});
}
self.loading(self.$rightContent, false);
// TODO: Move this to event handler
self.groups.selectGroup({id:contacts_lastgroup});
var id = $.QueryString['id']; // Keep for backwards compatible links.
self.currentid = parseInt(id);
if(!self.currentid) {
self.currentid = parseInt(window.location.hash.substr(1));
}
console.log('Groups loaded, currentid', self.currentid);
if(self.currentid) {
self.openContact(self.currentid);
}
if(!contacts_properties_indexed) {
// Wait a couple of mins then check if contacts are indexed.
setTimeout(function() {
$.when($.post(OC.Router.generate('contacts_index_properties')))
.then(function(response) {
if(!response.isIndexed) {
OC.notify({message:t('contacts', 'Indexing contacts'), timeout:20});
}
});
}, 10000);
} else {
console.log('contacts are indexed.');
}
}).fail(function(response) {
console.warn(response);
});
}).fail(function(response) {
console.log(response.message);
$(document).trigger('status.contact.error', {
message: response.message
});
});
OCCategories.changed = this.groups.categoriesChanged;
OCCategories.app = 'contacts';
@ -368,38 +389,13 @@ OC.Contacts = OC.Contacts || {
}*/
});
$(document).bind('status.contacts.loaded', function(e, result) {
console.log('status.contacts.loaded', result);
if(result.status !== true) {
alert('Error loading contacts!');
} else {
self.numcontacts = result.numcontacts;
self.loading(self.$rightContent, false);
self.groups.loadGroups(self.numcontacts, function() {
self.loading(self.$navigation, false);
var id = $.QueryString['id']; // Keep for backwards compatible links.
self.currentid = parseInt(id);
if(!self.currentid) {
self.currentid = parseInt(window.location.hash.substr(1));
}
console.log('Groups loaded, currentid', self.currentid);
if(self.currentid) {
self.openContact(self.currentid);
}
$(document).bind('status.contacts.loaded', function(e, response) {
console.log('status.contacts.loaded', response);
if(response.error) {
$(document).trigger('status.contact.error', {
message: response.message
});
if(!contacts_properties_indexed) {
// Wait a couple of mins then check if contacts are indexed.
setTimeout(function() {
$.when($.post(OC.Router.generate('contacts_index_properties')))
.then(function(response) {
if(!response.isIndexed) {
OC.notify({message:t('contacts', 'Indexing contacts'), timeout:20});
}
});
}, 10000);
} else {
console.log('contacts are indexed.');
}
console.log('Error loading contacts!');
}
});
@ -553,6 +549,11 @@ OC.Contacts = OC.Contacts || {
self.contacts.showFromAddressbook(result.id, result.activate);
});
$(document).bind('request.groups.reload', function(e, result) {
console.log('request.groups.reload', result);
self.groups.loadGroups();
});
$(document).bind('status.contact.removedfromgroup', function(e, result) {
console.log('status.contact.removedfromgroup', result);
if(self.currentgroup == result.groupid) {

View File

@ -27,6 +27,7 @@ OCP\JSON::checkAppEnabled('contacts');
$user = OCP\User::getUser();
echo 'var contacts_groups_sortorder=[' . OCP\Config::getUserValue($user, 'contacts', 'groupsort', '') . '],';
echo 'contacts_lastgroup=\'' . OCP\Config::getUserValue($user, 'contacts', 'lastgroup', 'all') . '\',';
echo 'contacts_properties_indexed = '
. (OCP\Config::getUserValue($user, 'contacts', 'contacts_properties_indexed', 'no') === 'no'
? 'false' : 'true') . ',';

View File

@ -1721,17 +1721,18 @@ OC.Contacts = OC.Contacts || {};
});
$(document).bind('status.addressbook.removed', function(e, data) {
var addressBook = data.addressbook;
$.each(self.contacts, function(idx, contact) {
if(contact.getBackend() === addressBook.getBackend()
&& contact.getParent() === addressBook.getId()) {
console.log('Removing', contact);
delete self.contacts[contact.getId()];
//var c = self.contacts.splice(self.contacts.indexOf(contact.getId()), 1);
//console.log('Removed', c);
contact.detach();
contact = null;
}
});
self.purgeFromAddressbook(addressBook);
$(document).trigger('request.groups.reload');
});
$(document).bind('status.addressbook.imported', function(e, data) {
console.log('status.addressbook.imported', data);
var addressBook = data.addressbook;
self.purgeFromAddressbook(addressBook);
$.when(self.loadContacts(addressBook.getBackend(), addressBook.getId()))
.then(function() {
self.doSort();
$(document).trigger('request.groups.reload');
});
});
};
@ -1743,6 +1744,30 @@ OC.Contacts = OC.Contacts || {};
return Object.keys(this.contacts.contacts).length
};
/**
* Remove contacts from the internal list and the DOM
*
* @param AddressBook addressBook
*/
ContactList.prototype.purgeFromAddressbook = function(addressBook) {
var self = this;
$.each(this.contacts, function(idx, contact) {
if(contact.getBackend() === addressBook.getBackend()
&& contact.getParent() === addressBook.getId()) {
//console.log('Removing', contact);
delete self.contacts[contact.getId()];
//var c = self.contacts.splice(self.contacts.indexOf(contact.getId()), 1);
//console.log('Removed', c);
contact.detach();
contact = null;
self.length -= 1;
}
});
$(document).trigger('status.contacts.count', {
count: self.length
});
}
/**
* Show/hide contacts belonging to an addressbook.
* @param int aid. Addressbook id.
@ -2110,11 +2135,11 @@ OC.Contacts = OC.Contacts || {};
* Load contacts
* @param string backend Name of the backend ('local', 'ldap' etc.)
* @param string addressBookId
* @param function cb Optional call back function.
*/
ContactList.prototype.loadContacts = function(backend, addressBookId, cb) {
ContactList.prototype.loadContacts = function(backend, addressBookId) {
var self = this;
$.when(this.storage.getContacts(backend, addressBookId)).then(function(response) {
return $.when(this.storage.getContacts(backend, addressBookId)).then(function(response) {
var defer = this;
//console.log('ContactList.loadContacts', response);
if(!response.error) {
var items = [];
@ -2143,21 +2168,17 @@ OC.Contacts = OC.Contacts || {};
if(items.length > 0) {
self.$contactList.append(items);
}
cb({error:false});
$(document).trigger('status.contacts.count', {
count: self.length
});
} else {
$(document).trigger('status.contact.error', {
message: response.message
});
cb({
error:true,
message: response.message
});
defer.reject(response);
}
})
.fail(function(jqxhr, textStatus, error) {
var err = textStatus + ', ' + error;
console.warn( "Request Failed: " + err);
cb({error: true, message: err});
this.reject({error: true, message: err});
});
};

View File

@ -61,6 +61,11 @@ OC.Contacts = OC.Contacts || {};
}
});
$(document).bind('status.contacts.count', function(e, data) {
console.log('Num contacts:', data.count);
self.findById('all').find('.numcontacts').text(data.count);
});
this.$groupListItemTemplate = listItemTmpl;
this.categories = [];
};
@ -74,6 +79,14 @@ OC.Contacts = OC.Contacts || {};
* the group hasn't been saved/created yet.
*/
GroupList.prototype.selectGroup = function(params) {
var self = this;
if(!this.loaded) {
console.log('Not loaded');
setTimeout(function() {
self.selectGroup(params);
}, 100);
return;
}
var id, $elem;
if(typeof params.id !== 'undefined') {
id = params.id;
@ -83,7 +96,7 @@ OC.Contacts = OC.Contacts || {};
$elem = params.element;
}
if(!$elem) {
self.selectGroup('all');
self.selectGroup({id:'all'});
return;
}
console.log('selectGroup', id, $elem);
@ -100,6 +113,10 @@ OC.Contacts = OC.Contacts || {};
});
};
GroupList.prototype.triggerLastGroup = function() {
this.selectGroup({id:this.lastgroup});
}
/**
* Get the group name by id.
*
@ -623,8 +640,10 @@ OC.Contacts = OC.Contacts || {};
var $groupList = this.$groupList;
var tmpl = this.$groupListItemTemplate;
tmpl.octemplate({id: 'all', type: 'all', num: numcontacts, name: t('contacts', 'All')}).appendTo($groupList);
$.when(this.storage.getGroupsForUser()).then(function(response) {
if(!this.findById('all').length) {
tmpl.octemplate({id: 'all', type: 'all', num: numcontacts, name: t('contacts', 'All')}).appendTo($groupList);
}
return $.when(this.storage.getGroupsForUser()).then(function(response) {
if (response && !response.error) {
self.lastgroup = response.data.lastgroup;
self.sortorder = contacts_groups_sortorder;
@ -632,7 +651,8 @@ OC.Contacts = OC.Contacts || {};
// Favorites
// Map to strings easier lookup an contacts list.
var contacts = $.map(response.data.favorites, function(c) {return String(c);});
var $elem = tmpl.octemplate({
var $elem = self.findById('fav');
$elem = $elem.length ? $elem : tmpl.octemplate({
id: 'fav',
type: 'fav',
num: contacts.length,
@ -640,7 +660,9 @@ OC.Contacts = OC.Contacts || {};
}).appendTo($groupList);
$elem.data('obj', self);
$elem.data('rawname', t('contacts', 'Favorites'));
$elem.data('contacts', contacts).find('.numcontacts').before('<span class="starred action" />');
if(!$elem.find('.starred').length) {
$elem.data('contacts', contacts).find('.numcontacts').before('<span class="starred action" />');
}
$elem.droppable({
drop: self.contactDropped,
over: function( event, ui ) {
@ -657,27 +679,32 @@ OC.Contacts = OC.Contacts || {};
// Normal groups
$.each(response.data.categories, function(c, category) {
var contacts = $.map(category.contacts, function(c) {return String(c);});
var $elem = (tmpl).octemplate({
id: category.id,
type: 'category',
num: contacts.length,
name: category.name
});
self.categories.push({id: category.id, name: category.name});
$elem.data('obj', self);
var $elem = self.findById(category.id);
if($elem.length) {
$elem.find('.numcontacts').text(contacts.length);
} else {
$elem = $elem.length ? $elem : (tmpl).octemplate({
id: category.id,
type: 'category',
num: contacts.length,
name: category.name
});
self.categories.push({id: category.id, name: category.name});
$elem.data('obj', self);
$elem.data('rawname', category.name);
$elem.data('id', category.id);
$elem.droppable({
drop: self.contactDropped,
over: function( event, ui ) {
console.log('over group', ui.draggable);
},
activeClass: 'ui-state-active',
hoverClass: 'ui-state-hover',
scope: 'contacts'
});
$elem.appendTo($groupList);
}
$elem.data('contacts', contacts);
$elem.data('rawname', category.name);
$elem.data('id', category.id);
$elem.droppable({
drop: self.contactDropped,
over: function( event, ui ) {
console.log('over group', ui.draggable);
},
activeClass: 'ui-state-active',
hoverClass: 'ui-state-hover',
scope: 'contacts'
});
$elem.appendTo($groupList);
});
var elems = $groupList.find('li[data-type="category"]').get();
@ -694,7 +721,8 @@ OC.Contacts = OC.Contacts || {};
$.each(response.data.shared, function(c, shared) {
var sharedindicator = '<img class="shared svg" src="' + OC.imagePath('core', 'actions/shared') + '"'
+ 'title="' + t('contacts', 'Shared by {owner}', {owner:shared.owner}) + '" />';
var $elem = (tmpl).octemplate({
var $elem = self.findById(shared.id);
$elem = $elem.length ? $elem : (tmpl).octemplate({
id: shared.id,
type: 'shared',
num: response.data.shared.length,
@ -722,11 +750,7 @@ OC.Contacts = OC.Contacts || {};
});
var $elem = self.findById(self.lastgroup);
$elem.addClass('active');
$(document).trigger('status.group.selected', {
id: self.lastgroup,
type: $elem.data('type'),
contacts: $elem.data('contacts')
});
self.loaded = true;
} // TODO: else
if(typeof cb === 'function') {
cb();

View File

@ -205,6 +205,8 @@ class ImportController extends BaseController {
sleep(3); // Give client side a chance to read the progress.
$response->setParams(
array(
'backend' => $params['backend'],
'addressbookid' => $params['addressbookid'],
'imported' => $imported,
'failed' => $failed,
)