switch.ts 1.3 KB

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