add_panel.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. panelSearchBlur() {
  40. // this.$timeout(() => {
  41. // this.rowCtrl.dropView = 0;
  42. // }, 400);
  43. }
  44. moveSelection(direction) {
  45. var max = this.panelHits.length;
  46. var newIndex = this.activeIndex + direction;
  47. this.activeIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  48. }
  49. panelSearchChanged() {
  50. var items = this.allPanels.slice();
  51. var startsWith = [];
  52. var contains = [];
  53. var searchLower = this.panelSearch.toLowerCase();
  54. var item;
  55. while (item = items.shift()) {
  56. var nameLower = item.name.toLowerCase();
  57. if (nameLower.indexOf(searchLower) === 0) {
  58. startsWith.push(item);
  59. } else if (nameLower.indexOf(searchLower) !== -1) {
  60. contains.push(item);
  61. }
  62. }
  63. this.panelHits = startsWith.concat(contains);
  64. this.activeIndex = 0;
  65. }
  66. addPanel(panelPluginInfo) {
  67. var defaultSpan = 12;
  68. var span = 12 - this.row.span;
  69. var panel = {
  70. id: null,
  71. title: config.new_panel_title,
  72. span: span < defaultSpan && span > 0 ? span : defaultSpan,
  73. type: panelPluginInfo.id,
  74. };
  75. this.rowCtrl.closeDropView();
  76. this.dashboard.addPanel(panel, this.row);
  77. this.$timeout(() => {
  78. this.$rootScope.$broadcast('render');
  79. //this.$rootScope.appEvent('panel-change-view', {
  80. // fullscreen: true, edit: true, panelId: panel.id
  81. //});
  82. });
  83. }
  84. }
  85. export function addPanelDirective() {
  86. return {
  87. restrict: 'E',
  88. templateUrl: 'public/app/features/dashboard/row/add_panel.html',
  89. controller: AddPanelCtrl,
  90. bindToController: true,
  91. controllerAs: 'ctrl',
  92. scope: {
  93. rowCtrl: "=",
  94. },
  95. };
  96. }
  97. coreModule.directive('dashRowAddPanel', addPanelDirective);