picker.ts 1.8 KB

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