connSearchCtrl.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. angular.module('push')
  2. .controller('ConnSearchCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $http, $ionicHistory, $stateParams) {
  3. //本地获取历史搜索记录
  4. $scope.searchKeyList = JSON.parse(localStorage.getItem("connSearchKeyList"));
  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("connSearchKeyList", str);
  16. };
  17. //删除全部历史记录
  18. $scope.onItemDeleteAll = function () {
  19. $scope.searchKeyList = [];
  20. $scope.showHistory = false;
  21. var str = JSON.stringify($scope.searchKeyList);
  22. localStorage.setItem("connSearchKeyList", 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("connSearchKeyList", str);
  44. }, 50);
  45. //跳转搜索结果页
  46. $ionicHistory.clearCache(["connsearchresult"]);
  47. $state.go('connsearchresult', {
  48. key: key,
  49. folderid: $stateParams.folderid,
  50. localfolderid: $stateParams.localfolderid
  51. });
  52. };
  53. $scope.goSearchResult = function (key) {
  54. $ionicHistory.clearCache(["connsearchresult"]);
  55. $state.go('connsearchresult', {
  56. key: key,
  57. folderid: $stateParams.folderid,
  58. localfolderid: $stateParams.localfolderid
  59. });
  60. };
  61. $scope.searchCont = {};
  62. //清空搜索内容
  63. $scope.clearSearch = function () {
  64. $scope.searchCont.key = "";
  65. $scope.showHistory = true;
  66. };
  67. //监测key变化,即时匹配词库
  68. $scope.fixKey = function () {
  69. if (angular.isDefined($scope.searchCont.key) && $scope.searchCont.key.length > 0) {
  70. $scope.searchKey($scope.searchCont.key);
  71. }
  72. };
  73. $scope.data = [];
  74. //百度词库
  75. $scope.searchKey = function (searchName) {
  76. var search_history = angular.element(document.getElementById("his_search"));
  77. if (search_history.val().length != 0) {
  78. var baidu = angular.element(document.getElementById("baidu_search"));
  79. baidu.css("display", "block");
  80. }
  81. $http({
  82. method: 'JSONP',
  83. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK'
  84. }).success(function (data) {
  85. $scope.data = data.s;
  86. });
  87. };
  88. $scope.gobackback = function () {
  89. $timeout(function () {
  90. $scope.goback();
  91. }, 300)
  92. }
  93. });