search_results.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import _ from 'lodash';
  2. import coreModule from '../../core_module';
  3. import appEvents from 'app/core/app_events';
  4. export class SearchResultsCtrl {
  5. results: any;
  6. onSelectionChanged: any;
  7. onTagSelected: any;
  8. onFolderExpanding: any;
  9. editable: boolean;
  10. /** @ngInject */
  11. constructor(private $location) {}
  12. toggleFolderExpand(section) {
  13. if (section.toggle) {
  14. if (!section.expanded && this.onFolderExpanding) {
  15. this.onFolderExpanding();
  16. }
  17. section.toggle(section).then(f => {
  18. if (this.editable && f.expanded) {
  19. if (f.items) {
  20. _.each(f.items, i => {
  21. i.checked = f.checked;
  22. });
  23. if (this.onSelectionChanged) {
  24. this.onSelectionChanged();
  25. }
  26. }
  27. }
  28. });
  29. }
  30. }
  31. navigateToFolder(section, evt) {
  32. this.$location.path(section.url);
  33. if (evt) {
  34. evt.stopPropagation();
  35. evt.preventDefault();
  36. }
  37. }
  38. toggleSelection(item, evt) {
  39. item.checked = !item.checked;
  40. if (item.items) {
  41. _.each(item.items, i => {
  42. i.checked = item.checked;
  43. });
  44. }
  45. if (this.onSelectionChanged) {
  46. this.onSelectionChanged();
  47. }
  48. if (evt) {
  49. evt.stopPropagation();
  50. evt.preventDefault();
  51. }
  52. }
  53. onItemClick(item) {
  54. if (this.$location.path().indexOf(item.url) > -1) {
  55. appEvents.emit('hide-dash-search');
  56. }
  57. }
  58. selectTag(tag, evt) {
  59. if (this.onTagSelected) {
  60. this.onTagSelected({ $tag: tag });
  61. }
  62. if (evt) {
  63. evt.stopPropagation();
  64. evt.preventDefault();
  65. }
  66. }
  67. }
  68. export function searchResultsDirective() {
  69. return {
  70. restrict: 'E',
  71. templateUrl: 'public/app/core/components/search/search_results.html',
  72. controller: SearchResultsCtrl,
  73. bindToController: true,
  74. controllerAs: 'ctrl',
  75. scope: {
  76. editable: '@',
  77. results: '=',
  78. onSelectionChanged: '&',
  79. onTagSelected: '&',
  80. onFolderExpanding: '&',
  81. },
  82. };
  83. }
  84. coreModule.directive('dashboardSearchResults', searchResultsDirective);