switch.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-{{$id}}" class="gf-form-label {{ctrl.labelClass}} pointer">{{ctrl.label}}</label>
  9. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  10. <input id="check-{{$id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  11. <label for="check-{{$id}}" data-on="Yes" data-off="No"></label>
  12. </div>
  13. `;
  14. export class SwitchCtrl {
  15. onChange: any;
  16. checked: any;
  17. show: any;
  18. /** @ngInject */
  19. constructor() {
  20. this.show = true;
  21. }
  22. internalOnChange() {
  23. return new Promise(resolve => {
  24. setTimeout(() => {
  25. this.onChange();
  26. resolve();
  27. });
  28. });
  29. }
  30. }
  31. export function switchDirective() {
  32. return {
  33. restrict: 'E',
  34. controller: SwitchCtrl,
  35. controllerAs: 'ctrl',
  36. bindToController: true,
  37. scope: {
  38. checked: "=",
  39. label: "@",
  40. labelClass: "@",
  41. tooltip: "@",
  42. switchClass: "@",
  43. onChange: "&",
  44. },
  45. template: template,
  46. link: (scope, elem) => {
  47. if (scope.ctrl.tooltip) {
  48. var drop = new Drop({
  49. target: elem[0],
  50. content: scope.ctrl.tooltip,
  51. position: "right middle",
  52. classes: 'drop-help',
  53. openOn: 'hover',
  54. hoverOpenDelay: 400,
  55. });
  56. scope.$on('$destroy', function() {
  57. drop.destroy();
  58. });
  59. }
  60. }
  61. };
  62. }
  63. coreModule.directive('gfFormSwitch', switchDirective);