switch.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. export class SwitchCtrl {
  17. onChange: any;
  18. checked: any;
  19. show: any;
  20. id: any;
  21. label: string;
  22. /** @ngInject */
  23. constructor($scope, private $timeout) {
  24. this.show = true;
  25. this.id = $scope.$id;
  26. }
  27. internalOnChange() {
  28. return this.$timeout(() => {
  29. return this.onChange();
  30. });
  31. }
  32. }
  33. export function switchDirective() {
  34. return {
  35. restrict: 'E',
  36. controller: SwitchCtrl,
  37. controllerAs: 'ctrl',
  38. bindToController: true,
  39. scope: {
  40. checked: '=',
  41. label: '@',
  42. labelClass: '@',
  43. tooltip: '@',
  44. switchClass: '@',
  45. onChange: '&',
  46. },
  47. template: template,
  48. };
  49. }
  50. coreModule.directive('gfFormSwitch', switchDirective);