-
Notifications
You must be signed in to change notification settings - Fork 0
/
gender.js
182 lines (148 loc) · 4.95 KB
/
gender.js
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
/*global window: false, $:false, document:false, jQuery:false, XDomainRequest:false*/
/*!
* gender.js v0.5
* Copyright 2014 gender-api.com
* https://github.com/markus-perl/gender-api/blob/master/LICENSE
*
*
*/
(function ($) {
var GenderApi = function (element, config) {
var TYPE_NAME = 'name';
var TYPE_EMAIL = 'email';
var $this = $(element);
var block = false; //only 1 request at a time
var timeout = 500; // 500 ms
var timer = null;
var currentQuery = null;
var attached = true; //enables or disables the detection
var type = TYPE_NAME;
/**
* Can be called to disable detection
* $(element).genderApi('detach');
*/
$this.detachApi = function () {
attached = false;
};
/**
* Can be called to enable detection
* $(element).genderApi('attach');
*/
$this.attachApi = function () {
attached = true;
};
var data = {
ip: 'auto'
};
if (config.key) {
data.key = config.key;
}
if (config.country) {
data.country = config.country;
}
if (config.language) {
data.language = config.language;
}
if (config.type) {
type = config.type;
}
var protocol = 'https:' == document.location.protocol ? 'https://' : 'http://';
var url = config.url ? config.url : protocol + 'gender-api.com/get';
$this.query = function (value, callback) {
value = $.trim(value);
if ((type == TYPE_EMAIL && value.indexOf('@') > 0) || ( type == TYPE_NAME && value.length > 1)) {
if (type == TYPE_EMAIL) {
data.email = value;
} else {
data.name = value;
}
if (block == true) return;
if (value == currentQuery) return;
block = true;
currentQuery = value;
if (!$.support.cors && $.ajaxTransport && window.XDomainRequest) {
var xdr = new XDomainRequest();
if (xdr) {
if (window.location && window.location.href) {
data.ref = window.location.href;
}
xdr.onload = function () {
var result = $.parseJSON(xdr.responseText);
callback(result);
block = false;
};
var dataString = '';
$.each(data, function (key, value) {
dataString += '&' + key + '=' + encodeURIComponent(value);
});
xdr.open("get", url + '?' + dataString.substr(1));
xdr.send();
}
} else {
$.ajax({
url: url,
data: data,
dataType: 'json'
}).done(function (result) {
callback(result);
block = false;
});
}
}
else {
callback({name: value, gender: null});
}
};
if (config.hasOwnProperty('name') && config.callback) {
$this.query(config.name, config.callback);
return;
}
var startTimer = function () {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(parse, timeout);
};
var parse = function () {
if (attached) {
var value = $this.val();
$this.query(value, function (result) {
if (result.gender) {
$this.trigger('gender-found', result);
}
block = false;
});
}
};
$this.on({
'change': parse,
'focusout': parse,
'keyup': startTimer,
'paste': function () {
setTimeout(parse, 100)
}
});
$this.data('genderapi', $this);
};
/** @namespace $.fn */
$.fn.genderApi = function (config) {
return this.each(function () {
var api = $(this).data('genderapi');
switch (config) {
case 'detach':
if (api) {
api.detachApi();
return api;
}
return null;
case 'attach':
if (api) {
api.attachApi();
return api;
}
return null;
}
return new GenderApi(this, config);
});
}
})(jQuery);