/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["app/core/config","lodash","../../core_module"],function(a){function b(){return{restrict:"E",templateUrl:"public/app/core/components/search/search.html",controller:f,bindToController:!0,controllerAs:"ctrl"}}var c,d,e,f;return a("searchDirective",b),{setters:[function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f=function(){function a(a,b,c,d,e,f){this.$scope=a,this.$location=b,this.$timeout=c,this.backendSrv=d,this.contextSrv=e,this.$rootScope=f,f.onAppEvent("show-dash-search",this.openSearch.bind(this),a),f.onAppEvent("hide-dash-search",this.closeSearch.bind(this),a)}return a.$inject=["$scope","$location","$timeout","backendSrv","contextSrv","$rootScope"],a.prototype.closeSearch=function(){this.isOpen=this.ignoreClose},a.prototype.openSearch=function(){var a=this;return this.isOpen?void(this.isOpen=!1):(this.isOpen=!0,this.giveSearchFocus=0,this.selectedIndex=-1,this.results=[],this.query={query:"",tag:[],starred:!1},this.currentSearchId=0,this.ignoreClose=!0,void this.$timeout(function(){a.ignoreClose=!1,a.giveSearchFocus=a.giveSearchFocus+1,a.query.query="",a.search()},100))},a.prototype.keyDown=function(a){if(27===a.keyCode&&this.closeSearch(),40===a.keyCode&&this.moveSelection(1),38===a.keyCode&&this.moveSelection(-1),13===a.keyCode){if(this.$scope.tagMode){var b=this.results[this.selectedIndex];return void(b&&this.filterByTag(b.term,null))}var c=this.results[this.selectedIndex];c&&(this.$location.search({}),this.$location.path(c.url))}},a.prototype.moveSelection=function(a){var b=(this.results||[]).length,c=this.selectedIndex+a;this.selectedIndex=(c%=b)<0?c+b:c},a.prototype.searchDashboards=function(){var a=this;this.tagsMode=!1,this.currentSearchId=this.currentSearchId+1;var b=this.currentSearchId;return this.backendSrv.search(this.query).then(function(e){b<a.currentSearchId||(a.results=d["default"].map(e,function(a){return a.url="dashboard/"+a.uri,a}),a.queryHasNoFilters()&&a.results.unshift({title:"Home",url:c["default"].appSubUrl+"/",type:"dash-home"}))})},a.prototype.queryHasNoFilters=function(){var a=this.query;return""===a.query&&a.starred===!1&&0===a.tag.length},a.prototype.filterByTag=function(a,b){this.query.tag.push(a),this.search(),this.giveSearchFocus=this.giveSearchFocus+1,b&&(b.stopPropagation(),b.preventDefault())},a.prototype.removeTag=function(a,b){this.query.tag=d["default"].without(this.query.tag,a),this.search(),this.giveSearchFocus=this.giveSearchFocus+1,b.stopPropagation(),b.preventDefault()},a.prototype.getTags=function(){var a=this;return this.backendSrv.get("/api/dashboards/tags").then(function(b){a.tagsMode=!a.tagsMode,a.results=b,a.giveSearchFocus=a.giveSearchFocus+1,a.tagsMode||a.search()})},a.prototype.showStarred=function(){this.query.starred=!this.query.starred,this.giveSearchFocus=this.giveSearchFocus+1,this.search()},a.prototype.search=function(){this.showImport=!1,this.selectedIndex=0,this.searchDashboards()},a}(),a("SearchCtrl",f),e["default"].directive("dashboardSearch",b)}}});