picker.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import coreModule from 'app/core/core_module';
  3. import appEvents from 'app/core/app_events';
  4. import _ from 'lodash';
  5. export class FolderPickerCtrl {
  6. folders: Folder[];
  7. selectedOption: any;
  8. initialTitle: string;
  9. onChange: any;
  10. labelClass: string;
  11. /** @ngInject */
  12. constructor(private backendSrv, private $scope, private $sce) {
  13. if (!this.labelClass) {
  14. this.labelClass = "width-7";
  15. }
  16. this.selectedOption = {text: this.initialTitle, value: null};
  17. }
  18. getOptions(query) {
  19. var params = {
  20. query: query,
  21. type: 'dash-folder',
  22. };
  23. return this.backendSrv.search(params).then(result => {
  24. if (query === "") {
  25. result.unshift({title: "Root", value: 0});
  26. }
  27. return _.map(result, item => {
  28. return {text: item.title, value: item.id};
  29. });
  30. });
  31. }
  32. folderChanged(option) {
  33. this.onChange({$folder: {id: option.value, title: option.text}});
  34. }
  35. }
  36. export interface Folder {
  37. id: number;
  38. title: string;
  39. uri?: string;
  40. type: string;
  41. tags?: string[];
  42. isStarred?: boolean;
  43. parentId?: number;
  44. dashboards?: any;
  45. }
  46. const template = `
  47. <div class="gf-form">
  48. <label class="gf-form-label {{ctrl.labelClass}}">Folder</label>
  49. <div class="dropdown">
  50. <gf-form-dropdown model="ctrl.selectedOption"
  51. get-options="ctrl.getOptions($query)"
  52. on-change="ctrl.folderChanged($option)">
  53. </gf-form-dropdown>
  54. </div>
  55. </div>
  56. `;
  57. export function folderPicker() {
  58. return {
  59. restrict: 'E',
  60. template: template,
  61. controller: FolderPickerCtrl,
  62. bindToController: true,
  63. controllerAs: 'ctrl',
  64. scope: {
  65. initialTitle: "<",
  66. onChange: "&",
  67. labelClass: "@",
  68. }
  69. };
  70. }
  71. coreModule.directive('folderPicker', folderPicker);