-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmention.min.js
10 lines (9 loc) · 2.02 KB
/
mention.min.js
1
2
3
4
5
6
7
8
9
10
/*
* Mentioned Div
* Written by: Alireza Hatami
*
* Using jquery.js
*
* License: MIT License - http://www.opensource.org/licenses/mit-license.php
*/
(function(e){e.fn.mention=function(t){function r(e){e.focus();if(typeof window.getSelection!="undefined"&&typeof document.createRange!="undefined"){var t=document.createRange();t.selectNodeContents(e);t.collapse(false);var n=window.getSelection();n.removeAllRanges();n.addRange(t)}else if(typeof document.body.createTextRange!="undefined"){var r=document.body.createTextRange();r.moveToElementText(e);r.collapse(false);r.select()}}function i(t,n){return e.ajax({type:"POST",url:t,data:{data:JSON.stringify(n)}})}function v(e){display.html(e).show()}var n={placeholder:"Type the name of someone or something...",callback_file:"a_php_file.php"};t=e.extend({},n,t);var s=/@/ig;var o=/@(\w.+)/ig;var u="display";display="";var a="msg";msg="";window.editable="";var f="mentioned";var l="add_mention";var c=t.placeholder;var h="";var p=false;var d="";e(this).click("a",function(){return false});e(this).on("keyup",function(n){editable=e(this);r(editable[0]);display=e(this).parent().children("#"+u);msg=e(this).parent().children("#"+a);if(!display.length)e('<div id="'+u+'"></div>').insertAfter(e(this)).hide();if(!msg.length)e('<div id="'+a+'"></div>').insertAfter(e(this)).hide();if(e(this).html().length==0){display.hide().remove();msg.hide().remove()}var f=editable.text();var c=f.match(s);var d=f.match(o);display.children("."+l).each(function(){var t=e(this).html();if(d=="@"+t.slice("0",h.length).toLowerCase())p=true;else p=false});if(c.length>0){msg.html(t.placeholder).show();h=new Text(d);if(d.length>0&&!p){var m=[{"do":"search"},{term:d}];e.when(i(t.callback_file,m)).done(function(e){v(e)})}}return false});e(document).on("click","."+l,function(){var t=e(this).attr("title");var n=editable.html();var i=n.replace(o," ");var s=i+"<a class='"+f+"' contenteditable='false'>"+t+"</a> ";editable.html(s);display.hide().remove();msg.hide().remove();p=false;d="";r(editable[0]);editable=""})}})(jQuery)