searchController.js 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

19 20
'use strict';

21 22
angular.module('dgc.search').controller('SearchController', ['$scope', '$location', '$http', '$state', '$stateParams', 'SearchResource', 'NotificationService',
    function($scope, $location, $http, $state, $stateParams, SearchResource, NotificationService) {
23

24
        $scope.types = ['table','column','db','view','loadprocess','storagedesc'];
25
        $scope.results = [];
26
        $scope.resultCount=0;
27
        $scope.isCollapsed = true;
28 29 30 31 32 33 34 35 36 37
        $scope.currentPage = 1;
        $scope.numPerPage = 10;
        $scope.itemsPerPage = 2;
        $scope.maxSize = 5;
        $scope.$watch("currentPage + numPerPage", function() {
            var begin = (($scope.currentPage - 1) * $scope.numPerPage);
            var end = begin + $scope.numPerPage;

            $scope.filteredResults = $scope.results.slice(begin, end);
        });
38
        $scope.search = function(query) {
39 40
            $scope.results = [];
            NotificationService.reset();
41
            $scope.limit = 4;
42 43 44
            SearchResource.search({query:query}, function searchSuccess(response) {
                $scope.results = response.results;
                $scope.resultCount=response.count;
45
                if ($scope.results.length < 1) {
46
                    NotificationService.error('No Result found', false);
47
                }
48 49
                $state.go('search.results', {query:query}, {
                    location: 'replace'
50
                });
51 52
            }, function searchError(err) {
                NotificationService.error('Error occurred during executing search query, error status code = ' + err.status + ', status text = ' + err.statusText, false);
53 54
            });
        };
55

56
        $scope.typeAvailable = function() {
57
            return $scope.types.indexOf(this.results.dataType.typeName && this.results.dataType.typeName.toLowerCase()) > -1;
58 59
        };

60 61 62 63
        $scope.doToggle = function($event,el) {
            this.isCollapsed = !el;
            var currElem = $event.currentTarget;
        };
64
        $scope.filterSearchResults = function(items) {
65 66 67
            var res = {};
            angular.forEach(items, function(value, key) {
                if(!(typeof value == 'object'))
68
                    res[key] = value;
69 70 71 72
            });
            return res;
        }

73
        $scope.query=$stateParams.query;
74
        if ($scope.query) {
75
            $scope.search($scope.query);
76 77 78
        }
    }
]);