forked from yuku/textcomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.textcomplete.min.js
4 lines (4 loc) · 8.62 KB
/
jquery.textcomplete.min.js
1
2
3
4
/*! jquery-textcomplete - v0.2.2 - 2014-06-08 */!function(a){"use strict";var b=function(a){var b,d,e;return b=function(){d=!1},function(){var b=c(arguments);if(d)return e=b,void 0;d=!0;var f=this;b.unshift(function g(){if(e){var b=e;e=void 0,b.unshift(g),a.apply(f,b)}else d=!1}),a.apply(this,b)}},c=function(a){var b;return b=Array.prototype.slice.call(a)},d=function(){var b;return b=a("<div></div>").css(["color"]).color,"undefined"!=typeof b?function(a,b){return a.css(b)}:function(b,c){var d;return d={},a.each(c,function(a,c){d[c]=b.css(c)}),d}}(),e=function(a){return a},f=function(a){var b={};return function(c,d){b[c]?d(b[c]):a.call(this,c,function(a){b[c]=(b[c]||[]).concat(a),d.apply(null,arguments)})}},g=function(a,b){var c,d;if(a.indexOf)return-1!=a.indexOf(b);for(c=0,d=a.length;d>c;c++)if(a[c]===b)return!0;return!1},h=function(){function c(b,c){var d;this.el=b.get(0),d=this.el===document.activeElement,this.$el=b,this.id="textComplete"+h++,this.strategies=[],this.option=c,d?(this.initialize(),this.$el.focus()):this.$el.one("focus.textComplete",a.proxy(this.initialize,this))}var e,f,g,h;return e={list:'<ul class="dropdown-menu"></ul>'},f={list:{position:"absolute",left:0,zIndex:"100",display:"none"}},g=a(e.list).css(f.list),h=0,a.extend(c.prototype,{initialize:function(){var b,c,d;b=g.clone(),this.listView=new i(b,this),this.$el.on({"keyup.textComplete":a.proxy(this.onKeyup,this),"keydown.textComplete":a.proxy(this.listView.onKeydown,this.listView)}),d=this.option.appendTo,d?b.appendTo(d instanceof a?d:a(d)):b.appendTo(a("body")),c={},c["click."+this.id]=a.proxy(this.onClickDocument,this),c["keyup."+this.id]=a.proxy(this.onKeyupDocument,this),a(document).on(c)},register:function(a){this.strategies=this.strategies.concat(a)},renderList:function(a){this.clearAtNext&&(this.listView.clear(),this.clearAtNext=!1),a.length&&(this.listView.strategy=this.strategy,this.listView.shown||this.listView.setPosition(this.getCaretPosition()).clear().activate(),a=a.slice(0,this.strategy.maxCount),this.listView.render(a)),!this.listView.data.length&&this.listView.shown&&this.listView.deactivate()},searchCallbackFactory:function(a){var b=this;return function(c,d){b.renderList(c),d||(a(),b.clearAtNext=!0)}},onKeyup:function(a){this.skipSearch(a)||this.trigger(null,!0)},trigger:function(a,b){var c,d;if(a||(a=this.getTextFromHeadToCaret()),c=this.extractSearchQuery(a),c.length){if(d=c[1],b&&this.term===d)return;this.term=d,this.search(c)}else this.term=null,this.listView.deactivate()},skipSearch:function(a){switch(a.keyCode){case 40:case 38:return!0}if(a.ctrlKey)switch(a.keyCode){case 78:case 80:return!0}},onSelect:function(b){var c,d,e,f,g,h;if(c=this.getTextFromHeadToCaret(),"true"==this.el.contentEditable){f=window.getSelection(),g=f.getRangeAt(0),h=g.cloneRange(),h.selectNodeContents(g.startContainer);var i=h.toString();d=i.substring(g.startOffset)}else d=this.el.value.substring(this.el.selectionEnd);if(e=this.strategy.replace(b),a.isArray(e)&&(d=e[1]+d,e=e[0]),c=c.replace(this.strategy.match,e),"true"==this.el.contentEditable){g.selectNodeContents(g.startContainer),g.deleteContents();var j=document.createTextNode(c+d);g.insertNode(j),g.setStart(j,c.length),g.collapse(!0),f.removeAllRanges(),f.addRange(g)}else this.$el.val(c+d),this.el.selectionStart=this.el.selectionEnd=c.length;this.$el.trigger("change").trigger("textComplete:select",b),this.el.focus()},onClickDocument:function(a){a.originalEvent&&!a.originalEvent.keepTextCompleteDropdown&&this.listView.deactivate()},onKeyupDocument:function(a){this.listView.shown&&27===a.keyCode&&(this.listView.deactivate(),this.$el.focus())},destroy:function(){this.$el.off(".textComplete"),a(document).off("."+this.id),this.listView&&this.listView.destroy(),this.$el.removeData("textComplete"),this.$el=null},getCaretPosition:function(){var a,b;return a=this.getCaretRelativePosition(),b=this.$el.offset(),a.top+=b.top,a.left+=b.left,a},getCaretRelativePosition:function(){var b,c,e,f,g,h,i,j,k,l;return"true"!=this.el.contentEditable?(b=["border-width","font-family","font-size","font-style","font-variant","font-weight","height","letter-spacing","word-spacing","line-height","text-decoration","text-align","width","padding-top","padding-right","padding-bottom","padding-left","margin-top","margin-right","margin-bottom","margin-left","border-style","box-sizing"],i=this.$el[0].scrollHeight>this.$el[0].offsetHeight,c=a.extend({position:"absolute",overflow:i?"scroll":"auto","white-space":"pre-wrap",top:0,left:-9999,direction:h},d(this.$el,b)),e=a("<div></div>").css(c).text(this.getTextFromHeadToCaret()),f=a("<span></span>").text(".").appendTo(e),this.$el.before(e),g=f.position(),g.top+=f.height()-this.$el.scrollTop(),e.remove()):(j=window.getSelection().getRangeAt(0).cloneRange(),k=document.createElement("span"),j.insertNode(k),j.selectNodeContents(k),j.deleteContents(),l=a(k),g=l.offset(),g.top+=l.height()-this.$el.offset().top),h=this.$el.attr("dir")||this.$el.css("direction"),"rtl"===h&&(g.left-=this.listView.$el.width()),g},getTextFromHeadToCaret:function(){var a,b,c;if("true"==this.el.contentEditable){if(window.getSelection){var c=window.getSelection().getRangeAt(0),d=c.cloneRange();d.selectNodeContents(c.startContainer),a=d.toString().substring(0,c.startOffset)}}else b=this.el.selectionEnd,"number"==typeof b?a=this.el.value.substring(0,b):document.selection&&(c=this.el.createTextRange(),c.moveStart("character",0),c.moveEnd("textedit"),a=c.text);return a},extractSearchQuery:function(a){var b,c,d,e;for(b=0,c=this.strategies.length;c>b;b++)if(d=this.strategies[b],e=a.match(d.match))return[d,e[d.index]];return[]},search:b(function(a,b){var c;this.strategy=b[0],c=b[1],this.strategy.search(c,this.searchCallbackFactory(a))})}),c}(),i=function(){function b(b,c){this.data=[],this.$el=b,this.index=0,this.completer=c,this.$el.on("mousedown.textComplete","li.textcomplete-item",a.proxy(this.onClick,this))}return a.extend(b.prototype,{shown:!1,render:function(b){var c,d,e,f,h,i;for(c="",this.strategy.header&&(i=a.isFunction(this.strategy.header)?this.strategy.header(b):this.strategy.header,c+='<li class="textcomplete-header">'+i+"</li>"),d=0,e=b.length;e>d&&(h=b[d],g(this.data,h)||(f=this.data.length,this.data.push(h),c+='<li class="textcomplete-item" data-index="'+f+'"><a>',c+=this.strategy.template(h),c+="</a></li>",this.data.length!==this.strategy.maxCount));d++);this.strategy.footer&&(i=a.isFunction(this.strategy.footer)?this.strategy.footer(b):this.strategy.footer,c+='<li class="textcomplete-footer">'+i+"</li>"),this.$el.append(c),this.data.length?this.activateIndexedItem():this.deactivate()},clear:function(){return this.data=[],this.$el.html(""),this.index=0,this},activateIndexedItem:function(){this.$el.find(".active").removeClass("active"),this.getActiveItem().addClass("active")},getActiveItem:function(){return a(this.$el.children(".textcomplete-item").get(this.index))},activate:function(){return this.shown||(this.$el.show(),this.completer.$el.trigger("textComplete:show"),this.shown=!0),this},deactivate:function(){return this.shown&&(this.$el.hide(),this.completer.$el.trigger("textComplete:hide"),this.shown=!1,this.data=[],this.index=null),this},setPosition:function(a){var b;return"top"===this.strategy.placement?(b=parseInt(this.$el.css("font-size")),a={top:"auto",bottom:this.$el.parent().height()-a.top+b,left:a.left}):a.bottom="auto",this.$el.css(a),this},select:function(a){var b=this;this.completer.onSelect(this.data[a]),setTimeout(function(){b.deactivate()},0)},onKeydown:function(a){if(this.shown){var b=a.ctrlKey||a.altKey||a.metaKey||a.shiftKey;38===a.keyCode||a.ctrlKey&&80===a.keyCode?(a.preventDefault(),0===this.index?this.index=this.data.length-1:this.index-=1,this.activateIndexedItem()):40===a.keyCode||a.ctrlKey&&78===a.keyCode?(a.preventDefault(),this.index===this.data.length-1?this.index=0:this.index+=1,this.activateIndexedItem()):b||13!==a.keyCode&&9!==a.keyCode||(a.preventDefault(),this.select(parseInt(this.getActiveItem().data("index"),10)))}},onClick:function(b){var c=a(b.target);b.preventDefault(),b.originalEvent.keepTextCompleteDropdown=!0,c.hasClass("textcomplete-item")||(c=c.parents("li.textcomplete-item")),this.select(parseInt(c.data("index"),10))},destroy:function(){this.deactivate(),this.$el.off("click.textComplete").remove(),this.$el=null}}),b}();a.fn.textcomplete=function(b,c){var d,g,i,j;if(j="textComplete",c||(c={}),"destroy"===b)return this.each(function(){var b=a(this).data(j);b&&b.destroy()});for(d=0,g=b.length;g>d;d++)i=b[d],i.template||(i.template=e),null==i.index&&(i.index=2),i.cache&&(i.search=f(i.search)),i.maxCount||(i.maxCount=10);return this.each(function(){var d,e;d=a(this),e=d.data(j),e||(e=new h(d,c),d.data(j,e)),e.register(b)})}}(window.jQuery||window.Zepto);
/*
//@ sourceMappingURL=jquery.textcomplete.min.map
*/