diff options
author | Fabio Comuni <fabrix.xm@gmail.com> | 2011-10-25 15:01:58 +0200 |
---|---|---|
committer | Fabio Comuni <fabrix.xm@gmail.com> | 2011-10-25 15:01:58 +0200 |
commit | bcb672e7ff859168b639ffffc38553bbcf7e918c (patch) | |
tree | ea969ff32e9dbf1c4cd0d898ec02b9802bec9c7f /js | |
parent | afce790e56ba7cd4c5c049e7c3a9aac20846981e (diff) | |
download | volse-hubzilla-bcb672e7ff859168b639ffffc38553bbcf7e918c.tar.gz volse-hubzilla-bcb672e7ff859168b639ffffc38553bbcf7e918c.tar.bz2 volse-hubzilla-bcb672e7ff859168b639ffffc38553bbcf7e918c.zip |
autocomplete contacts nickname while writting in a comment box.
Diffstat (limited to 'js')
-rw-r--r-- | js/fk.autocomplete.js | 154 | ||||
-rw-r--r-- | js/jquery.textinputs.js | 20 | ||||
-rw-r--r-- | js/main.js | 9 |
3 files changed, 180 insertions, 3 deletions
diff --git a/js/fk.autocomplete.js b/js/fk.autocomplete.js new file mode 100644 index 000000000..ac7c2ce4e --- /dev/null +++ b/js/fk.autocomplete.js @@ -0,0 +1,154 @@ +/** + * Friendika people autocomplete + * + * require jQuery, jquery.textareas + */ + + + +function ACPopup(elm,backend_url){ + this.idsel=-1; + this.element = elm; + this.searchText=""; + this.ready=true; + this.kp_timer = false; + this.url = backend_url; + + style = $(elm).offset(); + w = $(elm).width(); + h = $(elm).height(); + style.top=style.top+h; + style.height = '150px'; + style.width = w; + style.border = '1px solid red'; + style.background = '#cccccc'; + style.position = 'absolute'; + style.overflow = 'auto'; + style['z-index'] = '100000'; + + this.cont = $("<div class='acpopup'></div>"); + this.cont.css(style); + + $(elm).after(this.cont); +} +ACPopup.prototype.close = function(){ + $(this.cont).remove(); + this.ready=false; +} +ACPopup.prototype.search = function(text){ + this.searchText=text; + /*if (this.kp_timer) clearTimeout(this.kp_timer); + this.kp_timer = setTimeout( this._search, 1000);*/ + this._search(); +} +ACPopup.prototype._search = function(){ + + var that = this; + var postdata = { + start:0, + count:100, + search:this.searchText, + } + + $.ajax({ + type:'POST', + url: this.url, + data: postdata, + dataType: 'json', + success:function(data){ + that.cont.html(""); + $(data.items).each(function(){ + if (this.type=="c"){ + html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick) + that.add(html, this.nick); + } + }); + } + }); + +} +ACPopup.prototype.add = function(label, value){ + var that=this; + var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>"); + elm.click(function(e){ + t = $(this).attr('title'); + el=$(that.element); + sel = el.getSelection(); + sel.start = sel.start- that.searchText.length; + el.setSelection(sel.start,sel.end).replaceSelectedText(t).collapseSelection(false); + that.close(); + }); + $(this.cont).append(elm); +} +ACPopup.prototype.onkey = function(event){ + if (event.keyCode == '13' && this.idsel>-1) { + this.cont.children()[this.idsel].click(); + event.preventDefault(); + } + if (event.keyCode == '38') { //cursor up + cmax = this.cont.children().size()-1; + this.idsel--; + if (this.idsel<0) this.idsel=cmax; + event.preventDefault(); + } + if (event.keyCode == '40') { //cursor down + cmax = this.cont.children().size()-1; + this.idsel++; + if (this.idsel>cmax) this.idsel=0; + event.preventDefault(); + } + + if (event.keyCode == '38' || event.keyCode == '40' ) { + this.cont.children().css({background:'#cccccc'}).removeClass('selected'); + $(this.cont.children()[this.idsel]).css({background:'#ccccff'}).addClass('selected'); + } + + if (event.keyCode == '27') { //ESC + this.close(); + } +} + +function ContactAutocomplete(element,backend_url){ + this.pattern=/@([^ \n]+)$/; + this.popup=null; + var that = this; + + $(element).keydown(function(event){ + if (that.popup!==null) that.popup.onkey(event); + }); + + $(element).keyup(function(event){ + cpos = $(this).getSelection(); + if (cpos.start==cpos.end){ + match = $(this).val().substring(0,cpos.start).match(that.pattern); + if (match!==null){ + if (that.popup===null){ + that.popup = new ACPopup(this, backend_url); + } + if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]); + if (!that.popup.ready) that.popup=null; + + } else { + if (that.popup!==null) {that.popup.close(); that.popup=null;} + } + + + } + }); + +} + +/** + * jQuery plugin 'contact_autocomplete' + */ +(function( $ ){ + $.fn.contact_autocomplete = function(backend_url) { + this.each(function(){ + new ContactAutocomplete(this, backend_url); + }); + }; +})( jQuery ); + + + + diff --git a/js/jquery.textinputs.js b/js/jquery.textinputs.js new file mode 100644 index 000000000..fd6d14569 --- /dev/null +++ b/js/jquery.textinputs.js @@ -0,0 +1,20 @@ +/* + Rangy Text Inputs, a cross-browser textarea and text input library plug-in for jQuery. + + Part of Rangy, a cross-browser JavaScript range and selection library + http://code.google.com/p/rangy/ + + Depends on jQuery 1.0 or later. + + Copyright 2010, Tim Down + Licensed under the MIT license. + Version: 0.1.205 + Build date: 5 November 2010 +*/ +(function(n){function o(e,g){var a=typeof e[g];return a==="function"||!!(a=="object"&&e[g])||a=="unknown"}function p(e,g,a){if(g<0)g+=e.value.length;if(typeof a=="undefined")a=g;if(a<0)a+=e.value.length;return{start:g,end:a}}function k(){return typeof document.body=="object"&&document.body?document.body:document.getElementsByTagName("body")[0]}var i,h,q,l,r,s,t,u,m;n(document).ready(function(){function e(a,b){return function(){var c=this.jquery?this[0]:this,d=c.nodeName.toLowerCase();if(c.nodeType== +1&&(d=="textarea"||d=="input"&&c.type=="text")){c=[c].concat(Array.prototype.slice.call(arguments));c=a.apply(this,c);if(!b)return c}if(b)return this}}var g=document.createElement("textarea");k().appendChild(g);if(typeof g.selectionStart!="undefined"&&typeof g.selectionEnd!="undefined"){i=function(a){return{start:a.selectionStart,end:a.selectionEnd,length:a.selectionEnd-a.selectionStart,text:a.value.slice(a.selectionStart,a.selectionEnd)}};h=function(a,b,c){b=p(a,b,c);a.selectionStart=b.start;a.selectionEnd= +b.end};m=function(a,b){if(b)a.selectionEnd=a.selectionStart;else a.selectionStart=a.selectionEnd}}else if(o(g,"createTextRange")&&typeof document.selection=="object"&&document.selection&&o(document.selection,"createRange")){i=function(a){var b=0,c=0,d,f,j;if((j=document.selection.createRange())&&j.parentElement()==a){f=a.value.length;d=a.value.replace(/\r\n/g,"\n");c=a.createTextRange();c.moveToBookmark(j.getBookmark());j=a.createTextRange();j.collapse(false);if(c.compareEndPoints("StartToEnd",j)> +-1)b=c=f;else{b=-c.moveStart("character",-f);b+=d.slice(0,b).split("\n").length-1;if(c.compareEndPoints("EndToEnd",j)>-1)c=f;else{c=-c.moveEnd("character",-f);c+=d.slice(0,c).split("\n").length-1}}}return{start:b,end:c,length:c-b,text:a.value.slice(b,c)}};h=function(a,b,c){b=p(a,b,c);c=a.createTextRange();var d=b.start-(a.value.slice(0,b.start).split("\r\n").length-1);c.collapse(true);if(b.start==b.end)c.move("character",d);else{c.moveEnd("character",b.end-(a.value.slice(0,b.end).split("\r\n").length- +1));c.moveStart("character",d)}c.select()};m=function(a,b){var c=document.selection.createRange();c.collapse(b);c.select()}}else{k().removeChild(g);window.console&&window.console.log&&window.console.log("TextInputs module for Rangy not supported in your browser. Reason: No means of finding text input caret position");return}k().removeChild(g);l=function(a,b,c,d){var f;if(b!=c){f=a.value;a.value=f.slice(0,b)+f.slice(c)}d&&h(a,b,b)};q=function(a){var b=i(a);l(a,b.start,b.end,true)};u=function(a){var b= +i(a),c;if(b.start!=b.end){c=a.value;a.value=c.slice(0,b.start)+c.slice(b.end)}h(a,b.start,b.start);return b.text};r=function(a,b,c,d){var f=a.value;a.value=f.slice(0,c)+b+f.slice(c);if(d){b=c+b.length;h(a,b,b)}};s=function(a,b){var c=i(a),d=a.value;a.value=d.slice(0,c.start)+b+d.slice(c.end);c=c.start+b.length;h(a,c,c)};t=function(a,b,c){var d=i(a),f=a.value;a.value=f.slice(0,d.start)+b+d.text+c+f.slice(d.end);b=d.start+b.length;h(a,b,b+d.length)};n.fn.extend({getSelection:e(i,false),setSelection:e(h, +true),collapseSelection:e(m,true),deleteSelectedText:e(q,true),deleteText:e(l,true),extractSelectedText:e(u,false),insertText:e(r,true),replaceSelectedText:e(s,true),surroundSelectedText:e(t,true)})})})(jQuery);
\ No newline at end of file diff --git a/js/main.js b/js/main.js index 03ba11ed8..15c5b5d1f 100644 --- a/js/main.js +++ b/js/main.js @@ -78,8 +78,6 @@ menu.toggle(); return false; }); - - /* notifications template */ var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html()); @@ -163,7 +161,9 @@ } } - }); + }); + + }); function NavUpdate() { @@ -245,6 +245,8 @@ commentBusy = false; $('body').css('cursor', 'auto'); } + /* autocomplete @nicknames */ + $(".comment-edit-wrapper textarea").contact_autocomplete(baseurl+"/acl"); }); } @@ -445,6 +447,7 @@ function setupFieldRichtext(){ }); } + /** * sprintf in javascript * "{0} and {1}".format('zero','uno'); |