add_panel.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // import VirtualScroll from 'virtual-scroll';
  6. // console.log(VirtualScroll);
  7. export class AddPanelCtrl {
  8. row: any;
  9. dashboard: any;
  10. rowCtrl: any;
  11. allPanels: any;
  12. panelHits: any;
  13. activeIndex: any;
  14. panelSearch: any;
  15. /** @ngInject */
  16. constructor(private $scope, private $timeout, private $rootScope) {
  17. this.row = this.rowCtrl.row;
  18. this.dashboard = this.rowCtrl.dashboard;
  19. this.allPanels = _.orderBy(_.map(config.panels, item => item), 'sort');
  20. this.panelHits = this.allPanels;
  21. this.activeIndex = 0;
  22. }
  23. keyDown(evt) {
  24. if (evt.keyCode === 27) {
  25. this.rowCtrl.showOptions = false;
  26. return;
  27. }
  28. if (evt.keyCode === 40 || evt.keyCode === 39) {
  29. this.moveSelection(1);
  30. }
  31. if (evt.keyCode === 38 || evt.keyCode === 37) {
  32. this.moveSelection(-1);
  33. }
  34. if (evt.keyCode === 13) {
  35. var selectedPanel = this.panelHits[this.activeIndex];
  36. if (selectedPanel) {
  37. this.addPanel(selectedPanel);
  38. }
  39. }
  40. }
  41. moveSelection(direction) {
  42. var max = this.panelHits.length;
  43. var newIndex = this.activeIndex + direction;
  44. this.activeIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  45. }
  46. panelSearchChanged() {
  47. var items = this.allPanels.slice();
  48. var startsWith = [];
  49. var contains = [];
  50. var searchLower = this.panelSearch.toLowerCase();
  51. var item;
  52. while (item = items.shift()) {
  53. var nameLower = item.name.toLowerCase();
  54. if (nameLower.indexOf(searchLower) === 0) {
  55. startsWith.push(item);
  56. } else if (nameLower.indexOf(searchLower) !== -1) {
  57. contains.push(item);
  58. }
  59. }
  60. this.panelHits = startsWith.concat(contains);
  61. this.activeIndex = 0;
  62. }
  63. addPanel(panelPluginInfo) {
  64. var defaultSpan = 12;
  65. var _as = 12 - this.dashboard.rowSpan(this.row);
  66. var panel = {
  67. id: null,
  68. title: config.new_panel_title,
  69. error: false,
  70. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  71. editable: true,
  72. type: panelPluginInfo.id,
  73. isNew: true,
  74. };
  75. this.rowCtrl.dropView = 0;
  76. this.dashboard.addPanel(panel, this.row);
  77. this.$timeout(() => {
  78. this.$rootScope.appEvent('panel-change-view', {
  79. fullscreen: true, edit: true, panelId: panel.id
  80. });
  81. });
  82. }
  83. }
  84. export function addPanelDirective() {
  85. return {
  86. restrict: 'E',
  87. templateUrl: 'public/app/features/dashboard/row/add_panel.html',
  88. controller: AddPanelCtrl,
  89. bindToController: true,
  90. controllerAs: 'ctrl',
  91. scope: {
  92. rowCtrl: "=",
  93. },
  94. };
  95. }
  96. coreModule.directive('dashRowAddPanel', addPanelDirective);