(function() {
'use strict';
$(document).on(
'focus',
'[data-atwho-users-edit]',
function() {
// Only initialize if URL is present and
// atwho is not initialized yet
if (
$(document.body).is('[data-atwho-users-url]') &&
_.isUndefined($(this).data('atwho'))
) {
var dataUrl = $(document.body).attr('data-atwho-users-url');
$(this)
.atwho({
at: '@',
callbacks: {
remoteFilter: function(query, callback) {
$.getJSON(
dataUrl,
{query: query},
function(data) {
callback(data.users);
}
);
},
sorter: function(query, items, _searchKey) {
// Sorting is already done on server-side
return items;
},
tplEval: function(_tpl, map) {
var res;
try {
res = '';
res += '
';
res += '
';
res += '';
res += map.full_name;
res += '';
res += '';
res += ' ';
res += '·';
res += ' ';
res += '';
res += map.email;
res += '';
res += '';
res += '';
} catch (_error) {
res = '';
}
return res;
},
highlighter: function(li, query) {
function highlight(el, sel, re) {
var prevVal = el.find(sel).html();
var newVal = prevVal.replace(re, '$&');
el.find(sel).html(newVal);
}
if (!query) {
return li;
}
var $li = $(li);
var re = new RegExp(query, 'gi');
highlight($li, '[data-val=full-name]', re);
highlight($li, '[data-val=email]', re);
return $li[0].outerHTML;
},
beforeInsert: function(value, li) {
var res = '';
res += '[@' + li.attr('data-full-name');
res += '~' + li.attr('data-id') + ']';
return res;
}
},
headerTpl:
'',
limit: <%= Constants::ATWHO_SEARCH_LIMIT %>,
startsWithSpace: true,
acceptSpaceBar: true
});
}
}
);
})();