grafana/public/app/core/components/switch.ts

91 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import coreModule from 'app/core/core_module';
2018-08-29 07:26:50 -05:00
const template = `
2018-11-20 02:39:56 -06:00
<label for="check-{{ctrl.id}}" class="gf-form-switch-container">
<div class="gf-form-label {{ctrl.labelClass}}" ng-show="ctrl.label">
{{ctrl.label}}
<info-popover mode="right-normal" ng-if="ctrl.tooltip" position="top center">
{{ctrl.tooltip}}
</info-popover>
</div>
<div class="gf-form-switch {{ctrl.switchClass}}" ng-if="ctrl.show">
<input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
<span class="gf-form-switch__slider"></span>
</div>
2016-04-15 18:56:39 -05:00
</label>
`;
2018-11-19 10:41:48 -06:00
const checkboxTemplate = `
2018-11-20 02:39:56 -06:00
<label for="check-{{ctrl.id}}" class="gf-form-switch-container">
<div class="gf-form-label {{ctrl.labelClass}}" ng-show="ctrl.label">
{{ctrl.label}}
<info-popover mode="right-normal" ng-if="ctrl.tooltip" position="top center">
{{ctrl.tooltip}}
</info-popover>
</div>
<div class="gf-form-checkbox {{ctrl.switchClass}}" ng-if="ctrl.show">
2018-11-19 10:41:48 -06:00
<input id="check-{{ctrl.id}}" type="checkbox" ng-model="ctrl.checked" ng-change="ctrl.internalOnChange()">
<span class="gf-form-switch__checkbox"></span>
</div>
</label>
`;
export class SwitchCtrl {
onChange: any;
checked: any;
2016-03-28 10:08:31 -05:00
show: any;
id: any;
label: string;
/** @ngInject */
2019-05-13 02:38:19 -05:00
constructor($scope: any, private $timeout: any) {
2016-03-28 10:08:31 -05:00
this.show = true;
this.id = $scope.$id;
}
internalOnChange() {
2016-04-11 15:21:25 -05:00
return this.$timeout(() => {
return this.onChange();
});
}
}
export function switchDirective() {
return {
2017-12-20 05:33:33 -06:00
restrict: 'E',
controller: SwitchCtrl,
2017-12-20 05:33:33 -06:00
controllerAs: 'ctrl',
bindToController: true,
scope: {
2017-12-20 05:33:33 -06:00
checked: '=',
label: '@',
labelClass: '@',
tooltip: '@',
switchClass: '@',
onChange: '&',
},
2017-12-20 05:33:33 -06:00
template: template,
};
}
2018-11-19 10:41:48 -06:00
export function checkboxDirective() {
return {
restrict: 'E',
controller: SwitchCtrl,
controllerAs: 'ctrl',
bindToController: true,
scope: {
checked: '=',
label: '@',
labelClass: '@',
tooltip: '@',
switchClass: '@',
onChange: '&',
},
template: checkboxTemplate,
};
}
2017-12-20 05:33:33 -06:00
coreModule.directive('gfFormSwitch', switchDirective);
2018-11-19 10:41:48 -06:00
coreModule.directive('gfFormCheckbox', checkboxDirective);