Skip to content

Add contact to address book when composing mail. #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modoboa_webmail/lib/imapemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ def _insert_contact_links(self, addresses):
"""Insert 'add to address book' links."""
result = []
title = _("Add to contacts")
url = reverse("api:contact-list")
link_tpl = (
" <a class='addcontact' href='{}' title='{}'>"
" <a class='addcontact' href='#' title='{}'>"
"<span class='fa fa-vcard'></span></a>"
)
for address in addresses:
address += link_tpl.format(url, title)
address += link_tpl.format(title)
result.append(address)
return result

Expand Down
48 changes: 25 additions & 23 deletions modoboa_webmail/static/modoboa_webmail/js/webmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Webmail.prototype = {
ro_mboxes: ["INBOX"],
trash: "",
hdelimiter: '.',
mboxes_col_width: 200
mboxes_col_width: 200,
contactListUrl: null,
contactAddUrl: null
},

initialize: function(options) {
Expand Down Expand Up @@ -1145,11 +1147,12 @@ Webmail.prototype = {
valueField: 'address',
searchField: 'address',
options: [],
create: function (input) {
create: $.proxy(function (input) {
this.createContact(input, input);
return {
address: input
};
},
}, this),
load: function (query, callback) {
if (!query.length) {
callback();
Expand Down Expand Up @@ -1434,6 +1437,22 @@ Webmail.prototype = {
});
},

createContact: function (name, address) {
var data = {
'display_name': name,
emails: [{address: address, type: 'other'}]
};
$.ajax({
url: this.options.contactAddUrl,
data: JSON.stringify(data),
type: 'POST',
dataType: 'json',
contentType: 'application/json'
}).done(function (data) {
$("body").notify("success", gettext("Contact added!"), 2000);
});
},

/**
* Add a new contact to address book.
*/
Expand All @@ -1449,34 +1468,17 @@ Webmail.prototype = {
} else {
name = $span.html();
}

var createContact = function () {
var data = {
'display_name': name,
emails: [{address: address, type: 'other'}]
};
$.ajax({
url: $link.attr('href'),
data: JSON.stringify(data),
type: 'POST',
dataType: 'json',
contentType: 'application/json'
}).done(function(data) {
$("body").notify("success", gettext("Contact added!"), 2000);
});
};

$.ajax({
url: '{0}?search={1}'.format(this.options.contactListUrl, address)
}).success(function (data) {
}).success($.proxy(function (data) {
if (data.length) {
$('body').notify(
'warning', gettext('A contact with this address already exists'),
1500);
return;
}
createContact();
});
this.createContact(name, address);
}, this));
},

/**
Expand Down
39 changes: 21 additions & 18 deletions modoboa_webmail/templates/modoboa_webmail/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@
<script type="text/javascript" src="{% static 'js/jquery.infinitescroll.js' %}"></script>
<script type="text/javascript" src="{% static 'modoboa_webmail/js/webmail.js' %}"></script>
<script type="text/javascript">
var webmail = undefined;
var webmail = undefined;

$(document).ready(function() {
webmail = new Webmail({
poller_interval: {{ refreshrate }},
poller_url: "{% url 'modoboa_webmail:unseen_messages_check' %}",
listing_url: "{% url 'modoboa_webmail:index' %}?action=listmailbox",
move_url: "{% url 'modoboa_webmail:mail_move' %}",
submboxes_url: "{% url 'modoboa_webmail:submailboxes_get' %}",
delattachment_url: "{% url 'modoboa_webmail:attachment_delete' %}",
contactListUrl: {% if contacts_plugin_enabled %}"{% url 'api:emailaddress-list' %}"{% else %}null{% endif %},
deflocation: "{{ deflocation }}",
defcallback: "{{ defcallback }}",
ro_mboxes: {% tolist ro_mboxes %},
trash: "{{ trash }}",
hdelimiter: '{{ hdelimiter }}',
mboxes_col_width: {{ mboxes_col_width }}
});
});
$(document).ready(function() {
webmail = new Webmail({
poller_interval: {{ refreshrate }},
poller_url: "{% url 'modoboa_webmail:unseen_messages_check' %}",
listing_url: "{% url 'modoboa_webmail:index' %}?action=listmailbox",
move_url: "{% url 'modoboa_webmail:mail_move' %}",
submboxes_url: "{% url 'modoboa_webmail:submailboxes_get' %}",
delattachment_url: "{% url 'modoboa_webmail:attachment_delete' %}",
{% if contacts_plugin_enabled %}
contactListUrl: "{% url 'api:emailaddress-list' %}",
contactAddUrl: "{% url 'api:contact-list' %}",
{% endif %}
deflocation: "{{ deflocation }}",
defcallback: "{{ defcallback }}",
ro_mboxes: {% tolist ro_mboxes %},
trash: "{{ trash }}",
hdelimiter: '{{ hdelimiter }}',
mboxes_col_width: {{ mboxes_col_width }}
});
});
</script>
{% endblock %}

Expand Down