switch.ts 1.7 KB

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