-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (72 loc) · 1.57 KB
/
script.js
File metadata and controls
80 lines (72 loc) · 1.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
/**
* @developer wpdevelopment.me <shramee@wpdvelopment.me>
*/
(
function ( $ ) {
var ajaxRef, lastResponse, $r, items;
$( '.sfp-live-search-field' ).keyup( function () {
var $t = $( this ),
val = $t.val();
$r = $t.siblings( '.sfp-live-search-results' );
if ( ajaxRef && ajaxRef.abort ) {
ajaxRef.abort();
}
timeOut = null;
if ( 1 > val.length ) {
$r.html( '' );
return;
}
var keyword = encodeURIComponent( val ).toLowerCase();
$r.html( '' );
items = 0;
var $cnt = $( '<div></div>' );
$.each( wclsAjax.prods, function ( l, itm ) {
if ( itm.title.toLowerCase().indexOf( keyword ) > - 1 ) {
if ( items > 7 ) {
return false;
}
addItem( itm, $cnt );
items ++;
}
} );
$r.append( $cnt );
/*
ajaxRef = $
.ajax( {
method: 'GET',
url: wclsAjax.url + '?s=' + keyword,
} )
.done( parseResp );
*/
} );
function addItem( itm, $cnt ) {
$cnt.append(
"<a class='wcls-prod' href='" + itm.url + "'>" +
(
itm.img ? "<img src='" + wclsAjax.upload_dir + '/' + itm.img + "'>" : ''
) +
itm.title + "</a>"
);
}
parseResp = function ( r ) {
$r.html( '' );
if ( typeof r === 'string' ) {
r = JSON.parse( r );
}
if ( r ) {
lastResponse = r;
$.each( r, function ( k, e ) {
if ( 0 !== k.indexOf( '_' ) ) {
var $cnt = $( '<div><h3>' + k + '</h3></div>' );
$.each( e, function ( l, itm ) {
addItem( itm, $cnt )
} );
}
$r.append( $cnt );
} );
} else {
console.log( r )
}
}
}
)( jQuery );