1 /** Init for Autocomplete (requires jquery-ui)
4 * @author Mikael Nordfeldth <mmn@hethane.se>
5 * @copyright 2013 Free Software Foundation, Inc.
6 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
7 * @link http://status.net/
10 var origInit = SN.Init.NoticeFormSetup;
11 SN.Init.NoticeFormSetup = function(form) {
14 // Only attach to traditional-style forms
15 var textarea = form.find('.notice_data-text:first');
16 if (textarea.length == 0) {
20 function termSplit(val) {
21 return val.split(/ \s*/);
23 function extractLast( term ) {
24 return termSplit(term).pop();
26 var apiUrl = $('#autocomplete-api').attr('data-url');
27 // migrated "multiple" and "multipleSeparator" from
28 // http://www.learningjquery.com/2010/06/autocomplete-migration-guide
30 .bind('keydown', function( event ) {
31 if ( event.keyCode === $.ui.keyCode.TAB &&
32 $( this ).data( "ui-autocomplete" ).menu.active ) {
33 event.preventDefault();
37 minLength: 1, // 1 is default
38 source: function (request, response) {
40 term: extractLast(request.term)
44 // custom minLength, we though we match the 1 below
45 var term = extractLast(this.value);
46 if (term.length <= 1) {
51 // prevent value inserted on focus
54 select: function (event, ui) {
55 var terms = termSplit(this.value);
56 terms.pop(); // remove latest term
57 terms.push(ui.item.value); // insert
58 terms.push(''); // empty element, for the join()
59 this.value = terms.join(' ');
63 .data('ui-autocomplete')._renderItem = function (ul, item) {
65 .data('ui-autocomplete-item', item)
66 .append('<a><img style="display:inline; vertical-align: middle"><span /></a>')
67 .find('img').attr('src', item.avatar).end()
68 .find('span').text(item.label).end()