|
1 | 1 | (function () { |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | | - angular.module('ui.toggle',[]) |
| 4 | + angular.module('ui.toggle', []) |
5 | 5 |
|
6 | 6 | .value('$toggleSuppressError', false) |
7 | 7 |
|
|
41 | 41 | * Default: '' |
42 | 42 | * Description: Appends the value to the class attribute of the toggle. This can be used to apply custom styles. Refer to Custom Styles for reference. |
43 | 43 | */ |
44 | | - style: '', |
45 | | - /** |
46 | | - * Type: integer |
47 | | - * Default: null |
48 | | - * Description: Sets the width of the toggle. if set to null, width will be calculated. |
49 | | - */ |
50 | | - width: null, |
51 | | - /** |
52 | | - * Type: integer |
53 | | - * Default: null |
54 | | - * Description: Sets the height of the toggle. if set to null, height will be calculated. |
55 | | - */ |
56 | | - height: null |
| 44 | + style: '' |
57 | 45 | }) |
58 | 46 |
|
59 | 47 | .controller('ToggleController', |
|
63 | 51 | ngModelCtrl = {$setViewValue: angular.noop}; |
64 | 52 |
|
65 | 53 | // Configuration attributes |
66 | | - angular.forEach(['on', 'off', 'size', 'onstyle', 'offstyle', 'style', 'width', 'height'], function (key, index) { |
| 54 | + angular.forEach(['on', 'off', 'size', 'onstyle', 'offstyle', 'style'], function (key, index) { |
| 55 | + //$log.info(key + ':' + $attrs[key]); |
67 | 56 | self[key] = angular.isDefined($attrs[key]) ? (index < 6 ? $interpolate($attrs[key])($scope.$parent) : $scope.$parent.$eval($attrs[key])) : toggleConfig[key]; |
| 57 | + //$log.info(key + ':' + self[key]); |
68 | 58 | }); |
69 | 59 |
|
70 | 60 | this.init = function (ngModelCtrl_) { |
|
81 | 71 | var labels = self.element.find('label'); |
82 | 72 | angular.element(labels[0]).html(self.on); |
83 | 73 | angular.element(labels[1]).html(self.off); |
84 | | - |
85 | 74 | var spans = self.element.find('span'); |
| 75 | + var wrapperComputedWidth = self.width || Math.max(labels[0].offsetWidth, labels[1].offsetWidth) + (spans[0].offsetWidth / 2); |
| 76 | + var wrapperComputedHeight = self.height || Math.max(labels[0].offsetHeight, labels[1].offsetHeight); |
86 | 77 |
|
87 | | - var width = self.width || Math.max(labels[0].offsetWidth, labels[1].offsetWidth) + (spans[0].offsetWidth / 2); |
88 | | - var height = self.height || Math.max(labels[0].offsetHeight, labels[1].offsetHeight); |
| 78 | + var divs = self.element.find('div'); |
| 79 | + var wrapperWidth = divs[0].offsetWidth; |
| 80 | + var wrapperHeight = divs[1].offsetHeight; |
89 | 81 |
|
90 | | - $scope.wrapperStyle = {width: width, height: height}; |
| 82 | + $scope.wrapperStyle = {}; |
| 83 | + if (wrapperWidth < wrapperComputedWidth) { |
| 84 | + $scope.wrapperStyle.width = wrapperComputedWidth + 'px'; |
| 85 | + } |
91 | 86 |
|
92 | | - $scope.onClass = [self.size, 'toggle-on']; |
93 | | - $scope.offClass = [self.size, 'toggle-off']; |
| 87 | + if (wrapperHeight < wrapperComputedHeight && self.size != 'btn-xs' && self.size != 'btn-sm') { |
| 88 | + $scope.wrapperStyle.height = wrapperComputedHeight + 'px'; |
| 89 | + } |
| 90 | + $scope.onClass = [self.onstyle, self.size, 'toggle-on']; |
| 91 | + $scope.offClass = [self.offstyle, self.size, 'toggle-off']; |
94 | 92 | $scope.handleClass = [self.size, 'toggle-handle']; |
95 | 93 | }; |
96 | 94 |
|
|
127 | 125 | return { |
128 | 126 | restrict: 'E', |
129 | 127 | transclude: true, |
130 | | - template: '<div class="toggle btn" ng-class="wrapperClass" ng-style="::wrapperStyle" ng-click="onSwitch()"><div class="toggle-group"><label class="btn btn-primary" ng-class="::onClass"></label><label class="btn btn-default active" ng-class="::offClass"></label><span class="btn btn-default" ng-class="::handleClass"></span></div></div>', |
| 128 | + template: '<div class="toggle btn" ng-class="wrapperClass" ng-style="::wrapperStyle" ng-click="onSwitch()"><div class="toggle-group"><label class="btn" ng-class="::onClass"></label><label class="btn active" ng-class="::offClass"></label><span class="btn btn-default" ng-class="::handleClass"></span></div></div>', |
131 | 129 | scope: { |
132 | 130 | bindModel: '=ngModel' |
133 | 131 | }, |
|
0 commit comments