]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Autocomplete/js/autocomplete.go.js
Event upgraded to microformats2
[quix0rs-gnu-social.git] / plugins / Autocomplete / js / autocomplete.go.js
1 /** Init for Autocomplete (requires jquery-ui)
2  *
3  * @package   Plugin
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/
8  */
9
10 var origInit = SN.Init.NoticeFormSetup;
11 SN.Init.NoticeFormSetup = function(form) {
12     origInit(form);
13
14     // Only attach to traditional-style forms
15     var textarea = form.find('.notice_data-text:first');
16     if (textarea.length == 0) {
17         return;
18     }
19
20     function termSplit(val) {
21         return val.split(/ \s*/);
22     }
23     function extractLast( term ) {
24         return termSplit(term).pop();
25     }
26     var apiUrl = $('#autocomplete-api').attr('data-url');
27     // migrated "multiple" and "multipleSeparator" from
28     // http://www.learningjquery.com/2010/06/autocomplete-migration-guide
29     textarea
30         .bind('keydown', function( event ) {
31             if ( event.keyCode === $.ui.keyCode.TAB &&
32                 $( this ).data( "ui-autocomplete" ).menu.active ) {
33                 event.preventDefault();
34             }
35         })
36         .autocomplete({
37             minLength: 1,   // 1 is default
38             source: function (request, response) {
39                 $.getJSON( apiUrl, {
40                     term: extractLast(request.term)
41                 }, response );
42             },
43             search: function () {
44                 // custom minLength, we though we match the 1 below
45                 var term = extractLast(this.value);
46                 if (term.length <= 1) {
47                     return false;
48                 }
49             },
50             focus: function () {
51                 // prevent value inserted on focus
52                 return false;
53             },
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(' ');
60                 return false;
61             },
62         })
63         .data('ui-autocomplete')._renderItem = function (ul, item) {
64             return $('<li></li>')
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()
69                 .appendTo(ul);
70         };
71 };