text: /*! grafana - v4.0.0-1478693311beta1 - 2016-11-09
   * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */
  
  System.register(["angular","lodash","app/core/core_module","./variable"],function(a){var b,c,d,e,f;return{setters:[function(a){b=a},function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f=function(){function a(a,b,c,d,e){this.$rootScope=a,this.$q=b,this.$location=c,this.$injector=d,this.templateSrv=e,a.$on("refresh",this.onDashboardRefresh.bind(this),a),a.$on("template-variable-value-updated",this.updateUrlParamsWithCurrentVariables.bind(this),a)}return a.$inject=["$rootScope","$q","$location","$injector","templateSrv"],a.prototype.init=function(a){var b=this;this.dashboard=a,this.variables=a.templating.list.map(this.createVariableFromModel.bind(this)),this.templateSrv.init(this.variables),this.dashboard.events.on("prepare-save-model",this.syncToDashboardModel.bind(this));for(var c=0,d=this.variables;c<d.length;c++){var e=d[c];e.initLock=this.$q.defer()}var f=this.$location.search();return this.$q.all(this.variables.map(function(a){return b.processVariable(a,f)})).then(function(){b.templateSrv.updateTemplateData()})},a.prototype.onDashboardRefresh=function(){var a=this,c=this.variables.filter(function(a){return 2===a.refresh}).map(function(c){var d=c.options.slice();return c.updateOptions().then(a.variableUpdated.bind(a,c)).then(function(){b["default"].toJson(d)!==b["default"].toJson(c.options)&&a.$rootScope.$emit("template-variable-value-updated")})});return this.$q.all(c)},a.prototype.processVariable=function(a,b){for(var c=this,d=[],e=0,f=this.variables;e<f.length;e++){var g=f[e];a.dependsOn(g)&&d.push(g.initLock.promise)}return this.$q.all(d).then(function(){var c=b["var-"+a.name];return void 0!==c?a.setValueFromUrl(c).then(a.initLock.resolve):1===a.refresh||2===a.refresh?a.updateOptions().then(a.initLock.resolve):void a.initLock.resolve()})["finally"](function(){c.templateSrv.variableInitialized(a),delete a.initLock})},a.prototype.createVariableFromModel=function(a){var b=e.variableTypes[a.type].ctor;if(!b)throw"Unable to find variable constructor for "+a.type;var c=this.$injector.instantiate(b,{model:a});return c},a.prototype.addVariable=function(a){var b=this.createVariableFromModel(a);return this.variables.push(this.createVariableFromModel(b)),b},a.prototype.syncToDashboardModel=function(){this.dashboard.templating.list=this.variables.map(function(a){return a.getModel()})},a.prototype.updateOptions=function(a){return a.updateOptions()},a.prototype.variableUpdated=function(a){var b=this;if(a.initLock)return this.$q.when();var d=c["default"].map(this.variables,function(c){if(c!==a)return c.dependsOn(a)?b.updateOptions(c):void 0});return this.$q.all(d)},a.prototype.selectOptionsForCurrentValue=function(a){var b,d,e,f,g=[];for(b=0;b<a.options.length;b++)if(f=a.options[b],f.selected=!1,c["default"].isArray(a.current.value))for(d=0;d<a.current.value.length;d++)e=a.current.value[d],f.value===e&&(f.selected=!0,g.push(f));else f.value===a.current.value&&(f.selected=!0,g.push(f));return g},a.prototype.validateVariableSelectionState=function(a){if(a.current||(a.current={}),c["default"].isArray(a.current.value)){var b=this.selectOptionsForCurrentValue(a);return b=0===b.length?a.options[0]:{value:c["default"].map(b,function(a){return a.value}),text:c["default"].map(b,function(a){return a.text}).join(" + ")},a.setValue(b)}var d=c["default"].find(a.options,{text:a.current.text});return d?a.setValue(d):a.options.length?a.setValue(a.options[0]):Promise.resolve()},a.prototype.setOptionFromUrl=function(a,b){var d=this.$q.when();return a.refresh&&(d=a.updateOptions()),d.then(function(){var d=c["default"].find(a.options,function(a){return a.text===b||a.value===b});return d=d||{text:b,value:b},a.setValue(d)})},a.prototype.setOptionAsCurrent=function(a,b){return a.current=c["default"].cloneDeep(b),c["default"].isArray(a.current.text)&&(a.current.text=a.current.text.join(" + ")),this.selectOptionsForCurrentValue(a),this.variableUpdated(a)},a.prototype.updateUrlParamsWithCurrentVariables=function(){var a=this.$location.search();c["default"].each(a,function(b,c){0===c.indexOf("var-")&&delete a[c]}),this.templateSrv.fillVariableValuesForUrl(a),this.$location.search(a)},a}(),a("VariableSrv",f),d["default"].service("variableSrv",f)}}});