picker.ts 2.3 KB

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