switch.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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}}" >
  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. internalOnChange() {
  16. return new Promise(resolve => {
  17. setTimeout(() => {
  18. this.onChange();
  19. resolve();
  20. });
  21. });
  22. }
  23. }
  24. export function switchDirective() {
  25. return {
  26. restrict: 'E',
  27. controller: SwitchCtrl,
  28. controllerAs: 'ctrl',
  29. bindToController: true,
  30. scope: {
  31. checked: "=",
  32. label: "@",
  33. labelClass: "@",
  34. switchClass: "@",
  35. onChange: "&",
  36. },
  37. template: template,
  38. };
  39. }
  40. coreModule.directive('gfFormSwitch', switchDirective);