switch.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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" ng-show="ctrl.label">
  9. {{ctrl.label}}
  10. <info-popover mode="right-normal" ng-if="ctrl.tooltip" position="top center">
  11. {{ctrl.tooltip}}
  12. </info-popover>
  13. </label>
  14. <div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
  15. <input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
  16. <label for="check-{{ctrl.id}}" data-on="Yes" data-off="No"></label>
  17. </div>
  18. `;
  19. export class SwitchCtrl {
  20. onChange: any;
  21. checked: any;
  22. show: any;
  23. id: any;
  24. label: string;
  25. /** @ngInject */
  26. constructor($scope, private $timeout) {
  27. this.show = true;
  28. this.id = $scope.$id;
  29. }
  30. internalOnChange() {
  31. return this.$timeout(() => {
  32. return this.onChange();
  33. });
  34. }
  35. }
  36. export function switchDirective() {
  37. return {
  38. restrict: 'E',
  39. controller: SwitchCtrl,
  40. controllerAs: 'ctrl',
  41. bindToController: true,
  42. scope: {
  43. checked: "=",
  44. label: "@",
  45. labelClass: "@",
  46. tooltip: "@",
  47. switchClass: "@",
  48. onChange: "&",
  49. },
  50. template: template,
  51. };
  52. }
  53. coreModule.directive('gfFormSwitch', switchDirective);