Fixed operations on addressbooks using new (in Roundcube 1.0) treelist widget (Bug #1766)

This commit is contained in:
Aleksander Machniak 2013-05-28 15:52:24 +02:00
parent 014a04d641
commit b1e5827e48

View file

@ -84,125 +84,137 @@ rcube_webmail.prototype.book_save = function()
}; };
// action executed after book delete // action executed after book delete
rcube_webmail.prototype.book_delete_done = function(id) rcube_webmail.prototype.book_delete_done = function(id, recur)
{ {
var n, g, li = this.get_folder_li(id), groups = this.env.contactgroups; var n, groups = this.env.contactgroups,
sources = this.env.address_sources,
olddata = sources[id];
this.treelist.remove(id);
// remove folder and its groups rows
for (n in groups) for (n in groups)
if (groups[n].source == id && (g = this.get_folder_li(n))) { if (groups[n].source == id) {
$(g).remove();
delete this.env.contactgroups[n]; delete this.env.contactgroups[n];
delete this.env.contactfolders[n];
} }
$(li).remove();
delete this.env.address_sources[id]; delete this.env.address_sources[id];
delete this.env.contactfolders[id]; delete this.env.contactfolders[id];
if (recur)
return;
// remove subfolders
olddata.realname += this.env.delimiter;
for (n in sources)
if (sources[n].realname && sources[n].realname.indexOf(olddata.realname) == 0)
this.book_delete_done(n, true);
}; };
// action executed after book create/update // action executed after book create/update
rcube_webmail.prototype.book_update = function(data, old) rcube_webmail.prototype.book_update = function(data, old, recur)
{ {
var n, i, id, len, link, row, refrow, olddata, name = '', realname = '', sources, level, var n, i, id, len, link, row, prop, olddata, oldid, name, sources, level,
folders = [], class_name = 'addressbook', folders = [], classes = ['addressbook'],
list = this.gui_objects.folderlist,
groups = this.env.contactgroups; groups = this.env.contactgroups;
this.env.contactfolders[data.id] = this.env.address_sources[data.id] = data; this.env.contactfolders[data.id] = this.env.address_sources[data.id] = data;
this.show_contentframe(false); this.show_contentframe(false);
// update // update (remove old row)
if (old && old != data.id) { if (old && old != data.id) {
olddata = this.env.address_sources[old]; olddata = this.env.address_sources[old];
delete this.env.address_sources[old]; delete this.env.address_sources[old];
delete this.env.contactfolders[old]; delete this.env.contactfolders[old];
refrow = $('#rcmli'+old); this.treelist.remove(old);
} }
sources = this.env.address_sources; sources = this.env.address_sources;
// set row attributes // set row attributes
if (data.readonly) if (data.readonly)
class_name += ' readonly'; classes.push('readonly');
if (data.class_name) if (data.class_name)
class_name += ' '+data.class_name; classes.push(data.class_name);
// updated currently selected book // updated currently selected book
if (this.env.source != '' && this.env.source == old) { if (this.env.source != '' && this.env.source == old) {
class_name += ' selected'; classes.push('selected');
this.env.source = data.id; this.env.source = data.id;
} }
link = $('<a>').attr({'href': '#', 'rel': data.id}).html(data.name) link = $('<a>').html(data.name)
.click(function() { return rcmail.command('list', data.id, this); }); .attr({
row = $('<li>').attr({id: 'rcmli'+data.id, 'class': class_name}) href: '#', rel: data.id,
.append(link); onclick: "return rcmail.command('list', '" + data.id + "', this)"
});
// sort kolab folders, to put the new one in order // add row at the end of the list
for (n in sources) // treelist widget is not very smart, we need
if (sources[n].kolab && (name = sources[n].realname)) // to do sorting and add groups list by ourselves
folders.push(name); this.treelist.insert({id: data.id, html:link, classes: classes, childlistclass: 'groups'}, '', false);
row = $(this.treelist.get_item(data.id));
row.append($('<ul class="groups">').hide());
// we need to sort rows because treelist can't sort by property
$.each(sources, function(i, v) {
if (v.kolab && v.realname)
folders.push(v.realname);
});
folders.sort(); folders.sort();
// find current id
for (n=0, len=folders.length; n<len; n++) for (n=0, len=folders.length; n<len; n++)
if (folders[n] == data.realname) if (folders[n] == data.realname)
break; break;
// add row // find the row before and re-insert after it
if (n && n < len) { if (n && n < len - 1) {
// find the row before
name = folders[n-1]; name = folders[n-1];
for (n in sources) for (n in sources)
if (sources[n].realname && sources[n].realname == name) { if (sources[n].realname && sources[n].realname == name) {
// folder row found row.detach().insertAfter(this.treelist.get_item(n));
n = $('#rcmli'+n);
// skip groups
while (n.next().hasClass('contactgroup'))
n = n.next();
row.insertAfter(n);
break; break;
} }
} }
else if (olddata) {
row.insertBefore(refrow);
}
else {
row.appendTo(list);
}
if (olddata) { if (olddata) {
// remove old row (just after the new row has been inserted)
refrow.remove();
// update groups // update groups
for (n in groups) { for (n in groups) {
if (groups[n].source == old) { if (groups[n].source == old) {
// update existing row prop = groups[n];
refrow = $('#rcmli'+n); prop.type = 'group';
refrow.remove().attr({id: 'rcmliG'+data.id+groups[n].id}); prop.source = data.id;
$('a', refrow).removeAttr('onclick').unbind() id = 'G' + prop.source + prop.id;
.click({source: data.id, id: groups[n].id}, function(e) {
return rcmail.command('listgroup', {'source': e.data.source, 'id': e.data.id}, this);
});
refrow.insertAfter(row);
row = refrow;
// update group data link = $('<a>').text(prop.name)
groups[n].source = data.id; .attr({
this.env.contactgroups['G'+data.id+groups[n].id] = groups[n]; href: '#', rel: prop.source + ':' + prop.id,
onclick: "return rcmail.command('listgroup', {source: '"+prop.source+"', id: '"+prop.id+"'}, this)"
});
this.treelist.insert({id:id, html:link, classes:['contactgroup']}, prop.source, true);
this.env.contactfolders[id] = this.env.contactgroups[id] = prop;
delete this.env.contactgroups[n]; delete this.env.contactgroups[n];
delete this.env.contactfolders[n];
} }
} }
old += '-'; if (recur)
return;
// update subfolders
old += '_';
level = olddata.realname.split(this.env.delimiter).length - data.realname.split(this.env.delimiter).length; level = olddata.realname.split(this.env.delimiter).length - data.realname.split(this.env.delimiter).length;
// update (realname and ID of) subfolders olddata.realname += this.env.delimiter;
for (n in sources) { for (n in sources) {
if (n != data.id && n.indexOf(old) == 0) { if (sources[n].realname && sources[n].realname.indexOf(olddata.realname) == 0) {
prop = sources[n];
oldid = sources[n].id;
// new ID // new ID
id = data.id + '-' + n.substr(old.length); prop.id = data.id + '_' + n.substr(old.length);
name = sources[n].name; prop.realname = data.realname + prop.realname.substr(olddata.realname.length - 1);
realname = data.realname + sources[n].realname.substr(olddata.realname.length); name = prop.name;
// update display name // update display name
if (level > 0) { if (level > 0) {
@ -214,43 +226,8 @@ rcube_webmail.prototype.book_update = function(data, old)
name = '&nbsp;&nbsp;' + name; name = '&nbsp;&nbsp;' + name;
} }
// update existing row prop.name = name;
refrow = $('#rcmli'+n); this.book_update(prop, oldid, true)
refrow.remove().attr({id: 'rcmli'+id});
$('a', refrow).html(name).removeAttr('onclick').unbind().attr({rel: id, href: '#'})
.click({id: id}, function(e) { return rcmail.command('list', e.data.id, this); });
// move the row to the new place
refrow.insertAfter(row);
row = refrow;
// update list data
sources[n].id = id;
sources[n].name = name;
sources[n].realname = realname;
this.env.address_sources[id] = this.env.contactfolders[id] = sources[n];
delete this.env.address_sources[n];
delete this.env.contactfolders[n];
// update groups
for (i in groups) {
if (groups[i].source == n) {
// update existing row
refrow = $('#rcmli'+i);
refrow.remove().attr({id: 'rcmliG'+id+groups[i].id});
$('a', refrow).removeAttr('onclick').unbind()
.click({source: id, id: groups[i].id}, function(e) {
return rcmail.command('listgroup', {'source': e.data.source, 'id': e.data.id}, this);
});
refrow.insertAfter(row);
row = refrow;
// update group data
groups[i].source = id;
this.env.contactgroups['G'+id+groups[i].id] = groups[i];
delete this.env.contactgroups[i];
}
}
} }
} }
} }