Refactor magic recipients field when sending new message.

Uses local cache of user mappings instead of AJAX calls; has just enough
customization hooks to allow for a department/user mapping for MJ...
This commit is contained in:
Lance Edgar 2016-02-11 17:05:56 -06:00
parent ad9cd8be8e
commit c65bc6f229
3 changed files with 86 additions and 66 deletions

View file

@ -7,43 +7,53 @@
${h.stylesheet_link(request.static_url('tailbone:static/css/jquery.tagit.css'))}
<script type="text/javascript">
% if initial_recipient_names is not Undefined:
var initial_recipients = {
% for i, (uuid, name) in enumerate(initial_recipient_names.iteritems(), 1):
'${uuid}': "${name.replace('"', '\\""')|n}"${',' if i < len(initial_recipient_names) else ''}
% endfor
};
% endif
var recipient_mappings = new Map([
<% last = len(available_recipients) %>
% for i, (uuid, entry) in enumerate(sorted(available_recipients.iteritems(), key=lambda r: r[1]), 1):
['${uuid}', ${json.dumps(entry)|n}]${',' if i < last else ''}
% endfor
]);
$(function() {
var recipients = $('.recipients input');
recipients.tagit({
autocomplete: {
delay: 0,
minLength: 2,
autoFocus: true,
removeConfirmation: true,
source: function(request, response) {
$.get('${url('messages.recipients')}', {term: request.term}, response);
},
select: function(event, ui) {
recipients.tagit('createTag', ui.item.value + ',' + ui.item.label);
// Preventing the tag input to be updated with the chosen value.
return false;
var term = request.term.toLowerCase();
var data = [];
recipient_mappings.forEach(function(name, uuid) {
if (!name.toLowerCase && name.name) {
name = name.name;
}
if (name.toLowerCase().indexOf(term) >= 0) {
data.push({value: uuid, label: name});
}
});
response(data);
}
},
beforeTagAdded: function(event, ui) {
var label = ui.tagLabel.split(',');
var value = label.shift();
$(ui.tag).find('.tagit-hidden-field').val(value);
if (label.length) {
$(ui.tag).find('.tagit-label').text(label.join());
} else {
$(ui.tag).find('.tagit-label').text(initial_recipients[value]);
}
},
removeConfirmation: true
beforeTagAdded: ${self.before_tag_added()},
beforeTagRemoved: function(event, ui) {
// Unfortunately we're responsible for cleaning up the hidden
// field, since the values there do not match the tag labels.
var tags = recipients.tagit('assignedTags');
var uuid = ui.tag.data('uuid');
tags = tags.filter(function(element) {
return element != uuid;
});
recipients.data('ui-tagit')._updateSingleTagsField(tags);
}
});
// set focus to recipients field
@ -61,6 +71,18 @@
</style>
</%def>
<%def name="before_tag_added()">
function(event, ui) {
// Lookup the name in cached mapping, and show that on the tag, instead
// of the UUID. The tagit widget should take care of keeping the
// hidden field in sync for us, still using the UUID.
var uuid = ui.tagLabel;
var name = recipient_mappings.get(uuid);
ui.tag.find('.tagit-label').html(name);
}
</%def>
<%def name="context_menu_items()">
% if request.has_perm('messages.list'):
<li>${h.link_to("Go to my Message Inbox", url('messages.inbox'))}</li>