all files / src/components/search/controllers/ osSearch.ts

51.35% Statements 57/111
20% Branches 6/30
50% Functions 15/30
51.35% Lines 57/111
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166  14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14×                                                                           14×               14× 14× 14×                                                           15×     14× 14×     14×                                                              
"use strict";
var OsSearch = (function () {
    function OsSearch($scope, $element, rx, observeOnScope, $timeout, $http, $document) {
        var _this = this;
        this.$scope = $scope;
        this.$element = $element;
        this.rx = rx;
        this.observeOnScope = observeOnScope;
        this.$timeout = $timeout;
        this.$http = $http;
        this.$document = $document;
        this.searchText = '';
        this.searchResults = {};
        this.searchHidden = false;
        this.subscribe = function (term) {
            var filtered = _this.searchProviders.filter(function (element) {
                return element.minLength <= term.length;
            });
            var observables = filtered.map(function (provider) {
                var providerObservable = _this.createProviderObservable(provider, term);
                providerObservable.providerId = provider.id;
                providerObservable.term = term;
                providerObservable.sent = new Date();
                providerObservable.config = provider;
                return providerObservable;
            });
            observables.forEach(function (providerObservable) {
                providerObservable.subscribe(function (response) {
                    _this.$timeout(function () {
                        if (providerObservable.config.transformResponse) {
                            response = providerObservable.config.transformResponse.call(_this, response);
                        }
                        if (_this.searchText === providerObservable.term) {
                            _this.searchResults[providerObservable.providerId].inProgress = false;
                            _this.searchResults[providerObservable.providerId].results = response.results;
                            _this.searchResults[providerObservable.providerId].error = "";
                            _this.searchResults[providerObservable.providerId].sent = providerObservable.sent;
                            _this.searchResults[providerObservable.providerId].received = new Date();
                            if (!_this.$scope.$$phase) {
                                _this.$scope.$digest();
                            }
                        }
                    });
                }, function (error) {
                    _this.searchResults[providerObservable.providerId].inProgress = false;
                    _this.searchResults[providerObservable.providerId].results = [];
                    _this.searchResults[providerObservable.providerId].error = error.data.error || error.data;
                    _this.searchResults[providerObservable.providerId].received = Infinity;
                    _this.searchResults[providerObservable.providerId].sent = providerObservable.sent;
                });
            });
        };
        this.handleClick = function (event) {
            if (_this.closestByClass(event.target, 'os-header-searchContainer'))
                return false;
            if (!_this.closestByClass(event.target, 'os-search')
                && !_this.closestByClass(event.target, 'os-search-container')) {
                _this.hideSearch();
            }
        };
        this.configProviders();
        this.init();
        this.$scope.$on('osHeader.openSearch', function () {
            _this.open();
        });
    }
    OsSearch.prototype.observableWithAJAXConfig = function (provider, term) {
        var config = {
            params: angular.copy(provider.params),
            data: angular.copy(provider.data),
            dataType: provider.dataType,
            url: provider.url,
            method: provider.method
        };
        for (var k in config.params) {
            config.params[k] = config.params[k].replace('%s', term);
        }
        for (var k in config.data) {
            config.data[k] = config.data[k].replace('%s', term);
        }
        return this.rx.Observable.fromPromise(this.$http(config));
    };
    ;
    OsSearch.prototype.observableFromFn = function (fn, term) {
        return this.rx.Observable.fromPromise(fn(term));
    };
    ;
    OsSearch.prototype.createProviderObservable = function (provider, term) {
        if (provider.hasOwnProperty('fn')) {
            return this.observableFromFn(provider.fn, term);
        }
        else {
            return this.observableWithAJAXConfig(provider, term);
        }
    };
    ;
    OsSearch.prototype.configProviders = function () {
        this.internalSearchProviders = this.searchProviders.reduce(function (providerHashMap, provider) {
            providerHashMap[provider.id] = provider;
            return providerHashMap;
        }, {});
    };
    OsSearch.prototype.init = function () {
        var _this = this;
        var throttledInput = this.observeOnScope(this.$scope, 'osSearch.searchText').debounce(200).map(function (e) {
            return e.newValue;
        }).distinctUntilChanged();
        throttledInput.filter(function (term) {
            _this.searchProviders.forEach(function (provider) {
                _this.searchResults[provider.id] = _this.searchResults[provider.id] || {};
                _this.searchResults[provider.id].providerId = provider.id;
                _this.searchResults[provider.id].title = provider.title;
                _this.searchResults[provider.id].width = provider.columnWidth;
                _this.searchResults[provider.id].results = [];
            });
            return term.toLowerCase() && term.length;
        }).subscribe(this.subscribe);
    };
    OsSearch.prototype.clear = function () {
        this.searchText = '';
    };
    OsSearch.prototype.open = function () {
        this.searcherHidden = false;
        this.bindEvents();
    };
    OsSearch.prototype.close = function () {
        this.searcherHidden = true;
        this.unBindEvents();
    };
    OsSearch.prototype.closestByClass = function (el, className) {
        while (!(el.classList.contains(className))) {
            el = el.parentNode;
            if (!el || !el.classList) {
                return null;
            }
        }
        return el;
    };
    OsSearch.prototype.bindEvents = function () {
        angular.element(this.$document.querySelector('html')).on('click', this.handleClick);
    };
    OsSearch.prototype.unBindEvents = function () {
        angular.element(this.$document.querySelector('html')).off('click', this.handleClick);
    };
    OsSearch.prototype.resultsAvailable = function () {
        var _this = this;
        return this.searchProviders.filter(function (provider) {
            var sr = _this.searchResults[provider.id];
            return sr && (sr.inProgress || sr.error || sr.results.length > 0);
        }).length > 0;
    };
    OsSearch.prototype.hideSearch = function () {
        this.searchHidden = true;
        this.close();
    };
    OsSearch.prototype.selectResult = function (result, cb) {
        if (cb) {
            cb.call(null, result, this.hideSearch.bind(this));
        }
    };
    OsSearch.$inject = ['$scope', '$element', 'rx', 'observeOnScope', '$timeout', '$http', '$document'];
    return OsSearch;
}());
exports.OsSearch = OsSearch;