|
62 | 62 | * Example: <toggle ... toggle-style="{'border': '1px dashed #f00'}"> |
63 | 63 | */ |
64 | 64 | toggleStyle: '', |
| 65 | + /** |
| 66 | + * Type: string |
| 67 | + * Default: '' |
| 68 | + * Description: Passes a class to the toggle's first immediate child |
| 69 | + **/ |
| 70 | + toggleClass: '', |
| 71 | + style: '', |
65 | 72 | /** |
66 | 73 | * Type: string |
67 | 74 | * Default: '' |
|
89 | 96 | }) |
90 | 97 |
|
91 | 98 | .controller('ToggleController', |
92 | | - ['$scope', '$attrs', '$interpolate', '$log', 'toggleConfig', '$toggleSuppressError', |
93 | | - function ($scope, $attrs, $interpolate, $log, toggleConfig, $toggleSuppressError) { |
| 99 | + ['$scope', '$attrs', '$interpolate', '$log', '$document', 'toggleConfig', '$toggleSuppressError', |
| 100 | + function ($scope, $attrs, $interpolate, $log, $document, toggleConfig, $toggleSuppressError) { |
94 | 101 | var self = this; |
95 | 102 | var labels, spans, divs; |
96 | 103 | var ngModelCtrl; |
|
122 | 129 | if (self.offstyle) { |
123 | 130 | self.offClass = self.offstyle; |
124 | 131 | } |
| 132 | + // Special treatment for style, now deprecated and replaced with toggleClass |
| 133 | + if (self.style) { |
| 134 | + self.toggleClass = self.style; |
| 135 | + } |
| 136 | + |
| 137 | + // Special case: empty on and off labels (replace with blank space) |
| 138 | + if (self.on === '') { |
| 139 | + self.on = ' '; |
| 140 | + } |
| 141 | + if (self.off === '') { |
| 142 | + self.off = ' '; |
| 143 | + } |
125 | 144 |
|
126 | 145 | this.init = function (ngModelCtrl_) { |
127 | 146 | ngModelCtrl = ngModelCtrl_; |
|
139 | 158 | angular.element(self.onElement).html(self.on); |
140 | 159 | angular.element(self.offElement).html(self.off); |
141 | 160 |
|
| 161 | + // Set the button size |
| 162 | + angular.element(self.wrapperElement).addClass(self.size); |
| 163 | + angular.element(self.onElement).addClass(self.size); |
| 164 | + angular.element(self.offElement).addClass(self.size); |
| 165 | + angular.element(self.handleElement).addClass(self.size); |
| 166 | + |
| 167 | + // Set the toggleClass on the wrapper |
| 168 | + angular.element(self.wrapperElement).addClass(self.toggleClass); |
| 169 | + |
| 170 | + // Calculate the propery width |
| 171 | + if (!self.width) { |
| 172 | + var wrapperComputedWidth = Math.max( |
| 173 | + self.onElement.scrollWidth, |
| 174 | + self.offElement.scrollWidth |
| 175 | + ) + Math.max(self.handleElement.scrollWidth, 16) / 2 + 2; |
| 176 | + self.width = wrapperComputedWidth + 'px'; |
| 177 | + } |
| 178 | + |
| 179 | + // Calculate the proper height |
| 180 | + if (!self.height) { |
| 181 | + var wrapperComputedHeight = Math.max( |
| 182 | + self.onElement.scrollHeight, |
| 183 | + self.offElement.scrollHeight) + 2; |
| 184 | + self.height = wrapperComputedHeight + 'px'; |
| 185 | + } |
| 186 | + |
| 187 | + // Add the toggle-on and toggle-off classes, that change position and size of the labels |
| 188 | + // and make sure that the buttons are properly placed. |
| 189 | + // Once this is done, the height and width properties of the labels is no longer relevant, |
| 190 | + // because of their new placement. |
| 191 | + angular.element(self.onElement).addClass(self.onClass).addClass('toggle-on'); |
| 192 | + angular.element(self.offElement).addClass(self.offClass).addClass('toggle-off'); |
| 193 | + |
| 194 | + // Compute first style |
142 | 195 | self.computeStyle(); |
143 | 196 |
|
144 | 197 | ngModelCtrl.$render = function () { |
|
165 | 218 |
|
166 | 219 | // Build an object for widget's ng-style |
167 | 220 | $scope.wrapperStyle = (self.toggleStyle) ? $scope.$parent.$eval(self.toggleStyle) : {}; |
| 221 | + $scope.wrapperStyle.width = self.width; |
| 222 | + $scope.wrapperStyle.height = self.height; |
168 | 223 |
|
169 | | - // Calculate the proper width |
170 | | - if (self.width) { |
171 | | - $scope.wrapperStyle.width = self.width; |
172 | | - } else { |
173 | | - var wrapperComputedWidth = Math.max( |
174 | | - self.onElement.scrollWidth, |
175 | | - self.offElement.scrollWidth) + 12; |
176 | | - $scope.wrapperStyle.width = wrapperComputedWidth + 'px'; |
177 | | - } |
178 | | - |
179 | | - // Calculate the proper height |
180 | | - if (self.height) { |
181 | | - $scope.wrapperStyle.height = self.height; |
182 | | - } else { |
183 | | - var wrapperComputedHeight = Math.max( |
184 | | - self.onElement.offsetHeight, |
185 | | - self.offElement.offsetHeight); |
186 | | - var wrapperHeight = self.wrapperElement.offsetHeight; |
187 | | - |
188 | | - if (wrapperHeight < wrapperComputedHeight && |
189 | | - self.size !== 'btn-xs' && self.size !== 'btn-sm') { |
190 | | - $scope.wrapperStyle.height = wrapperComputedHeight + 'px'; |
191 | | - } else { |
192 | | - $scope.wrapperStyle.height = wrapperHeight + 'px'; |
193 | | - } |
194 | | - } |
195 | | - |
196 | | - // Build arrays that will be passed to widget's ng-class. |
197 | | - $scope.onComputedClass = [self.onClass , self.size, 'toggle-on']; |
198 | | - $scope.offComputedClass = [self.offClass, self.size, 'toggle-off']; |
199 | | - $scope.handleComputedClass = [self.size , 'toggle-handle']; |
200 | 224 | }; |
201 | 225 |
|
202 | 226 | this.toggle = function () { |
|
229 | 253 | } |
230 | 254 | }); |
231 | 255 | }); |
| 256 | + |
232 | 257 | }]) |
233 | 258 |
|
234 | 259 | .directive('toggle', function () { |
|
237 | 262 | template: '<div ng-cloak class="toggle btn off" ng-style="wrapperStyle"' + |
238 | 263 | 'ng-click="onSwitch($event)">' + |
239 | 264 | '<div class="toggle-group">' + |
240 | | - '<label class="btn" ng-class="onComputedClass"></label>' + |
241 | | - '<label class="btn active" ng-class="offComputedClass"></label>' + |
242 | | - '<span class="btn btn-default" ng-class="handleComputedClass"></span>' + |
| 265 | + '<label class="btn toggle-on-pad"></label>' + |
| 266 | + '<label class="btn toggle-off-pad active"></label>' + |
| 267 | + '<span class="btn btn-default toggle-handle"></span>' + |
243 | 268 | '</div>' + |
244 | 269 | '</div>', |
245 | 270 | scope: { |
|
0 commit comments