-
Notifications
You must be signed in to change notification settings - Fork 40
/
jquery.sew.min.js
1 lines (1 loc) · 4.55 KB
/
jquery.sew.min.js
1
!function(a,b,c){function g(b,c){this.element=b,this.$element=a(b),this.$itemList=a(g.MENU_TEMPLATE),this.options=a.extend({},f,c),this.reset(),this._defaults=f,this._name=e,this.expression=new RegExp("(^|\\b|\\s)"+this.options.token+"([\\w.]*)$"),this.cleanupHandle=null,this.init()}var d=function(a,b){a.text(b.val)},e="sew",f={token:"@",elementFactory:d,values:[],unique:!1,repeat:!0};g.MENU_TEMPLATE="<div class='-sew-list-container' style='display: none; position: absolute;'><ul class='-sew-list'></ul></div>",g.ITEM_TEMPLATE='<li class="-sew-list-item"></li>',g.KEYS=[40,38,13,27,9],g.prototype.init=function(){this.options.values.length<1||this.$element.bind("keyup",a.proxy(this.onKeyUp,this)).bind("keydown",a.proxy(this.onKeyDown,this)).bind("focus",a.proxy(this.renderElements,this,this.options.values)).bind("blur",a.proxy(this.remove,this))},g.prototype.reset=function(){this.options.unique&&(this.options.values=g.getUniqueElements(this.options.values)),this.index=0,this.matched=!1,this.dontFilter=!1,this.lastFilter=c,this.filtered=this.options.values.slice(0)},g.prototype.next=function(){this.index=(this.index+1)%this.filtered.length,this.hightlightItem()},g.prototype.prev=function(){this.index=(this.index+this.filtered.length-1)%this.filtered.length,this.hightlightItem()},g.prototype.select=function(){this.replace(this.filtered[this.index].val),this.$element.trigger("mention-selected",this.filtered[this.index]),this.hideList()},g.prototype.remove=function(){this.$itemList.fadeOut("slow"),this.cleanupHandle=b.setTimeout(a.proxy(function(){this.$itemList.remove()},this),1e3)},g.prototype.replace=function(a){var b=this.$element.getCursorPosition(),c=this.getText(),d=c.substring(0,b);d=d.replace(this.expression,"$1"+this.options.token+a);var e=c.substring(b,c.length),f=e.match(/^\s/)?"":" ",g=d+f+e;this.setText(g),this.$element.setCursorPosition(d.length+1)},g.prototype.hightlightItem=function(){this.$itemList.find(".-sew-list-item").removeClass("selected");var a=this.$itemList.find(".-sew-list-item").parent(),b=this.filtered[this.index].element.addClass("selected"),c=b.position().top;a.scrollTop(a.scrollTop()+c)},g.prototype.renderElements=function(b){a("body").append(this.$itemList);var c=this.$itemList.find("ul").empty();b.forEach(a.proxy(function(b,d){var e=a(g.ITEM_TEMPLATE);this.options.elementFactory(e,b),b.element=e.appendTo(c).bind("click",a.proxy(this.onItemClick,this,b)).bind("mouseover",a.proxy(this.onItemHover,this,d))},this)),this.index=0,this.hightlightItem()},g.prototype.displayList=function(){if(this.filtered.length){this.$itemList.show();var a=this.$element,b=this.$element.offset(),c=a.getCaretPosition();this.$itemList.css({left:b.left+c.left,top:b.top+c.top})}},g.prototype.hideList=function(){this.$itemList.hide(),this.reset()},g.prototype.filterList=function(b){if(b!=this.lastFilter){this.lastFilter=b,this.$itemList.find(".-sew-list-item").remove();var c=this.options.values,d=this.filtered=c.filter(a.proxy(function(a){var c=new RegExp("\\W*"+this.options.token+a.val+"(\\W|$)");return!this.options.repeat&&this.getText().match(c)?!1:""===b||a.val.toLowerCase().indexOf(b.toLowerCase())>=0||(a.meta||"").toLowerCase().indexOf(b.toLowerCase())>=0},this));d.length?(this.renderElements(d),this.$itemList.show()):this.hideList()}},g.getUniqueElements=function(a){var b=[];return a.forEach(function(a){var c=b.map(function(a){return a.val}).indexOf(a.val)>=0;c||b.push(a)}),b},g.prototype.getText=function(){return this.$element.val()||this.$element.text()},g.prototype.setText=function(a){this.$element.is("input,textarea")?this.$element.val(a):this.$element.html(a)},g.prototype.onKeyUp=function(){var b=this.$element.getCursorPosition(),c=this.getText().substring(0,b),d=c.match(this.expression);return!d&&this.matched?(this.matched=!1,this.dontFilter=!1,this.hideList(),void 0):(d&&!this.matched&&(this.displayList(),this.lastFilter="\n",this.matched=!0),d&&!this.dontFilter&&this.filterList(d[2]),void 0)},g.prototype.onKeyDown=function(a){var b=this.$itemList.is(":visible");if(b&&!(g.KEYS.indexOf(a.keyCode)<0)){switch(a.keyCode){case 9:case 13:this.select();break;case 40:this.next();break;case 38:this.prev();break;case 27:this.$itemList.hide(),this.dontFilter=!0}a.preventDefault()}},g.prototype.onItemClick=function(a){this.cleanupHandle&&b.clearTimeout(this.cleanupHandle),this.replace(a.val),this.$element.trigger("mention-selected",this.filtered[this.index]),this.hideList()},g.prototype.onItemHover=function(a){this.index=a,this.hightlightItem()},a.fn[e]=function(b){return this.each(function(){a.data(this,"plugin_"+e)||a.data(this,"plugin_"+e,new g(this,b))})}}(jQuery,window);