switch.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import coreModule from 'app/core/core_module';
  2. const template = `
  3. <label for="check-{{ctrl.id}}" class="gf-form-switch-container pointer">
  4. <div class="gf-form-label {{ctrl.labelClass}}" ng-show="ctrl.label">
  5. {{ctrl.label}}
  6. <info-popover mode="right-normal" ng-if="ctrl.tooltip" position="top center">
  7. {{ctrl.tooltip}}
  8. </info-popover>
  9. </div>
  10. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  11. <input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  12. <span class="gf-form-switch__slider"></span>
  13. </div>
  14. </label>
  15. `;
  16. const checkboxTemplate = `
  17. <label for="check-{{ctrl.id}}" class="gf-form-check-container">
  18. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  19. <input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  20. <span class="gf-form-switch__checkbox"></span>
  21. </div>
  22. </label>
  23. `;
  24. export class SwitchCtrl {
  25. onChange: any;
  26. checked: any;
  27. show: any;
  28. id: any;
  29. label: string;
  30. /** @ngInject */
  31. constructor($scope, private $timeout) {
  32. this.show = true;
  33. this.id = $scope.$id;
  34. }
  35. internalOnChange() {
  36. return this.$timeout(() => {
  37. return this.onChange();
  38. });
  39. }
  40. }
  41. export function switchDirective() {
  42. return {
  43. restrict: 'E',
  44. controller: SwitchCtrl,
  45. controllerAs: 'ctrl',
  46. bindToController: true,
  47. scope: {
  48. checked: '=',
  49. label: '@',
  50. labelClass: '@',
  51. tooltip: '@',
  52. switchClass: '@',
  53. onChange: '&',
  54. },
  55. template: template,
  56. };
  57. }
  58. export function checkboxDirective() {
  59. return {
  60. restrict: 'E',
  61. controller: SwitchCtrl,
  62. controllerAs: 'ctrl',
  63. bindToController: true,
  64. scope: {
  65. checked: '=',
  66. label: '@',
  67. labelClass: '@',
  68. tooltip: '@',
  69. switchClass: '@',
  70. onChange: '&',
  71. },
  72. template: checkboxTemplate,
  73. };
  74. }
  75. coreModule.directive('gfFormSwitch', switchDirective);
  76. coreModule.directive('gfFormCheckbox', checkboxDirective);