]> git.mxchange.org Git - friendica.git/blobdiff - js/autocomplete.js
rework autocomplete: use minified version + delete old fk.autocomplete.js
[friendica.git] / js / autocomplete.js
index aa8b6836c52eac1122d2b15174a5aa2dc9cfc8ea..dfe0bb3209e2df75f9230950f24938ff3f76cc9f 100644 (file)
@@ -24,7 +24,7 @@ function contact_search(term, callback, backend_url, type, mode) {
                if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
                        // 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
-                       matching.unshift({taggable:false, text: term, replace: term});
+                       matching.unshift({forum:false, text: term, replace: term});
                        setTimeout(function() { callback(matching); } , 1); // Use "pseudo-thread" to avoid some problems
                        return;
                }
@@ -68,9 +68,10 @@ 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 forum = ((item.forum) ? 'forum' : '');
                if(typeof desc === 'undefined') desc = '';
                if(desc) desc = ' ('+desc+')';
-               return "<div class='{0}' title='{4}'><img class='acpopup-img' src='{1}'><span class='acpopup-contactname'>{2}</span><span class='acpopup-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 class='acpopup-img' src='{1}'><span class='acpopup-contactname'>{2}</span><span class='acpopup-sub-text'>{3}</span><div class='clear'></div></div>".format(forum, item.photo, item.name, desc, item.link);
        }
        else
                return "<div>" + item.text + "</div>";
@@ -110,15 +111,70 @@ function submit_form(e) {
        $(e).parents('form').submit();
 }
 
+function getWord(text, caretPos) {
+       var index = text.indexOf(caretPos);
+       var postText = text.substring(caretPos, caretPos+8);
+       if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) {
+               return postText;
+       }
+}
+
+function getCaretPosition(ctrl) {
+       var CaretPos = 0;   // IE Support
+       if (document.selection) {
+               ctrl.focus();
+               var Sel = document.selection.createRange();
+               Sel.moveStart('character', -ctrl.value.length);
+               CaretPos = Sel.text.length;
+       }
+       // Firefox support
+       else if (ctrl.selectionStart || ctrl.selectionStart == '0')
+               CaretPos = ctrl.selectionStart;
+       return (CaretPos);
+}
+
+function setCaretPosition(ctrl, pos){
+       if(ctrl.setSelectionRange) {
+               ctrl.focus();
+               ctrl.setSelectionRange(pos,pos);
+       }
+       else if (ctrl.createTextRange) {
+               var range = ctrl.createTextRange();
+               range.collapse(true);
+               range.moveEnd('character', pos);
+               range.moveStart('character', pos);
+               range.select();
+       }
+}
+
+function listNewLineAutocomplete(id) {
+       var text = document.getElementById(id);
+       var caretPos = getCaretPosition(text)
+       var word = getWord(text.value, caretPos);
+       if (word != null) {
+               var textBefore = text.value.substring(0, caretPos);
+               var textAfter  = text.value.substring(caretPos, text.length);
+               $('#' + id).val(textBefore + '\r\n[*] ' + textAfter);
+               setCaretPosition(text, caretPos + 5);
+               return true;
+       }
+}
+
+function string2bb(element) {
+       if(element == 'bold') return 'b';
+       else if(element == 'italic') return 'i';
+       else if(element == 'underline') return 'u';
+       else if(element == 'overline') return 'o';
+       else if(element == 'strike') return 's';
+       else return element;
+}
+
 /**
  * jQuery plugin 'editor_autocomplete'
  */
 (function( $ ) {
        $.fn.editor_autocomplete = function(backend_url) {
 
-               // list of supported bbtags
-               var bbelements = ['b', 'u', 'i', 'img', 'url', 'quote', 'code', 'spoiler', 'audio', 'video', 'youtube', 'map', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 's', 'o', 'list', 'center', 'nosmile', 'vimeo' ];
-
                // Autocomplete contacts
                contacts = {
                        match: /(^|\s)(@\!*)([^ \n]+)$/,
@@ -137,15 +193,8 @@ function submit_form(e) {
                        replace: function(item) { return "$1" + item.text + ' '; },
                };
 
-               // Autocomplete BBTags
-               bbtags = {
-                       match: /\[(\w*)$/,
-                       index: 1,
-                       search: function (term, callback) { callback($.map(bbelements, function (element) { return element.indexOf(term) === 0 ? element : null; })); },
-                       replace: function (element) { return ['[' + element + ']', '[/' + element + ']']; },
-               };
                this.attr('autocomplete','off');
-               this.textcomplete([contacts,smilies, bbtags], {className:'acpopup', zIndex:1020});
+               this.textcomplete([contacts,smilies], {className:'acpopup', zIndex:10000});
        };
 })( jQuery );
 
@@ -172,7 +221,7 @@ function submit_form(e) {
                        template: contact_format,
                };
                this.attr('autocomplete', 'off');
-               var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 1020, appendTo:'#nav-search-box'});
+               var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
                a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
        };
 })( jQuery );
@@ -192,7 +241,7 @@ function submit_form(e) {
                };
 
                this.attr('autocomplete','off');
-               var a = this.textcomplete([contacts], {className:'acpopup', zIndex:1020});
+               var a = this.textcomplete([contacts], {className:'acpopup', zIndex:10000});
 
                if(autosubmit)
                        a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
@@ -218,7 +267,7 @@ function submit_form(e) {
                };
 
                this.attr('autocomplete','off');
-               var a = this.textcomplete([names], {className:'acpopup', zIndex:1020});
+               var a = this.textcomplete([names], {className:'acpopup', zIndex:10000});
 
                if(autosubmit)
                        a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
@@ -228,6 +277,58 @@ function submit_form(e) {
        };
 })( jQuery );
 
+(function( $ ) {
+       $.fn.bbco_autocomplete = function(type) {
+
+               if(type=='bbcode') {
+                       var open_close_elements = ['bold', 'italic', 'underline', 'overline', 'strike', 'quote', 'code', 'spoiler', 'map', 'nobb', 'list', 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'center', 'color', 'font', 'size'];
+                       var open_elements = ['*', 'hr'];
+
+                       var elements = open_close_elements.concat(open_elements);
+               }
+
+               bbco = {
+                       match: /\[(\w*\**)$/,
+                       search: function (term, callback) {
+                               callback($.map(elements, function (element) {
+                                       return element.indexOf(term) === 0 ? element : null;
+                               }));
+                       },
+                       index: 1,
+                       replace: function (element) {
+                               element = string2bb(element);
+                               if(open_elements.indexOf(element) < 0) {
+                                       if(element === 'list' || element === 'ol' || element === 'ul') {
+                                               return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]'];
+                                       }
+                                       else if(element === 'table') {
+                                               return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]'];
+                                       }
+                                       else {
+                                               return ['\[' + element + '\]', '\[/' + element + '\]'];
+                                       }
+                               }
+                               else {
+                                       return '\[' + element + '\] ';
+                               }
+                       }
+               };
+
+               this.attr('autocomplete','off');
+               var a = this.textcomplete([bbco], {className:'acpopup', zIndex:10000});
+
+               a.on('textComplete:select', function(e, value, strategy) { value; });
+
+               a.keypress(function(e){
+                       e.stopImmediatePropagation();
+                       if (e.keyCode == 13) {
+                               var x = listNewLineAutocomplete(this.id);
+                               if(x)
+                                       e.preventDefault();
+                       }
+               });
+       };
+})( jQuery );
 
 /**
  * Friendica people autocomplete legacy