diff options
author | RedMatrix <info@friendica.com> | 2015-03-16 10:51:15 +1100 |
---|---|---|
committer | RedMatrix <info@friendica.com> | 2015-03-16 10:51:15 +1100 |
commit | e780dada832acbb9d46870178f9a6488f4e265d9 (patch) | |
tree | 4dafc508962ed625ebf8a7f462cf15d59c8e2f4c /view/js/autocomplete.js | |
parent | fdcbb61bcb61a980a7a9171432498a9cac6e0965 (diff) | |
parent | 864116805b7adf8e82be582c2df8524c44a829fd (diff) | |
download | volse-hubzilla-e780dada832acbb9d46870178f9a6488f4e265d9.tar.gz volse-hubzilla-e780dada832acbb9d46870178f9a6488f4e265d9.tar.bz2 volse-hubzilla-e780dada832acbb9d46870178f9a6488f4e265d9.zip |
Merge pull request #931 from dawnbreak/master
Some JavaScript cleanups.
Diffstat (limited to 'view/js/autocomplete.js')
-rw-r--r-- | view/js/autocomplete.js | 146 |
1 files changed, 71 insertions, 75 deletions
diff --git a/view/js/autocomplete.js b/view/js/autocomplete.js index b93cc16b3..e077dc88d 100644 --- a/view/js/autocomplete.js +++ b/view/js/autocomplete.js @@ -4,7 +4,7 @@ * require jQuery, jquery.textcomplete */ function contact_search(term, callback, backend_url, type, extra_channels, spinelement) { - if(spinelement){ + if(spinelement) { $(spinelement).spin('tiny'); } // Check if there is a cached result that contains the same information we would get with a full server-side search @@ -12,13 +12,13 @@ function contact_search(term, callback, backend_url, type, extra_channels, spine if(!(bt in contact_search.cache)) contact_search.cache[bt] = {}; var lterm = term.toLowerCase(); // Ignore case - for(t in contact_search.cache[bt]) { + for(var t in contact_search.cache[bt]) { if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results $(spinelement).spin(false); // Filter old results locally - var matching = contact_search.cache[bt][t].filter(function (x) { return (x.name.toLowerCase().indexOf(lterm) >= 0 || (typeof x.nick !== 'undefined' && x.nick.toLowerCase().indexOf(lterm) >= 0)); }); // Need to check that nick exists because groups don't have one + var matching = contact_search.cache[bt][t].filter(function (x) { return (x.name.toLowerCase().indexOf(lterm) >= 0 || (typeof x.nick !== 'undefined' && x.nick.toLowerCase().indexOf(lterm) >= 0)); }); // Need to check that nick exists because groups don't have one matching.unshift({taggable:false, text: term, replace: term}); - setTimeout(function() { callback(matching)} , 1); // Use "pseudo-thread" to avoid some problems + setTimeout(function() { callback(matching); } , 1); // Use "pseudo-thread" to avoid some problems return; } } @@ -28,17 +28,17 @@ function contact_search(term, callback, backend_url, type, extra_channels, spine count:100, search:term, type:type, - } + }; if(typeof extra_channels !== 'undefined' && extra_channels) postdata['extra_channels[]'] = extra_channels; - + $.ajax({ type:'POST', url: backend_url, data: postdata, dataType: 'json', - success:function(data){ + success: function(data){ // Cache results if we got them all (more information would not improve results) // data.count represents the maximum number of items if(data.items.length -1 < data.count) { @@ -57,18 +57,18 @@ contact_search.cache = {}; function contact_format(item) { // Show contact information if not explicitly told to show something else if(typeof item.text === 'undefined') { - var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick) + var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick); if(typeof desc === 'undefined') desc = ''; if(desc) desc = ' ('+desc+')'; - return "<div class='{0}' title='{4}'><img src='{1}'><span class='contactname'>{2}</span><span class='dropdown-sub-text'>{3}</span><div class='clear'></div></div>".format(item.taggable, item.photo, item.name, desc, item.link) + return "<div class='{0}' title='{4}'><img src='{1}'><span class='contactname'>{2}</span><span class='dropdown-sub-text'>{3}</span><div class='clear'></div></div>".format(item.taggable, item.photo, item.name, desc, item.link); } else - return "<div>"+item.text+"</div>" + return "<div>" + item.text + "</div>"; } function editor_replace(item) { if(typeof item.replace !== 'undefined') { - return '$1$2'+item.replace; + return '$1$2' + item.replace; } // $2 ensures that prefix (@,@!) is preserved @@ -77,7 +77,8 @@ function editor_replace(item) { // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id). if(id.length > 16) id = item.id.substring(0,16); - return '$1$2'+item.nick.replace(' ','') + '+' + id + ' '; + + return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' '; } function basic_replace(item) { @@ -94,76 +95,71 @@ function submit_form(e) { /** * jQuery plugin 'editor_autocomplete' */ -(function( $ ){ +(function( $ ) { $.fn.editor_autocomplete = function(backend_url, extra_channels) { - if (typeof extra_channels === 'undefined') extra_channels = false; - - // Autocomplete contacts - contacts = { - match: /(^|\s)(@\!*)([^ \n]+)$/, - index: 3, - search: function(term, callback) { contact_search(term, callback, backend_url, 'c', extra_channels, spinelement=false); }, - replace: editor_replace, - template: contact_format, - } - - smilies = { - match: /(^|\s)(:[a-z]{2,})$/, - index: 2, - search: function(term, callback) { $.getJSON('/smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry['text'].indexOf(term) === 0 ? entry : null })) }) }, - template: function(item) { return item['icon'] + item['text'] }, - replace: function(item) { return "$1"+item['text'] + ' '; }, - } - this.attr('autocomplete','off'); - this.textcomplete([contacts,smilies],{className:'acpopup',zIndex:1020}); - }; + if (typeof extra_channels === 'undefined') extra_channels = false; + + // Autocomplete contacts + contacts = { + match: /(^|\s)(@\!*)([^ \n]+)$/, + index: 3, + search: function(term, callback) { contact_search(term, callback, backend_url, 'c', extra_channels, spinelement=false); }, + replace: editor_replace, + template: contact_format, + }; + + smilies = { + match: /(^|\s)(:[a-z]{2,})$/, + index: 2, + search: function(term, callback) { $.getJSON('/smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); }, + template: function(item) { return item.icon + item.text; }, + replace: function(item) { return "$1" + item.text + ' '; }, + }; + this.attr('autocomplete','off'); + this.textcomplete([contacts,smilies], {className:'acpopup', zIndex:1020}); + }; })( jQuery ); /** * jQuery plugin 'search_autocomplete' */ -(function( $ ){ +(function( $ ) { $.fn.search_autocomplete = function(backend_url) { - - // Autocomplete contacts - contacts = { - match: /(^@)([^\n]{2,})$/, - index: 2, - search: function(term, callback) { contact_search(term, callback, backend_url, 'x', [], spinelement='#nav-search-spinner'); }, - replace: basic_replace, - template: contact_format, - } - this.attr('autocomplete','off'); - var a = this.textcomplete([contacts],{className:'acpopup',maxCount:100,zIndex: 1020,appendTo:'nav'}); - - a.on('textComplete:select', function(e,value,strategy) { submit_form(this); }); - - }; + // Autocomplete contacts + contacts = { + match: /(^@)([^\n]{2,})$/, + index: 2, + search: function(term, callback) { contact_search(term, callback, backend_url, 'x', [], spinelement='#nav-search-spinner'); }, + replace: basic_replace, + template: contact_format, + }; + this.attr('autocomplete', 'off'); + var a = this.textcomplete([contacts], {className:'acpopup', maxCount:100, zIndex: 1020, appendTo:'nav'}); + a.on('textComplete:select', function(e, value, strategy) { submit_form(this); }); + }; })( jQuery ); -(function( $ ){ +(function( $ ) { $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) { - - if(typeof typ === 'undefined') typ = ''; - if(typeof autosubmit === 'undefined') autosubmit = false; - - // Autocomplete contacts - contacts = { - match: /(^)([^\n]+)$/, - index: 2, - search: function(term, callback) { contact_search(term, callback, backend_url, typ,[], spinelement=false); }, - replace: basic_replace, - template: contact_format, - } - - this.attr('autocomplete','off'); - var a = this.textcomplete([contacts],{className:'acpopup',zIndex:1020}); - - if(autosubmit) - a.on('textComplete:select', function(e,value,strategy) { submit_form(this); }); - - if(typeof onselect !== 'undefined') - a.on('textComplete:select',function(e,value,strategy) { onselect(value); }); - }; -})( jQuery ); - + if(typeof typ === 'undefined') typ = ''; + if(typeof autosubmit === 'undefined') autosubmit = false; + + // Autocomplete contacts + contacts = { + match: /(^)([^\n]+)$/, + index: 2, + search: function(term, callback) { contact_search(term, callback, backend_url, typ,[], spinelement=false); }, + replace: basic_replace, + template: contact_format, + }; + + this.attr('autocomplete','off'); + var a = this.textcomplete([contacts], {className:'acpopup', zIndex:1020}); + + if(autosubmit) + a.on('textComplete:select', function(e,value,strategy) { submit_form(this); }); + + if(typeof onselect !== 'undefined') + a.on('textComplete:select', function(e, value, strategy) { onselect(value); }); + }; +})( jQuery );
\ No newline at end of file |