switch.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ///<reference path="../../headers/common.d.ts" />
  2. import coreModule from 'app/core/core_module';
  3. var template = `
  4. <label for="check-{{ctrl.id}}" class="gf-form-label {{ctrl.labelClass}} pointer" 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. </label>
  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. <label for="check-{{ctrl.id}}" data-on="Yes" data-off="No"></label>
  13. </div>
  14. `;
  15. export class SwitchCtrl {
  16. onChange: any;
  17. checked: any;
  18. show: any;
  19. id: any;
  20. label: string;
  21. /** @ngInject */
  22. constructor($scope, private $timeout) {
  23. this.show = true;
  24. this.id = $scope.$id;
  25. }
  26. internalOnChange() {
  27. return this.$timeout(() => {
  28. return this.onChange();
  29. });
  30. }
  31. }
  32. export function switchDirective() {
  33. return {
  34. restrict: 'E',
  35. controller: SwitchCtrl,
  36. controllerAs: 'ctrl',
  37. bindToController: true,
  38. scope: {
  39. checked: '=',
  40. label: '@',
  41. labelClass: '@',
  42. tooltip: '@',
  43. switchClass: '@',
  44. onChange: '&',
  45. },
  46. template: template,
  47. };
  48. }
  49. coreModule.directive('gfFormSwitch', switchDirective);