]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Autocomplete/Autocomplete.js
Update Autocomplete plugin for reusable notice forms in 1.0.x. It's fun!
[quix0rs-gnu-social.git] / plugins / Autocomplete / Autocomplete.js
1 (function(SN, $) {
2
3 var origInit = SN.Init.NoticeFormSetup;
4 SN.Init.NoticeFormSetup = function(form) {
5     origInit(form);
6
7     // Only attach to traditional-style forms
8     var textarea = form.find('.notice_data-text:first');
9     if (textarea.length == 0) {
10         return;
11     }
12
13     function fullName(row) {
14         if (typeof row.fullname == "string" && row.fullname != '') {
15             return row.nickname + ' (' + row.fullname + ')';
16         } else {
17             return row.nickname;
18         }
19     }
20
21     var apiUrl = $('#autocomplete-api').attr('data-url');
22     textarea.autocomplete(apiUrl, {
23         multiple: true,
24         multipleSeparator: " ",
25         minChars: 1,
26         formatItem: function(row, i, max){
27             row = eval("(" + row + ")");
28             // the display:inline is because our INSANE stylesheets
29             // override the standard display of all img tags for no
30             // good reason.
31             var div = $('<div><img style="display:inline; vertical-align: middle"> <span></span></div>')
32                 .find('img').attr('src', row.avatar).end()
33                 .find('span').text(fullName(row)).end()
34             return div.html();
35         },
36         formatMatch: function(row, i, max){
37             row = eval("(" + row + ")");
38             return row.nickname;
39         },
40         formatResult: function(row){
41             row = eval("(" + row + ")");
42             switch(row.type)
43             {
44                 case 'user':
45                     return '@' + row.nickname;
46                 case 'group':
47                     return '!' + row.nickname;
48             }
49         }
50     });
51 };
52
53 })(SN, jQuery);