Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
4872cef8
Commit
4872cef8
authored
Dec 05, 2018
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored and added tests for panel model remember properties
parent
b8c65776
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
26 deletions
+63
-26
public/app/features/dashboard/panel_model.ts
+9
-26
public/app/features/dashboard/specs/panel_model.test.ts
+54
-0
No files found.
public/app/features/dashboard/panel_model.ts
View file @
4872cef8
...
@@ -55,12 +55,6 @@ const mustKeepProps: { [str: string]: boolean } = {
...
@@ -55,12 +55,6 @@ const mustKeepProps: { [str: string]: boolean } = {
cachedPluginOptions
:
true
,
cachedPluginOptions
:
true
,
};
};
// Keep current option value when switching visualization
const
keepLatestProps
:
{
[
str
:
string
]:
boolean
}
=
{
title
:
true
,
description
:
true
,
};
const
defaults
:
any
=
{
const
defaults
:
any
=
{
gridPos
:
{
x
:
0
,
y
:
0
,
h
:
3
,
w
:
6
},
gridPos
:
{
x
:
0
,
y
:
0
,
h
:
3
,
w
:
6
},
datasource
:
null
,
datasource
:
null
,
...
@@ -132,7 +126,7 @@ export class PanelModel {
...
@@ -132,7 +126,7 @@ export class PanelModel {
}
}
private
getOptionsKey
()
{
private
getOptionsKey
()
{
return
'options-'
+
this
.
type
;
return
PANEL_OPTIONS_KEY_PREFIX
+
this
.
type
;
}
}
getSaveModel
()
{
getSaveModel
()
{
...
@@ -196,9 +190,9 @@ export class PanelModel {
...
@@ -196,9 +190,9 @@ export class PanelModel {
this
.
events
.
emit
(
'panel-initialized'
);
this
.
events
.
emit
(
'panel-initialized'
);
}
}
getPanelOptions
()
{
private
getOptionsToRemember
()
{
return
Object
.
keys
(
this
).
reduce
((
acc
,
property
)
=>
{
return
Object
.
keys
(
this
).
reduce
((
acc
,
property
)
=>
{
if
(
notPersistedProperties
[
property
])
{
if
(
notPersistedProperties
[
property
]
||
mustKeepProps
[
property
]
)
{
return
acc
;
return
acc
;
}
}
return
{
return
{
...
@@ -208,23 +202,15 @@ export class PanelModel {
...
@@ -208,23 +202,15 @@ export class PanelModel {
},
{});
},
{});
}
}
saveCurrentPanelOptions
()
{
private
saveCurrentPanelOptions
()
{
const
currentOptions
=
this
.
getPanelOptions
();
this
.
cachedPluginOptions
[
this
.
type
]
=
this
.
getOptionsToRemember
();
this
.
cachedPluginOptions
[
this
.
type
]
=
currentOptions
;
}
}
restorePanelOptions
(
pluginId
:
string
)
{
private
restorePanelOptions
(
pluginId
:
string
)
{
const
currentOptions
=
this
.
getPanelOptions
();
const
prevOptions
=
this
.
cachedPluginOptions
[
pluginId
]
||
{};
const
prevOptions
=
this
.
cachedPluginOptions
[
pluginId
]
||
{};
const
newOptions
=
Object
.
keys
(
prevOptions
).
reduce
((
acc
,
currKey
:
string
)
=>
{
return
{
...
acc
,
[
currKey
]:
keepLatestProps
[
currKey
]
?
currentOptions
[
currKey
]
:
prevOptions
[
currKey
],
};
},
{});
Object
.
keys
(
new
Options
).
map
(
property
=>
{
Object
.
keys
(
prev
Options
).
map
(
property
=>
{
this
[
property
]
=
new
Options
[
property
];
this
[
property
]
=
prev
Options
[
property
];
});
});
}
}
...
@@ -232,15 +218,12 @@ export class PanelModel {
...
@@ -232,15 +218,12 @@ export class PanelModel {
this
.
saveCurrentPanelOptions
();
this
.
saveCurrentPanelOptions
();
this
.
type
=
pluginId
;
this
.
type
=
pluginId
;
// for now we need to remove alert rules when changing type
delete
this
.
alert
;
// for angular panels only we need to remove all events and let angular panels do some cleanup
// for angular panels only we need to remove all events and let angular panels do some cleanup
if
(
fromAngularPanel
)
{
if
(
fromAngularPanel
)
{
this
.
destroy
();
this
.
destroy
();
for
(
const
key
of
_
.
keys
(
this
))
{
for
(
const
key
of
_
.
keys
(
this
))
{
if
(
mustKeepProps
[
key
]
||
key
.
indexOf
(
PANEL_OPTIONS_KEY_PREFIX
)
===
0
)
{
if
(
mustKeepProps
[
key
])
{
continue
;
continue
;
}
}
...
...
public/app/features/dashboard/specs/panel_model.test.ts
0 → 100644
View file @
4872cef8
import
_
from
'lodash'
;
import
{
PanelModel
}
from
'../panel_model'
;
describe
(
'PanelModel'
,
()
=>
{
describe
(
'when creating new panel model'
,
()
=>
{
let
model
;
beforeEach
(()
=>
{
model
=
new
PanelModel
({
type
:
'table'
,
showColumns
:
true
,
});
});
it
(
'should apply defaults'
,
()
=>
{
expect
(
model
.
gridPos
.
h
).
toBe
(
3
);
});
it
(
'should set model props on instance'
,
()
=>
{
expect
(
model
.
showColumns
).
toBe
(
true
);
});
it
(
'getSaveModel should remove defaults'
,
()
=>
{
const
saveModel
=
model
.
getSaveModel
();
expect
(
saveModel
.
gridPos
).
toBe
(
undefined
);
});
it
(
'getSaveModel should remove nonPersistedProperties'
,
()
=>
{
const
saveModel
=
model
.
getSaveModel
();
expect
(
saveModel
.
events
).
toBe
(
undefined
);
});
describe
(
'when changing panel type'
,
()
=>
{
beforeEach
(()
=>
{
model
.
changeType
(
'graph'
,
true
);
model
.
alert
=
{
id
:
2
};
});
it
(
'should remove table properties but keep core props'
,
()
=>
{
expect
(
model
.
showColumns
).
toBe
(
undefined
);
});
it
(
'should restore table properties when changing back'
,
()
=>
{
model
.
changeType
(
'table'
,
true
);
expect
(
model
.
showColumns
).
toBe
(
true
);
});
it
(
'should remove alert rule when changing type that does not support it'
,
()
=>
{
model
.
changeType
(
'table'
,
true
);
expect
(
model
.
alert
).
toBe
(
undefined
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment