picker.ts 1.8 KB

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