|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import angular from 'angular'; |
| 21 | +import { uiModules } from 'ui/modules'; |
| 22 | +const module = uiModules.get('kibana'); |
| 23 | + |
| 24 | +module.constant('buttonConfig', { |
| 25 | + activeClass: 'active', |
| 26 | + toggleEvent: 'click' |
| 27 | +}); |
| 28 | + |
| 29 | +module.controller('ButtonsController', ['buttonConfig', function (buttonConfig) { |
| 30 | + this.activeClass = buttonConfig.activeClass || 'active'; |
| 31 | + this.toggleEvent = buttonConfig.toggleEvent || 'click'; |
| 32 | +}]); |
| 33 | + |
| 34 | +module.directive('btnRadio', function () { |
| 35 | + return { |
| 36 | + require: ['btnRadio', 'ngModel'], |
| 37 | + controller: 'ButtonsController', |
| 38 | + link: function (scope, element, attrs, ctrls) { |
| 39 | + const buttonsCtrl = ctrls[0]; |
| 40 | + const ngModelCtrl = ctrls[1]; |
| 41 | + |
| 42 | + //model -> UI |
| 43 | + ngModelCtrl.$render = function () { |
| 44 | + element.toggleClass(buttonsCtrl.activeClass, angular.equals(ngModelCtrl.$modelValue, scope.$eval(attrs.btnRadio))); |
| 45 | + }; |
| 46 | + |
| 47 | + //ui->model |
| 48 | + element.bind(buttonsCtrl.toggleEvent, function () { |
| 49 | + const isActive = element.hasClass(buttonsCtrl.activeClass); |
| 50 | + |
| 51 | + if (!isActive || angular.isDefined(attrs.uncheckable)) { |
| 52 | + scope.$apply(function () { |
| 53 | + ngModelCtrl.$setViewValue(isActive ? null : scope.$eval(attrs.btnRadio)); |
| 54 | + ngModelCtrl.$render(); |
| 55 | + }); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + }; |
| 60 | +}); |
0 commit comments