-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery.emailComplete.js
More file actions
executable file
·321 lines (311 loc) · 9.57 KB
/
jquery.emailComplete.js
File metadata and controls
executable file
·321 lines (311 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* jquery.emailComplete.js - jQuery Plugin for Email Complete
*
* Version: v3.1.2
*
* Author: fidding
* blog: http://www.fidding.me/
* github: https://github.com/fidding/
* email: 395455856@qq.com
*
* Licensed MIT
*
* Build: 2016-08-08
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node / CommonJS
factory(require('jquery'));
} else {
// Browser globals.
factory(jQuery);
}
})(function ($) {
'use strict';
var Fidding = function (element, options) {
var _this = this;
// integration config
_this.options = $.extend({}, Fidding.defaults, options);
_this.init(element);
}
/**
* [plugin params]
*/
Fidding.version = '3.0.0';//version
Fidding.pluginName = 'emailComplete';// plugin name
Fidding.dataName = "emailDataName";// dataName cache the data in dom
Fidding.options = {};// config
Fidding.active = -1;// active item index
/**
* [plugin element]
*/
Fidding.$element = null;// email element
Fidding.$container = null;// email container
Fidding.$containerItem = null;// email container item
/**
* [plugin defaults config]
*/
Fidding.defaults = {
opacity : 1, // email container opacity
radius: 0, // email container border-radius (px)
data : ['qq.com','163.com','126.com','sina.com','sohu.com'], // email suffix
callback : $.noop // callback
}
/**
* [plugin prototype function]
*/
Fidding.prototype = {
// init
init : function (element) {
var _this = this;
// assignment objects
_this.$element = $(element);
_this.$parent = _this.$element.parent();
_this.$element.after('<div class="complete-container" style="border-radius:'+_this.options.radius+'px;opacity:'+_this.options.opacity+'"></div>');
_this.$container = _this.$element.next( ".complete-container" );
_this.$containerItem = _this.$container.find('div.complete-item');
// input event
_this.$element.on('input', function (e) {
if(_this.$element.val() == ''){
_this.hidden();
}else{
_this.onInput(e);
}
});
// keyup event
_this.$element.on('keyup', function (e) {
if(_this.$element.val() == ''){
_this.hidden();
}else{
_this.onKeyup(e);
}
});
// focus event
_this.$element.on('focus', function () {
_this.onFocus($(this));
});
// blur event
_this.$element.on('blur', function () {
_this.onBlur();
});
// item mouse event
_this.$parent.on('mouseover', '.complete-item', function(e){
_this.onMouseOver($(this));
});
// item mouse event
_this.$parent.on('mousedown', '.complete-item', function(e){
_this.onMouseDown($(this));
});
// prevent form submit
_this.preventForm();
},
// item active
addActive: function (item) {
item.addClass('complete-active');
},
// remove item active
removeActive: function (item) {
item.removeClass('complete-active');
},
// get email position
getPos : function () {
var _this = this;
var width = _this.$element.outerWidth(),
height = _this.$element.outerHeight(),
top = _this.$element.position().top+height+3,
left = _this.$element.position().left;
return { top: top, left: left, width: width };
},
// set email position
setPos : function(data){
var _this = this;
_this.$container.css({ top: data.top, left: data.left, width: data.width });
},
// show email container
show : function(){
var _this = this;
_this.setPos(_this.getPos());//set container position
_this.$container.css({'visibility':'visible'});
},
// hidden email container
hidden : function(){
var _this = this;
_this.$container.css({'visibility':'hidden'}).empty();
_this.$containerItem = _this.$container.find('div.complete-item');
_this.active = -1;
},
// email text change
onInput : function(e){
var _this = this;
_this.active = -1;
_this.$container.empty();
var completeItem ='';
var index = _this.$element.val().indexOf('@');// weather has @
if(index != -1){// has @
var startVal = _this.$element.val().substring(0, index);// get the string before @
var endVal = _this.$element.val().substring(index+1, _this.$element.val().length);// get the string after @
if(endVal == ''){
//add all options.data
$.each(_this.options.data, function (n, value) {
completeItem += '<div class="complete-item">'+startVal+'@'+value+'</div>';
});
}else{
var isHas = 0;
$.each(_this.options.data, function (n, value) {
if(value.indexOf(endVal) == 0){// there are compliant mail suffix
var itemContent = startVal+'@'+value;
if(isHas == 0){
completeItem += '<div class="complete-item complete-active" >'+itemContent+'</div>';
_this.active = 0;
}else{
completeItem += '<div class="complete-item" >'+itemContent+'</div>';
}
isHas++;
}
});
if(isHas == 0){// if no compliant mail suffix to hide email container
_this.hidden();
return true;
}
}
}else{// no @
$.each(_this.options.data, function (n,value) {
completeItem += '<div class="complete-item">'+_this.$element.val()+'@'+value+'</div>';
});
}
_this.$container.append(completeItem);
_this.$containerItem = _this.$container.find('div.complete-item');
_this.show();// show email
},
// keyboard operation event
onKeyup : function (e) {
var _this = this;
if(_this.$element.is(':visible')){
var itemLength = _this.$containerItem.length;
switch (e.keyCode) {
case 13||10 :// enter
if(_this.active == -1){
}else{
_this.$element.val(_this.$containerItem.eq(_this.active).html());
_this.hidden();
_this.onCallBack();
}
break;
case 38 :// up arrow
if(_this.active == -1){
_this.addActive(_this.$containerItem.eq(itemLength-1));
_this.active = itemLength-1;
}else{
if(_this.active-1 < 0){
_this.removeActive(_this.$containerItem.eq(_this.active));
_this.active = -1;
}else{
_this.removeActive(_this.$containerItem.eq(_this.active));
_this.addActive(_this.$containerItem.eq(_this.active-1));
_this.active--;
}
}
break;
case 40 :// down arrow
if(this.active == -1){
_this.addActive(_this.$containerItem.eq(0));
_this.active = 0;
}else{
_this.removeActive(_this.$containerItem.eq(_this.active));
if(_this.active+1 == itemLength){
_this.active = -1;
}else{
_this.addActive(_this.$containerItem.eq(_this.active+1));
_this.active++;
}
}
break;
case 27 :// esc
_this.hidden();
break;
default :
break;
}
}
},
// email input blur event
onBlur : function(){
var _this = this;
_this.active = -1;
_this.hidden();
},
// email input focus event
onFocus : function(obj){
var _this = this;
if(!_this.$containerItem.length){
_this.hidden();
return true;
}
_this.show();
},
// item hover event
onMouseOver : function(obj){
var _this = this;
_this.$containerItem.each(function(e){
_this.removeActive($(this));
});
_this.addActive(obj);
_this.active = obj.index();
},
// item hover down event
onMouseDown : function(obj){
var _this = this;
_this.$element.val(obj.html());
_this.hidden();
_this.onCallBack();
},
// callback function
onCallBack : function(){
var _this = this;
if(typeof _this.options.callback == 'function'){
_this.options.callback();
}else{
console.log(_this.options.callback);
}
},
// prevent form submit function
preventForm: function () {
var _this = this;
if($('form').length){ // has form table
$('form').submit(function(value){
if(_this.active != -1){
_this.$element.val(_this.$containerItem.eq(_this.active).html());
_this.hidden();
_this.onCallBack();
return false;
}
});
}
},
}
$.fn[Fidding.pluginName] = function (options) {
return this.each(function (e) {
var _this = $(this);
var data = _this.data(Fidding.dataName);
//var options = typeof option == 'object' && option;
// cache data object
if (!data) {
_this.data(Fidding.dataName, (data = new Fidding(this, options)));
}
//If the name of the plug-parameter is a string, then directly call widget method for this string
if (typeof option == 'string') data[option]();
});
}
$.fn[Fidding.pluginName].Constructor = Fidding;
// data-role='emailComplete'
$(document).ready(function () {
$('[data-role="'+Fidding.pluginName+'"]').each(function () {
var _this = $(this);
var data = _this.data();
$.fn[Fidding.pluginName].call(_this, data);
});
});
})