From f6845cd107c1a59c3d11199a1b4499a5f23b2197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 9 Jun 2015 10:06:36 +0200 Subject: [PATCH] Variable value select fixes and refactorings --- public/app/directives/all.js | 2 +- ...lectDropDown.js => valueSelectDropdown.js} | 16 +++++++---- public/app/partials/submenu.html | 2 +- ...Dropdown.html => valueSelectDropdown.html} | 0 ...-specs.js => valueSelectDropdown-specs.js} | 27 +++++++++++++++++-- public/test/test-main.js | 2 +- 6 files changed, 39 insertions(+), 10 deletions(-) rename public/app/directives/{selectDropDown.js => valueSelectDropdown.js} (94%) rename public/app/partials/{selectDropdown.html => valueSelectDropdown.html} (100%) rename public/test/specs/{selectDropdownCtrl-specs.js => valueSelectDropdown-specs.js} (83%) diff --git a/public/app/directives/all.js b/public/app/directives/all.js index 0a9ac603c57..13a8accffbd 100644 --- a/public/app/directives/all.js +++ b/public/app/directives/all.js @@ -11,7 +11,7 @@ define([ './spectrumPicker', './tags', './bodyClass', - './selectDropDown', + './valueSelectDropdown', './metric.segment', './grafanaVersionCheck', './dropdown.typeahead', diff --git a/public/app/directives/selectDropDown.js b/public/app/directives/valueSelectDropdown.js similarity index 94% rename from public/app/directives/selectDropDown.js rename to public/app/directives/valueSelectDropdown.js index 312b6cdd7d1..14791eba0f3 100644 --- a/public/app/directives/selectDropDown.js +++ b/public/app/directives/valueSelectDropdown.js @@ -9,7 +9,7 @@ function (angular, app, _) { angular .module('grafana.controllers') - .controller('SelectDropdownCtrl', function($q) { + .controller('ValueSelectDropdownCtrl', function($q) { var vm = this; vm.show = function() { @@ -31,7 +31,13 @@ function (angular, app, _) { vm.selectedValues = _.filter(vm.options, {selected: true}); vm.tags = _.map(vm.variable.tags, function(value) { - return { text: value, selected: false }; + var tag = { text: value, selected: false }; + _.each(vm.variable.current.tags, function(tagObj) { + if (tagObj.text === value) { + tag.selected = true; + } + }); + return tag; }); vm.search = {query: '', options: vm.options}; @@ -225,12 +231,12 @@ function (angular, app, _) { angular .module('grafana.directives') - .directive('selectDropdown', function($compile, $window, $timeout) { + .directive('valueSelectDropdown', function($compile, $window, $timeout) { return { scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" }, - templateUrl: 'app/partials/selectDropdown.html', - controller: 'SelectDropdownCtrl', + templateUrl: 'app/partials/valueSelectDropdown.html', + controller: 'ValueSelectDropdownCtrl', controllerAs: 'vm', bindToController: true, link: function(scope, elem) { diff --git a/public/app/partials/submenu.html b/public/app/partials/submenu.html index 0246d4a7673..2feec7edbca 100644 --- a/public/app/partials/submenu.html +++ b/public/app/partials/submenu.html @@ -6,7 +6,7 @@ {{variable.label || variable.name}}: - + diff --git a/public/app/partials/selectDropdown.html b/public/app/partials/valueSelectDropdown.html similarity index 100% rename from public/app/partials/selectDropdown.html rename to public/app/partials/valueSelectDropdown.html diff --git a/public/test/specs/selectDropdownCtrl-specs.js b/public/test/specs/valueSelectDropdown-specs.js similarity index 83% rename from public/test/specs/selectDropdownCtrl-specs.js rename to public/test/specs/valueSelectDropdown-specs.js index 3aae3429a7c..9fadb893467 100644 --- a/public/test/specs/selectDropdownCtrl-specs.js +++ b/public/test/specs/valueSelectDropdown-specs.js @@ -1,5 +1,5 @@ define([ - 'directives/variableValueSelect', + 'directives/valueSelectDropdown', ], function () { 'use strict'; @@ -15,7 +15,7 @@ function () { beforeEach(inject(function($controller, $rootScope, $q) { rootScope = $rootScope; scope = $rootScope.$new(); - ctrl = $controller('SelectDropdownCtrl', {$scope: scope}); + ctrl = $controller('ValueSelectDropdownCtrl', {$scope: scope}); ctrl.getValuesForTag = function(obj) { return $q.when(tagValuesMap[obj.tagKey]); }; @@ -134,5 +134,28 @@ function () { }); }); }); + + describe("Given variable with selected tags", function() { + beforeEach(function() { + ctrl.variable = { + current: {text: 'server-1', value: 'server-1', tags: [{text: 'key1'}] }, + options: [ + {text: 'server-1', value: 'server-1'}, + {text: 'server-2', value: 'server-2'}, + {text: 'server-3', value: 'server-3'}, + ], + tags: ["key1", "key2", "key3"], + multi: true + }; + ctrl.init(); + ctrl.show(); + }); + + it("should set tag as selected", function() { + expect(ctrl.tags[0].selected).to.be(true); + }); + + }); + }); }); diff --git a/public/test/test-main.js b/public/test/test-main.js index 6b62df02102..bbc6b239fa0 100644 --- a/public/test/test-main.js +++ b/public/test/test-main.js @@ -144,7 +144,7 @@ require([ 'specs/singlestat-specs', 'specs/dynamicDashboardSrv-specs', 'specs/unsavedChangesSrv-specs', - 'specs/selectDropdownCtrl-specs', + 'specs/valueSelectDropdown-specs', ]; var pluginSpecs = (config.plugins.specs || []).map(function (spec) {