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()
74 * Called when a people tag edit box is shown in the interface
76 * - loads the jQuery UI autocomplete plugin
77 * - sets event handlers for tag completion
80 SN.Init.PeopletagAutocomplete = function(txtBox) {
84 split = function(val) {
85 return val.split( /\s+/ );
88 extractLast = function(term) {
89 return split(term).pop();
92 // don't navigate away from the field on tab when selecting an item
94 .on('keydown', function(event) {
95 if (event.keyCode === $.ui.keyCode.TAB &&
96 $(this).data('ui-autocomplete').menu.active) {
97 event.preventDefault();
102 source: function(request, response) {
103 // delegate back to autocomplete, but extract the last term
104 response($.ui.autocomplete.filter(
105 SN.C.PtagACData, extractLast(request.term)));
110 select: function(event, ui) {
111 var terms = split(this.value);
113 terms.push(ui.item.value);
115 this.value = terms.join(' ');
119 .data('ui-autocomplete')
120 ._renderItem = function (ul, item) {
121 // FIXME: with jQuery UI you cannot have it highlight the match
122 var _l = '<a class="ptag-ac-line-tag">' + item.tag +
123 ' <em class="privacy_mode">' + item.mode + '</em>' +
124 '<span class="freq">' + item.freq + '</span></a>';
127 .addClass('mode-' + item.mode)
128 .addClass('ptag-ac-line')
129 .data('item.autocomplete', item)
135 $(document).on('click', '.peopletags_edit_button', function () {
136 SN.Init.PeopletagAutocomplete($(this).closest('dd').find('[name="tags"]'));