DashboardSrv.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import coreModule from 'app/core/core_module';
  2. import { appEvents } from 'app/core/app_events';
  3. import locationUtil from 'app/core/utils/location_util';
  4. import { DashboardModel } from '../state/DashboardModel';
  5. import { removePanel } from '../utils/panel';
  6. import { DashboardMeta } from 'app/types';
  7. import { BackendSrv } from 'app/core/services/backend_srv';
  8. import { ILocationService } from 'angular';
  9. interface DashboardSaveOptions {
  10. folderId?: number;
  11. overwrite?: boolean;
  12. message?: string;
  13. makeEditable?: boolean;
  14. }
  15. export class DashboardSrv {
  16. dashboard: DashboardModel;
  17. /** @ngInject */
  18. constructor(private backendSrv: BackendSrv, private $rootScope: any, private $location: ILocationService) {
  19. appEvents.on('save-dashboard', this.saveDashboard.bind(this), $rootScope);
  20. appEvents.on('panel-change-view', this.onPanelChangeView);
  21. appEvents.on('remove-panel', this.onRemovePanel);
  22. // Export to react
  23. setDashboardSrv(this);
  24. }
  25. create(dashboard: any, meta: DashboardMeta) {
  26. return new DashboardModel(dashboard, meta);
  27. }
  28. setCurrent(dashboard: DashboardModel) {
  29. this.dashboard = dashboard;
  30. }
  31. getCurrent(): DashboardModel {
  32. return this.dashboard;
  33. }
  34. onRemovePanel = (panelId: number) => {
  35. const dashboard = this.getCurrent();
  36. removePanel(dashboard, dashboard.getPanelById(panelId), true);
  37. };
  38. onPanelChangeView = ({
  39. fullscreen = false,
  40. edit = false,
  41. panelId,
  42. }: {
  43. fullscreen?: boolean;
  44. edit?: boolean;
  45. panelId?: number;
  46. }) => {
  47. const urlParams = this.$location.search();
  48. // handle toggle logic
  49. // I hate using these truthy converters (!!) but in this case
  50. // I think it's appropriate. edit can be null/false/undefined and
  51. // here i want all of those to compare the same
  52. if (fullscreen === urlParams.fullscreen && edit === !!urlParams.edit) {
  53. const paramsToRemove = ['fullscreen', 'edit', 'panelId', 'tab'];
  54. for (const key of paramsToRemove) {
  55. delete urlParams[key];
  56. }
  57. this.$location.search(urlParams);
  58. return;
  59. }
  60. const newUrlParams = {
  61. ...urlParams,
  62. fullscreen: fullscreen || undefined,
  63. edit: edit || undefined,
  64. tab: edit ? urlParams.tab : undefined,
  65. panelId,
  66. };
  67. Object.keys(newUrlParams).forEach(key => {
  68. if (newUrlParams[key] === undefined) {
  69. delete newUrlParams[key];
  70. }
  71. });
  72. this.$location.search(newUrlParams);
  73. };
  74. handleSaveDashboardError(
  75. clone: any,
  76. options: DashboardSaveOptions,
  77. err: { data: { status: string; message: any }; isHandled: boolean }
  78. ) {
  79. options.overwrite = true;
  80. if (err.data && err.data.status === 'version-mismatch') {
  81. err.isHandled = true;
  82. this.$rootScope.appEvent('confirm-modal', {
  83. title: 'Conflict',
  84. text: 'Someone else has updated this dashboard.',
  85. text2: 'Would you still like to save this dashboard?',
  86. yesText: 'Save & Overwrite',
  87. icon: 'fa-warning',
  88. onConfirm: () => {
  89. this.save(clone, options);
  90. },
  91. });
  92. }
  93. if (err.data && err.data.status === 'name-exists') {
  94. err.isHandled = true;
  95. this.$rootScope.appEvent('confirm-modal', {
  96. title: 'Conflict',
  97. text: 'A dashboard with the same name in selected folder already exists.',
  98. text2: 'Would you still like to save this dashboard?',
  99. yesText: 'Save & Overwrite',
  100. icon: 'fa-warning',
  101. onConfirm: () => {
  102. this.save(clone, options);
  103. },
  104. });
  105. }
  106. if (err.data && err.data.status === 'plugin-dashboard') {
  107. err.isHandled = true;
  108. this.$rootScope.appEvent('confirm-modal', {
  109. title: 'Plugin Dashboard',
  110. text: err.data.message,
  111. text2: 'Your changes will be lost when you update the plugin. Use Save As to create custom version.',
  112. yesText: 'Overwrite',
  113. icon: 'fa-warning',
  114. altActionText: 'Save As',
  115. onAltAction: () => {
  116. this.showSaveAsModal();
  117. },
  118. onConfirm: () => {
  119. this.save(clone, { ...options, overwrite: true });
  120. },
  121. });
  122. }
  123. }
  124. postSave(data: { version: number; url: string }) {
  125. this.dashboard.version = data.version;
  126. // important that these happen before location redirect below
  127. this.$rootScope.appEvent('dashboard-saved', this.dashboard);
  128. this.$rootScope.appEvent('alert-success', ['Dashboard saved']);
  129. const newUrl = locationUtil.stripBaseFromUrl(data.url);
  130. const currentPath = this.$location.path();
  131. if (newUrl !== currentPath) {
  132. this.$location.url(newUrl).replace();
  133. }
  134. return this.dashboard;
  135. }
  136. save(clone: any, options?: DashboardSaveOptions) {
  137. options.folderId = options.folderId >= 0 ? options.folderId : this.dashboard.meta.folderId || clone.folderId;
  138. return this.backendSrv
  139. .saveDashboard(clone, options)
  140. .then((data: any) => this.postSave(data))
  141. .catch(this.handleSaveDashboardError.bind(this, clone, { folderId: options.folderId }));
  142. }
  143. saveDashboard(
  144. clone?: DashboardModel,
  145. { makeEditable = false, folderId, overwrite = false, message }: DashboardSaveOptions = {}
  146. ) {
  147. if (clone) {
  148. this.setCurrent(this.create(clone, this.dashboard.meta));
  149. }
  150. if (this.dashboard.meta.provisioned) {
  151. return this.showDashboardProvisionedModal();
  152. }
  153. if (!(this.dashboard.meta.canSave || makeEditable)) {
  154. return Promise.resolve();
  155. }
  156. if (this.dashboard.title === 'New dashboard') {
  157. return this.showSaveAsModal();
  158. }
  159. if (this.dashboard.version > 0) {
  160. return this.showSaveModal();
  161. }
  162. return this.save(this.dashboard.getSaveModelClone(), { folderId, overwrite, message });
  163. }
  164. saveJSONDashboard(json: string) {
  165. return this.save(JSON.parse(json), {});
  166. }
  167. showDashboardProvisionedModal() {
  168. this.$rootScope.appEvent('show-modal', {
  169. templateHtml: '<save-provisioned-dashboard-modal dismiss="dismiss()"></save-provisioned-dashboard-modal>',
  170. });
  171. }
  172. showSaveAsModal() {
  173. this.$rootScope.appEvent('show-modal', {
  174. templateHtml: '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
  175. modalClass: 'modal--narrow',
  176. });
  177. }
  178. showSaveModal() {
  179. this.$rootScope.appEvent('show-modal', {
  180. templateHtml: '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  181. modalClass: 'modal--narrow',
  182. });
  183. }
  184. starDashboard(dashboardId: string, isStarred: any) {
  185. let promise;
  186. if (isStarred) {
  187. promise = this.backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
  188. return false;
  189. });
  190. } else {
  191. promise = this.backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
  192. return true;
  193. });
  194. }
  195. return promise.then((res: boolean) => {
  196. if (this.dashboard && this.dashboard.id === dashboardId) {
  197. this.dashboard.meta.isStarred = res;
  198. }
  199. return res;
  200. });
  201. }
  202. }
  203. coreModule.service('dashboardSrv', DashboardSrv);
  204. //
  205. // Code below is to export the service to react components
  206. //
  207. let singletonInstance: DashboardSrv;
  208. export function setDashboardSrv(instance: DashboardSrv) {
  209. singletonInstance = instance;
  210. }
  211. export function getDashboardSrv(): DashboardSrv {
  212. return singletonInstance;
  213. }