1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00

Refactored add group.

This commit is contained in:
Thomas Tanghus 2012-11-14 15:27:38 +01:00
parent 099f23af43
commit 749dcad116

181
js/app.js
View File

@ -379,9 +379,9 @@ GroupList.prototype.contactDropped = function(event, ui) {
} }
} }
GroupList.prototype.addGroup = function(name, contacts, cb) { GroupList.prototype.addGroup = function(name, cb) {
console.log('GroupList.addGroup', name); console.log('GroupList.addGroup', name);
contacts = $.map(contacts, function(c) {return parseInt(c)}); contacts = []; // $.map(contacts, function(c) {return parseInt(c)});
var self = this, exists = false; var self = this, exists = false;
self.$groupList.find('h3[data-type="category"]').each(function() { self.$groupList.find('h3[data-type="category"]').each(function() {
if ($(this).data('name').toLowerCase() === name.toLowerCase()) { if ($(this).data('name').toLowerCase() === name.toLowerCase()) {
@ -391,7 +391,7 @@ GroupList.prototype.addGroup = function(name, contacts, cb) {
}); });
if(exists) { if(exists) {
if(typeof cb === 'function') { if(typeof cb === 'function') {
cb({result:'error', message:t('contacts', 'A group named {group} already exists', {group: name})}); cb({status:'error', message:t('contacts', 'A group named {group} already exists', {group: name})});
} }
return; return;
} }
@ -418,14 +418,14 @@ GroupList.prototype.addGroup = function(name, contacts, cb) {
} }
}); });
if(!added) { if(!added) {
$elem.appendTo(self.$groupList); $elem.insertAfter(self.$groupList.find('h3.group[data-type="category"]').last());
} }
if(typeof cb === 'function') { if(typeof cb === 'function') {
cb({result:'success', id:jsondata.data.id, name:name}); cb({status:'success', id:parseInt(jsondata.data.id), name:name});
} }
} else { } else {
if(typeof cb === 'function') { if(typeof cb === 'function') {
cb({result:'error', message:jsondata.data.message}); cb({status:'error', message:jsondata.data.message});
} }
} }
}); });
@ -873,6 +873,13 @@ OC.Contacts = OC.Contacts || {
self.uploadPhoto(this.files); self.uploadPhoto(this.files);
}); });
$('#groupactions > .addgroup').on('click keydown',function(event) {
if(wrongKey(event)) {
return;
}
self.addGroup();
});
this.$ninjahelp.find('.close').on('click keydown',function(event) { this.$ninjahelp.find('.close').on('click keydown',function(event) {
if(wrongKey(event)) { if(wrongKey(event)) {
return; return;
@ -907,60 +914,9 @@ OC.Contacts = OC.Contacts || {
}); });
this.$groups.on('change', function() { this.$groups.on('change', function() {
// TODO: This should go in separate method
var $opt = $(this).find('option:selected'); var $opt = $(this).find('option:selected');
var action = $opt.parent().data('action'); var action = $opt.parent().data('action');
var ids, buildnow = false; var ids, groupName, groupId, buildnow = false;
if($opt.val() === 'add') {
console.log('add group...');
self.$groups.val(-1);
if(!this.$addGroupTmpl) {
this.$addGroupTmpl = $('#addGroupTemplate');
}
$('body').append('<div id="add_group_dialog"></div>');
var $dlg = this.$addGroupTmpl.octemplate();
$('#add_group_dialog').html($dlg).dialog({
modal: true,
closeOnEscape: true,
title: t('contacts', 'Add group'),
height: 'auto', width: 'auto',
buttons: {
'Ok':function() {
var contacts = self.Contacts.getSelectedContacts();
self.Groups.addGroup(
$dlg.find('input:text').val(),
contacts,
function(response) {
if(response.result === 'success') {
for(var id in contacts) {
if(typeof contacts[id] === 'number') {
console.log('add', contacts[id], 'to', response.name);
self.Contacts.contacts[contacts[id]].addToGroup(response.name);
}
}
} else {
OC.notify({message: response.message});
}
});
$(this).dialog('close');
},
'Cancel':function() { $(this).dialog('close'); }
},
close: function(event, ui) {
$(this).dialog('destroy').remove();
$('#add_group_dialog').remove();
$.each(self.$contactList.find('input:checkbox:checked'), function() {
console.log('unchecking', $(this));
$(this).prop('checked', false);
});
},
open: function(event, ui) {
$dlg.find('input').focus();
},
});
return;
}
// If a contact is open the action is only applied to that, // If a contact is open the action is only applied to that,
// otherwise on all selected items. // otherwise on all selected items.
@ -970,33 +926,76 @@ OC.Contacts = OC.Contacts || {
} else { } else {
ids = self.Contacts.getSelectedContacts(); ids = self.Contacts.getSelectedContacts();
} }
console.log('ids', ids);
$.each(self.$contactList.find('input:checkbox:checked'), function() {
$(this).prop('checked', false);
});
if($opt.val() === 'add') { // Add new group
action = 'add';
console.log('add group...');
self.$groups.val(-1);
self.addGroup(function(response) {
if(response.status === 'success') {
groupId = response.id;
groupName = response.name;
self.Groups.addTo(ids, groupId, function(result) {
if(result.status === 'success') {
$.each(ids, function(idx, id) {
// Delay each contact to not trigger too many ajax calls
// at a time.
setTimeout(function() {
self.Contacts.contacts[id].addToGroup(groupName);
// I don't think this is used...
if(buildnow) {
self.buildGroupSelect();
}
$(document).trigger('status.contact.addedtogroup', {
contactid: id,
groupid: groupId,
groupname: groupName,
});
}, 1000);
});
} else {
// TODO: Use message return from Groups object.
OC.notify({message:t('contacts', t('contacts', 'Error adding to group.'))});
}
});
} else {
OC.notify({message: response.message});
}
});
return;
}
groupName = $opt.text(), groupId = $opt.val();
console.log('trut', groupName, groupId);
if(action === 'add') { if(action === 'add') {
self.Groups.addTo(ids, $opt.val(), function(result) { self.Groups.addTo(ids, $opt.val(), function(result) {
console.log('after add', result); console.log('after add', result);
if(result.status === 'success') { if(result.status === 'success') {
//console.log('typeof', typeof parseInt(id), id);
var groupname = $opt.text(), groupid = $opt.val();
$.each(result.ids, function(idx, id) { $.each(result.ids, function(idx, id) {
// Delay each contact to not trigger too many ajax calls // Delay each contact to not trigger too many ajax calls
// at a time. // at a time.
setTimeout(function() { setTimeout(function() {
console.log('adding', id, 'to', groupname); console.log('adding', id, 'to', groupName);
self.Contacts.contacts[id].addToGroup(groupname); self.Contacts.contacts[id].addToGroup(groupName);
// I don't think this is used... // I don't think this is used...
if(buildnow) { if(buildnow) {
self.buildGroupSelect(); self.buildGroupSelect();
} }
$(document).trigger('status.contact.addedtogroup', { $(document).trigger('status.contact.addedtogroup', {
contactid: id, contactid: id,
groupid: groupid, groupid: groupId,
groupname: groupname, groupname: groupName,
}); });
}, 1000); }, 1000);
}); });
} else { } else {
// TODO: Use message return from Groups object. var msg = result.message ? result.message : t('contacts', 'Error adding to group.');
OC.notify({message:t('contacts', t('contacts', 'Error adding to group.'))}); OC.notify({message:msg});
} }
}); });
if(!buildnow) { if(!buildnow) {
@ -1015,12 +1014,13 @@ OC.Contacts = OC.Contacts || {
// If a group is selected the contact has to be removed from the list // If a group is selected the contact has to be removed from the list
$(document).trigger('status.contact.removedfromgroup', { $(document).trigger('status.contact.removedfromgroup', {
contactid: id, contactid: id,
groupid: groupid, groupid: groupId,
groupname: groupname, groupname: groupName,
}); });
}); });
} else { } else {
OC.notify({message:t('contacts', t('contacts', 'Error removing from group.'))}); var msg = result.message ? result.message : t('contacts', 'Error removing from group.');
OC.notify({message:msg});
} }
}); });
if(!buildnow) { if(!buildnow) {
@ -1205,6 +1205,47 @@ OC.Contacts = OC.Contacts || {
$('#content > [title]').tipsy(); // find all with a title attribute and tipsy them $('#content > [title]').tipsy(); // find all with a title attribute and tipsy them
}, },
addGroup: function(cb) {
var self = this;
$('body').append('<div id="add_group_dialog"></div>');
if(!this.$addGroupTmpl) {
this.$addGroupTmpl = $('#addGroupTemplate');
}
var $dlg = this.$addGroupTmpl.octemplate();
$('#add_group_dialog').html($dlg).dialog({
modal: true,
closeOnEscape: true,
title: t('contacts', 'Add group'),
height: 'auto', width: 'auto',
buttons: {
'Ok':function() {
self.Groups.addGroup(
$dlg.find('input:text').val(),
function(response) {
if(typeof cb === 'function') {
cb(response);
} else {
if(response.status !== 'success') {
OC.notify({message: response.message});
}
}
});
$(this).dialog('close');
},
'Cancel':function() {
$(this).dialog('close');
return false;
}
},
close: function(event, ui) {
$(this).dialog('destroy').remove();
$('#add_group_dialog').remove();
},
open: function(event, ui) {
$dlg.find('input').focus();
},
});
},
jumpToContact: function(id) { jumpToContact: function(id) {
this.$rightContent.scrollTop(this.Contacts.contactPos(id)); this.$rightContent.scrollTop(this.Contacts.contactPos(id));
}, },