forked from lizheming/chatonline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
at.js
1 lines (1 loc) · 16.2 KB
/
at.js
1
(function(){(function(e){return typeof define=="function"&&define.amd?define(["jquery"],e):e(window.jQuery)})(function(e){"use strict";var t,n,r,i,s,o;return o="caret",t=function(){function t(e){this.$inputor=e,this.domInputor=this.$inputor[0]}return t.prototype.setPos=function(e){return this.domInputor},t.prototype.getIEPosition=function(){return e.noop()},t.prototype.getPosition=function(){return e.noop()},t.prototype.getOldIEPos=function(){var e,t;return t=document.selection.createRange(),e=document.body.createTextRange(),e.moveToElementText(this.domInputor),e.setEndPoint("EndToEnd",t),e.text.length},t.prototype.getPos=function(){var e,t,n;if(n=this.range())return e=n.cloneRange(),e.selectNodeContents(this.domInputor),e.setEnd(n.endContainer,n.endOffset),t=e.toString().length,e.detach(),t;if(document.selection)return this.getOldIEPos()},t.prototype.getOldIEOffset=function(){var e,t;return e=document.selection.createRange().duplicate(),e.moveStart("character",-1),t=e.getBoundingClientRect(),{height:t.bottom-t.top,left:t.left,top:t.top}},t.prototype.getOffset=function(t){var n,r,i,s;r=null;if(window.getSelection&&(i=this.range())){if(i.endOffset-1<0)return null;n=i.cloneRange(),n.setStart(i.endContainer,i.endOffset-1),n.setEnd(i.endContainer,i.endOffset),s=n.getBoundingClientRect(),r={height:s.height,left:s.left+s.width,top:s.top},n.detach(),r}else document.selection&&this.getOldIEOffset();return r&&(r.top+=e(window).scrollTop(),r.left+=e(window).scrollLeft()),r},t.prototype.range=function(){var e;if(!window.getSelection)return;return e=window.getSelection(),e.rangeCount>0?e.getRangeAt(0):null},t}(),n=function(){function t(e){this.$inputor=e,this.domInputor=this.$inputor[0]}return t.prototype.getIEPos=function(){var e,t,n,r,i,s,o;return t=this.domInputor,s=document.selection.createRange(),i=0,s&&s.parentElement()===t&&(r=t.value.replace(/\r\n/g,"\n"),n=r.length,o=t.createTextRange(),o.moveToBookmark(s.getBookmark()),e=t.createTextRange(),e.collapse(!1),o.compareEndPoints("StartToEnd",e)>-1?i=n:i=-o.moveStart("character",-n)),i},t.prototype.getPos=function(){return document.selection?this.getIEPos():this.domInputor.selectionStart},t.prototype.setPos=function(e){var t,n;return t=this.domInputor,document.selection?(n=t.createTextRange(),n.move("character",e),n.select()):t.setSelectionRange&&t.setSelectionRange(e,e),t},t.prototype.getIEOffset=function(e){var t,n,r,i,s;return r=this.domInputor.createTextRange(),e?r.move("character",e):(n=document.selection.createRange(),r.moveToBookmark(n.getBookmark())),i=r.boundingLeft,s=r.boundingTop,t=r.boundingHeight,{left:i,top:s,height:t}},t.prototype.getOffset=function(t){var n,r,i;return n=this.$inputor,document.selection?(r=this.getIEOffset(t),r.top+=e(window).scrollTop()+n.scrollTop(),r.left+=e(window).scrollLeft()+n.scrollLeft(),r):(r=n.offset(),i=this.getPosition(t),r={left:r.left+i.left-n.scrollLeft(),top:r.top+i.top-n.scrollTop(),height:i.height})},t.prototype.getPosition=function(e){var t,n,i,s,o,u;return t=this.$inputor,i=function(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/`/g,"`").replace(/"/g,""").replace(/\r\n|\r|\n/g,"<br />")},e===void 0&&(e=this.getPos()),u=t.val().slice(0,e),s="<span>"+i(u)+"</span>",s+="<span id='caret'>|</span>",o=new r(t),n=o.create(s).rect()},t.prototype.getIEPosition=function(e){var t,n,r,i,s;return r=this.getIEOffset(e),n=this.$inputor.offset(),i=r.left-n.left,s=r.top-n.top,t=r.height,{left:i,top:s,height:t}},t}(),r=function(){function t(e){this.$inputor=e}return t.prototype.css_attr=["overflowY","height","width","paddingTop","paddingLeft","paddingRight","paddingBottom","marginTop","marginLeft","marginRight","marginBottom","fontFamily","borderStyle","borderWidth","wordWrap","fontSize","lineHeight","overflowX","text-align"],t.prototype.mirrorCss=function(){var t,n=this;return t={position:"absolute",left:-9999,top:0,zIndex:-2e4,"white-space":"pre-wrap"},e.each(this.css_attr,function(e,r){return t[r]=n.$inputor.css(r)}),t},t.prototype.create=function(t){return this.$mirror=e("<div></div>"),this.$mirror.css(this.mirrorCss()),this.$mirror.html(t),this.$inputor.after(this.$mirror),this},t.prototype.rect=function(){var e,t,n;return e=this.$mirror.find("#caret"),t=e.position(),n={left:t.left,top:t.top,height:e.height()},this.$mirror.remove(),n},t}(),i={adjustOffset:function(t,n){if(!t)return;return t.top+=e(window).scrollTop(),t.left+=e(window).scrollLeft(),t},contentEditable:function(e){return!!e[0].contentEditable&&e[0].contentEditable==="true"}},s={pos:function(e){return e?this.setPos(e):this.getPos()},position:function(e){return document.selection?this.getIEPosition(e):this.getPosition(e)},offset:function(e){return this.getOffset(e)}},e.fn.caret=function(r){var o;return o=i.contentEditable(this)?new t(this):new n(this),s[r]?s[r].apply(o,Array.prototype.slice.call(arguments,1)):e.error("Method "+r+" does not exist on jQuery.caret")},e.fn.caret.EditableCaret=t,e.fn.caret.InputCaret=n,e.fn.caret.Utils=i,e.fn.caret.apis=s})}).call(this),function(){var e=[].slice;(function(e){return typeof define=="function"&&define.amd?define(["jquery"],e):e(window.jQuery)})(function(t){var n,r,i,s,o,u,a,f,l;return i=function(){function e(e){this.current_flag=null,this.controllers={},this.alias_maps={},this.$inputor=t(e),this.listen()}return e.prototype.controller=function(e){return this.controllers[this.alias_maps[e]||e||this.current_flag]},e.prototype.set_context_for=function(e){return this.current_flag=e,this},e.prototype.reg=function(e,t){var n,r;return n=(r=this.controllers)[e]||(r[e]=new o(this,e)),t.alias&&(this.alias_maps[t.alias]=e),n.init(t),this},e.prototype.listen=function(){var e=this;return this.$inputor.on("keyup.atwho",function(t){return e.on_keyup(t)}).on("keydown.atwho",function(t){return e.on_keydown(t)}).on("scroll.atwho",function(t){var n;return(n=e.controller())!=null?n.view.hide():void 0}).on("blur.atwho",function(t){var n;if(n=e.controller())return n.view.hide(n.get_opt("display_timeout"))})},e.prototype.dispatch=function(){var e=this;return t.map(this.controllers,function(t){if(t.look_up())return e.set_context_for(t.at)})},e.prototype.on_keyup=function(e){var n;switch(e.keyCode){case a.ESC:e.preventDefault(),(n=this.controller())!=null&&n.view.hide();break;case a.DOWN:case a.UP:t.noop();break;default:this.dispatch()}},e.prototype.on_keydown=function(e){var n,r;n=(r=this.controller())!=null?r.view:void 0;if(!n||!n.visible())return;switch(e.keyCode){case a.ESC:e.preventDefault(),n.hide();break;case a.UP:e.preventDefault(),n.prev();break;case a.DOWN:e.preventDefault(),n.next();break;case a.TAB:case a.ENTER:if(!n.visible())return;e.preventDefault(),n.choose();break;default:t.noop()}},e}(),o=function(){function s(e,i){this.app=e,this.at=i,this.$inputor=this.app.$inputor,this.id=this.$inputor[0].id||r(),this.setting=null,this.query=null,this.pos=0,this.cur_rect=null,this.range=null,n.append(this.$el=t("<div id='atwho-ground-"+this.id+"'></div>")),this.model=new f(this),this.view=new l(this)}var r,i;return i=0,r=function(){return i+=1},s.prototype.init=function(e){return this.setting=t.extend({},this.setting||t.fn.atwho["default"],e),this.view.init(),this.model.reload(this.setting.data)},s.prototype.call_default=function(){var n,r;r=arguments[0],n=2<=arguments.length?e.call(arguments,1):[];try{return u[r].apply(this,n)}catch(i){return t.error(""+i+" Or maybe At.js doesn't have function "+r)}},s.prototype.trigger=function(e,t){var n,r;return t.push(this),n=this.get_opt("alias"),r=n?""+e+"-"+n+".atwho":""+e+".atwho",this.$inputor.trigger(r,t)},s.prototype.callbacks=function(e){return this.get_opt("callbacks")[e]||u[e]},s.prototype.get_opt=function(e,t){try{return this.setting[e]}catch(n){return null}},s.prototype.content=function(){return this.$inputor.is("textarea, input")?this.$inputor.val():this.$inputor.text()},s.prototype.catch_query=function(){var e,t,n,r,i,s;return t=this.content(),e=this.$inputor.caret("pos"),s=t.slice(0,e),r=this.callbacks("matcher").call(this,this.at,s,this.get_opt("start_with_space")),typeof r=="string"&&r.length<=this.get_opt("max_len",20)?(i=e-r.length,n=i+r.length,this.pos=i,r={text:r.toLowerCase(),head_pos:i,end_pos:n},this.trigger("matched",[this.at,r.text])):this.view.hide(),this.query=r},s.prototype.rect=function(){var e,t;if(!(e=this.$inputor.caret("offset",this.pos-1)))return;return this.$inputor.attr("contentEditable")==="true"&&(e=this.cur_rect||(this.cur_rect=e)||e),t=document.selection?0:2,{left:e.left,top:e.top,bottom:e.top+e.height+t}},s.prototype.reset_rect=function(){if(this.$inputor.attr("contentEditable")==="true")return this.cur_rect=null},s.prototype.mark_range=function(){return this.range=this.get_range()||this.get_ie_range()},s.prototype.clear_range=function(){return this.range=null},s.prototype.get_range=function(){return this.range||(window.getSelection?window.getSelection().getRangeAt(0):void 0)},s.prototype.get_ie_range=function(){return this.range||(document.selection?document.selection.createRange():void 0)},s.prototype.insert_content_for=function(e){var n,r,i;return r=e.data("value"),i=this.get_opt("insert_tpl"),this.$inputor.is("textarea, input")||!i?r:(n=t.extend({},e.data("item-data"),{"atwho-data-value":r,"atwho-at":this.at}),this.callbacks("tpl_eval").call(this,i,n))},s.prototype.insert=function(e,n){var r,i,s,o,u,a,f,l,c,h,p;r=this.$inputor,r.attr("contentEditable")==="true"&&(s="atwho-view-flag atwho-view-flag-"+(this.get_opt("alias")||this.at),o=""+e+"<span contenteditable='false'> <span>",u="<span contenteditable='false' class='"+s+"'>"+o+"</span>",i=t(u).data("atwho-data-item",n.data("item-data")),document.selection&&(i=t("<span contenteditable='true'></span>").html(i)));if(r.is("textarea, input"))e=""+e,c=r.val(),h=c.slice(0,Math.max(this.query.head_pos-this.at.length,0)),p=""+h+e+" "+c.slice(this.query.end_pos||0),r.val(p),r.caret("pos",h.length+e.length+1);else if(f=this.get_range())a=f.startOffset-(this.query.end_pos-this.query.head_pos)-this.at.length,f.setStart(f.endContainer,Math.max(a,0)),f.setEnd(f.endContainer,f.endOffset),f.deleteContents(),f.insertNode(i[0]),f.collapse(!1),l=window.getSelection(),l.removeAllRanges(),l.addRange(f);else if(f=this.get_ie_range())f.moveStart("character",this.query.end_pos-this.query.head_pos-this.at.length),f.pasteHTML(i[0]),f.collapse(!1),f.select();return r.focus(),r.change()},s.prototype.render_view=function(e){var t;return t=this.get_opt("search_key"),e=this.callbacks("sorter").call(this,this.query.text,e.slice(0,1001),t),this.view.render(e.slice(0,this.get_opt("limit")))},s.prototype.look_up=function(){var e,n;if(!(e=this.catch_query()))return;return n=function(e){return e&&e.length>0?this.render_view(e):this.view.hide()},this.model.query(e.text,t.proxy(n,this)),e},s}(),f=function(){function e(e){this.context=e,this.at=this.context.at,this.storage=this.context.$inputor}return e.prototype.saved=function(){return this.fetch()>0},e.prototype.query=function(e,t){var n,r,i;return n=this.fetch(),r=this.context.get_opt("search_key"),n=this.context.callbacks("filter").call(this.context,e,n,r)||[],i=this.context.callbacks("remote_filter"),n.length>0||!i&&n.length===0?t(n):i.call(this.context,e,t)},e.prototype.fetch=function(){return this.storage.data(this.at)||[]},e.prototype.save=function(e){return this.storage.data(this.at,this.context.callbacks("before_save").call(this.context,e||[]))},e.prototype.load=function(e){if(!this.saved()&&!!e)return this._load(e)},e.prototype.reload=function(e){return this._load(e)},e.prototype._load=function(e){var n=this;return typeof e=="string"?t.ajax(e,{dataType:"json"}).done(function(e){return n.save(e)}):this.save(e)},e}(),l=function(){function e(e){this.context=e,this.$el=t("<div class='atwho-view'><ul class='atwho-view-ul'></ul></div>"),this.timeout_id=null,this.context.$el.append(this.$el),this.bind_event()}return e.prototype.init=function(){var e;return e=this.context.get_opt("alias")||this.context.at.charCodeAt(0),this.$el.attr({id:"at-view-"+e})},e.prototype.bind_event=function(){var e,n=this;return e=this.$el.find("ul"),e.on("mouseenter.atwho-view","li",function(n){return e.find(".cur").removeClass("cur"),t(n.currentTarget).addClass("cur")}).on("click",function(e){return n.choose(),e.preventDefault()}),this.$el.on("mouseenter.atwho-view","ul",function(e){return n.context.mark_range()}).on("mouseleave.atwho-view","ul",function(e){return n.context.clear_range()})},e.prototype.visible=function(){return this.$el.is(":visible")},e.prototype.choose=function(){var e,t;return e=this.$el.find(".cur"),t=this.context.insert_content_for(e),this.context.insert(this.context.callbacks("before_insert").call(this.context,t,e),e),this.context.trigger("inserted",[e]),this.hide()},e.prototype.reposition=function(e){var n;return e.bottom=e.top-$('li', this.$el).length * 32,n={left:e.left,top:e.bottom},this.$el.offset(n),this.context.trigger("reposition",[n])},e.prototype.next=function(){var e,t;return e=this.$el.find(".cur").removeClass("cur"),t=e.next(),t.length||(t=this.$el.find("li:first")),t.addClass("cur")},e.prototype.prev=function(){var e,t;return e=this.$el.find(".cur").removeClass("cur"),t=e.prev(),t.length||(t=this.$el.find("li:last")),t.addClass("cur")},e.prototype.show=function(){var e;this.visible()||this.$el.show();if(e=this.context.rect())return this.reposition(e)},e.prototype.hide=function(e){var t,n=this;return isNaN(e&&this.visible())?(this.context.reset_rect(),this.$el.hide()):(t=function(){return n.hide()},clearTimeout(this.timeout_id),this.timeout_id=setTimeout(t,e))},e.prototype.render=function(e){var n,r,i,s,o,u,a;if(!(t.isArray(e)&&e.length>0)){this.hide();return}this.$el.find("ul").empty(),r=this.$el.find("ul"),o=this.context.get_opt("tpl");for(u=0,a=e.length;u<a;u++)i=e[u],i=t.extend({},i,{"atwho-at":this.context.at}),s=this.context.callbacks("tpl_eval").call(this.context,o,i),n=t(this.context.callbacks("highlighter").call(this.context,s,this.context.query.text)),n.data("item-data",i),r.append(n);return this.show(),r.find("li:first").addClass("cur")},e}(),a={DOWN:40,UP:38,ESC:27,TAB:9,ENTER:13},u={before_save:function(e){var n,r,i,s;if(!t.isArray(e))return e;s=[];for(r=0,i=e.length;r<i;r++)n=e[r],t.isPlainObject(n)?s.push(n):s.push({name:n});return s},matcher:function(e,t,n){var r,i;return e=e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),n&&(e="(?:^|\\s)"+e),i=new RegExp(e+"([A-Za-z0-9_+-]*)$|"+e+"([^\\x00-\\xff]*)$","gi"),r=i.exec(t),r?r[2]||r[1]:null},filter:function(e,t,n){var r,i,s,o;o=[];for(i=0,s=t.length;i<s;i++)r=t[i],~r[n].toLowerCase().indexOf(e)&&o.push(r);return o},remote_filter:null,sorter:function(e,t,n){var r,i,s,o;if(!e)return t;o=[];for(i=0,s=t.length;i<s;i++)r=t[i],r.atwho_order=r[n].toLowerCase().indexOf(e),r.atwho_order>-1&&o.push(r);return o.sort(function(e,t){return e.atwho_order-t.atwho_order})},tpl_eval:function(e,t){try{return e.replace(/\$\{([^\}]*)\}/g,function(e,n,r){return t[n]})}catch(n){return""}},highlighter:function(e,t){var n;return t?(n=new RegExp(">\\s*(\\w*)("+t.replace("+","\\+")+")(\\w*)\\s*<","ig"),e.replace(n,function(e,t,n,r){return"> "+t+n+r+" <"})):e},before_insert:function(e,t){return e}},r={load:function(e,t){var n;if(n=this.controller(e))return n.model.load(t)},getInsertedItemsWithIDs:function(e){var n,r,i;return(n=this.controller(e))?(e&&(e="-"+(n.get_opt("alias")||n.at)),r=[],i=t.map(this.$inputor.find("span.atwho-view-flag"+(e||"")),function(e){var n;n=t(e).data("atwho-data-item");if(r.indexOf(n.id)>-1)return;return n.id&&(r.push=n.id),n}),[r,i]):[null,null]},getInsertedItems:function(e){return r.getInsertedItemsWithIDs.apply(this,[e])[1]},getInsertedIDs:function(e){return r.getInsertedItemsWithIDs.apply(this,[e])[0]},run:function(){return this.dispatch()}},s={init:function(e){var n,r;return r=(n=t(this)).data("atwho"),r||n.data("atwho",r=new i(this)),r.reg(e.at,e),this}},n=t("<div id='atwho-container'></div>"),t.fn.atwho=function(e){var i,o;return o=arguments,t("body").append(n),i=null,this.filter("textarea, input, [contenteditable=true]").each(function(){var n;if(typeof e=="object"||!e)return s.init.apply(this,o);if(!r[e])return t.error("Method "+e+" does not exist on jQuery.caret");if(n=t(this).data("atwho"))return i=r[e].apply(n,Array.prototype.slice.call(o,1))}),i||this},t.fn.atwho["default"]={at:void 0,alias:void 0,data:null,tpl:"<li data-value='${atwho-at}${name}'>${name}</li>",insert_tpl:"<span>${atwho-data-value}</span>",callbacks:u,search_key:"name",start_with_space:!0,limit:5,max_len:20,display_timeout:300}})}.call(this);