2018-07-31 10:08:51 +00:00
|
|
|
/**
|
2018-07-10 16:40:59 +00:00
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-07-28 18:13:27 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
2018-07-25 14:30:23 +00:00
|
|
|
const state = {
|
2018-07-28 18:13:27 +00:00
|
|
|
// Using objects for performance
|
|
|
|
// https://jsperf.com/ensure-unique-id-objects-vs-array
|
2018-07-30 11:35:35 +00:00
|
|
|
contacts: {},
|
|
|
|
sortedContacts: [],
|
|
|
|
orderKey: 'displayName'
|
2018-07-25 14:30:23 +00:00
|
|
|
}
|
2018-07-31 10:08:51 +00:00
|
|
|
|
2018-07-25 14:30:23 +00:00
|
|
|
const mutations = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store contacts into state
|
|
|
|
*
|
|
|
|
* @param {Object} state Default state
|
|
|
|
* @param {Array} contacts Contacts
|
|
|
|
*/
|
2018-07-28 18:13:27 +00:00
|
|
|
async appendContacts(state, contacts = []) {
|
|
|
|
state.contacts = contacts.reduce(function(list, contact) {
|
|
|
|
Vue.set(list, contact.key, contact)
|
|
|
|
return list
|
|
|
|
}, state.contacts)
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a contact from the global contacts list
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Contact} contact
|
|
|
|
*/
|
|
|
|
deleteContact(state, contact) {
|
2018-07-30 11:35:35 +00:00
|
|
|
let index = state.sortedContacts.findIndex(search => search.key === contact.key)
|
|
|
|
state.sortedContacts.splice(index, 1)
|
2018-07-28 18:13:27 +00:00
|
|
|
Vue.delete(state.contacts, contact.key)
|
2018-07-27 13:03:43 +00:00
|
|
|
},
|
|
|
|
|
2018-07-30 11:35:35 +00:00
|
|
|
/**
|
|
|
|
* Insert new contact into sorted array
|
|
|
|
* Not using sort, splice has far better performances
|
|
|
|
* https://jsperf.com/sort-vs-splice-in-array
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Contact} contact
|
|
|
|
*/
|
|
|
|
addContact(state, contact) {
|
|
|
|
let sortedContact = {
|
|
|
|
key: contact.key,
|
|
|
|
value: contact[state.orderKey]
|
|
|
|
}
|
|
|
|
for (var i = 0, len = state.sortedContacts.length; i < len; i++) {
|
|
|
|
var nameA = state.sortedContacts[i].value.toUpperCase() // ignore upper and lowercase
|
|
|
|
var nameB = sortedContact.value.toUpperCase() // ignore upper and lowercase
|
|
|
|
if (nameA.localeCompare(nameB) > 0) {
|
|
|
|
state.sortedContacts.splice(i, 0, sortedContact)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vue.set(state.contacts, contact.key, contact)
|
|
|
|
},
|
|
|
|
|
2018-07-28 18:13:27 +00:00
|
|
|
/**
|
|
|
|
* Order the contacts list. Filters have terrible performances.
|
|
|
|
* We do not want to run the sorting function every time.
|
2018-07-30 11:35:35 +00:00
|
|
|
* Let's only run it on additions and create an index
|
2018-07-28 18:13:27 +00:00
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
*/
|
2018-07-30 11:35:35 +00:00
|
|
|
async sortContacts(state) {
|
|
|
|
state.sortedContacts = Object.values(state.contacts)
|
2018-07-31 10:08:51 +00:00
|
|
|
// exclude groups
|
|
|
|
.filter(contact => contact.kind !== 'group')
|
2018-07-30 11:35:35 +00:00
|
|
|
.map(contact => { return { key: contact.key, value: contact[state.orderKey] } })
|
2018-07-28 18:13:27 +00:00
|
|
|
.sort((a, b) => {
|
2018-07-30 11:35:35 +00:00
|
|
|
var nameA = a.value.toUpperCase() // ignore upper and lowercase
|
|
|
|
var nameB = b.value.toUpperCase() // ignore upper and lowercase
|
2018-07-28 18:13:27 +00:00
|
|
|
return nameA.localeCompare(nameB)
|
|
|
|
})
|
2018-07-30 11:35:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the order key
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {string} [orderKey='displayName']
|
|
|
|
*/
|
|
|
|
setOrder(state, orderKey = 'displayName') {
|
|
|
|
state.orderKey = orderKey
|
2018-07-25 14:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-31 10:08:51 +00:00
|
|
|
|
2018-07-25 14:30:23 +00:00
|
|
|
const getters = {
|
2018-07-28 18:13:27 +00:00
|
|
|
getContacts: state => state.contacts,
|
2018-07-30 11:35:35 +00:00
|
|
|
getSortedContacts: state => state.sortedContacts,
|
|
|
|
getContact: (state) => (uid) => state.contacts[uid],
|
|
|
|
getOrderKey: state => state.orderKey
|
2018-07-28 18:13:27 +00:00
|
|
|
}
|
2018-07-31 10:08:51 +00:00
|
|
|
|
2018-07-28 18:13:27 +00:00
|
|
|
const actions = {
|
2018-08-07 14:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a contact from the list and from the associated addressbook
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Contact} contact the contact to delete
|
|
|
|
*/
|
2018-07-28 18:13:27 +00:00
|
|
|
deleteContact(context, contact) {
|
|
|
|
context.commit('deleteContact', contact)
|
|
|
|
context.commit('deleteContactFromAddressbook', contact)
|
2018-07-25 14:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-10 16:40:59 +00:00
|
|
|
|
|
|
|
export default { state, mutations, getters, actions }
|