switch.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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);