稍微完善以下历史搜索

This commit is contained in:
luchenqun 2017-04-04 22:28:02 +08:00
parent c2a71e16b9
commit 560d3f8515
2 changed files with 58 additions and 37 deletions

View File

@ -69,7 +69,6 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time
$window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(searchWord), '_blank'); $window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(searchWord), '_blank');
} }
// 如果第一个显示的搜索分类跟关键字跟列表一样,那么不要更新
var newItem = { var newItem = {
t: searchOption, t: searchOption,
d: searchWord, d: searchWord,
@ -85,33 +84,10 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time
$scope.searchHistory.splice(delIndex, 1); $scope.searchHistory.splice(delIndex, 1);
} }
var datas = [];
$scope.searchHistory.slice(0, 15); // 最多保留15个历史记录
$scope.searchHistory.forEach((item, index) => {
datas.push({
t: item.t,
d: item.d,
})
})
var parmes = {
searchHistory: JSON.stringify(data),
};
// 大于30的不保存到数据库 // 大于30的不保存到数据库
if (searchWord.length >= 30) { if (searchWord.length <= 30) {
return; saveHistory();
} }
bookmarkService.updateSearchHistory(parmes)
.then((data) => {
if (data.retCode == 0) {
// toastr.success('历史搜索更新成功', "提示");
} else {
toastr.error('历史搜索更新失败。错误信息:' + data.msg, "错误");
}
})
.catch((err) => {
toastr.error('历史搜索更新失败。错误信息:' + JSON.stringify(err), "错误");
});
} }
$scope.searchByHistory = function(type, data) { $scope.searchByHistory = function(type, data) {
@ -125,9 +101,28 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time
types[4] = '百度'; types[4] = '百度';
$('.js-search-option').dropdown('set text', types[type]); $('.js-search-option').dropdown('set text', types[type]);
$('.js-search-option').dropdown('save defaults', types[type]); $('.js-search-option').dropdown('save defaults', types[type]);
$('.js-search-option .menu .item').removeClass('active');
$('.js-search-option .menu .item:eq(' + type + ')').addClass('active');
$('.js-history-popup').removeClass('visible').addClass('hidden');
$scope.search(data); $scope.search(data);
} }
$scope.delHistory = function(type, data) {
var delIndex = -1;
$scope.searchHistory.forEach((item, index) => {
if (index >= 1 && item.t == type && item.d == data) {
delIndex = index;
}
})
if (delIndex >= 0) {
$scope.searchHistory.splice(delIndex, 1);
}
saveHistory();
$timeout(function() {
$('.js-history-popup').removeClass('hidden').addClass('visible');
}, 500)
}
$scope.updateShowStyle = function(showStyle) { $scope.updateShowStyle = function(showStyle) {
console.log('updateShowStyle', showStyle); console.log('updateShowStyle', showStyle);
$scope.showStyle = showStyle; $scope.showStyle = showStyle;
@ -159,16 +154,39 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time
$('.ui.menu a.item:eq(' + index + ')').addClass('selected'); $('.ui.menu a.item:eq(' + index + ')').addClass('selected');
} }
function saveHistory() {
var datas = [];
$scope.searchHistory.slice(0, 15); // 最多保留15个历史记录
$scope.searchHistory.forEach((item, index) => {
datas.push({
t: item.t,
d: item.d,
})
})
var parmes = {
searchHistory: JSON.stringify(datas),
};
bookmarkService.updateSearchHistory(parmes)
.then((data) => {
if (data.retCode == 0) {
// toastr.success('历史搜索更新成功', "提示");
} else {
toastr.error('历史搜索更新失败。错误信息:' + data.msg, "错误");
}
})
.catch((err) => {
toastr.error('历史搜索更新失败。错误信息:' + JSON.stringify(err), "错误");
});
}
bookmarkService.userInfo({}) bookmarkService.userInfo({})
.then((data) => { .then((data) => {
$scope.searchHistory = JSON.parse(data.search_history || '[]'); $scope.searchHistory = JSON.parse(data.search_history || '[]');
$timeout(function() { $('.search-item').popup({
$('.search-item') on: 'focus',
.popup({ inline: true
on: 'focus', });
inline: true
});
}, 500)
}) })
.catch((err) => { .catch((err) => {
toastr.error('获取信息失败。错误信息:' + JSON.stringify(err), "错误"); toastr.error('获取信息失败。错误信息:' + JSON.stringify(err), "错误");

View File

@ -51,13 +51,16 @@
</div> </div>
</label> </label>
<input class="prompt search-item" type="text" ng-model="searchWord" placeholder="" ng-keypress="($event.which === 13)?search(searchWord):0" data-position="bottom left" data-variation="large" id="lcq"> <input class="prompt search-item" type="text" ng-model="searchWord" placeholder="" ng-keypress="($event.which === 13)?search(searchWord):0" data-position="bottom left" data-variation="large" id="lcq">
<div class="ui fluid popup top left transition hidden"> <div class="ui fluid popup top left transition hidden js-history-popup" style="padding-left: 0px; padding-right: 0px;" ng-if="searchHistory.length>0">
<div class="ui selection list"> <div class="ui selection list">
<div class="item" ng-repeat="item in searchHistory" ng-click="searchByHistory(item.t, item.d)"> <div class="item" ng-repeat="item in searchHistory" ng-click="searchByHistory(item.t, item.d)" style="height:30px;">
<div class="right floated content"> <div class="right floated content">
{{ item.t | searchType }} <span style="margin-right:10px;">{{ item.t | searchType }}
<i class="remove icon" ng-click="delHistory(item.t, item.d);$event.stopPropagation()" style="cursor:default;"></i>
</span>
</div> </div>
<div class="content">{{ item.d}} <div class="content">
<span style="margin-left:10px;">{{ item.d}}</span>
</div> </div>
</div> </div>
</div> </div>