1 | (function () {
|
---|
2 | angular.module('search')
|
---|
3 | .controller('SearchCtrl', ['$scope', '$http', 'SearchService', '$filter', '$window', function ($scope, $http, SearchService, $filter, $window) {
|
---|
4 | $scope.filter = {
|
---|
5 | includeDocuments: false,
|
---|
6 | pageSize: 10,
|
---|
7 | dateFrom: null,
|
---|
8 | dateTo: null
|
---|
9 | };
|
---|
10 |
|
---|
11 | $scope.displayTooltip = function (prop) {
|
---|
12 | $scope[prop] = !$scope[prop];
|
---|
13 |
|
---|
14 | if ($scope[prop]) {
|
---|
15 | $window.onclick = function (event) {
|
---|
16 | if (event.target.id != prop) {
|
---|
17 | $scope.$apply(function () {
|
---|
18 | $scope[prop] = false;
|
---|
19 | });
|
---|
20 | }
|
---|
21 | };
|
---|
22 | }
|
---|
23 | };
|
---|
24 |
|
---|
25 | $scope.siteSections = [];
|
---|
26 | $scope.newsAndEventsIds = [];
|
---|
27 | $scope.searchService = {};
|
---|
28 | $scope.searchedTerm = "";
|
---|
29 | $scope.searchResults = [];
|
---|
30 | $scope.searchPerformed = false;
|
---|
31 | $scope.datepickerDisabled = true;
|
---|
32 |
|
---|
33 | $scope.pauseSearch = function () {
|
---|
34 | return !!($scope.searchPerformed === false || $scope.searchService.busy);
|
---|
35 | };
|
---|
36 |
|
---|
37 | $scope.hasSearchResults = function () {
|
---|
38 | return $scope.searchPerformed && !$scope.loadingAsyncResults() && $scope.searchService.results.length > 0;
|
---|
39 | };
|
---|
40 |
|
---|
41 | $scope.loadingAsyncResults = function () {
|
---|
42 | return $scope.searchService.busy;
|
---|
43 | };
|
---|
44 |
|
---|
45 | $scope.noResults = function () {
|
---|
46 | return $scope.searchPerformed && $scope.searchService.results.length === 0 && !$scope.loadingAsyncResults();
|
---|
47 | };
|
---|
48 |
|
---|
49 | $scope.getSearchResults = function () {
|
---|
50 | $scope.filter.searchedTerm = $scope.filter.search;
|
---|
51 | $scope.searchService = new SearchService.GetInstance($scope.filter);
|
---|
52 | $scope.searchService.nextPage(); // 1st set of results
|
---|
53 | $scope.searchPerformed = true;
|
---|
54 | };
|
---|
55 |
|
---|
56 | $scope.$watch('filter.searchPassed', function (val) {
|
---|
57 | if (val === true) {
|
---|
58 | $scope.getSearchResults();
|
---|
59 | }
|
---|
60 | });
|
---|
61 |
|
---|
62 | // Enable Datepicker when news or events item is selected
|
---|
63 | $scope.$watch('filter.siteSections', function (siteSections) {
|
---|
64 | if (siteSections && (siteSections.length <= $scope.newsAndEventsIds.length)) {
|
---|
65 |
|
---|
66 | var shouldEnableDatepicker = false;
|
---|
67 |
|
---|
68 | angular.forEach($scope.newsAndEventsIds, function (newsAndEventsId) {
|
---|
69 | if (siteSections.indexOf(newsAndEventsId) > -1) {
|
---|
70 | shouldEnableDatepicker = true;
|
---|
71 | }
|
---|
72 | });
|
---|
73 |
|
---|
74 | $scope.datepickerDisabled = !shouldEnableDatepicker;
|
---|
75 | } else {
|
---|
76 | $scope.datepickerDisabled = true;
|
---|
77 | $scope.filter.dateFrom = "";
|
---|
78 | $scope.filter.dateTo = "";
|
---|
79 | }
|
---|
80 | });
|
---|
81 |
|
---|
82 |
|
---|
83 | getNavSections();
|
---|
84 |
|
---|
85 | // Private function to populate site-sections drop down
|
---|
86 | function getNavSections() {
|
---|
87 | $http.get("/api/cms/GetNavSections")
|
---|
88 | .success(function (data, status) {
|
---|
89 | $scope.siteSections = data;
|
---|
90 | $scope.newsAndEventsIds = findNewsOrEventItems(data);
|
---|
91 | })
|
---|
92 | .error(function (data, status) {
|
---|
93 | $scope.siteSections = [];
|
---|
94 | // console.error("Could not get nav sections from service:", status);
|
---|
95 | });
|
---|
96 |
|
---|
97 | function findNewsOrEventItems(navObjectArray) {
|
---|
98 | var newsAndEventIds = [];
|
---|
99 |
|
---|
100 | var newsAndEventObjects = $filter('filter')(navObjectArray, function (item) {
|
---|
101 | return (item.Heading).toLowerCase().indexOf('news') >= 0 || (item.Heading).toLowerCase().indexOf('event') >= 0;
|
---|
102 | });
|
---|
103 |
|
---|
104 | angular.forEach(newsAndEventObjects, function (item) {
|
---|
105 | this.push(item.TargetId);
|
---|
106 | }, newsAndEventIds);
|
---|
107 |
|
---|
108 | return newsAndEventIds;
|
---|
109 | };
|
---|
110 | };
|
---|
111 |
|
---|
112 | }]);
|
---|
113 | }()); |
---|