Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17523 from arcturus/multipledelete2
Browse files Browse the repository at this point in the history
Bug_956219 Multiple Contact Delete
  • Loading branch information
arcturus committed Mar 24, 2014
2 parents 32078fe + 0e093ae commit ba7d1fe
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 9 deletions.
10 changes: 10 additions & 0 deletions apps/communications/contacts/elements/settings.html 100644 → 100755
Expand Up @@ -58,6 +58,16 @@ <h2 data-l10n-id="facebook">Facebook</h2>
<p id="no-connection" data-l10n-id="noConnection11"></p>
</li>
</ul>
<header>
<h2 data-l10n-id="ContactManagementTitle">Contacts Management</h2>
</header>
<ul data-type="list" id="contactsManagement">
<li>
<button id="bulkDelete" class="fillflow-row action action-delete danger">
<b data-l10n-id="bulkDeleteButton">Bulk delete</b>
</button>
</li>
</ul>
</section>
</article>
</section>
Expand Down
5 changes: 5 additions & 0 deletions apps/communications/contacts/js/contacts.js 100644 → 100755
Expand Up @@ -909,6 +909,10 @@ var Contacts = (function() {
load('utilities', utility, callback);
}

var updateSelectCountTitle = function updateSelectCountTitle(count) {
appTitleElement.textContent = _('SelectedTxt', {n: count});
};

return {
'goBack' : handleBack,
'cancel': handleCancel,
Expand Down Expand Up @@ -936,6 +940,7 @@ var Contacts = (function() {
'close': close,
'view': loadView,
'utility': loadUtility,
'updateSelectCountTitle': updateSelectCountTitle,
get asyncScriptsLoaded() {
return asyncScriptsLoaded;
}
Expand Down
94 changes: 94 additions & 0 deletions apps/communications/contacts/js/contacts_bulk_delete.js
@@ -0,0 +1,94 @@
/* globals Contacts, _, utils, contactsRemover */
'use strict';

var contacts = window.contacts || {};

contacts.BulkDelete = (function() {

var cancelled = false;

/**
* Loads the overlay class before showing
*/
function requireOverlay(callback) {
Contacts.utility('Overlay', callback);
}

// Shows a dialog to confirm the bulk delete
var showConfirm = function showConfirm(n) {
var response = confirm(_('ContactConfirmDel', {n: n}));
return response;
};

var doDelete = function doDelete(ids) {
cancelled = false;
var progress = utils.overlay.show(_('DeletingContacts'), 'progressBar');
progress.setTotal(ids.length);
utils.overlay.showMenu();

utils.overlay.oncancel = function() {
cancelled = true;
contactsRemoverObj.finish();
};

var contactsRemoverObj = new contactsRemover();
contactsRemoverObj.init(ids, function onInitDone() {
contactsRemoverObj.start();
});

contactsRemoverObj.onDeleted = function onDeleted(currentId) {
if (contacts.Search && contacts.Search.isInSearchMode()) {
contacts.Search.invalidateCache();
contacts.Search.removeContact(currentId);
}
contacts.List.remove(currentId);
progress.update();
};

contactsRemoverObj.onCancelled = function onCancelled() {
Contacts.hideOverlay();
Contacts.showStatus(_('DeletedTxt',
{n: contactsRemoverObj.getDeletedCount()}));
contacts.Settings.refresh();
};

contactsRemoverObj.onError = function onError() {
Contacts.hideOverlay();
Contacts.showStatus(_('deleteError-general'));
contacts.Settings.refresh();
};

contactsRemoverObj.onFinished = function onFinished() {
Contacts.hideOverlay();
Contacts.showStatus(_('DeletedTxt',
{n: contactsRemoverObj.getDeletedCount()}));
contacts.Settings.refresh();
};

};
// Start the delete of the contacts
var performDelete = function performDelete(promise) {
requireOverlay(function onOverlay() {
utils.overlay.show(_('preparing-contacts'), 'spinner');
promise.onsuccess = function onSucces(ids) {
Contacts.hideOverlay();
var confirmDelete = showConfirm(ids.length);
if (confirmDelete) {
doDelete(ids);
} else {
Contacts.showStatus(_('BulkDelCancel'));
}
};
promise.onerror = function onError() {
Contacts.hideOverlay();
};
});
};

return {
'performDelete': performDelete,
'doDelete': doDelete
};

})();

113 changes: 113 additions & 0 deletions apps/communications/contacts/js/contacts_remover.js
@@ -0,0 +1,113 @@
/* globals mozContact, fb */
'use strict';

/*
* Contacts Remover delete the contacts present in phone.
* - start: To initiate the deleting process.
* - finish: To cancel the deleting process.
* - onDeleted: A Contact has been deleted
* - onFinished: All Selected contacts have been deleted;
* - onError: Error while deleting contacts.
*/

function contactsRemover() {

var totalRemoved = 0;
var totalSelected = 0;
var cancelled = false;
/*jshint validthis:true */
var self = this;
var ids;

this.init = function(cIds, cb) {
if (cIds === null || cIds.length === 0) {
return;
}
ids = cIds;
cb();
};

this.start = function() {
totalRemoved = 0;
cancelled = false;
totalSelected = ids.length;
continueCb(ids);
};

this.finish = function() {
cancelled = true;
};

this.getDeletedCount = function() {
return totalRemoved;
};

function continueCb(ids) {
var currentId = ids.shift();
if (!currentId || cancelled) {
if (totalRemoved === totalSelected) {
notifyFinished();
}
else {
notifyCancelled();
}
return;
}
var request = deleteContact(currentId);
request.onSuccess = function() {
totalRemoved++;
notifyDeleted(currentId);
continueCb(ids);
};
request.onError = function() {
notifyError();
};
}

function notifyError() {
if (typeof self.onError === 'function') {
self.onError();
}
}

function notifyCancelled() {
if (typeof self.onCancelled === 'function') {
window.setTimeout(self.onCancelled, 100);
}
}

function notifyFinished() {
if (typeof self.onFinished === 'function') {
window.setTimeout(self.onFinished, 100);
}
}

function notifyDeleted(currentId) {
if (typeof self.onDeleted === 'function') {
self.onDeleted(currentId);
}
}

function deleteContact(currentID) {
var request;
var outreq = {onSuccess: null, onError: null};
var contact = new mozContact();
contact.id = currentID;

if (fb.isFbContact(contact)) {
var fbContact = new fb.Contact(contact);
request = fbContact.remove();
} else {
request = navigator.mozContacts.remove(contact);
}
request.onsuccess = function() {
outreq.onSuccess();
};
request.onerror = function() {
outreq.onError();
};
return outreq;
}
}

window.contactsRemover = contactsRemover;
5 changes: 5 additions & 0 deletions apps/communications/contacts/js/views/list.js 100644 → 100755
Expand Up @@ -1417,6 +1417,7 @@ contacts.List = (function() {
ones are selected.
*/
var selectAction = function selectAction(action) {
updateSelectCount(0);
if (action == null) {
exitSelectMode();
return;
Expand Down Expand Up @@ -1498,6 +1499,7 @@ contacts.List = (function() {
selectActionButton.disabled = currentlySelected == 0;
selectAll.disabled = selectAllDisabled;
deselectAll.disabled = deselectAllDisabled;
updateSelectCount(currentlySelected);
};

/*
Expand Down Expand Up @@ -1802,6 +1804,9 @@ contacts.List = (function() {
}
}
};
function updateSelectCount(count) {
Contacts.updateSelectCountTitle(count);
};

return {
'init': init,
Expand Down
41 changes: 35 additions & 6 deletions apps/communications/contacts/js/views/settings.js 100644 → 100755
Expand Up @@ -34,7 +34,8 @@ contacts.Settings = (function() {
newOrderByLastName = null,
ORDER_KEY = 'order.lastname',
PENDING_LOGOUT_KEY = 'pendingLogout',
umsSettingsKey = 'ums.enabled';
umsSettingsKey = 'ums.enabled',
bulkDeleteButton;

// Initialise the settings screen (components, listeners ...)
var init = function initialize() {
Expand Down Expand Up @@ -111,7 +112,7 @@ contacts.Settings = (function() {
window.addEventListener('message', function updateList(e) {
if (e.data.type === 'import_updated') {
updateTimestamps();
checkExport();
checkNoContacts();
}
});

Expand All @@ -135,6 +136,9 @@ contacts.Settings = (function() {
exportOptions = document.getElementById('export-options');
exportOptions.addEventListener('click', exportOptionsHandler);

// Bulk delete
bulkDeleteButton = document.getElementById('bulkDelete');
bulkDeleteButton.addEventListener('click', bulkDeleteHandler);
if (fb.isEnabled) {
fbImportOption = document.querySelector('#settingsFb');
document.querySelector('#settingsFb > .fb-item').onclick = onFbEnable;
Expand Down Expand Up @@ -228,6 +232,28 @@ contacts.Settings = (function() {
}
};

var bulkDeleteHandler = function bulkDeleteHandler() {
LazyLoader.load(
[
'/contacts/js/contacts_bulk_delete.js',
'/contacts/js/contacts_remover.js'
],
function() {
Contacts.view('search', function() {
contacts.List.selectFromList(_('DeleteTitle'),
function onSelectedContacts(promise) {
contacts.List.exitSelectMode();
contacts.BulkDelete.performDelete(promise);
},
null,
navigationHandler,
'popup'
);
});
}
);
};

function exportOptionsHandler(e) {
var source = getSource(e);
switch (source) {
Expand Down Expand Up @@ -653,7 +679,7 @@ contacts.Settings = (function() {
window.importUtils.setTimestamp(source, function() {
// Once the timestamp is saved, update the list
updateTimestamps();
checkExport();
checkNoContacts();
});
}
if (!cancelled) {
Expand Down Expand Up @@ -750,7 +776,7 @@ contacts.Settings = (function() {
window.importUtils.setTimestamp('sd', function() {
// Once the timestamp is saved, update the list
updateTimestamps();
checkExport();
checkNoContacts();
resetWait(wakeLock);
if (!cancelled) {
Contacts.showStatus(_('memoryCardContacts-imported3', {
Expand Down Expand Up @@ -838,15 +864,17 @@ contacts.Settings = (function() {
updateOptionStatus(importLiveOption, !navigator.onLine, true);
};

var checkExport = function checkExport() {
var checkNoContacts = function checkNoContacts() {
var exportButton = exportContacts.firstElementChild;
var req = navigator.mozContacts.getCount();
req.onsuccess = function() {
if (req.result === 0) {
exportButton.setAttribute('disabled', 'disabled');
bulkDeleteButton.setAttribute('disabled', 'disabled');
}
else {
exportButton.removeAttribute('disabled');
bulkDeleteButton.removeAttribute('disabled');
}
};

Expand All @@ -855,6 +883,7 @@ contacts.Settings = (function() {
req.error.name);
// In case of error is safer to leave enabled
exportButton.removeAttribute('disabled');
bulkDeleteButton.removeAttribute('disabled');
};
};

Expand Down Expand Up @@ -932,7 +961,7 @@ contacts.Settings = (function() {
checkSIMCard();
enableStorageOptions(utils.sdcard.checkStorageCard());
updateTimestamps();
checkExport();
checkNoContacts();
};

return {
Expand Down

0 comments on commit ba7d1fe

Please sign in to comment.