yohocart.js
4.59 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
var jQuery = require('yoho.jquery');
var $yohocookie = require('./yohocookie');
(function($) {
$.fn.miniCart = function(options) {
var defaults = {
'cookie': '_g',
'cartNum': ''
};
var params = $.extend(defaults, options);
var cartInfo = eval('(' + $.cookie(params.cookie) + ')');
if (cartInfo != null) {
var totalNum = parseInt(cartInfo._nac) + parseInt(cartInfo._ac);
if (totalNum == 0) {
$('#icart-num').attr('class', 'icart-num icart-none');
} else {
$('#icart-num').attr('class', 'icart-num');
}
$(params.cartNum).html(totalNum);
}
}
$.fn.search = function(searchDomain) {
var query_num = 0;
var list_index = 0;
var tmp_list = 0;
function getKeywords(obj) {
var key = $.trim($(obj).val());
key = key.replace(new RegExp("'", "gm"), ''); //去掉特殊字符
if (key == '') {
$('.search-list').hide();
return false;
}
key = encodeURI(key); //编码
$.get(searchDomain + '?callback=?&query=' + key, function(htmlData) {
$('.search-list').html(htmlData['data']);
query_num = $('.search-list').children('li').length;
list_index = -1;
if (query_num > 0) {
$('.search-list').show();
} else {
$('.search-list').hide();
}
//绑定事件
$('.search-list').find('a').hover(function() {
$(this).css("background-color", "#eee");
}, function() {
$(this).css("background-color", "#fff");
});
}, 'jsonp');
}
function getText(obj) {
$('.search-list li:eq(' + tmp_list + ')').children('a').css("background-color", "#fff");
$('.search-list li:eq(' + list_index + ')').children('a').css("background-color", "#eee");
var text = $('.search-list li:eq(' + list_index + ')').children('a').attr("title");
$('#query_key').val(text);
}
return this.each(function() {
$(this).keyup(function(event) {
if (event.which == 38) {
if (query_num == 0) {
return false;
}
if (list_index == -1) {
list_index = 0;
}
tmp_list = list_index;
list_index = (list_index - 1 + query_num) % query_num;
getText(this);
} else if (event.which == 40) {
if (query_num == 0) {
return false;
}
tmp_list = list_index;
list_index = (list_index + 1) % query_num;
getText(this);
} else if (event.which == 13) {
submitSearch();
} else {
getKeywords(this);
}
});
});
};
$.extend({
getData: function(domain, options, onSuccess) {
var defaults = {
'page': 1,
'method': '',
'v': 1,
'return_type': 'jsonp',
'open_key': '12345',
'tmp': Math.random()
};
if (typeof(domain) == undefined || domain == '') {
console.log('请设置请求的api地址');
return false;
}
var params = $.extend(defaults, options);
params.page = params.page || 1;
if (params.method == '') {
console.log('请设置请求的URL');
return false;
}
try {
$.getJSON(domain + '/?callback=?', params, function(_data) {
if (onSuccess != '') {
eval(onSuccess(_data.data));
return false;
}
});
} catch (e) {
console.log(e.message);
}
},
getShoppingKey: function() {
var shoppingInfo = $.cookie('_g');
if (typeof shoppingInfo == 'undefined' || shoppingInfo == null) {
return '';
}
var shoppingData = eval('(' + shoppingInfo + ')');
return shoppingData._k;
},
})
})(jQuery);
exports.init = function() {
}