switch.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import coreModule from 'app/core/core_module';
  2. const template = `
  3. <label for="check-{{ctrl.id}}" class="gf-form-label {{ctrl.labelClass}} pointer" ng-show="ctrl.label">
  4. {{ctrl.label}}
  5. <info-popover mode="right-normal" ng-if="ctrl.tooltip" position="top center">
  6. {{ctrl.tooltip}}
  7. </info-popover>
  8. </label>
  9. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  10. <input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  11. <label for="check-{{ctrl.id}}" data-on="Yes" data-off="No"></label>
  12. </div>
  13. `;
  14. export class SwitchCtrl {
  15. onChange: any;
  16. checked: any;
  17. show: any;
  18. id: any;
  19. label: string;
  20. /** @ngInject */
  21. constructor($scope, private $timeout) {
  22. this.show = true;
  23. this.id = $scope.$id;
  24. }
  25. internalOnChange() {
  26. return this.$timeout(() => {
  27. return this.onChange();
  28. });
  29. }
  30. }
  31. export function switchDirective() {
  32. return {
  33. restrict: 'E',
  34. controller: SwitchCtrl,
  35. controllerAs: 'ctrl',
  36. bindToController: true,
  37. scope: {
  38. checked: '=',
  39. label: '@',
  40. labelClass: '@',
  41. tooltip: '@',
  42. switchClass: '@',
  43. onChange: '&',
  44. },
  45. template: template,
  46. };
  47. }
  48. coreModule.directive('gfFormSwitch', switchDirective);