grafana-dash-gen 3.3.2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,25 +22,35 @@
22
22
 
23
23
  function Custom(opts) {
24
24
  opts = opts || {};
25
- var self = this;
26
- var defaultState = {
25
+ this.state = {
27
26
  allFormat: 'glob',
28
27
  current: null,
29
28
  datasource: null,
30
29
  includeAll: false,
30
+ allValue: '',
31
31
  name: 'template',
32
32
  options: [],
33
33
  query: null,
34
34
  'refresh_on_load': false,
35
35
  type: 'custom'
36
36
  };
37
- this.state = defaultState;
37
+ this.defaultValue = '';
38
38
 
39
39
  // Overwrite defaults with custom values
40
- Object.keys(opts).forEach(function eachOpt(opt) {
41
- self.state[opt] = opts[opt];
40
+ Object.keys(opts).forEach(key => {
41
+ switch (key) {
42
+ case 'defaultValue':
43
+ this.defaultValue = opts[key];
44
+ break;
45
+ default:
46
+ this.state[key] = opts[key];
47
+ }
42
48
  });
43
49
 
50
+ if (this.defaultValue && !this.state.options.length) {
51
+ throw new SyntaxError("cannot define default value without any options")
52
+ }
53
+
44
54
  if (this.state.options.length) {
45
55
  this._processOptions();
46
56
  }
@@ -56,21 +66,25 @@ Custom.prototype._processOptions = function _processOptions() {
56
66
  var newOptions = [];
57
67
  var newQuery = [];
58
68
 
59
- this.state.options.forEach(function processOption(option) {
60
- var opt = {};
61
- if (typeof option === 'object') {
62
- opt = option;
63
- } else {
64
- opt = {
65
- text: option,
66
- value: option
67
- };
68
- }
69
- newOptions.push(opt);
70
- newQuery.push(opt.value);
69
+ this.state.options.forEach(option => {
70
+ const isObject = typeof option === 'object' && option.constructor === Object;
71
+ const opt = isObject ? option : {
72
+ text: option,
73
+ value: option
74
+ };
75
+ newOptions.push(opt);
76
+ newQuery.push(opt.value);
71
77
  });
72
78
 
73
- self.state.current = newOptions[0];
79
+ if (this.defaultValue !== '') {
80
+ const defaultOption = newOptions.find(option => option.value === this.defaultValue);
81
+ if (!defaultOption) {
82
+ throw new SyntaxError("default value not found in options list")
83
+ }
84
+ self.state.current = defaultOption
85
+ } else {
86
+ self.state.current = this.state.includeAll ? { text: "All", value: this.state.allValue} : newOptions[0];
87
+ }
74
88
 
75
89
  this.state.options = newOptions;
76
90
  this.state.query = newQuery.join(',');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grafana-dash-gen",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
4
4
  "description": "A grafana dashboard generator",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,8 +18,6 @@
18
18
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  // THE SOFTWARE.
20
20
 
21
- var ExternalLink = require('../../grafana/external-link')
22
-
23
21
  'use strict';
24
22
  module.exports = {
25
23
  id: null,
@@ -45,6 +43,7 @@ module.exports = {
45
43
  },
46
44
  datasource: null,
47
45
  includeAll: false,
46
+ allValue: '',
48
47
  name: 'myvar',
49
48
  options: [{
50
49
  text: 'a',
@@ -64,6 +63,7 @@ module.exports = {
64
63
  },
65
64
  datasource: null,
66
65
  includeAll: false,
66
+ allValue: '',
67
67
  name: 'smoothing',
68
68
  options: [{
69
69
  text: '30min',
@@ -33,6 +33,7 @@ module.exports = {
33
33
  datasource: null,
34
34
  'refresh_on_load': false,
35
35
  includeAll: false,
36
+ allValue: '',
36
37
  allFormat: 'glob',
37
38
  query: 'a,b',
38
39
  current: {
@@ -30,6 +30,7 @@ module.exports = {
30
30
  datasource: null,
31
31
  'refresh_on_load': false,
32
32
  includeAll: false,
33
+ allValue: '',
33
34
  allFormat: 'glob',
34
35
  query: 'myValue',
35
36
  current: {
@@ -25,6 +25,7 @@ module.exports = {
25
25
  datasource: null,
26
26
  refresh_on_load: false,
27
27
  includeAll: false,
28
+ allValue: '',
28
29
  allFormat: 'glob',
29
30
  query: null,
30
31
  current: null
@@ -94,5 +94,57 @@ test('Custom template overwrites default state', function t(assert) {
94
94
  arbitraryProperty: 'foo'
95
95
  });
96
96
  assert.equal(customTemplate.state.includeAll, true);
97
+ assert.equal(customTemplate.state.allValue, '')
98
+
99
+ var customWithAllValue = new Custom({
100
+ includeAll: true,
101
+ arbitraryProperty: 'foo',
102
+ allValue: 'grafana',
103
+ });
104
+ assert.equal(customWithAllValue.state.includeAll, true);
105
+ assert.equal(customWithAllValue.state.allValue, 'grafana')
106
+
107
+ var allIsDefault = new Custom({
108
+ includeAll: true,
109
+ arbitraryProperty: 'foo',
110
+ options: [{ text: 'grafana', value: 'grafana' }]
111
+ });
112
+ assert.equal(allIsDefault.state.includeAll, true);
113
+ assert.equal(allIsDefault.state.allValue, '')
114
+ assert.equal(allIsDefault.state.current.text, "All");
115
+ assert.equal(allIsDefault.state.current.value, '');
116
+
97
117
  assert.end();
98
118
  });
119
+
120
+ test('Custom template supports custom default', function t(assert) {
121
+ const defaultOption = { text: 'dash-gen', value: 'dash-gen' }
122
+ var definedDefault = new Custom({
123
+ includeAll: true,
124
+ defaultValue: defaultOption.value,
125
+ options: [{ text: 'grafana', value: 'grafana' }, defaultOption]
126
+ });
127
+ assert.equal(definedDefault.state.includeAll, true);
128
+ assert.equal(definedDefault.state.allValue, '')
129
+ assert.equal(definedDefault.state.current, defaultOption);
130
+
131
+ assert.throws(
132
+ () => new Custom({
133
+ includeAll: true,
134
+ defaultValue: defaultOption.value,
135
+ options: [{ text: 'grafana', value: 'grafana' }]
136
+ }),
137
+ new SyntaxError("default value not found in options list"),
138
+ );
139
+
140
+ assert.throws(
141
+ () => new Custom({
142
+ includeAll: true,
143
+ defaultValue: defaultOption.value,
144
+ }),
145
+ new SyntaxError("cannot define default value without any options"),
146
+ );
147
+
148
+ assert.end();
149
+ });
150
+