snappymail/dev/View/Popup/Contacts.js

782 lines
19 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
_ = require('_'),
$ = require('$'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
key = require('key'),
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Consts = require('Common/Consts'),
Globals = require('Common/Globals'),
Utils = require('Common/Utils'),
Selector = require('Common/Selector'),
2014-10-06 02:37:31 +08:00
Links = require('Common/Links'),
Translator = require('Common/Translator'),
2014-08-21 23:08:34 +08:00
2015-02-03 07:58:58 +08:00
SettingsStore = require('Stores/User/Settings'),
2015-02-22 06:00:51 +08:00
ContactStore = require('Stores/User/Contact'),
2015-02-23 00:35:17 +08:00
Remote = require('Remote/User/Ajax'),
2014-08-21 23:08:34 +08:00
EmailModel = require('Model/Email'),
ContactModel = require('Model/Contact'),
ContactPropertyModel = require('Model/ContactProperty'),
2014-08-25 15:10:51 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function ContactsPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsContacts');
2014-08-21 23:08:34 +08:00
var
self = this,
fFastClearEmptyListHelper = function (aList) {
if (aList && 0 < aList.length) {
self.viewProperties.removeAll(aList);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(aList);
2014-08-21 23:08:34 +08:00
}
}
;
this.bBackToCompose = false;
2015-02-16 05:55:59 +08:00
this.sLastComposeFocusedField = '';
2015-02-22 06:00:51 +08:00
this.allowContactsSync = ContactStore.allowContactsSync;
this.enableContactsSync = ContactStore.enableContactsSync;
2014-08-21 23:08:34 +08:00
this.allowExport = !Globals.bMobileDevice;
2014-08-21 23:08:34 +08:00
this.search = ko.observable('');
this.contactsCount = ko.observable(0);
2015-02-22 06:00:51 +08:00
this.contacts = ContactStore.contacts;
2014-08-21 23:08:34 +08:00
this.currentContact = ko.observable(null);
2014-08-21 23:08:34 +08:00
this.importUploaderButton = ko.observable(null);
2014-08-21 23:08:34 +08:00
this.contactsPage = ko.observable(1);
this.contactsPageCount = ko.computed(function () {
var iPage = window.Math.ceil(this.contactsCount() / Consts.Defaults.ContactsPerPage);
return 0 >= iPage ? 1 : iPage;
}, this);
2014-08-21 23:08:34 +08:00
this.contactsPagenator = ko.computed(Utils.computedPagenatorHelper(this.contactsPage, this.contactsPageCount));
2014-08-21 23:08:34 +08:00
this.emptySelection = ko.observable(true);
this.viewClearSearch = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.viewID = ko.observable('');
this.viewReadOnly = ko.observable(false);
this.viewProperties = ko.observableArray([]);
2014-08-21 23:08:34 +08:00
this.viewSaveTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
2014-08-21 23:08:34 +08:00
this.viewPropertiesNames = this.viewProperties.filter(function(oProperty) {
return -1 < Utils.inArray(oProperty.type(), [
2014-08-21 23:08:34 +08:00
Enums.ContactPropertyType.FirstName, Enums.ContactPropertyType.LastName
]);
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesOther = this.viewProperties.filter(function(oProperty) {
return -1 < Utils.inArray(oProperty.type(), [
Enums.ContactPropertyType.Note
]);
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesOther = ko.computed(function () {
2014-08-21 23:08:34 +08:00
var aList = _.filter(this.viewProperties(), function (oProperty) {
return -1 < Utils.inArray(oProperty.type(), [
Enums.ContactPropertyType.Nick
]);
});
2014-08-21 23:08:34 +08:00
return _.sortBy(aList, function (oProperty) {
return oProperty.type();
});
2014-08-21 23:08:34 +08:00
}, this);
2014-08-21 23:08:34 +08:00
this.viewPropertiesEmails = this.viewProperties.filter(function(oProperty) {
return Enums.ContactPropertyType.Email === oProperty.type();
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesWeb = this.viewProperties.filter(function(oProperty) {
return Enums.ContactPropertyType.Web === oProperty.type();
});
2014-08-21 23:08:34 +08:00
this.viewHasNonEmptyRequaredProperties = ko.computed(function() {
2014-08-21 23:08:34 +08:00
var
aNames = this.viewPropertiesNames(),
aEmail = this.viewPropertiesEmails(),
fHelper = function (oProperty) {
return '' !== Utils.trim(oProperty.value());
}
;
2014-08-21 23:08:34 +08:00
return !!(_.find(aNames, fHelper) || _.find(aEmail, fHelper));
}, this);
2014-08-21 23:08:34 +08:00
this.viewPropertiesPhones = this.viewProperties.filter(function(oProperty) {
return Enums.ContactPropertyType.Phone === oProperty.type();
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesEmailsNonEmpty = this.viewPropertiesNames.filter(function(oProperty) {
return '' !== Utils.trim(oProperty.value());
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesEmailsEmptyAndOnFocused = this.viewPropertiesEmails.filter(function(oProperty) {
var bF = oProperty.focused();
return '' === Utils.trim(oProperty.value()) && !bF;
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesPhonesEmptyAndOnFocused = this.viewPropertiesPhones.filter(function(oProperty) {
var bF = oProperty.focused();
return '' === Utils.trim(oProperty.value()) && !bF;
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesWebEmptyAndOnFocused = this.viewPropertiesWeb.filter(function(oProperty) {
var bF = oProperty.focused();
return '' === Utils.trim(oProperty.value()) && !bF;
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesOtherEmptyAndOnFocused = ko.computed(function () {
return _.filter(this.viewPropertiesOther(), function (oProperty) {
var bF = oProperty.focused();
return '' === Utils.trim(oProperty.value()) && !bF;
});
}, this);
2014-08-21 23:08:34 +08:00
this.viewPropertiesEmailsEmptyAndOnFocused.subscribe(function(aList) {
fFastClearEmptyListHelper(aList);
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesPhonesEmptyAndOnFocused.subscribe(function(aList) {
fFastClearEmptyListHelper(aList);
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesWebEmptyAndOnFocused.subscribe(function(aList) {
fFastClearEmptyListHelper(aList);
});
2014-08-21 23:08:34 +08:00
this.viewPropertiesOtherEmptyAndOnFocused.subscribe(function(aList) {
fFastClearEmptyListHelper(aList);
});
2014-08-21 23:08:34 +08:00
this.viewSaving = ko.observable(false);
2015-02-03 07:58:58 +08:00
this.useCheckboxesInList = SettingsStore.useCheckboxesInList;
2014-08-21 23:08:34 +08:00
this.search.subscribe(function () {
this.reloadContactList();
}, this);
this.contacts.subscribe(Utils.windowResizeCallback);
this.viewProperties.subscribe(Utils.windowResizeCallback);
2014-08-21 23:08:34 +08:00
this.contactsChecked = ko.computed(function () {
return _.filter(this.contacts(), function (oItem) {
return oItem.checked();
});
}, this);
2014-08-21 23:08:34 +08:00
this.contactsCheckedOrSelected = ko.computed(function () {
2014-08-21 23:08:34 +08:00
var
aChecked = this.contactsChecked(),
oSelected = this.currentContact()
;
2014-08-21 23:08:34 +08:00
return _.union(aChecked, oSelected ? [oSelected] : []);
2014-08-21 23:08:34 +08:00
}, this);
2014-08-21 23:08:34 +08:00
this.contactsCheckedOrSelectedUids = ko.computed(function () {
return _.map(this.contactsCheckedOrSelected(), function (oContact) {
return oContact.idContact;
});
}, this);
2014-08-21 23:08:34 +08:00
this.selector = new Selector(this.contacts, this.currentContact,
'.e-contact-item .actionHandle', '.e-contact-item.selected', '.e-contact-item .checkboxItem',
'.e-contact-item.focused');
2014-08-21 23:08:34 +08:00
this.selector.on('onItemSelect', _.bind(function (oContact) {
this.populateViewContact(oContact ? oContact : null);
if (!oContact)
{
this.emptySelection(true);
}
}, this));
2014-08-21 23:08:34 +08:00
this.selector.on('onItemGetUid', function (oContact) {
return oContact ? oContact.generateUid() : '';
});
this.newCommand = Utils.createCommand(this, function () {
this.populateViewContact(null);
this.currentContact(null);
});
this.deleteCommand = Utils.createCommand(this, function () {
this.deleteSelectedContacts();
this.emptySelection(true);
}, function () {
return 0 < this.contactsCheckedOrSelected().length;
});
2014-08-21 23:08:34 +08:00
this.newMessageCommand = Utils.createCommand(this, function () {
2015-02-16 05:55:59 +08:00
var
aE = [],
aC = this.contactsCheckedOrSelected(),
aToEmails = null,
aCcEmails = null,
aBccEmails = null
2015-02-16 05:55:59 +08:00
;
2014-08-21 23:08:34 +08:00
if (Utils.isNonEmptyArray(aC))
{
aE = _.map(aC, function (oItem) {
if (oItem)
{
2014-08-21 23:08:34 +08:00
var
aData = oItem.getNameAndEmailHelper(),
oEmail = aData ? new EmailModel(aData[0], aData[1]) : null
;
if (oEmail && oEmail.validate())
{
return oEmail;
}
}
2014-08-21 23:08:34 +08:00
return null;
});
2014-08-21 23:08:34 +08:00
aE = _.compact(aE);
}
2014-08-21 23:08:34 +08:00
if (Utils.isNonEmptyArray(aE))
{
self.bBackToCompose = false;
kn.hideScreenPopup(require('View/Popup/Contacts'));
2015-02-16 05:55:59 +08:00
switch (self.sLastComposeFocusedField)
{
default:
case 'to':
aToEmails = aE;
break;
case 'cc':
aCcEmails = aE;
break;
case 'bcc':
aBccEmails = aE;
break;
}
self.sLastComposeFocusedField = '';
_.delay(function () {
2015-02-16 05:55:59 +08:00
kn.showScreenPopup(require('View/Popup/Compose'),
[Enums.ComposeType.Empty, null, aToEmails, aCcEmails, aBccEmails]);
}, 200);
2014-08-21 23:08:34 +08:00
}
2014-08-21 23:08:34 +08:00
}, function () {
return 0 < this.contactsCheckedOrSelected().length;
});
2014-08-21 23:08:34 +08:00
this.clearCommand = Utils.createCommand(this, function () {
this.search('');
});
2014-08-21 23:08:34 +08:00
this.saveCommand = Utils.createCommand(this, function () {
2014-08-21 23:08:34 +08:00
this.viewSaving(true);
this.viewSaveTrigger(Enums.SaveSettingsStep.Animate);
2014-08-21 23:08:34 +08:00
var
sRequestUid = Utils.fakeMd5(),
aProperties = []
;
2014-08-21 23:08:34 +08:00
_.each(this.viewProperties(), function (oItem) {
if (oItem.type() && '' !== Utils.trim(oItem.value()))
{
aProperties.push([oItem.type(), oItem.value(), oItem.typeStr()]);
}
});
2014-08-21 23:08:34 +08:00
Remote.contactSave(function (sResult, oData) {
2014-08-21 23:08:34 +08:00
var bRes = false;
self.viewSaving(false);
2014-08-21 23:08:34 +08:00
if (Enums.StorageResultType.Success === sResult && oData && oData.Result &&
oData.Result.RequestUid === sRequestUid && 0 < Utils.pInt(oData.Result.ResultID))
{
2014-08-21 23:08:34 +08:00
if ('' === self.viewID())
{
self.viewID(Utils.pInt(oData.Result.ResultID));
}
self.reloadContactList();
bRes = true;
}
2014-08-21 23:08:34 +08:00
_.delay(function () {
self.viewSaveTrigger(bRes ? Enums.SaveSettingsStep.TrueResult : Enums.SaveSettingsStep.FalseResult);
}, 300);
2014-08-21 23:08:34 +08:00
if (bRes)
{
self.watchDirty(false);
2014-08-21 23:08:34 +08:00
_.delay(function () {
self.viewSaveTrigger(Enums.SaveSettingsStep.Idle);
}, 1000);
}
2013-12-23 08:06:48 +08:00
2014-10-04 03:09:13 +08:00
}, sRequestUid, this.viewID(), aProperties);
2014-08-21 23:08:34 +08:00
}, function () {
var
bV = this.viewHasNonEmptyRequaredProperties(),
bReadOnly = this.viewReadOnly()
;
return !this.viewSaving() && bV && !bReadOnly;
});
2014-08-21 23:08:34 +08:00
this.syncCommand = Utils.createCommand(this, function () {
2014-08-27 23:59:44 +08:00
var self = this;
2014-10-18 21:43:44 +08:00
require('App/User').contactsSync(function (sResult, oData) {
2014-08-21 23:08:34 +08:00
if (Enums.StorageResultType.Success !== sResult || !oData || !oData.Result)
{
window.alert(Translator.getNotification(
2014-08-21 23:08:34 +08:00
oData && oData.ErrorCode ? oData.ErrorCode : Enums.Notification.ContactsSyncError));
}
2014-08-21 23:08:34 +08:00
self.reloadContactList(true);
});
}, function () {
return !this.contacts.syncing() && !this.contacts.importing();
});
this.bDropPageAfterDelete = false;
this.watchDirty = ko.observable(false);
this.watchHash = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.viewHash = ko.computed(function () {
2014-10-04 03:09:13 +08:00
return '' + _.map(self.viewProperties(), function (oItem) {
2014-08-21 23:08:34 +08:00
return oItem.value();
}).join('');
});
2014-08-21 23:08:34 +08:00
// this.saveCommandDebounce = _.debounce(_.bind(this.saveCommand, this), 1000);
2014-08-21 23:08:34 +08:00
this.viewHash.subscribe(function () {
if (this.watchHash() && !this.viewReadOnly() && !this.watchDirty())
{
this.watchDirty(true);
}
}, this);
2014-08-21 23:08:34 +08:00
this.sDefaultKeyScope = Enums.KeyState.ContactList;
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/Contacts', 'PopupsContactsViewModel'], ContactsPopupView);
_.extend(ContactsPopupView.prototype, AbstractView.prototype);
ContactsPopupView.prototype.getPropertyPlceholder = function (sType)
2014-08-21 23:08:34 +08:00
{
var sResult = '';
switch (sType)
{
2014-08-21 23:08:34 +08:00
case Enums.ContactPropertyType.LastName:
sResult = 'CONTACTS/PLACEHOLDER_ENTER_LAST_NAME';
break;
case Enums.ContactPropertyType.FirstName:
sResult = 'CONTACTS/PLACEHOLDER_ENTER_FIRST_NAME';
break;
case Enums.ContactPropertyType.Nick:
sResult = 'CONTACTS/PLACEHOLDER_ENTER_NICK_NAME';
break;
}
2014-08-21 23:08:34 +08:00
return sResult;
};
ContactsPopupView.prototype.addNewProperty = function (sType, sTypeStr)
2014-08-21 23:08:34 +08:00
{
this.viewProperties.push(new ContactPropertyModel(sType, sTypeStr || '', '', true, this.getPropertyPlceholder(sType)));
};
ContactsPopupView.prototype.addNewOrFocusProperty = function (sType, sTypeStr)
{
2014-08-21 23:08:34 +08:00
var oItem = _.find(this.viewProperties(), function (oItem) {
return sType === oItem.type();
});
2014-08-21 23:08:34 +08:00
if (oItem)
{
oItem.focused(true);
}
else
{
this.addNewProperty(sType, sTypeStr);
}
};
ContactsPopupView.prototype.addNewEmail = function ()
2014-08-21 23:08:34 +08:00
{
this.addNewProperty(Enums.ContactPropertyType.Email, 'Home');
};
ContactsPopupView.prototype.addNewPhone = function ()
{
2014-08-21 23:08:34 +08:00
this.addNewProperty(Enums.ContactPropertyType.Phone, 'Mobile');
};
ContactsPopupView.prototype.addNewWeb = function ()
{
2014-08-21 23:08:34 +08:00
this.addNewProperty(Enums.ContactPropertyType.Web);
};
ContactsPopupView.prototype.addNewNickname = function ()
{
2014-08-21 23:08:34 +08:00
this.addNewOrFocusProperty(Enums.ContactPropertyType.Nick);
};
ContactsPopupView.prototype.addNewNotes = function ()
2014-08-21 23:08:34 +08:00
{
this.addNewOrFocusProperty(Enums.ContactPropertyType.Note);
};
ContactsPopupView.prototype.addNewBirthday = function ()
2014-08-21 23:08:34 +08:00
{
this.addNewOrFocusProperty(Enums.ContactPropertyType.Birthday);
};
ContactsPopupView.prototype.exportVcf = function ()
2014-08-21 23:08:34 +08:00
{
2014-10-18 21:43:44 +08:00
require('App/User').download(Links.exportContactsVcf());
2014-08-21 23:08:34 +08:00
};
ContactsPopupView.prototype.exportCsv = function ()
2014-08-21 23:08:34 +08:00
{
2014-10-18 21:43:44 +08:00
require('App/User').download(Links.exportContactsCsv());
2014-08-21 23:08:34 +08:00
};
ContactsPopupView.prototype.initUploader = function ()
{
2014-08-21 23:08:34 +08:00
if (this.importUploaderButton())
{
var
oJua = new Jua({
2014-10-06 02:37:31 +08:00
'action': Links.uploadContacts(),
2014-08-21 23:08:34 +08:00
'name': 'uploader',
'queueSize': 1,
'multipleSizeLimit': 1,
'disableFolderDragAndDrop': true,
'disableDragAndDrop': true,
'disableMultiple': true,
'disableDocumentDropPrevent': true,
'clickElement': this.importUploaderButton()
})
;
2014-08-21 23:08:34 +08:00
if (oJua)
{
2014-08-21 23:08:34 +08:00
oJua
.on('onStart', _.bind(function () {
this.contacts.importing(true);
}, this))
.on('onComplete', _.bind(function (sId, bResult, oData) {
this.contacts.importing(false);
this.reloadContactList();
if (!sId || !bResult || !oData || !oData.Result)
{
window.alert(Translator.i18n('CONTACTS/ERROR_IMPORT_FILE'));
2014-08-21 23:08:34 +08:00
}
}, this))
;
}
2014-08-21 23:08:34 +08:00
}
};
ContactsPopupView.prototype.removeCheckedOrSelectedContactsFromList = function ()
2014-08-21 23:08:34 +08:00
{
var
self = this,
oKoContacts = this.contacts,
oCurrentContact = this.currentContact(),
iCount = this.contacts().length,
aContacts = this.contactsCheckedOrSelected()
;
2014-08-21 23:08:34 +08:00
if (0 < aContacts.length)
{
2014-08-21 23:08:34 +08:00
_.each(aContacts, function (oContact) {
2014-08-21 23:08:34 +08:00
if (oCurrentContact && oCurrentContact.idContact === oContact.idContact)
{
oCurrentContact = null;
self.currentContact(null);
}
2014-08-21 23:08:34 +08:00
oContact.deleted(true);
iCount--;
});
2014-08-21 23:08:34 +08:00
if (iCount <= 0)
{
this.bDropPageAfterDelete = true;
}
_.delay(function () {
_.each(aContacts, function (oContact) {
oKoContacts.remove(oContact);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(oContact);
2014-08-21 23:08:34 +08:00
});
2014-08-21 23:08:34 +08:00
}, 500);
}
};
ContactsPopupView.prototype.deleteSelectedContacts = function ()
{
2014-08-21 23:08:34 +08:00
if (0 < this.contactsCheckedOrSelected().length)
{
Remote.contactsDelete(
_.bind(this.deleteResponse, this),
this.contactsCheckedOrSelectedUids()
);
2014-08-21 23:08:34 +08:00
this.removeCheckedOrSelectedContactsFromList();
}
};
/**
* @param {string} sResult
* @param {AjaxJsonDefaultResponse} oData
*/
ContactsPopupView.prototype.deleteResponse = function (sResult, oData)
{
2014-08-21 23:08:34 +08:00
if (500 < (Enums.StorageResultType.Success === sResult && oData && oData.Time ? Utils.pInt(oData.Time) : 0))
{
this.reloadContactList(this.bDropPageAfterDelete);
}
else
{
_.delay((function (self) {
return function () {
self.reloadContactList(self.bDropPageAfterDelete);
};
}(this)), 500);
}
};
ContactsPopupView.prototype.removeProperty = function (oProp)
{
2014-08-21 23:08:34 +08:00
this.viewProperties.remove(oProp);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(oProp);
2014-08-21 23:08:34 +08:00
};
/**
* @param {?ContactModel} oContact
*/
ContactsPopupView.prototype.populateViewContact = function (oContact)
2014-08-21 23:08:34 +08:00
{
var
sId = '',
sLastName = '',
sFirstName = '',
aList = []
;
2014-08-21 23:08:34 +08:00
this.watchHash(false);
2014-08-21 23:08:34 +08:00
this.emptySelection(false);
this.viewReadOnly(false);
2014-08-21 23:08:34 +08:00
if (oContact)
{
2014-08-21 23:08:34 +08:00
sId = oContact.idContact;
if (Utils.isNonEmptyArray(oContact.properties))
{
_.each(oContact.properties, function (aProperty) {
if (aProperty && aProperty[0])
2013-12-23 08:06:48 +08:00
{
2014-08-21 23:08:34 +08:00
if (Enums.ContactPropertyType.LastName === aProperty[0])
{
sLastName = aProperty[1];
}
else if (Enums.ContactPropertyType.FirstName === aProperty[0])
{
sFirstName = aProperty[1];
}
else
{
aList.push(new ContactPropertyModel(aProperty[0], aProperty[2] || '', aProperty[1]));
}
}
2014-08-21 23:08:34 +08:00
});
}
2014-08-21 23:08:34 +08:00
this.viewReadOnly(!!oContact.readOnly);
}
2014-08-21 23:08:34 +08:00
aList.unshift(new ContactPropertyModel(Enums.ContactPropertyType.LastName, '', sLastName, false,
this.getPropertyPlceholder(Enums.ContactPropertyType.LastName)));
2014-08-21 23:08:34 +08:00
aList.unshift(new ContactPropertyModel(Enums.ContactPropertyType.FirstName, '', sFirstName, !oContact,
this.getPropertyPlceholder(Enums.ContactPropertyType.FirstName)));
2014-08-21 23:08:34 +08:00
this.viewID(sId);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(this.viewProperties());
2014-08-21 23:08:34 +08:00
this.viewProperties([]);
this.viewProperties(aList);
2014-08-21 23:08:34 +08:00
this.watchDirty(false);
this.watchHash(true);
};
2014-08-21 23:08:34 +08:00
/**
* @param {boolean=} bDropPagePosition = false
*/
ContactsPopupView.prototype.reloadContactList = function (bDropPagePosition)
{
var
2014-08-21 23:08:34 +08:00
self = this,
iOffset = (this.contactsPage() - 1) * Consts.Defaults.ContactsPerPage
;
2014-08-21 23:08:34 +08:00
this.bDropPageAfterDelete = false;
if (Utils.isUnd(bDropPagePosition) ? false : !!bDropPagePosition)
{
2014-08-21 23:08:34 +08:00
this.contactsPage(1);
iOffset = 0;
}
this.contacts.loading(true);
Remote.contacts(function (sResult, oData) {
2014-10-04 03:09:13 +08:00
2014-08-21 23:08:34 +08:00
var
iCount = 0,
2014-10-04 03:09:13 +08:00
aList = []
2014-08-21 23:08:34 +08:00
;
if (Enums.StorageResultType.Success === sResult && oData && oData.Result && oData.Result.List)
{
2014-08-21 23:08:34 +08:00
if (Utils.isNonEmptyArray(oData.Result.List))
{
aList = _.map(oData.Result.List, function (oItem) {
var oContact = new ContactModel();
return oContact.parse(oItem) ? oContact : null;
});
2014-08-21 23:08:34 +08:00
aList = _.compact(aList);
2014-08-21 23:08:34 +08:00
iCount = Utils.pInt(oData.Result.Count);
iCount = 0 < iCount ? iCount : 0;
}
}
2014-08-21 23:08:34 +08:00
self.contactsCount(iCount);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(self.contacts());
2014-08-21 23:08:34 +08:00
self.contacts(aList);
2014-10-04 19:58:01 +08:00
self.contacts.loading(false);
2014-08-21 23:08:34 +08:00
self.viewClearSearch('' !== self.search());
2014-08-21 23:08:34 +08:00
}, iOffset, Consts.Defaults.ContactsPerPage, this.search());
};
ContactsPopupView.prototype.onBuild = function (oDom)
2014-08-21 23:08:34 +08:00
{
this.oContentVisible = $('.b-list-content', oDom);
this.oContentScrollable = $('.content', this.oContentVisible);
2014-08-21 23:08:34 +08:00
this.selector.init(this.oContentVisible, this.oContentScrollable, Enums.KeyState.ContactList);
2014-08-21 23:08:34 +08:00
var self = this;
2014-08-21 23:08:34 +08:00
key('delete', Enums.KeyState.ContactList, function () {
self.deleteCommand();
return false;
});
2014-08-21 23:08:34 +08:00
oDom
.on('click', '.e-pagenator .e-page', function () {
var oPage = ko.dataFor(this);
if (oPage)
{
self.contactsPage(Utils.pInt(oPage.value));
self.reloadContactList();
}
})
;
this.initUploader();
};
2015-02-16 05:55:59 +08:00
ContactsPopupView.prototype.onShow = function (bBackToCompose, sLastComposeFocusedField)
2014-08-21 23:08:34 +08:00
{
this.bBackToCompose = Utils.isUnd(bBackToCompose) ? false : !!bBackToCompose;
2015-02-16 05:55:59 +08:00
this.sLastComposeFocusedField = Utils.isUnd(sLastComposeFocusedField) ? '' : sLastComposeFocusedField;
2014-08-21 23:08:34 +08:00
kn.routeOff();
this.reloadContactList(true);
};
ContactsPopupView.prototype.onHide = function ()
2014-08-21 23:08:34 +08:00
{
kn.routeOn();
2015-02-16 05:55:59 +08:00
2014-08-21 23:08:34 +08:00
this.currentContact(null);
this.emptySelection(true);
this.search('');
this.contactsCount(0);
2014-10-04 19:58:01 +08:00
Utils.delegateRunOnDestroy(this.contacts());
2014-08-21 23:08:34 +08:00
this.contacts([]);
2015-02-16 05:55:59 +08:00
this.sLastComposeFocusedField = '';
if (this.bBackToCompose)
{
this.bBackToCompose = false;
kn.showScreenPopup(require('View/Popup/Compose'));
}
2014-08-21 23:08:34 +08:00
};
module.exports = ContactsPopupView;
2014-09-05 06:49:03 +08:00
}());