switch.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. var template = `
  7. <label for="check-{{$id}}" class="gf-form-label {{ctrl.labelClass}} pointer">{{ctrl.label}}</label>
  8. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  9. <input id="check-{{$id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  10. <label for="check-{{$id}}" data-on="Yes" data-off="No"></label>
  11. </div>
  12. `;
  13. export class SwitchCtrl {
  14. onChange: any;
  15. checked: any;
  16. show: any;
  17. /** @ngInject */
  18. constructor() {
  19. this.show = true;
  20. }
  21. internalOnChange() {
  22. return new Promise(resolve => {
  23. setTimeout(() => {
  24. this.onChange();
  25. resolve();
  26. });
  27. });
  28. }
  29. }
  30. export function switchDirective() {
  31. return {
  32. restrict: 'E',
  33. controller: SwitchCtrl,
  34. controllerAs: 'ctrl',
  35. bindToController: true,
  36. scope: {
  37. checked: "=",
  38. label: "@",
  39. labelClass: "@",
  40. switchClass: "@",
  41. onChange: "&",
  42. },
  43. template: template,
  44. };
  45. }
  46. coreModule.directive('gfFormSwitch', switchDirective);