邮箱自动完成JS

/*!
* Completer v0.1.2
* https://github.com/fengyuanchen/completer
*
* Copyright 2014-2015 Fengyuan Chen
* Released under the MIT license
*
* Date: 2015-01-03T12:17:43.862Z
*/
!function(a){
"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)
}(function(a){
"use strict";
var b=a(window),
c=a(document),
d=function(b,c){
this.$element=a(b),this.defaults=a.extend({

},d.defaults,this.$element.data(),a.isPlainObject(c)?c:{

}),this.init()
},
e=function(a){
return"string"==typeof a&&""!==a?(a=f(a),new RegExp(a+"+[^"+a+"]*$","i")):null
},
f=function(a){
return a.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g,"\\$1")
},
g=function(b){
return"string"==typeof b&&(b=b.replace(/[\{\}\[\]"']+/g,"").split(/\s*,+\s*/)),b=a.map(b,function(a){
return"string"!=typeof a?a.toString():a
})
};
d.prototype={
constructor:d,
init:function(){
var b=this.defaults,c=g(b.source);
c.length>0&&(this.data=c,this.regexp=e(b.separator),this.$completer=a(b.template),this.$completer.hide().appendTo("body"),this.place(),this.$element.attr("autocomplete","off").on({
focus:a.proxy(this.enable,this),blur:a.proxy(this.disable,this)
}),this.$element.is(":focus")&&this.enable())
},
enable:function(){
this.active||(this.active=!0,this.$element.on({
keydown:a.proxy(this.keydown,this),keyup:a.proxy(this.keyup,this)
}),this.$completer.on({
mousedown:a.proxy(this.mousedown,this),mouseover:a.proxy(this.mouseover,this)
}))
},
disable:function(){
this.active&&(this.active=!1,this.$element.off({
keydown:this.keydown,keyup:this.keyup
}),this.$completer.off({
mousedown:this.mousedown,mouseover:this.mouseover
}))
},
attach:function(b){
var c,d,e=this.defaults.separator,
g=this.regexp,
h=g?b.match(g):null,i=[],j=[],
k=this;
var index = b.indexOf('@');
var keym='',prestr='';
if(index<0){
}
else{
if(index+1<b.length){
keym = b.substr(index+1,b.length);
prestr = b.substr(0,index+1);
}
}
h&&(h=h[0],b=b.replace(g,""),
c=new RegExp("^"+f(h),"i")),
a.each(this.data,function(a,f){
f=e+f,
keym==''?d=k.template(b+f):((keym!='' && f.indexOf(keym)==0)?d=k.template(prestr+f):d=''),
c&&c.test(f)?i.push(d):j.push(d)
}),i=i.length?i.sort():j,"top"===this.defaults.position&&(i=i.reverse()),
this.fill(i.join(""))
},
suggest:function(b){
var c=new RegExp(f(b),"i"),d=this,e=[];
a.each(this.data,function(a,b){
c.test(b)&&e.push(b)
}),e.sort(function(a,c){
return a.indexOf(b)-c.indexOf(b)
}),a.each(e,function(a,b){
e[a]=d.template(b)
}),this.fill(e.join(""))
},
template:function(a){
var b=this.defaults.itemTag;
return"<"+b+">"+a+"</"+b+">"
},
fill:function(a){
var b;
this.$completer.empty(),a?(b="top"===this.defaults.position?":last":":first",this.$completer.html(a),this.$completer.children(b).addClass(this.defaults.selectedClass),this.show()):this.hide()
},
complete:function(){
var a=this.defaults,
b=a.filter(this.$element.val()).toString();
if(b.indexOf('@')<0){
return void this.hide();
}else{
return""===b?void this.hide():void(a.suggest?this.suggest(b):this.attach(b))
}
},
keydown:function(a){
13===a.keyCode&&(a.stopPropagation(),a.preventDefault())
},
keyup:function(a){
var b=a.keyCode;
13===b||38===b||40===b?this.toggle(b):this.complete()
},
mouseover:function(b){
var c=this.defaults,d=c.selectedClass,e=a(b.target);
e.is(c.itemTag)&&e.addClass(d).siblings().removeClass(d)
},
mousedown:function(b){
b.stopPropagation(),b.preventDefault(),this.setValue(a(b.target).text())
},
setValue:function(a){
this.$element.val(a),this.defaults.complete(),this.hide()
},
toggle:function(a){
var b=this.defaults.selectedClass,c=this.$completer.find("."+b);
switch(a){
case 40:c.removeClass(b),c=c.next();
break;
case 38:c.removeClass(b),c=c.prev();
break;
case 13:this.setValue(c.text())
}0===c.length&&(c=this.$completer.children(40===a?":first":":last")),c.addClass(b)
},
place:function(){
var a=this.$element,c=a.offset(),d=c.left,e=c.top,f=a.outerHeight(),g=a.outerWidth(),h={
minWidth:g,zIndex:this.defaults.zIndex
};
switch(this.defaults.position){
case"right":h.left=d+g,h.top=e;
break;
case"left":h.right=b.innerWidth()-d,h.top=e;
break;
case"top":h.left=d,h.bottom=b.innerHeight()-e;
break;
default:h.left=d,h.top=e+f
}
this.$completer.css(h)
},
show:function(){
this.$completer.show(),b.on("resize",a.proxy(this.place,this)),c.on("mousedown",a.proxy(this.hide,this))
},
hide:function(){
this.$completer.hide(),b.off("resize",this.place),c.off("mousedown",this.hide)
},
destroy:function(){
this.hide(),this.disable(),this.$element.off({
focus:this.enable,blur:this.disable
})
}
},

d.defaults={
itemTag:"li",position:"bottom",source:[],selectedClass:"completer-selected",separator:"",suggest:!1,template:'<ul class="completer-container"></ul>',zIndex:1,complete:a.noop,filter:function(a){
return a
}
},
d.setDefaults=function(b){
a.extend(d.defaults,b)
},
a.fn.completer=function(b){
var c,e=[].slice.call(arguments,1);
return this.each(function(){
var f,g=a(this),h=g.data("completer");
h||g.data("completer",h=new d(this,b)),"string"==typeof b&&a.isFunction(f=h[b])&&(c=f.apply(h,e))
}),"undefined"!=typeof c?c:this
},a.fn.completer.Constructor=d,a.fn.completer.setDefaults=d.setDefaults,a(function(){
a('[data-toggle="completer"],[completer]').completer()
})
});

posted @ 2016-07-18 08:48  电影放映员  阅读(150)  评论(0编辑  收藏  举报