plugin_page_ctrl.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import {NavModel} from 'app/core/core';
  5. var pluginInfoCache = {};
  6. export class AppPageCtrl {
  7. page: any;
  8. pluginId: any;
  9. appModel: any;
  10. navModel: NavModel;
  11. /** @ngInject */
  12. constructor(private backendSrv, private $routeParams: any, private $rootScope) {
  13. this.pluginId = $routeParams.pluginId;
  14. if (pluginInfoCache[this.pluginId]) {
  15. this.initPage(pluginInfoCache[this.pluginId]);
  16. } else {
  17. this.loadPluginInfo();
  18. }
  19. }
  20. initPage(app) {
  21. this.appModel = app;
  22. this.page = _.find(app.includes, {slug: this.$routeParams.slug});
  23. pluginInfoCache[this.pluginId] = app;
  24. if (!this.page) {
  25. this.$rootScope.appEvent('alert-error', ['App Page Not Found', '']);
  26. this.navModel = {
  27. section: {
  28. title: "Page not found",
  29. url: app.defaultNavUrl,
  30. icon: 'icon-gf icon-gf-sadface',
  31. },
  32. menu: [],
  33. };
  34. return;
  35. }
  36. let menu = [];
  37. for (let item of app.includes) {
  38. if (item.addToNav) {
  39. if (item.type === 'dashboard') {
  40. menu.push({
  41. title: item.name,
  42. url: 'dashboard/db/' + item.slug,
  43. icon: 'fa fa-fw fa-dot-circle-o',
  44. });
  45. }
  46. if (item.type === 'page') {
  47. menu.push({
  48. title: item.name,
  49. url: `plugins/${app.id}/page/${item.slug}`,
  50. icon: 'fa fa-fw fa-dot-circle-o',
  51. });
  52. }
  53. }
  54. }
  55. this.navModel = {
  56. section: {
  57. title: app.name,
  58. url: app.defaultNavUrl,
  59. iconUrl: app.info.logos.small,
  60. },
  61. menu: menu,
  62. };
  63. }
  64. loadPluginInfo() {
  65. this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(app => {
  66. this.initPage(app);
  67. });
  68. }
  69. }
  70. angular.module('grafana.controllers').controller('AppPageCtrl', AppPageCtrl);