2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2016-03-21 15:24:06 -05:00
|
|
|
|
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">
|
2018-11-16 06:02:29 -06:00
|
|
|
<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>
|
2016-03-21 15:24:06 -05:00
|
|
|
`;
|
|
|
|
|
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>
|
|
|
|
`;
|
|
|
|
|
2016-03-21 15:24:06 -05:00
|
|
|
export class SwitchCtrl {
|
|
|
|
onChange: any;
|
2016-03-28 06:10:42 -05:00
|
|
|
checked: any;
|
2016-03-28 10:08:31 -05:00
|
|
|
show: any;
|
2016-03-29 08:53:41 -05:00
|
|
|
id: any;
|
2017-06-06 04:03:56 -05:00
|
|
|
label: string;
|
2016-03-28 06:10:42 -05:00
|
|
|
|
2016-03-28 14:45:31 -05:00
|
|
|
/** @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;
|
2016-03-29 08:53:41 -05:00
|
|
|
this.id = $scope.$id;
|
2016-03-28 06:10:42 -05:00
|
|
|
}
|
2016-03-21 15:24:06 -05:00
|
|
|
|
|
|
|
internalOnChange() {
|
2016-04-11 15:21:25 -05:00
|
|
|
return this.$timeout(() => {
|
|
|
|
return this.onChange();
|
2016-03-21 15:24:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function switchDirective() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
2016-03-21 15:24:06 -05:00
|
|
|
controller: SwitchCtrl,
|
2017-12-20 05:33:33 -06:00
|
|
|
controllerAs: 'ctrl',
|
2016-03-21 15:24:06 -05:00
|
|
|
bindToController: true,
|
|
|
|
scope: {
|
2017-12-20 05:33:33 -06:00
|
|
|
checked: '=',
|
|
|
|
label: '@',
|
|
|
|
labelClass: '@',
|
|
|
|
tooltip: '@',
|
|
|
|
switchClass: '@',
|
|
|
|
onChange: '&',
|
2016-03-21 15:24:06 -05:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
template: template,
|
2016-03-21 15:24:06 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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);
|