add_panel.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {coreModule} from 'app/core/core';
  5. export class AddPanelCtrl {
  6. dashboard: any;
  7. allPanels: any;
  8. panelHits: any;
  9. activeIndex: any;
  10. panelSearch: any;
  11. /** @ngInject */
  12. constructor(private $rootScope, dashboardSrv) {
  13. this.dashboard = dashboardSrv.getCurrent();
  14. this.activeIndex = 0;
  15. this.allPanels = _.chain(config.panels)
  16. .filter({hideFromList: false})
  17. .map(item => item)
  18. .orderBy('sort')
  19. .value();
  20. this.panelHits = this.allPanels;
  21. }
  22. dismiss() {
  23. this.$rootScope.appEvent('hide-dash-editor');
  24. }
  25. keyDown(evt) {
  26. if (evt.keyCode === 27) {
  27. this.dismiss();
  28. return;
  29. }
  30. if (evt.keyCode === 40 || evt.keyCode === 39) {
  31. this.moveSelection(1);
  32. }
  33. if (evt.keyCode === 38 || evt.keyCode === 37) {
  34. this.moveSelection(-1);
  35. }
  36. if (evt.keyCode === 13) {
  37. var selectedPanel = this.panelHits[this.activeIndex];
  38. if (selectedPanel) {
  39. this.addPanel(selectedPanel);
  40. }
  41. }
  42. }
  43. moveSelection(direction) {
  44. var max = this.panelHits.length;
  45. var newIndex = this.activeIndex + direction;
  46. this.activeIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  47. }
  48. panelSearchChanged() {
  49. var items = this.allPanels.slice();
  50. var startsWith = [];
  51. var contains = [];
  52. var searchLower = this.panelSearch.toLowerCase();
  53. var item;
  54. while (item = items.shift()) {
  55. var nameLower = item.name.toLowerCase();
  56. if (nameLower.indexOf(searchLower) === 0) {
  57. startsWith.push(item);
  58. } else if (nameLower.indexOf(searchLower) !== -1) {
  59. contains.push(item);
  60. }
  61. }
  62. this.panelHits = startsWith.concat(contains);
  63. this.activeIndex = 0;
  64. }
  65. addPanel(panelPluginInfo) {
  66. let defaultHeight = 6;
  67. let defaultWidth = 6;
  68. if (panelPluginInfo.id === "singlestat") {
  69. defaultWidth = 3;
  70. defaultHeight = 3;
  71. }
  72. this.dashboard.addPanel({
  73. type: panelPluginInfo.id,
  74. x: 0,
  75. y: 0,
  76. width: defaultWidth,
  77. height: defaultHeight,
  78. title: 'New panel',
  79. });
  80. this.dismiss();
  81. }
  82. }
  83. export function addPanelDirective() {
  84. return {
  85. restrict: 'E',
  86. templateUrl: 'public/app/features/dashboard/row/add_panel.html',
  87. controller: AddPanelCtrl,
  88. bindToController: true,
  89. controllerAs: 'ctrl',
  90. scope: {},
  91. };
  92. }
  93. coreModule.directive('addPanel', addPanelDirective);