Improved Contacts to check if changes should be saved

This commit is contained in:
the-djmaze 2022-09-06 19:22:06 +02:00
parent c9ad0ef170
commit 2627a16c36
43 changed files with 102 additions and 63 deletions

View file

@ -2,12 +2,6 @@
* Inspired by https://github.com/mcpar-land/vcfer
*/
const
camelCase = str =>
str.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) =>
index === 0 ? word.toLowerCase() : word.toUpperCase()
).replace(/\s+/g, '');
export class VCardProperty {
/**
@ -68,7 +62,7 @@ export class VCardProperty {
parseFromJCardProperty(jCardProp)
{
jCardProp = JSON.parse(JSON.stringify(jCardProp));
this.field = camelCase(jCardProp[0]);
this.field = jCardProp[0].toLowerCase();
this.params = jCardProp[1];
this.type = jCardProp[2];
this.value = jCardProp[3];

View file

@ -93,7 +93,7 @@ export class ContactModel extends AbstractModel {
constructor() {
super();
this.jCard = new JCard();
this.jCard = ['vcard',[]];
this.addObservables({
focused: false,
@ -122,14 +122,13 @@ export class ContactModel extends AbstractModel {
this.addComputables({
hasValidName: () => !!(this.givenName() || this.surName()),
fullName: () => (this.givenName() + ' ' + this.surName()).trim(),
fullName: () => [this.namePrefix(), this.givenName(), this.middleName(), this.surName()].join(' ').trim(),
display: () => {
let a = this.jCard.getOne('fn')?.value,
b = this.fullName(),
c = this.jCard.getOne('email')?.value,
d = this.nickname();
return a || b || c || d;
let a = this.fullName(),
b = this.email()?.[0]?.value,
c = this.nickname();
return a || b || c;
}
/*
fullName: {
@ -200,12 +199,12 @@ export class ContactModel extends AbstractModel {
});
});
props = jCard.getOne('x-Crypto');
props = jCard.getOne('x-crypto');
contact.signpref(props?.params.signpref || 'Ask');
contact.encryptpref(props?.params.encryptpref || 'Ask');
// contact.encryptpref(props?.params.allowed || 'PGP/INLINE,PGP/MIME,S/MIME,S/MIMEOpaque');
contact.jCard = jCard;
contact.jCard = json.jCard;
}
return contact;
}
@ -246,9 +245,14 @@ export class ContactModel extends AbstractModel {
this.nickname() || this.nickname('');
}
hasChanges()
{
return this.toJSON().jCard != JSON.stringify(this.jCard);
}
toJSON()
{
let jCard = this.jCard;
let jCard = new JCard(this.jCard);
jCard.set('n', [
this.surName(),
this.givenName(),
@ -273,7 +277,7 @@ export class ContactModel extends AbstractModel {
values.forEach(value => value && jCard.add(field, value));
});
jCard.set('x-Crypto', '', {
jCard.set('x-crypto', '', {
allowed: 'PGP/INLINE,PGP/MIME,S/MIME,S/MIMEOpaque',
signpref: this.signpref(),
encryptpref: this.encryptpref()

View file

@ -142,13 +142,6 @@
text-align: right;
border-top: 1px solid rgba(128,128,128,0.4);
.button-save-contact {
&.dirty:enabled {
color: #51a351;
font-weight: bold;
}
}
.dropdown-menu.right-edge {
top: auto;
bottom: 100%;

View file

@ -15,7 +15,7 @@ import Remote from 'Remote/User/Fetch';
import { EmailModel } from 'Model/Email';
import { ContactModel } from 'Model/Contact';
import { decorateKoCommands, showScreenPopup } from 'Knoin/Knoin';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { AskPopupView } from 'View/Popup/Ask';
@ -43,8 +43,6 @@ export class ContactsPopupView extends AbstractViewPopup {
isSaving: false,
hasChanges: false,
contact: null
});
@ -61,9 +59,7 @@ export class ContactsPopupView extends AbstractViewPopup {
'.e-contact-item.focused'
);
this.selector.on('ItemSelect', contact => {
this.populateViewContact(contact);
});
this.selector.on('ItemSelect', contact => this.populateViewContact(contact));
this.selector.on('ItemGetUid', contact => contact ? contact.generateUid() : '');
@ -95,13 +91,10 @@ export class ContactsPopupView extends AbstractViewPopup {
this.saveCommand = this.saveCommand.bind(this);
// this.hasChanges(!!contact()?.toJSON().jCard);
decorateKoCommands(this, {
// close: self => !self.hasChanges(),
deleteCommand: self => 0 < self.contactsCheckedOrSelected().length,
newMessageCommand: self => 0 < self.contactsCheckedOrSelected().length,
saveCommand: self => !self.isSaving() && !self.hasChanges(),
saveCommand: self => !self.isSaving(),
syncCommand: self => !self.contacts.syncing() && !self.contacts.importing()
});
}
@ -171,22 +164,29 @@ export class ContactsPopupView extends AbstractViewPopup {
}
saveCommand() {
this.isSaving(true);
const contact = this.contact();
Remote.request('ContactSave',
(iError, oData) => {
this.isSaving(false);
if (!iError && oData.Result.ResultID) {
contact.id(oData.Result.ResultID);
this.reloadContactList(); // TODO: remove when e-contact-foreach is dynamic
this.hasChanges(false);
}
}, {
Contact: contact
// Uid: contact.id(),
// jCard: contact.jCard
}
);
this.saveContact(this.contact());
}
saveContact(contact) {
const data = contact.toJSON();
if (data.jCard != JSON.stringify(contact.jCard)) {
this.isSaving(true);
Remote.request('ContactSave',
(iError, oData) => {
this.isSaving(false);
if (iError) {
alert(oData?.ErrorMessage || getNotification(iError));
} else if (oData.Result.ResultID) {
if (contact.id()) {
contact.id(oData.Result.ResultID);
contact.jCard = JSON.parse(data.jCard);
} else {
this.reloadContactList(); // TODO: remove when e-contact-foreach is dynamic
}
}
}, data
);
}
}
syncCommand() {
@ -252,8 +252,14 @@ export class ContactsPopupView extends AbstractViewPopup {
* @param {?ContactModel} contact
*/
populateViewContact(contact) {
const oldContact = this.contact();
if (oldContact?.hasChanges()) {
AskPopupView.showModal([
i18n('GLOBAL/SAVE_CHANGES'),
() => this.saveContact(oldContact)
]);
}
this.contact(contact || new ContactModel);
this.hasChanges(false);
}
/**
@ -349,10 +355,16 @@ export class ContactsPopupView extends AbstractViewPopup {
}
}
tryToClose() {
(false === this.onClose()) || this.close();
}
onClose() {
if (this.hasChanges() && AskPopupView.hidden()) {
showScreenPopup(AskPopupView, [
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
const contact = this.contact();
if (AskPopupView.hidden() && contact?.hasChanges()) {
AskPopupView.showModal([
i18n('GLOBAL/SAVE_CHANGES'),
() => this.close() | this.saveContact(contact),
() => this.close()
]);
return false;

View file

@ -97,15 +97,14 @@ trait Contacts
$bResult = false;
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) {
$aContact = $this->GetActionParam('Contact');
if (\is_array($aContact) && isset($aContact['Uid'], $aContact['jCard'])) {
$vCard = \Sabre\VObject\Reader::readJson($aContact['jCard']);
if ($this->HasActionParam('Uid') && $this->HasActionParam('jCard')) {
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) {
$vCard = \Sabre\VObject\Reader::readJson($this->GetActionParam('jCard'));
if ($vCard && $vCard instanceof \Sabre\VObject\Component\VCard) {
$vCard->REV = \gmdate('Ymd\\THis\\Z');
$vCard->PRODID = 'SnappyMail-'.APP_VERSION;
$sUid = \trim($aContact['Uid']);
$sUid = \trim($this->GetActionParam('Uid'));
$oContact = $sUid ? $oAddressBookProvider->GetContactByID($sUid) : null;
if (!$oContact) {
$oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();

View file

@ -24,6 +24,7 @@
"PASSWORD": "كلمة السر",
"REPLY_TO": "الرد-الى",
"SAVE": "حفظ",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "لم يتم ايجاد جهة الإتصال",
"SPAM": "الإبلاغ عن الرسائل غير المرغوب فيها",
"SUBJECT": "الموضوع",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Парола",
"REPLY_TO": "Отговори на",
"SAVE": "Запази",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Не са намерени контакти",
"SPAM": "Спам",
"SUBJECT": "Заглавие",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Heslo",
"REPLY_TO": "Adresa odpovědi",
"SAVE": "Uložit",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nebyly nalezeny žádné kontakty",
"SPAM": "Je to spam",
"SUBJECT": "Předmět",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Adgangskode",
"REPLY_TO": "Svar til",
"SAVE": "Gem",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Ingen kontakter fundet",
"SPAM": "Uønsket",
"SUBJECT": "Emne",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Passwort",
"REPLY_TO": "Antwort an",
"SAVE": "Speichern",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Suchen",
"SPAM": "Spam",
"SUBJECT": "Betreff",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Κωδικός πρόσβασης",
"REPLY_TO": "Απάντηση στον",
"SAVE": "Αποθήκευση",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Δεν βρέθηκαν επαφές",
"SPAM": "Ανεπιθύμητο",
"SUBJECT": "Θέμα",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Password",
"REPLY_TO": "Reply-To",
"SAVE": "Save",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Search",
"SPAM": "Spam",
"SUBJECT": "Subject",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Password",
"REPLY_TO": "Reply-To",
"SAVE": "Save",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Search",
"SPAM": "Spam",
"SUBJECT": "Subject",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Contraseña",
"REPLY_TO": "Responder",
"SAVE": "Guardar",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Buscar",
"SPAM": "Es correo deseado",
"SUBJECT": "Asunto",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Salasõna",
"REPLY_TO": "Vastuse saaja",
"SAVE": "Salvesta",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Kontakte ei leitud",
"SPAM": "Märgi rämpspostiks",
"SUBJECT": "Pealkiri",

View file

@ -24,6 +24,7 @@
"PASSWORD": "گذرواژه",
"REPLY_TO": "پاسخ به",
"SAVE": "ذخیره کن",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "هیچ تماسی پیدا نشد",
"SPAM": "هرزنامه",
"SUBJECT": "موضوع",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Salasana",
"REPLY_TO": "Vastaa",
"SAVE": "Tallenna",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Yhtään yhteystietoa ei löytynyt",
"SPAM": "Merkitse roskapostiksi",
"SUBJECT": "Aihe",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Mot de passe",
"REPLY_TO": "Répondre à",
"SAVE": "Enregistrer",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Rechercher",
"SPAM": "Indésirable",
"SUBJECT": "Sujet",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Jelszó",
"REPLY_TO": "Válaszcím",
"SAVE": "Mentés",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nem találtam névjegyeket",
"SPAM": "Levélszemét",
"SUBJECT": "Tárgy",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Sandi",
"REPLY_TO": "Balas-Ke",
"SAVE": "Simpan",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Tidak ditemukan kontak",
"SPAM": "Spam",
"SUBJECT": "Judul",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Lykilorð",
"REPLY_TO": "Svara-til",
"SAVE": "Vista",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Engir tengiliðir fundust",
"SPAM": "Ruslpóstur",
"SUBJECT": "Viðfangsefni",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Parola d'ordine",
"REPLY_TO": "Rispondi a",
"SAVE": "Salva",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Cerca",
"SPAM": "Segnala come spam",
"SUBJECT": "Oggetto",

View file

@ -24,6 +24,7 @@
"PASSWORD": "パスワード",
"REPLY_TO": "Reply-To",
"SAVE": "保存",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "検索",
"SPAM": "迷惑メール",
"SUBJECT": "件名",

View file

@ -24,6 +24,7 @@
"PASSWORD": "비밀번호",
"REPLY_TO": "회신 주소",
"SAVE": "저장",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "연락처를 찾지 못했습니다.",
"SPAM": "스팸",
"SUBJECT": "제목",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Slaptažodis",
"REPLY_TO": "Kam atsakyti",
"SAVE": "Išsaugoti",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Kontaktų nerasta",
"SPAM": "Žymėti kaip šlamštas",
"SUBJECT": "Tema",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Parole",
"REPLY_TO": "Reply-To",
"SAVE": "Saglabāt",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Kontakti nav atrasti",
"SPAM": "Spams",
"SUBJECT": "Tēma",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Passord",
"REPLY_TO": "Svar til",
"SAVE": "Lagre",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Fant ingen kontakter",
"SPAM": "Søppelpost",
"SUBJECT": "Emne",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Wachtwoord",
"REPLY_TO": "Antwoordadres",
"SAVE": "Opslaan",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Zoeken",
"SPAM": "Ongewenste e-mail",
"SUBJECT": "Onderwerp",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Hasło",
"REPLY_TO": "Odpisz do",
"SAVE": "Zapisz",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nie znaleziono żadnych kontaktów",
"SPAM": "Oznacz jako SPAM",
"SUBJECT": "Temat",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Senha",
"REPLY_TO": "Responder para",
"SAVE": "Salvar",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nenhum contato encontrado",
"SPAM": "Lixo Eletrônico",
"SUBJECT": "Assunto",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Palavra-passe",
"REPLY_TO": "Resp.-Para",
"SAVE": "Guardar",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Pesquisar",
"SPAM": "Spam",
"SUBJECT": "Assunto",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Palavra-passe",
"REPLY_TO": "Resp.-Para",
"SAVE": "Guardar",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Pesquisar",
"SPAM": "Spam",
"SUBJECT": "Assunto",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Parolă",
"REPLY_TO": "Reply-To",
"SAVE": "Salvează",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nu am găsit nimic.",
"SPAM": "SPAM",
"SUBJECT": "Subiect",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Пароль",
"REPLY_TO": "Ответить-на",
"SAVE": "Сохранить",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Контакты не найдены",
"SPAM": "В спам",
"SUBJECT": "Тема",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Heslo",
"REPLY_TO": "Adresa pre odpoveď",
"SAVE": "Uložiť",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Nenašli sa žiadne kontakty",
"SPAM": "Spam",
"SUBJECT": "Predmet",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Geslo",
"REPLY_TO": "Odgovor na",
"SAVE": "Shrani",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Ni ujemajočih stikov",
"SPAM": "Vsiljena pošta",
"SUBJECT": "Zadeva",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Lösenord",
"REPLY_TO": "Svara till",
"SAVE": "Spara",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Inga kontakter funna",
"SPAM": "Spam",
"SUBJECT": "Ämne",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Şifre",
"REPLY_TO": "Reply-To",
"SAVE": "Kaydet",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Kişi bulunamadı",
"SPAM": "Spam",
"SUBJECT": "Konu",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Пароль",
"REPLY_TO": "Reply-To",
"SAVE": "Зберегти",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Контакти не знайдено",
"SPAM": "В спам",
"SUBJECT": "Тема",

View file

@ -24,6 +24,7 @@
"PASSWORD": "Mật khẩu",
"REPLY_TO": "Nhận thư phản hồi về",
"SAVE": "Lưu",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "Tìm kiếm",
"SPAM": "Thư rác",
"SUBJECT": "Tiêu đề",

View file

@ -24,6 +24,7 @@
"PASSWORD": "密码",
"REPLY_TO": "回复",
"SAVE": "保存",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "搜索",
"SPAM": "这是垃圾邮件",
"SUBJECT": "主题",

View file

@ -24,6 +24,7 @@
"PASSWORD": "密碼",
"REPLY_TO": "Reply-To",
"SAVE": "保存",
"SAVE_CHANGES": "Save changes?",
"SEARCH": "沒找到連絡人",
"SPAM": "垃圾郵件",
"SUBJECT": "主題",

View file

@ -1,6 +1,6 @@
<header class="b-header-toolbar g-ui-user-select-none">
<a href="#" class="close" data-bind="click: close">×</a>
<a href="#" class="close" data-bind="click: tryToClose">×</a>
<div class="btn-toolbar">
@ -228,7 +228,7 @@
<div class="b-view-content-toolbar btn-toolbar" data-bind="i18nUpdate: contact">
<!-- ko with: contact -->
<div class="btn-group">
<button class="btn button-save-contact" data-bind="visible: !readOnly(), command: $root.saveCommand, css: {'dirty': $root.hasChanges}">
<button class="btn button-save-contact" data-bind="visible: !readOnly(), command: $root.saveCommand">
<i data-bind="css: {'icon-ok': !$root.isSaving(), 'icon-spinner': $root.isSaving()}"></i>
<span data-i18n="CONTACTS/BUTTON_CREATE_CONTACT" data-bind="visible: !id()"></span>
<span data-i18n="CONTACTS/BUTTON_UPDATE_CONTACT" data-bind="visible: id"></span>