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} 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 $timeout, private $rootScope) {
  15. this.row = this.rowCtrl.row;
  16. this.dashboard = this.rowCtrl.dashboard;
  17. this.activeIndex = 0;
  18. this.allPanels = _.chain(config.panels)
  19. .filter({hideFromList: false})
  20. .map(item => item)
  21. .orderBy('sort')
  22. .value();
  23. this.panelHits = this.allPanels;
  24. }
  25. keyDown(evt) {
  26. if (evt.keyCode === 27) {
  27. this.rowCtrl.dropView = 0;
  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. var defaultSpan = 12;
  67. var span = 12 - this.row.span;
  68. var panel = {
  69. id: null,
  70. title: config.new_panel_title,
  71. span: span < defaultSpan && span > 0 ? span : defaultSpan,
  72. type: panelPluginInfo.id,
  73. };
  74. this.rowCtrl.closeDropView();
  75. this.dashboard.addPanel(panel, this.row);
  76. this.$timeout(() => {
  77. this.$rootScope.$broadcast('render');
  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);