From cb8b038795e305f8155dda328b5b83d976476f2e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Torkel=20=C3=96degaard?= <torkel.odegaard@gmail.com>
Date: Mon, 29 Feb 2016 17:31:31 +0100
Subject: [PATCH] feat(templating): progress on templating rethink

---
 .../core/directives/value_select_dropdown.js  |  4 ++++
 .../features/dashboard/partials/import.html   | 18 ++++++++----------
 public/app/features/templating/templateSrv.js |  9 +++++++--
 .../features/templating/templateValuesSrv.js  | 19 ++++++++-----------
 .../plugins/datasource/influxdb/datasource.ts |  2 +-
 5 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/public/app/core/directives/value_select_dropdown.js b/public/app/core/directives/value_select_dropdown.js
index e6e2a709a1a..60d257d6fa8 100644
--- a/public/app/core/directives/value_select_dropdown.js
+++ b/public/app/core/directives/value_select_dropdown.js
@@ -179,6 +179,10 @@ function (angular, _, coreModule) {
       vm.variable.current.text = _.pluck(vm.selectedValues, 'text').join(' + ');
       vm.variable.current.tags = vm.selectedTags;
 
+      if (!vm.variable.multi) {
+        vm.variable.current.value = vm.selectedValues[0].value;
+      }
+
       if (commitChange) {
         vm.commitChanges();
       }
diff --git a/public/app/features/dashboard/partials/import.html b/public/app/features/dashboard/partials/import.html
index d1f028f7222..3f23e4cb682 100644
--- a/public/app/features/dashboard/partials/import.html
+++ b/public/app/features/dashboard/partials/import.html
@@ -15,26 +15,24 @@
 			</form>
 	</div>
 
-	<h5 class="page-heading">
+	<h5 class="section-heading">
 		Migrate dashboards
 		<em style="font-size: 14px;padding-left: 10px;"><i class="fa fa-info-circle"></i> Import dashboards from Elasticsearch or InfluxDB</em>
 	</h5>
 
-	<div class="gf-form-group last">
+	<div class="gf-form-inline gf-form-group">
 		<div class="gf-form">
 			<div class="gf-form-label">Dashboard source</div>
-			<div>
-				<div class="gf-form-select-wrapper">
-					<select class="gf-form-input gf-size-auto" ng-model="sourceName" ng-options="f for f in datasources"></select>
-				</div>
-			</div>
-			<div class="gf-form-btn">
-				<button class="btn btn-success" ng-click="startImport()">Import</button>
+			<div class="gf-form-select-wrapper">
+				<select class="gf-form-input gf-size-auto" ng-model="sourceName" ng-options="f for f in datasources"></select>
 			</div>
 		</div>
+		<div class="gf-form">
+			<button class="btn btn-success gf-form-btn" ng-click="startImport()">Import</button>
+		</div>
 	</div>
 
-	<h5 class="page-heading" ng-if="importing">{{infoText}}</h5>
+	<h5 class="section-heading" ng-if="importing">{{infoText}}</h5>
 	<div class="editor-row" ng-if="importing">
 		<div class="editor-row row">
 			<table class="grafana-options-table span5">
diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js
index 6a2fa26ed6f..1e1ec72b02f 100644
--- a/public/app/features/templating/templateSrv.js
+++ b/public/app/features/templating/templateSrv.js
@@ -31,13 +31,18 @@ function (angular, _) {
        }, this);
     };
 
+    this.regexEscape = function(value) {
+      return value.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
+    };
+
     this.formatValue = function(value, format) {
       if (_.isString(value)) {
         return value;
       } else {
         switch(format) {
-          case "regex values": {
-            return '(' + value.join('|') + ')';
+          case "regex": {
+            var escapedValues = _.map(value, this.regexEscape);
+            return '(' + escapedValues.join('|') + ')';
           }
           case "lucene": {
             var quotedValues = _.map(value, function(val) {
diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js
index 517a69f4a6a..a88d4e9a700 100644
--- a/public/app/features/templating/templateValuesSrv.js
+++ b/public/app/features/templating/templateValuesSrv.js
@@ -226,22 +226,19 @@ function (angular, _, kbn) {
       return _.map(_.keys(options).sort(), function(key) {
         var option = { text: key, value: key };
 
-        // check if values need to be regex escaped
-        if (self.shouldRegexEscape(variable)) {
-          option.value = self.regexEscape(option.value);
-        }
+        // // check if values need to be regex escaped
+        // if (self.shouldRegexEscape(variable)) {
+        //   option.value = self.regexEscape(option.value);
+        // }
 
         return option;
       });
     };
 
-    this.shouldRegexEscape = function(variable) {
-      return (variable.includeAll || variable.multi) && variable.allFormat.indexOf('regex') !== -1;
-    };
-
-    this.regexEscape = function(value) {
-      return value.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
-    };
+    // this.shouldRegexEscape = function(variable) {
+    //   return (variable.includeAll || variable.multi) && variable.allFormat.indexOf('regex') !== -1;
+    // };
+    //
 
     this.addAllOption = function(variable) {
       // var allValue = '';
diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts
index fec1dd71d6c..d6583477c94 100644
--- a/public/app/plugins/datasource/influxdb/datasource.ts
+++ b/public/app/plugins/datasource/influxdb/datasource.ts
@@ -45,7 +45,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
     allQueries = allQueries.replace(/\$timeFilter/g, timeFilter);
 
     // replace templated variables
-    allQueries = templateSrv.replace(allQueries, options.scopedVars);
+    allQueries = templateSrv.replace(allQueries, options.scopedVars, 'regex');
 
     return this._seriesQuery(allQueries).then(function(data): any {
       if (!data || !data.results) {