resourceSearchCtrl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. angular.module('push')
  2. .controller('resourceSearchCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $http, $ionicHistory, $stateParams) {
  3. //本地获取历史搜索记录
  4. $scope.searchKeyList = JSON.parse(localStorage.getItem("resourceSearchKeyList"));
  5. if (angular.isUndefined($scope.searchKeyList) || $scope.searchKeyList == null) {
  6. $scope.searchKeyList = [];
  7. $scope.showHistory = false;
  8. } else {
  9. $scope.showHistory = true;
  10. }
  11. //删除单个历史记录
  12. $scope.onItemDelete = function (key) {
  13. $scope.searchKeyList.splice($scope.searchKeyList.indexOf(key), 1);
  14. var str = JSON.stringify($scope.searchKeyList);
  15. localStorage.setItem("resourceSearchKeyList", str);
  16. };
  17. //删除全部历史记录
  18. $scope.onItemDeleteAll = function () {
  19. $scope.searchKeyList = [];
  20. $scope.showHistory = false;
  21. var str = JSON.stringify($scope.searchKeyList);
  22. localStorage.setItem("resourceSearchKeyList", str);
  23. };
  24. //提交搜索关键词跳转到搜索结果页面
  25. $scope.SearchWordSubmit = function (key) {
  26. $timeout(function () {
  27. if (angular.isDefined(key) && key.length > 0) {
  28. var word = angular.copy(key);
  29. var index = $scope.searchKeyList.indexOf(word);
  30. if (index == -1) {
  31. if ($scope.searchKeyList.length >= 5) {
  32. $scope.searchKeyList.pop();
  33. $scope.searchKeyList.unshift(word);
  34. } else {
  35. $scope.searchKeyList.unshift(word);
  36. }
  37. } else {
  38. $scope.searchKeyList.splice(index, 1);
  39. $scope.searchKeyList.unshift(word);
  40. }
  41. }
  42. var str = JSON.stringify($scope.searchKeyList);
  43. localStorage.setItem("resourceSearchKeyList", str);
  44. }, 50);
  45. //跳转搜索结果页
  46. $ionicHistory.clearCache(["resourceSearchResult"]);
  47. $state.go('resourceSearchResult', {
  48. key: key,
  49. resourcetype: $stateParams.resourcetype,
  50. reclibid: $stateParams.reclibid,
  51. tabindex: $stateParams.tabindex
  52. });
  53. };
  54. $scope.goSearchResult = function (key) {
  55. $ionicHistory.clearCache(["resourceSearchResult"]);
  56. $state.go('resourceSearchResult', {
  57. key: key,
  58. resourcetype: $stateParams.resourcetype,
  59. reclibid: $stateParams.reclibid,
  60. tabindex: $stateParams.tabindex
  61. });
  62. };
  63. $scope.searchCont = {};
  64. //清空搜索内容
  65. $scope.clearSearch = function () {
  66. $scope.searchCont.key = "";
  67. $scope.showHistory = true;
  68. };
  69. //监测key变化,即时匹配词库
  70. $scope.fixKey = function () {
  71. if (angular.isDefined($scope.searchCont.key) && $scope.searchCont.key.length > 0) {
  72. $scope.searchKey($scope.searchCont.key);
  73. }
  74. };
  75. $scope.data = [];
  76. //百度词库
  77. $scope.searchKey = function (searchName) {
  78. var search_history = angular.element(document.getElementById("his_search"));
  79. if (search_history.val().length != 0) {
  80. var baidu = angular.element(document.getElementById("baidu_search"));
  81. baidu.css("display", "block");
  82. }
  83. $http({
  84. method: 'JSONP',
  85. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK'
  86. }).success(function (data) {
  87. $scope.data = data.s;
  88. });
  89. };
  90. $scope.gobackback = function () {
  91. $timeout(function () {
  92. $scope.goback();
  93. }, 350)
  94. };
  95. });