Skip to content

Commit 5ded88f

Browse files
committed
feat(templating): progress on variable system refactoring, grafana#6048
1 parent 524f505 commit 5ded88f

File tree

12 files changed

+139
-180
lines changed

12 files changed

+139
-180
lines changed

public/app/core/services/context_srv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class User {
99
isGrafanaAdmin: any;
1010
isSignedIn: any;
1111
orgRole: any;
12+
timezone: string;
1213

1314
constructor() {
1415
if (config.bootData.user) {

public/app/features/dashboard/dashboard_srv.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import moment from 'moment';
66
import _ from 'lodash';
77
import $ from 'jquery';
88

9+
import {Emitter} from 'app/core/core';
10+
import {contextSrv} from 'app/core/services/context_srv';
911
import coreModule from 'app/core/core_module';
1012

1113
export class DashboardModel {
@@ -31,14 +33,14 @@ export class DashboardModel {
3133
links: any;
3234
gnetId: any;
3335
meta: any;
34-
contextSrv: any;
36+
events: any;
3537

36-
constructor(data, meta, contextSrv) {
38+
constructor(data, meta) {
3739
if (!data) {
3840
data = {};
3941
}
4042

41-
this.contextSrv = contextSrv;
43+
this.events = new Emitter();
4244
this.id = data.id || null;
4345
this.title = data.title || 'No Title';
4446
this.autoUpdate = data.autoUpdate;
@@ -85,8 +87,18 @@ export class DashboardModel {
8587

8688
// cleans meta data and other non peristent state
8789
getSaveModelClone() {
90+
// temp remove stuff
91+
var events = this.events;
92+
var meta = this.meta;
93+
delete this.events;
94+
delete this.meta;
95+
96+
events.emit('prepare-save-model');
8897
var copy = $.extend(true, {}, this);
89-
delete copy.meta;
98+
99+
// restore properties
100+
this.events = events;
101+
this.meta = meta;
90102
return copy;
91103
}
92104

@@ -233,7 +245,7 @@ export class DashboardModel {
233245
}
234246

235247
getTimezone() {
236-
return this.timezone ? this.timezone : this.contextSrv.user.timezone;
248+
return this.timezone ? this.timezone : contextSrv.user.timezone;
237249
}
238250

239251
private updateSchema(old) {
@@ -561,12 +573,8 @@ export class DashboardModel {
561573
export class DashboardSrv {
562574
currentDashboard: any;
563575

564-
/** @ngInject */
565-
constructor(private contextSrv) {
566-
}
567-
568576
create(dashboard, meta) {
569-
return new DashboardModel(dashboard, meta, this.contextSrv);
577+
return new DashboardModel(dashboard, meta);
570578
}
571579

572580
setCurrent(dashboard) {

public/app/features/dashboard/specs/dashboard_srv_specs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('dashboardSrv', function() {
66
var _dashboardSrv;
77

88
beforeEach(() => {
9-
_dashboardSrv = new DashboardSrv({});
9+
_dashboardSrv = new DashboardSrv();
1010
});
1111

1212
describe('when creating new dashboard with defaults only', function() {

public/app/features/dashboard/viewStateSrv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function (angular, _, $) {
185185
DashboardViewState.prototype.enterFullscreen = function(panelScope) {
186186
var ctrl = panelScope.ctrl;
187187

188-
ctrl.editMode = this.state.edit && this.$scope.dashboardMeta.canEdit;
188+
ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
189189
ctrl.fullscreen = true;
190190

191191
this.oldTimeRange = ctrl.range;

public/app/features/templating/all.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import './templateSrv';
2-
import './templateValuesSrv';
32
import './editorCtrl';
43

54
import {VariableSrv} from './variable_srv';

public/app/features/templating/interval_variable.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ export class IntervalVariable implements Variable {
4242
return {text: text.trim(), value: text.trim()};
4343
});
4444

45-
if (this.auto) {
46-
this.updateAutoValue();
47-
}
45+
this.updateAutoValue();
4846
}
4947

5048
dependsOn(variable) {

public/app/features/templating/specs/variabe_srv_init_specs.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

public/app/features/templating/specs/variable_srv_init_specs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
22

3+
import '../all';
4+
35
import _ from 'lodash';
46
import helpers from 'test/specs/helpers';
5-
import '../all';
7+
import {Emitter} from 'app/core/core';
68

79
describe('VariableSrv init', function() {
810
var ctx = new helpers.ControllerTestContext();
@@ -17,7 +19,6 @@ describe('VariableSrv init', function() {
1719
ctx.$rootScope = $rootScope;
1820
ctx.$location = $location;
1921
ctx.variableSrv = $injector.get('variableSrv');
20-
ctx.variableSrv.init({templating: {list: []}});
2122
ctx.$rootScope.$digest();
2223
}));
2324

@@ -39,8 +40,8 @@ describe('VariableSrv init', function() {
3940
ctx.datasourceSrv.getMetricSources = sinon.stub().returns(scenario.metricSources);
4041

4142
ctx.$location.search = sinon.stub().returns(scenario.urlParams);
43+
ctx.dashboard = {templating: {list: scenario.variables}, events: new Emitter()};
4244

43-
ctx.dashboard = {templating: {list: scenario.variables}};
4445
ctx.variableSrv.init(ctx.dashboard);
4546
ctx.$rootScope.$digest();
4647

@@ -137,6 +138,5 @@ describe('VariableSrv init', function() {
137138
});
138139
});
139140

140-
141141
});
142142

public/app/features/templating/specs/variable_srv_specs.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
22

3+
import '../all';
4+
35
import moment from 'moment';
46
import helpers from 'test/specs/helpers';
5-
import '../all';
7+
import {Emitter} from 'app/core/core';
68

79
describe('VariableSrv', function() {
810
var ctx = new helpers.ControllerTestContext();
@@ -17,7 +19,10 @@ describe('VariableSrv', function() {
1719
ctx.$rootScope = $rootScope;
1820
ctx.$location = $location;
1921
ctx.variableSrv = $injector.get('variableSrv');
20-
ctx.variableSrv.init({templating: {list: []}});
22+
ctx.variableSrv.init({
23+
templating: {list: []},
24+
events: new Emitter(),
25+
});
2126
ctx.$rootScope.$digest();
2227
}));
2328

0 commit comments

Comments
 (0)