switch.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 this.$timeout(() => {
  26. return this.onChange();
  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. tooltip: "@",
  41. switchClass: "@",
  42. onChange: "&",
  43. },
  44. template: template,
  45. link: (scope, elem) => {
  46. if (scope.ctrl.tooltip) {
  47. var drop = new Drop({
  48. target: elem[0],
  49. content: scope.ctrl.tooltip,
  50. position: "right middle",
  51. classes: 'drop-help',
  52. openOn: 'hover',
  53. hoverOpenDelay: 400,
  54. });
  55. scope.$on('$destroy', function() {
  56. drop.destroy();
  57. });
  58. }
  59. }
  60. };
  61. }
  62. coreModule.directive('gfFormSwitch', switchDirective);