service.searchbox_wev8.js
5.7 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
define("mService/searchbox", ["mUtil"], function(mUtil) {
var storage = {
get: function(key){
return JSON.parse(localStorage.getItem(key) || "[]");
},
set: function(key, value){
try {
var datas = this.get(key) || [], index = datas.indexOf(value), length = datas.length;
if(length && index != -1){
datas.splice(index, 1);
}
if(length > 19){
datas.splice(0, length - 19);
}
datas.push(value);
localStorage.setItem(key, JSON.stringify(datas));
} catch (e) {
mUtil.console.error(e.message);
}
},
clear: function(key){
localStorage.removeItem(key);
}
};
return {
mounted: function($page, cfg){
var defCfg = {
tip: "",
scan: true,
history: true,
closeCallback : function(){mUtil.back();},
success : function(){}
};
$.extend(defCfg, cfg);
var $search = $page.find(".wev-search"),
$searchKey = $page.find("input[type='search']"),
$cancelBtn = $page.find(".btn.cancel"),
$clear = $page.find(".wev-clear-btn"),
$scan = $page.find(".wev-scan-btn"),
$historyWrap = $page.find(".wev-history"),
$historyContent = $page.find(".history-content"),
$delHistory = $page.find(".history-del"),
$noData = $page.find(".wev-no-data");
mUtil.getMultiLabel({
6037: "历史搜索",
6038: "暂无历史搜索记录"
}, function (tips) {
$page.find(".history-title").html(tips['6037']);
$noData.html(tips['6038']);
});
var storageKey = "mobile_searchbox_history_" + defCfg.compid,
recordHistory = defCfg.history;
$page.find("form[disabledEnterSubmit]").keydown(function(event){
if(event.keyCode == 13){return false;}
});
defCfg.scan && $scan.on("click", function(){
Mobile_NS.scanQRCode(function(result){
$searchKey.val(result).trigger("input");
result = result.trim();
defCfg.onSearch && defCfg.onSearch.call(this, result);
if(recordHistory && result){
storage.set(storageKey, result);
history.render();
}
});
}).show();
$searchKey.on("input", function(){
var v = this.value;
$search.toggleClass("wev-has-value", v != "");
}).keyup(function(event){
if(event.keyCode == 13){
var searchKey = this.value;
searchKey = searchKey.trim();
defCfg.onSearch && defCfg.onSearch.call(this, searchKey);
if(recordHistory && searchKey){
storage.set(storageKey, searchKey);
history.render();
}
this.blur();
}
}).attr("placeholder", defCfg.tip).focus();
defCfg.searchkey && !defCfg.toThirdPage && $searchKey.val(defCfg.searchkey).focus().triggerHandler("input");
$cancelBtn.on("click", function(){
var closeCallback = defCfg.closeCallback;
closeCallback && closeCallback();
});
$clear.on("click", function(){
$searchKey.val("").focus().triggerHandler("input");
});
$delHistory.on("click", function(){
mUtil.getLabel(6039, "确认删除全部历史记录?", function(msg){
Mobile_NS.confirm(msg, function(){
$historyContent.empty();
$delHistory.hide();
$noData.show();
storage.clear(storageKey);
});
});
});
$page.find(".history-content").on("click", ".btn", function(){
var searchKey = $(this).data("key") + "";
defCfg.onSearch && defCfg.onSearch.call(this, searchKey);
storage.set(storageKey, searchKey);
history.render();
$searchKey.val(searchKey).triggerHandler("input");
});
var history = {
render: function(){
var hd = storage.get(storageKey).reverse();
if(hd.length){
var templateHtml = [
'{@each datas as d}',
'<div data-key="${d}" class="btn">$${d.replaceAll(" ", " ")}</div>',
'{@/each}'
].join('');
var html = mUtil.parseTemplate(templateHtml, {datas: hd});
$historyContent.html(html);
$delHistory.show();
$noData.hide();
}else{
$delHistory.hide();
$noData.show();
}
}
};
if(recordHistory){
history.render();
}else{
$historyWrap.hide();
storage.clear(storageKey);
}
}
};
});