(feat) save/restore sorting state for address books

The initial loading doesn't work yet. Will work on this later.
This commit is contained in:
Ludovic Marcotte
2015-08-14 15:22:59 -04:00
parent ffa4079fa8
commit 93fa74d279
3 changed files with 91 additions and 83 deletions

View File

@@ -280,9 +280,9 @@
</md-menu-item>
<md-menu-divider> <!-- divider --></md-menu-divider>
<md-menu-item >
<md-button ng-click="addressbook.sort()">
<md-button ng-click="addressbook.sort(addressbook.selectedFolder.constructor.$query.sort)">
<md-checkbox
ng-model="addressbook.selectedFolder.$query.asc"
ng-model="addressbook.selectedFolder.constructor.$query.asc"
ng-true-value="0"
ng-false-value="1"><var:string label:value="Descending Order"/></md-checkbox>
</md-button>
@@ -381,26 +381,27 @@
ng-class="{'sg-active': currentCard.id == addressbook.selectedFolder.selectedCard}"
ng-click="addressbook.selectCard(currentCard)"
ui-sref="app.addressbook.card.view({addressbookId: addressbook.selectedFolder.id, cardId: currentCard.id})">
<!-- ui-sref-active="sg-active" -->
<div class="sg-selected-avatar" ng-show="currentCard.selected"
ng-click="currentCard.selected = !currentCard.selected">
<!-- selected avatar -->
</div>
<sg-avatar-image class="md-tile-left"
ng-show="addressbook.notSelectedComponent(currentCard, 'vcard')"
ng-click="currentCard.selected = !currentCard.selected"
sg-email="currentCard.$preferredEmail(addressbook.selectedFolder.$query)"
size="48">
<!-- contact avatar -->
</sg-avatar-image>
<div class="sg-list-avatar"
ng-show="addressbook.notSelectedComponent(currentCard, 'vlist')"
ng-click="currentCard.selected = !currentCard.selected">
<!-- list avatar -->
<div>
<div class="sg-selected-avatar" ng-show="currentCard.selected"
ng-click="currentCard.selected = !currentCard.selected">
<!-- selected avatar -->
</div>
<sg-avatar-image class="md-tile-left"
ng-show="addressbook.notSelectedComponent(currentCard, 'vcard')"
ng-click="currentCard.selected = !currentCard.selected"
sg-email="currentCard.$preferredEmail(addressbook.selectedFolder.constructor.$query)"
size="48">
<!-- contact avatar -->
</sg-avatar-image>
<div class="sg-list-avatar"
ng-show="addressbook.notSelectedComponent(currentCard, 'vlist')"
ng-click="currentCard.selected = !currentCard.selected">
<!-- list avatar -->
</div>
</div>
<div class="sg-tile-content">
<div class="sg-md-subhead-multi" ng-bind-html="currentCard.$fullname()"><!-- cn --></div>
<div class="sg-md-body-multi">{{currentCard.$preferredEmail(addressbook.selectedFolder.$query)}}</div>
<div class="sg-md-body-multi">{{currentCard.$preferredEmail(addressbook.selectedFolder.constructor.$query)}}</div>
</div>
</md-list-item>
</div>

View File

@@ -41,11 +41,18 @@
$Card: Card,
$$Acl: Acl,
$Preferences: Preferences,
$query: {search: 'name_or_address', value: '', sort: 'c_cn', asc: 1},
activeUser: Settings.activeUser(),
selectedFolder: null,
$refreshTimeout: null
});
// Initialize sort parameters from user's settings
Preferences.ready().then(function() {
if (Preferences.settings.Contact.SortingState) {
AddressBook.$query.sort = Preferences.settings.Contact.SortingState[0];
AddressBook.$query.asc = parseInt(Preferences.settings.Contact.SortingState[1]);
}
});
return AddressBook; // return constructor
}];
@@ -174,8 +181,7 @@
* @returns an AddressBook object instance
*/
AddressBook.$find = function(addressbookId) {
var futureAddressBookData = AddressBook.$$resource.fetch(addressbookId, 'view');
var futureAddressBookData = AddressBook.$$resource.fetch(addressbookId, 'view', AddressBook.$query);
return new AddressBook(futureAddressBookData);
};
@@ -213,7 +219,6 @@
// Add 'isOwned' and 'isSubscription' attributes based on active user (TODO: add it server-side?)
this.isOwned = AddressBook.activeUser.isSuperUser || this.owner == AddressBook.activeUser.login;
this.isSubscription = !this.isRemote && this.owner != AddressBook.activeUser.login;
this.$query = {search: 'name_or_address', value: '', sort: 'c_cn', asc: 'true'};
};
/**
@@ -323,72 +328,74 @@
AddressBook.prototype.$filter = function(search, options, excludedCards) {
var _this = this;
if (options) {
angular.extend(this.$query, options);
return AddressBook.$Preferences.ready().then(function() {
if (options) {
angular.extend(AddressBook.$query, options);
if (options.dry) {
if (!search) {
// No query specified
this.$cards = [];
return AddressBook.$q.when(this.$cards);
}
else if (this.$query.value == search) {
// Query hasn't changed
return AddressBook.$q.when(this.$cards);
if (options.dry) {
if (!search) {
// No query specified
_this.$cards = [];
return AddressBook.$q.when(_this.$cards);
}
else if (AddressBook.$query.value == search) {
// Query hasn't changed
return AddressBook.$q.when(_this.$cards);
}
}
}
}
this.$query.value = search;
AddressBook.$query.value = search;
return this.$id().then(function(addressbookId) {
return AddressBook.$$resource.fetch(addressbookId, 'view', _this.$query);
}).then(function(response) {
var results, cards, card, index,
compareIds = function(data) {
return this.id == data.id;
};
if (options && options.dry) {
// Don't keep a copy of the resulting cards.
// This is usefull when doing autocompletion.
cards = _this.$cards;
}
else {
cards = _this.cards;
}
if (excludedCards) {
// Remove excluded cards from results
results = _.filter(response.cards, function(card) {
return _.isUndefined(_.find(excludedCards, compareIds, card));
return _this.$id().then(function(addressbookId) {
return AddressBook.$$resource.fetch(addressbookId, 'view', AddressBook.$query);
}).then(function(response) {
var results, cards, card, index,
compareIds = function(data) {
return _this.id == data.id;
};
if (options && options.dry) {
// Don't keep a copy of the resulting cards.
// This is usefull when doing autocompletion.
cards = _this.$cards;
}
else {
cards = _this.cards;
}
if (excludedCards) {
// Remove excluded cards from results
results = _.filter(response.cards, function(card) {
return _.isUndefined(_.find(excludedCards, compareIds, card));
});
}
else {
results = response.cards;
}
// Remove cards that no longer match the search query
for (index = cards.length - 1; index >= 0; index--) {
card = cards[index];
if (_.isUndefined(_.find(results, compareIds, card))) {
cards.splice(index, 1);
}
}
// Add new cards matching the search query
_.each(results, function(data, index) {
if (_.isUndefined(_.find(cards, compareIds, data))) {
var card = new AddressBook.$Card(data, search);
cards.splice(index, 0, card);
}
});
}
else {
results = response.cards;
}
// Remove cards that no longer match the search query
for (index = cards.length - 1; index >= 0; index--) {
card = cards[index];
if (_.isUndefined(_.find(results, compareIds, card))) {
cards.splice(index, 1);
}
}
// Add new cards matching the search query
_.each(results, function(data, index) {
if (_.isUndefined(_.find(cards, compareIds, data))) {
var card = new AddressBook.$Card(data, search);
cards.splice(index, 0, card);
}
// Respect the order of the results
_.each(results, function(data, index) {
var oldIndex, removedCards;
if (cards[index].id != data.id) {
oldIndex = _.findIndex(cards, compareIds, data);
removedCards = cards.splice(oldIndex, 1);
cards.splice(index, 0, removedCards[0]);
}
});
return cards;
});
// Respect the order of the results
_.each(results, function(data, index) {
var oldIndex, removedCards;
if (cards[index].id != data.id) {
oldIndex = _.findIndex(cards, compareIds, data);
removedCards = cards.splice(oldIndex, 1);
cards.splice(index, 0, removedCards[0]);
}
});
return cards;
});
};

View File

@@ -115,7 +115,7 @@
}
function sortedBy(field) {
return vm.selectedFolder.$query.sort == field;
return AddressBook.$query.sort == field;
}
function cancelSearch() {