add_panel.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {coreModule, appEvents} from 'app/core/core';
  5. export class AddPanelCtrl {
  6. row: any;
  7. dashboard: any;
  8. rowCtrl: any;
  9. allPanels: any;
  10. panelHits: any;
  11. activeIndex: any;
  12. panelSearch: any;
  13. /** @ngInject */
  14. constructor(private $scope, private $timeout, private $rootScope) {
  15. this.row = this.rowCtrl.row;
  16. this.dashboard = this.rowCtrl.dashboard;
  17. this.allPanels = _.orderBy(_.map(config.panels, item => item), 'sort');
  18. this.panelHits = this.allPanels;
  19. this.activeIndex = 0;
  20. }
  21. keyDown(evt) {
  22. if (evt.keyCode === 27) {
  23. this.rowCtrl.dropView = 0;
  24. return;
  25. }
  26. if (evt.keyCode === 40 || evt.keyCode === 39) {
  27. this.moveSelection(1);
  28. }
  29. if (evt.keyCode === 38 || evt.keyCode === 37) {
  30. this.moveSelection(-1);
  31. }
  32. if (evt.keyCode === 13) {
  33. var selectedPanel = this.panelHits[this.activeIndex];
  34. if (selectedPanel) {
  35. this.addPanel(selectedPanel);
  36. }
  37. }
  38. }
  39. moveSelection(direction) {
  40. var max = this.panelHits.length;
  41. var newIndex = this.activeIndex + direction;
  42. this.activeIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  43. }
  44. panelSearchChanged() {
  45. var items = this.allPanels.slice();
  46. var startsWith = [];
  47. var contains = [];
  48. var searchLower = this.panelSearch.toLowerCase();
  49. var item;
  50. while (item = items.shift()) {
  51. var nameLower = item.name.toLowerCase();
  52. if (nameLower.indexOf(searchLower) === 0) {
  53. startsWith.push(item);
  54. } else if (nameLower.indexOf(searchLower) !== -1) {
  55. contains.push(item);
  56. }
  57. }
  58. this.panelHits = startsWith.concat(contains);
  59. this.activeIndex = 0;
  60. }
  61. addPanel(panelPluginInfo) {
  62. var defaultSpan = 12;
  63. var span = 12 - this.row.span;
  64. var panel = {
  65. id: null,
  66. title: config.new_panel_title,
  67. error: false,
  68. span: span < defaultSpan && span > 0 ? span : defaultSpan,
  69. editable: true,
  70. type: panelPluginInfo.id,
  71. isNew: true,
  72. };
  73. this.rowCtrl.closeDropView();
  74. this.dashboard.addPanel(panel, this.row);
  75. this.$timeout(() => {
  76. this.$rootScope.$broadcast('render');
  77. //this.$rootScope.appEvent('panel-change-view', {
  78. // fullscreen: true, edit: true, panelId: panel.id
  79. //});
  80. });
  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. rowCtrl: "=",
  92. },
  93. };
  94. }
  95. coreModule.directive('dashRowAddPanel', addPanelDirective);