switch.ts 2.2 KB

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