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';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
import AccountStore from 'Stores/User/Account';
|
2021-02-17 21:40:21 +08:00
|
|
|
import { IdentityUserStore } from 'Stores/User/Identity';
|
2020-09-15 01:40:56 +08:00
|
|
|
import Remote from 'Remote/User/Fetch';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
import { showScreenPopup } from 'Knoin/Knoin';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-01-26 05:00:13 +08:00
|
|
|
import { AccountPopupView } from 'View/Popup/Account';
|
|
|
|
import { IdentityPopupView } from 'View/Popup/Identity';
|
|
|
|
|
2021-01-22 23:32:08 +08:00
|
|
|
export class AccountsUserSettings {
|
2016-07-16 05:29:42 +08:00
|
|
|
constructor() {
|
2020-09-04 18:05:17 +08:00
|
|
|
this.allowAdditionalAccount = rl.settings.capa(Capa.AdditionalAccounts);
|
|
|
|
this.allowIdentities = rl.settings.capa(Capa.Identities);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.accounts = AccountStore.accounts;
|
2021-02-17 21:40:21 +08:00
|
|
|
this.identities = IdentityUserStore;
|
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
|
|
|
addNewAccount() {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(AccountPopupView);
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
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()) {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(AccountPopupView, [account]);
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
addNewIdentity() {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(IdentityPopupView);
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
2015-01-28 06:16:00 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
editIdentity(identity) {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(IdentityPopupView, [identity]);
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
Remote.accountDelete((result, data) => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (StorageResultType.Success === result && data && data.Result && data.Reload) {
|
2020-09-17 02:35:29 +08:00
|
|
|
rl.route.root();
|
2020-08-12 06:25:36 +08:00
|
|
|
setTimeout(() => location.reload(), 1);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2020-09-15 15:29:25 +08:00
|
|
|
rl.app.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) {
|
2021-02-17 21:40:21 +08:00
|
|
|
IdentityUserStore.remove(oIdentity => identityToRemove === oIdentity);
|
2020-10-30 23:42:01 +08:00
|
|
|
Remote.identityDelete(() => rl.app.accountsAndIdentities(), identityToRemove.id());
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
accountsAndIdentitiesAfterMove() {
|
2021-02-17 21:40:21 +08:00
|
|
|
Remote.accountsAndIdentitiesSortOrder(null, AccountStore.getEmailAddresses(), IdentityUserStore.getIDS());
|
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) {
|
2020-08-30 16:30:50 +08:00
|
|
|
oDom.addEventListener('click', event => {
|
|
|
|
let el = event.target.closestWithin('.accounts-list .account-item .e-action', oDom);
|
|
|
|
el && ko.dataFor(el) && this.editAccount(ko.dataFor(el));
|
|
|
|
|
|
|
|
el = event.target.closestWithin('.identities-list .identity-item .e-action', oDom);
|
|
|
|
el && ko.dataFor(el) && this.editIdentity(ko.dataFor(el));
|
|
|
|
});
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
|
|
|
}
|