search_results.ts 1.9 KB

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