-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.domainsbot.js
409 lines (307 loc) · 8.62 KB
/
jquery.domainsbot.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*Copyright 2012, DomainsBot Inc. */
(function ($) {
var data = [];
var maxCounter = 50;
var checkDelay = 10;
var isStart = false;
var methods = {
init: function (options) {
//checkDelay = options.delay;
this.each(function () {
var dummy ={};
dummy.target = $(this);
dummy.name = "";
dummy.isTyping = false;
dummy.notChangedCounter = 0;
dummy.onType = options.onType;
dummy.onTyped = options.onTyped;
data.push(dummy);
});
isStart = true;
setTimeout(CheckTyping, checkDelay);
return this;
},
start: function () {
},
destroy: function () {
data =[];
maxCounter = 50;
checkDelay = 10;
isStart = false;
}
};
$.fn.InstantSearch = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.InstantSearch');
}
};
function CheckTyping() {
if (!isStart)
return;
$.each(data, function (index, item) {
//Changed?
var text = item.target.val();
if (item.name != text) {
item.isTyping = true;
item.notChangedCounter = 0;
item.name = text;
// Callback
if(item.onType != null)
item.onType(text);
}
else if (item.notChangedCounter >= 0) {
item.notChangedCounter++;
}
if (item.notChangedCounter > maxCounter && item.name != "") {
item.isTyping = false;
item.notChangedCounter = -1;
if(item.onTyped != null)
item.onTyped(text);
}
});
setTimeout(CheckTyping, checkDelay);
}
})(jQuery);
(function( $ ) {
var DomainsBotApi = function(options)
{
var loader;
var checking;
if(typeof options.urlCheckout == "string")
{
var defaultDb = "available";
if(options.parameters != null && options.parameters.database != null)
{
defaultDb = options.parameters.database.split(" ")[0];
}
var checkoutHash = {};
checkoutHash[defaultDb] = options.urlCheckout;
options.urlCheckout = checkoutHash;
}
var settings = $.extend( {
'urlApi' : 'https://api-2445581410012.apicast.io/v5/recommend',
'urlCheckout' : null,
'searchTextbox' : null,
'searchSubmit' : null,
'loading' : null,
'checking' : null,
'results' : null,
'searchParameter' : null,
'parameters' : null,
'onSuccess' : null,
'onError' : null,
'onLoading' : null,
'onCheckout' : null,
'autoComplete' : false
}, options);
var getDataField = function (fieldName,data)
{
for(var i = 0; i<data.length; i++)
{
if(data[i].Name == fieldName)
return data[i].Data;
}
return null;
};
var getUrlVars = function ()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
};
var GetDomains = function (key,options)
{
var cnt = 0; var i;
// Define an empty string
var postString = "";
// Binds the post string with key term and suggestion
postString = "q=" + escape(key);
postString += "&callback=_domainsbot";
for(var index in options.parameters) {
postString += "&"+index+"="+escape(options.parameters[index]);
}
if(options.results != null)
$(options.results).html("");
if(options.loading != null){
$(options.results).append(loader);
// Set teh ajax loader image
$(loader).css('display','block');
}
if(options.onLoading != null){
options.onLoading();
}
$.ajax({
url:options.urlApi +"?" + postString,
dataType: 'jsonp',
jsonpCallback: '_domainsbot',
success:function(data)
{
if(data === null || data.error != null){
if(options.onError != null){
options.onError(data.error);
}
if(options.results != null)
{
$(options.results).html(data.error.message);
}
return;
}
if(options.onSuccess != null){
options.onSuccess(data);
}
if(options.results != null){
$(options.results).html("");
var htmlItem = "";
if(data){
$.each(data, function(i,domain){
htmlItem += "<div >";
var database = null;
if(domain.Data != null && domain.Data.length > 0)
{
database = getDataField("database", domain.Data[0]);
}
var url = "";
if(options.urlCheckout != null && database != null)
{
url = options.urlCheckout[database].replace("%domain%",domain.DomainName);
}
// cart url
htmlItem += "<a href='"+url+"' bind='domainsbotDomainLink' domainName='"+domain.Domain+"' >"+domain.Domain+"</a>";
//htmlItem += "<div bind='domainsbotDomain' index='"+i+"' domainName = '"+domain.DomainName+"' >" + ((options.checking != null && ((options.urlAvailability != null && options.urlAvailability != "") || (options.onAvailabilitySuccess != null && options.onAvailabilitySuccess != ""))) ? $(options.checking).clone().css('display','block').wrap('<p>').parent().html() : "" ) +"</div>";
htmlItem += "</div>";
});
}
else
{
if(options.onError != null){
options.onError(data);
}
}
$(options.results).html(htmlItem);
if(options.onCheckout != null)
{
$(options.results).find("a[bind='domainsbotDomainLink']").click(function(evt)
{
options.onCheckout({ Domain: $(this).attr("domainName")}, evt );
});
}
}
},
error: function(xhr, ajaxOptions, thrownError)
{
if(options.onError != null){
options.onError(thrownError);
}
else if(options.results != null)
{
$(options.results).html(thrownError);
}
}
});
};
if(settings.searchTextbox != null){
// Sets focus the search text box
$(settings.searchTextbox).focus();
// Enter button
// Check for Key down event on Search text box
$(settings.searchTextbox).keydown(function(event) {
// Check if user hits the <enter>
if(event.keyCode == 13){
// calls to function
GetDomains($(settings.searchTextbox).val(),settings);
}
});
if(settings.autoComplete)
{
$(settings.searchTextbox).InstantSearch({
onTyped: function(text){
if(text != null && text != "")
{
GetDomains(text,settings);
}
else
{
if(options.results != null)
$(options.results).html("");
}
},
onType: function(text){
if(options.results != null)
$(options.results).html("");
if(text != null && text != "")
{
if(options.loading != null){
$(options.results).append(loader);
// Set teh ajax loader image
$(loader).css('display','block');
}
if(options.onLoading != null){
options.onLoading();
}
}
}
});
}
else if($(settings.searchTextbox).val().length > 0)
{
GetDomains($(settings.searchTextbox).val(),settings);
}
}
if(settings.loading != null){
// Sets variable with ajax loader image
$(settings.loading).css('display','none');
loader = $(settings.loading).clone();
//this.loader.remove();
}
if(settings.checking != null){
// Sets variable with ajax loader image
$(settings.checking).css('display','none');
checking = $(settings.checking).clone();
//this.checking.remove();
}
if(settings.searchSubmit != null && settings.searchTextbox != null){
// Check the click event, search btn pressed
$(settings.searchSubmit).click(function(){
//call the function to get result
GetDomains($(settings.searchTextbox).val(), settings);
});
}
if(settings.searchParameter != null && settings.searchParameter != ""){
var p = getUrlVars();
if(p != null && p[settings.searchParameter] != null && p[settings.searchParameter] != ""){
console.log(p[settings.searchParameter] );
GetDomains(p[settings.searchParameter], settings);
}
}
// Public method
this.search = function(key)
{
GetDomains(key, settings);
};
};
var methods = {
init : function( options ) {
return new DomainsBotApi(options);
}
};
$.fn.domainsbot = function ( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.domainsbot' );
}
};
})( jQuery );