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

Maintain data structure client side.

This commit is contained in:
Thomas Tanghus 2012-10-24 20:30:41 +02:00
parent c7bc0e39d5
commit ef0ca10c60

View File

@ -31,11 +31,12 @@ OC.Contacts = OC.Contacts || {};
return; return;
} }
$(obj).prop('disabled', state); $(obj).prop('disabled', state);
if(state) { $(obj).toggleClass('loading', state);
/*if(state) {
$(obj).addClass('loading'); $(obj).addClass('loading');
} else { } else {
$(obj).removeClass('loading'); $(obj).removeClass('loading');
} }*/
} }
Contact.prototype.addProperty = function($option, name) { Contact.prototype.addProperty = function($option, name) {
@ -98,8 +99,19 @@ OC.Contacts = OC.Contacts || {};
return false; return false;
} }
if(jsondata.status == 'success') { if(jsondata.status == 'success') {
// TODO: Remove from internal data structure // TODO: Test if removing from internal data structure works
if(self.multi_properties.indexOf(element) !== -1) { if(self.multi_properties.indexOf(element) !== -1) {
// First find out if an existing element by looking for checksum
var checksum = self.checksumFor(obj);
if(checksum) {
for(var i in self.data[element]) {
if(self.data[element][i].checksum === checksum) {
// Found it
delete self.data[element][i];
break;
}
}
}
$container.remove(); $container.remove();
} else { } else {
self.setAsSaving(obj, false); self.setAsSaving(obj, false);
@ -160,10 +172,66 @@ OC.Contacts = OC.Contacts || {};
self.data[element] = []; self.data[element] = [];
} }
if(self.multi_properties.indexOf(element) !== -1) { if(self.multi_properties.indexOf(element) !== -1) {
// First find out if an existing element by looking for checksum
var checksum = self.checksumFor(obj);
if(checksum) {
for(var i in self.data[element]) {
if(self.data[element][i].checksum === checksum) {
self.data[element][i] = {
name: element,
value: self.valueFor(obj),
parameters: self.parametersFor(obj),
checksum: jsondata.data.checksum,
}
break;
}
}
} else {
self.data[element].push({
name: element,
value: self.valueFor(obj),
parameters: self.parametersFor(obj),
checksum: jsondata.data.checksum,
});
}
self.propertyContainerFor(obj).data('checksum', jsondata.data.checksum); self.propertyContainerFor(obj).data('checksum', jsondata.data.checksum);
} else { } else {
// TODO: Save value and parameters internally // Save value and parameters internally
self.data[element][0] = {name: element}; var value = self.valueFor(obj);
switch(element) {
case 'CATEGORIES': // We deal with this in addToGroup()
break;
case 'FN':
var nempty = true;
if(!self.data.N) {
self.data.N = [];
}
for(var i in self.data.N[0]['value']) {
if(self.data.N[0]['value'][i] != '') {
nempty = false;
break;
}
}
if(nempty) {
self.N[0]['value'] = [value, '', '', '', ''];
setTimeout(function() {
self.saveProperty({name:'N', value:this.data.N[0].value.join(';')})}
, 500);
}
case 'NICKNAME':
case 'BDAY':
case 'ORG':
case 'TITLE':
self.data[element][0] = {
name: element,
value: value,
parameters: self.parametersFor(obj),
checksum: jsondata.data.checksum,
};
break;
default:
break;
}
} }
self.setAsSaving(obj, false); self.setAsSaving(obj, false);
return true; return true;
@ -182,7 +250,7 @@ OC.Contacts = OC.Contacts || {};
* Remove any open contact from the DOM. * Remove any open contact from the DOM.
*/ */
Contact.prototype.close = function() { Contact.prototype.close = function() {
console.log('Closing', this); console.log('Contact.close', this);
if(this.$fullelem) { if(this.$fullelem) {
this.$fullelem.remove(); this.$fullelem.remove();
} }
@ -287,6 +355,20 @@ OC.Contacts = OC.Contacts || {};
return this.propertyContainerFor(obj).find('input.value').val(); return this.propertyContainerFor(obj).find('input.value').val();
} }
Contact.prototype.parametersFor = function(obj) {
var parameters = [];
$.each(this.propertyContainerFor(obj).find('select.parameter,input:checkbox:checked.parameter,textarea'), function(i, elem) {
var $elem = $(elem);
var paramname = $elem.data('parameter');
if(!parameters[paramname]) {
parameters[paramname] = [];
}
parameters[paramname].push($elem.val());
});
console.log('Contact.parametersFor', parameters);
return parameters;
}
Contact.prototype.propertyTypeFor = function(obj) { Contact.prototype.propertyTypeFor = function(obj) {
var ptype = this.propertyContainerFor(obj).data('element'); var ptype = this.propertyContainerFor(obj).data('element');
return ptype ? ptype.toUpperCase() : null; return ptype ? ptype.toUpperCase() : null;
@ -343,6 +425,19 @@ OC.Contacts = OC.Contacts || {};
self.addProperty($opt, $(this).val()); self.addProperty($opt, $(this).val());
$(this).val(''); $(this).val('');
}); });
var $fullname = this.$fullelem.find('.fullname');
this.$fullelem.find('.singleproperties').on('mouseenter', function() {
$fullname.next('.edit').css('opacity', '1');
}).on('mouseleave', function() {
$fullname.next('.edit').css('opacity', '0');
});
$fullname.next('.edit').on('click keydown', function(event) {
console.log('edit name', event);
$('.tipsy').remove();
if(wrongKey(event)) {
return;
}
});
var $singleelements = this.$fullelem.find('dd.propertycontainer'); var $singleelements = this.$fullelem.find('dd.propertycontainer');
$singleelements.find('.action').css('opacity', '0'); $singleelements.find('.action').css('opacity', '0');
$singleelements.on('mouseenter', function() { $singleelements.on('mouseenter', function() {
@ -384,11 +479,12 @@ OC.Contacts = OC.Contacts || {};
$favstar.removeClass('wait'); $favstar.removeClass('wait');
if(result.status === 'success') { if(result.status === 'success') {
self.is_favorite = result.state; self.is_favorite = result.state;
if(result.state === true) { $favstar.toggleClass('inactive', !result.state).toggleClass('active', result.state);
/*if(result.state === true) {
$favstar.removeClass('inactive').addClass('active'); $favstar.removeClass('inactive').addClass('active');
} else { } else {
$favstar.removeClass('active').addClass('inactive'); $favstar.removeClass('active').addClass('inactive');
} }*/
} else { } else {
// TODO:... // TODO:...
} }
@ -800,20 +896,19 @@ OC.Contacts = OC.Contacts || {};
* Show/hide contacts belonging to an addressbook. * Show/hide contacts belonging to an addressbook.
* @param int aid. Addressbook id. * @param int aid. Addressbook id.
* @param boolean show. Whether to show or hide. * @param boolean show. Whether to show or hide.
* @param boolean hideothers. Used when showing shared addressbook as a group.
*/ */
ContactList.prototype.showFromAddressbook = function(aid, show) { ContactList.prototype.showFromAddressbook = function(aid, show, hideothers) {
console.log('ContactList.showFromAddressbook', aid, show); console.log('ContactList.showFromAddressbook', aid, show);
aid = parseInt(aid); aid = parseInt(aid);
for(var contact in this.contacts) { for(var contact in this.contacts) {
if(this.contacts[contact].access.aid === aid) { if(this.contacts[contact].access.aid === aid) {
if(show) { this.contacts[contact].getListItemElement().toggle(show);
this.contacts[contact].getListItemElement().show(); } else if(hideothers) {
} else {
this.contacts[contact].getListItemElement().hide(); this.contacts[contact].getListItemElement().hide();
} }
} }
} }
}
/** /**
* Show/hide contacts belonging to shared addressbooks. * Show/hide contacts belonging to shared addressbooks.
@ -1082,7 +1177,7 @@ OC.Contacts = OC.Contacts || {};
// Should the actual ajax call be in the controller? // Should the actual ajax call be in the controller?
$.getJSON(OC.filePath('contacts', 'ajax', 'contact/list.php'), {offset: offset}, function(jsondata) { $.getJSON(OC.filePath('contacts', 'ajax', 'contact/list.php'), {offset: offset}, function(jsondata) {
if (jsondata && jsondata.status == 'success') { if (jsondata && jsondata.status == 'success') {
console.log('addressbooks', jsondata.data.addressbooks); console.log('ContactList.loadContacts', jsondata.data);
self.addressbooks = {}; self.addressbooks = {};
$.each(jsondata.data.addressbooks, function(i, book) { $.each(jsondata.data.addressbooks, function(i, book) {
self.addressbooks[parseInt(book.id)] = { self.addressbooks[parseInt(book.id)] = {
@ -1103,8 +1198,15 @@ OC.Contacts = OC.Contacts || {};
self.contactDetailTemplates self.contactDetailTemplates
); );
self.length +=1; self.length +=1;
var item = self.contacts[parseInt(contact.id)].renderListItem() var $item = self.contacts[parseInt(contact.id)].renderListItem();
self.$contactList.append(item); $item.draggable({
distance: 10,
revert: 'invalid',
//containment: '#content',
opacity: 0.8, helper: 'clone',
zIndex: 1000,
});
self.$contactList.append($item);
//self.insertContact(item); //self.insertContact(item);
}); });
self.doSort(); self.doSort();