jquery.mentionsInput.mod.min.js 8.2 KB
!function(e,t){t.templateSettings={interpolate:/<%=([\s\S]+?)%>/g};var n={BACKSPACE:8,TAB:9,RETURN:13,ESC:27,LEFT:37,UP:38,RIGHT:39,DOWN:40,COMMA:188,SPACE:32,HOME:36,END:35},i={triggerChar:"@",onDataRequest:e.noop,minChars:2,allowRepeat:!1,showAvatars:!0,elastic:!0,defaultValue:"",onCaret:!1,keepTriggerCharacter:!1,classes:{autoCompleteItemActive:"active"},templates:{wrapper:t.template('<div class="mentions-input-box"></div>'),autocompleteList:t.template('<div class="mentions-autocomplete-list"></div>'),autocompleteListItem:t.template('<li data-ref-id="<%= id %>" data-ref-type="<%= type %>" data-display="<%= display %>"><%= content %> <small><%= fullName %></small></li>'),autocompleteListItemAvatar:t.template('<img src="<%= avatar %>" />'),autocompleteListItemIcon:t.template('<div class="icon <%= icon %>"></div>'),mentionsOverlay:t.template('<div class="mentions"><div></div></div>'),mentionItemSyntax:t.template("@[<%= value %>](<%= type %>:<%= id %>)"),mentionItemHighlight:t.template("<strong><span><%= value %></span></strong>")}},a={htmlEncode:function(e){return t.escape(e)},regexpEncode:function(e){return e.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1")},highlightTerm:function(e,t){return t||t.length?e.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+t+")(?![^<>]*>)(?![^&;]+;)","gi"),"<b>$1</b>"):e},setCaratPosition:function(e,t){if(e.createTextRange){var n=e.createTextRange();n.move("character",t),n.select()}else e.selectionStart?(e.focus(),e.setSelectionRange(t,t)):e.focus()},rtrim:function(e){return e.replace(/\s+$/,"")}},o=function(o){function r(){L=e(M),"true"!==L.attr("data-mentions-input")&&(N=L.parent(),H=e(o.templates.wrapper()),L.wrapAll(H),H=N.find("> div.mentions-input-box"),L.attr("data-mentions-input","true"),L.bind("keydown",b),L.bind("keypress",E),L.bind("click",w),L.bind("blur",C),navigator.userAgent.indexOf("MSIE 8")>-1?L.bind("propertychange",y):L.bind("input",y),o.elastic&&L.elastic())}function l(){O=e(o.templates.autocompleteList()),O.appendTo(H),O.delegate("li","mousedown",x)}function s(){D=e(o.templates.mentionsOverlay()),D.prependTo(H)}function c(){var e=m();t.each(P,function(t){var n=o.templates.mentionItemSyntax(t);e=e.replace(new RegExp(a.regexpEncode(t.value),"g"),n)});var n=a.htmlEncode(e);t.each(P,function(e){var i=t.extend({},e,{value:a.htmlEncode(e.value)}),r=o.templates.mentionItemSyntax(i),l=o.templates.mentionItemHighlight(i);n=n.replace(new RegExp(a.regexpEncode(r),"g"),l)}),n=n.replace(/\n/g,"<br />"),n=n.replace(/ {2}/g,"&nbsp; "),L.data("messageText",e),L.trigger("updated"),D.find("div").html(n)}function d(){W=[]}function p(e){t.isArray(e)?W=e:t.isString(e)&&(W=e.split(""))}function u(){var e=m();P=t.reject(P,function(t){return!t.value||-1==e.indexOf(t.value)}),P=t.compact(P)}function f(e){for(var n,i=m(),r=L[0].selectionStart,l=!1,s=!1,p=new RegExp("\\"+o.triggerChar+j,"gi");n=p.exec(i);)(l===!1||Math.abs(p.lastIndex-r)<l)&&(l=Math.abs(p.lastIndex-r),s=p.lastIndex);var u=s-j.length-1,f=s,g=i.substr(0,u),h=i.substr(f,i.length),v=(g+e.value).length+1;t.find(P,function(t){return t.id==e.id})||P.push(e),d(),j="",T();var x=g;o.keepTriggerCharacter&&(x+=o.triggerChar),x+=e.value+" "+h,L.val(x),L.trigger("mention"),c(),L.focus(),a.setCaratPosition(L[0],v)}function m(){return e.trim(L.val())}function g(t){var n,i,a,o,r,l,s,c,d,p,u;if((d=t[0])&&e(d).is("textarea")&&null!=d.selectionEnd){for(s={position:"absolute",overflow:"auto",whiteSpace:"pre-wrap",wordWrap:"break-word",boxSizing:"content-box",top:0,left:-9999},c=["boxSizing","fontFamily","fontSize","fontStyle","fontVariant","fontWeight","height","letterSpacing","lineHeight","paddingBottom","paddingLeft","paddingRight","paddingTop","textDecoration","textIndent","textTransform","width","word-spacing"],p=0,u=c.length;u>p;p++)r=c[p],s[r]=e(d).css(r);return a=document.createElement("div"),e(a).css(s),e(d).after(a),i=document.createTextNode(d.value.substring(0,d.selectionEnd)),n=document.createTextNode(d.value.substring(d.selectionEnd)),o=document.createElement("span"),o.innerHTML="&nbsp;",a.appendChild(i),a.appendChild(o),a.appendChild(n),a.scrollTop=d.scrollTop,l=e(o).position(),e(a).remove(),l}}function h(t){var n,i,a,o,r,l,s,c,d,p,u;if((d=t[0])&&e(d).is("textarea")&&null!=d.selectionEnd){for(s={position:"absolute",overflow:"auto",whiteSpace:"pre-wrap",wordWrap:"break-word",boxSizing:"content-box",top:0,left:-9999},c=["boxSizing","fontFamily","fontSize","fontStyle","fontVariant","fontWeight","height","letterSpacing","lineHeight","paddingBottom","paddingLeft","paddingRight","paddingTop","textDecoration","textIndent","textTransform","width","word-spacing"],p=0,u=c.length;u>p;p++)r=c[p],s[r]=e(d).css(r);return a=document.createElement("div"),e(a).css(s),e(d).after(a),i=document.createTextNode(d.value.substring(0,d.selectionEnd)),n=document.createTextNode(d.value.substring(d.selectionEnd)),o=document.createElement("span"),o.innerHTML="&nbsp;",a.appendChild(i),a.appendChild(o),a.appendChild(n),a.scrollTop=d.scrollTop,l=e(o).offset(),e(a).remove(),l}}function v(){var t=e(L).offset().top,n=e("body").offset().top,i=e(window).scrollTop();i>t&&e(window).scrollTop(t-n)}function x(){var t=e(this),n=F[t.attr("data-uid")];return f(n),v(),!1}function w(){d()}function C(){T()}function y(){c(),u();var e=t.lastIndexOf(W,o.triggerChar);e>-1&&(j=W.slice(e+1).join(""),j=a.rtrim(j),t.defer(t.bind(A,this,j)))}function E(e){if(e.keyCode!==n.BACKSPACE){var t=String.fromCharCode(e.which||e.keyCode);W.push(t)}}function b(i){if(i.keyCode===n.LEFT||i.keyCode===n.RIGHT||i.keyCode===n.HOME||i.keyCode===n.END)return t.defer(d),void(navigator.userAgent.indexOf("MSIE 9")>-1&&t.defer(c));if(i.keyCode===n.BACKSPACE)return void(W=W.slice(0,-1+W.length));if(!O.is(":visible"))return!0;switch(i.keyCode){case n.UP:case n.DOWN:var a=null;return a=i.keyCode===n.DOWN?B&&B.length?B.next():O.find("li").first():e(B).prev(),a.length&&I(a),!1;case n.RETURN:case n.TAB:if(B&&B.length)return B.trigger("mousedown"),!1}return!0}function T(){B=null,O.empty().hide()}function I(e){e.addClass(o.classes.autoCompleteItemActive),e.siblings().removeClass(o.classes.autoCompleteItemActive),B=e}function S(n,i){if(O.show(),!o.allowRepeat){var r=t.pluck(P,"value");i=t.reject(i,function(e){return t.include(r,e.name)})}if(!i.length)return void T();O.empty();var l=e("<ul>").appendTo(O).hide();t.each(i,function(i,r){var s=t.uniqueId("mention_");F[s]=t.extend({},i,{value:i.name});var c=e(o.templates.autocompleteListItem({id:a.htmlEncode(i.id),display:a.htmlEncode(i.name),type:a.htmlEncode(i.type),fullName:a.htmlEncode(i.fullName),content:a.highlightTerm(a.htmlEncode(i.display?i.display:i.name),n)})).attr("data-uid",s);if(0===r&&I(c),o.showAvatars){var d;d=e(i.avatar?o.templates.autocompleteListItemAvatar({avatar:i.avatar}):o.templates.autocompleteListItemIcon({icon:i.icon})),d.prependTo(c)}c=c.appendTo(l)}),O.show(),o.onCaret&&R(O,L),l.show()}function A(e){e&&e.length&&e.length>=o.minChars?o.onDataRequest.call(this,"search",e,function(t){S(e,t)}):T()}function R(e,t){var n=e.css("position");if("absolute"==n){var i=g(t),a=parseInt(t.css("line-height"),10)||18;e.css("width","15em"),e.css("left",i.left),e.css("top",a+i.top);var o=t.offset().left+t.width(),r=e.offset().left+e.width();r>=o&&e.css("left",Math.abs(e.position().left-(r-o)))}else if("fixed"==n){var l=h(t),a=parseInt(t.css("line-height"),10)||18;e.css("width","15em"),e.css("left",l.left+1e4),e.css("top",a+l.top)}}function k(e){P=[];for(var t,n=a.htmlEncode(e),i=new RegExp("("+o.triggerChar+")\\[(.*?)\\]\\((.*?):(.*?)\\)","gi"),r=n;null!=(t=i.exec(n));)r=r.replace(t[0],t[1]+t[2]),P.push({id:t[4],type:t[3],value:t[2],trigger:t[1]});L.val(r),c()}var M,L,N,O,H,D,B,P=[],F={},W=[],j="";return o=e.extend(!0,{},i,o),window.setInputBuffer=p,{init:function(e){M=e,r(),l(),s(),k(o.defaultValue),o.prefillMention&&f(o.prefillMention)},val:function(e){t.isFunction(e)&&e.call(this,P.length?L.data("messageText"):m())},reset:function(){k()},reinit:function(){k(!1)},getMentions:function(e){t.isFunction(e)&&e.call(this,P)}}};e.fn.mentionsInput=function(n,i){var a=arguments;return"object"!=typeof n&&n||(i=n),this.each(function(){var r=e.data(this,"mentionsInput")||e.data(this,"mentionsInput",new o(i));return t.isFunction(r[n])?r[n].apply(this,Array.prototype.slice.call(a,1)):"object"!=typeof n&&n?void e.error("Method "+n+" does not exist"):r.init.call(this,this)})}}(jQuery,_.runInContext());