snappymail/dev/Settings/User/Accounts.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { Capa, StorageResultType } from 'Common/Enums';
import { root } from 'Common/Links';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { capa } from 'Storage/Settings';
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
import AccountStore from 'Stores/User/Account';
import IdentityStore from 'Stores/User/Identity';
import Remote from 'Remote/User/Ajax';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/User';
2019-07-05 03:19:24 +08:00
import { showScreenPopup, routeOff, setHash } from 'Knoin/Knoin';
2019-07-05 03:19:24 +08:00
class AccountsUserSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.allowAdditionalAccount = capa(Capa.AdditionalAccounts);
this.allowIdentities = capa(Capa.Identities);
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.accounts = AccountStore.accounts;
this.identities = IdentityStore.identities;
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.accountForDeletion = ko.observable(null).deleteAccessHelper();
this.identityForDeletion = ko.observable(null).deleteAccessHelper();
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
scrollableOptions(wrapper) {
return {
handle: '.drag-handle',
containment: wrapper || 'parent',
axis: 'y'
};
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
addNewAccount() {
showScreenPopup(require('View/Popup/Account'));
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
editAccount(account) {
2019-07-05 03:19:24 +08:00
if (account && account.canBeEdit()) {
2016-07-16 05:29:42 +08:00
showScreenPopup(require('View/Popup/Account'), [account]);
}
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
addNewIdentity() {
showScreenPopup(require('View/Popup/Identity'));
}
2015-01-28 06:16:00 +08:00
2016-07-16 05:29:42 +08:00
editIdentity(identity) {
showScreenPopup(require('View/Popup/Identity'), [identity]);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
/**
* @param {AccountModel} accountToRemove
* @returns {void}
*/
deleteAccount(accountToRemove) {
2019-07-05 03:19:24 +08:00
if (accountToRemove && accountToRemove.deleteAccess()) {
2016-07-16 05:29:42 +08:00
this.accountForDeletion(null);
2019-07-05 03:19:24 +08:00
if (accountToRemove) {
2016-07-16 05:29:42 +08:00
this.accounts.remove((account) => accountToRemove === account);
2014-10-22 00:49:15 +08:00
Remote.accountDelete((result, data) => {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data && data.Result && data.Reload) {
2016-07-16 05:29:42 +08:00
routeOff();
setHash(root(), true);
routeOff();
2014-08-21 23:08:34 +08:00
setTimeout(() => location.reload(), 1);
2019-07-05 03:19:24 +08:00
} else {
getApp().accountsAndIdentities();
2016-07-16 05:29:42 +08:00
}
}, accountToRemove.email);
}
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
/**
* @param {IdentityModel} identityToRemove
* @returns {void}
*/
deleteIdentity(identityToRemove) {
2019-07-05 03:19:24 +08:00
if (identityToRemove && identityToRemove.deleteAccess()) {
2016-07-16 05:29:42 +08:00
this.identityForDeletion(null);
2019-07-05 03:19:24 +08:00
if (identityToRemove) {
2016-07-16 05:29:42 +08:00
IdentityStore.identities.remove((oIdentity) => identityToRemove === oIdentity);
2016-07-16 05:29:42 +08:00
Remote.identityDelete(() => {
getApp().accountsAndIdentities();
2016-07-16 05:29:42 +08:00
}, identityToRemove.id);
}
2016-06-30 08:02:45 +08:00
}
}
2016-07-16 05:29:42 +08:00
accountsAndIdentitiesAfterMove() {
2019-07-05 03:19:24 +08:00
Remote.accountsAndIdentitiesSortOrder(null, AccountStore.accountsEmails.peek(), IdentityStore.identitiesIDS.peek());
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onBuild(oDom) {
const self = this;
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
oDom
2019-07-05 03:19:24 +08:00
.on('click', '.accounts-list .account-item .e-action', function() {
// eslint-disable-line prefer-arrow-callback
const account = ko.dataFor(this); // eslint-disable-line no-invalid-this
2019-07-05 03:19:24 +08:00
if (account) {
2016-07-16 05:29:42 +08:00
self.editAccount(account);
}
})
2019-07-05 03:19:24 +08:00
.on('click', '.identities-list .identity-item .e-action', function() {
// eslint-disable-line prefer-arrow-callback
const identity = ko.dataFor(this); // eslint-disable-line no-invalid-this
2019-07-05 03:19:24 +08:00
if (identity) {
2016-07-16 05:29:42 +08:00
self.editIdentity(identity);
}
});
}
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
export { AccountsUserSettings, AccountsUserSettings as default };