Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit ace4ae1

Browse files
committed
chore(dropdown): unify code style
- Update code style to be more consistent Closes #4167
1 parent 1abfd05 commit ace4ae1

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

Diff for: src/dropdown/dropdown.js

+52-54
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
77
.service('dropdownService', ['$document', '$rootScope', function($document, $rootScope) {
88
var openScope = null;
99

10-
this.open = function( dropdownScope ) {
11-
if ( !openScope ) {
10+
this.open = function(dropdownScope) {
11+
if (!openScope) {
1212
$document.bind('click', closeDropdown);
1313
$document.bind('keydown', keybindFilter);
1414
}
1515

16-
if ( openScope && openScope !== dropdownScope ) {
16+
if (openScope && openScope !== dropdownScope) {
1717
openScope.isOpen = false;
1818
}
1919

2020
openScope = dropdownScope;
2121
};
2222

23-
this.close = function( dropdownScope ) {
24-
if ( openScope === dropdownScope ) {
23+
this.close = function(dropdownScope) {
24+
if (openScope === dropdownScope) {
2525
openScope = null;
2626
$document.unbind('click', closeDropdown);
2727
$document.unbind('keydown', keybindFilter);
2828
}
2929
};
3030

31-
var closeDropdown = function( evt ) {
31+
var closeDropdown = function(evt) {
3232
// This method may still be called during the same mouse event that
3333
// unbound this event handler. So check openScope before proceeding.
3434
if (!openScope) { return; }
3535

36-
if( evt && openScope.getAutoClose() === 'disabled' ) { return ; }
36+
if (evt && openScope.getAutoClose() === 'disabled') { return ; }
3737

3838
var toggleElement = openScope.getToggleElement();
39-
if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) {
39+
if (evt && toggleElement && toggleElement[0].contains(evt.target)) {
4040
return;
4141
}
4242

@@ -53,12 +53,12 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
5353
}
5454
};
5555

56-
var keybindFilter = function( evt ) {
57-
if ( evt.which === 27 ) {
56+
var keybindFilter = function(evt) {
57+
if (evt.which === 27) {
5858
openScope.focusToggleElement();
5959
closeDropdown();
6060
}
61-
else if ( openScope.isKeynavEnabled() && /(38|40)/.test(evt.which) && openScope.isOpen ) {
61+
else if (openScope.isKeynavEnabled() && /(38|40)/.test(evt.which) && openScope.isOpen) {
6262
evt.preventDefault();
6363
evt.stopPropagation();
6464
openScope.focusDropdownEntry(evt.which);
@@ -69,7 +69,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
6969
.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', '$position', '$document', '$compile', '$templateRequest', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate, $position, $document, $compile, $templateRequest) {
7070
var self = this,
7171
scope = $scope.$new(), // create a child scope so we are not polluting original one
72-
templateScope,
72+
templateScope,
7373
openClass = dropdownConfig.openClass,
7474
getIsOpen,
7575
setIsOpen = angular.noop,
@@ -78,10 +78,10 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
7878
keynavEnabled =false,
7979
selectedOption = null;
8080

81-
this.init = function( element ) {
81+
this.init = function(element) {
8282
self.$element = element;
8383

84-
if ( $attrs.isOpen ) {
84+
if ($attrs.isOpen) {
8585
getIsOpen = $parse($attrs.isOpen);
8686
setIsOpen = getIsOpen.assign;
8787

@@ -93,15 +93,15 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
9393
appendToBody = angular.isDefined($attrs.dropdownAppendToBody);
9494
keynavEnabled = angular.isDefined($attrs.keyboardNav);
9595

96-
if ( appendToBody && self.dropdownMenu ) {
96+
if (appendToBody && self.dropdownMenu) {
9797
$document.find('body').append( self.dropdownMenu );
9898
element.on('$destroy', function handleDestroyEvent() {
9999
self.dropdownMenu.remove();
100100
});
101101
}
102102
};
103103

104-
this.toggle = function( open ) {
104+
this.toggle = function(open) {
105105
return scope.isOpen = arguments.length ? !!open : !scope.isOpen;
106106
};
107107

@@ -133,7 +133,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
133133

134134
switch (keyCode) {
135135
case (40): {
136-
if ( !angular.isNumber(self.selectedOption)) {
136+
if (!angular.isNumber(self.selectedOption)) {
137137
self.selectedOption = 0;
138138
} else {
139139
self.selectedOption = (self.selectedOption === elems.length -1 ?
@@ -143,12 +143,11 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
143143
break;
144144
}
145145
case (38): {
146-
if ( !angular.isNumber(self.selectedOption)) {
146+
if (!angular.isNumber(self.selectedOption)) {
147147
return;
148148
} else {
149-
self.selectedOption = (self.selectedOption === 0 ?
150-
0 :
151-
self.selectedOption - 1);
149+
self.selectedOption = self.selectedOption === 0 ?
150+
0 : self.selectedOption - 1;
152151
}
153152
break;
154153
}
@@ -161,38 +160,38 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
161160
};
162161

163162
scope.focusToggleElement = function() {
164-
if ( self.toggleElement ) {
163+
if (self.toggleElement) {
165164
self.toggleElement[0].focus();
166165
}
167166
};
168167

169-
scope.$watch('isOpen', function( isOpen, wasOpen ) {
168+
scope.$watch('isOpen', function(isOpen, wasOpen) {
170169
if (appendToBody && self.dropdownMenu) {
171-
var pos = $position.positionElements(self.$element, self.dropdownMenu, 'bottom-left', true);
172-
var css = {
173-
top: pos.top + 'px',
174-
display: isOpen ? 'block' : 'none'
175-
};
176-
177-
var rightalign = self.dropdownMenu.hasClass('dropdown-menu-right');
178-
if (!rightalign) {
179-
css.left = pos.left + 'px';
180-
css.right = 'auto';
181-
} else {
182-
css.left = 'auto';
183-
css.right = (window.innerWidth - (pos.left + self.$element.prop('offsetWidth'))) + 'px';
184-
}
170+
var pos = $position.positionElements(self.$element, self.dropdownMenu, 'bottom-left', true);
171+
var css = {
172+
top: pos.top + 'px',
173+
display: isOpen ? 'block' : 'none'
174+
};
175+
176+
var rightalign = self.dropdownMenu.hasClass('dropdown-menu-right');
177+
if (!rightalign) {
178+
css.left = pos.left + 'px';
179+
css.right = 'auto';
180+
} else {
181+
css.left = 'auto';
182+
css.right = (window.innerWidth - (pos.left + self.$element.prop('offsetWidth'))) + 'px';
183+
}
185184

186-
self.dropdownMenu.css(css);
185+
self.dropdownMenu.css(css);
187186
}
188187

189188
$animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass).then(function() {
190-
if (angular.isDefined(isOpen) && isOpen !== wasOpen) {
191-
toggleInvoker($scope, { open: !!isOpen });
192-
}
189+
if (angular.isDefined(isOpen) && isOpen !== wasOpen) {
190+
toggleInvoker($scope, { open: !!isOpen });
191+
}
193192
});
194193

195-
if ( isOpen ) {
194+
if (isOpen) {
196195
if (self.dropdownMenuTemplateUrl) {
197196
$templateRequest(self.dropdownMenuTemplateUrl).then(function(tplContent) {
198197
templateScope = scope.$new();
@@ -205,7 +204,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
205204
}
206205

207206
scope.focusToggleElement();
208-
dropdownService.open( scope );
207+
dropdownService.open(scope);
209208
} else {
210209
if (self.dropdownMenuTemplateUrl) {
211210
if (templateScope) {
@@ -216,7 +215,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
216215
self.dropdownMenu = newEl;
217216
}
218217

219-
dropdownService.close( scope );
218+
dropdownService.close(scope);
220219
self.selectedOption = null;
221220
}
222221

@@ -272,28 +271,27 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
272271
link: function (scope, element, attrs, dropdownCtrl) {
273272

274273
element.bind('keydown', function(e) {
275-
276274
if ([38, 40].indexOf(e.which) !== -1) {
277-
278275
e.preventDefault();
279276
e.stopPropagation();
280277

281278
var elems = dropdownCtrl.dropdownMenu.find('a');
282279

283280
switch (e.which) {
284281
case (40): { // Down
285-
if ( !angular.isNumber(dropdownCtrl.selectedOption)) {
282+
if (!angular.isNumber(dropdownCtrl.selectedOption)) {
286283
dropdownCtrl.selectedOption = 0;
287284
} else {
288-
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === elems.length -1 ? dropdownCtrl.selectedOption : dropdownCtrl.selectedOption+1);
285+
dropdownCtrl.selectedOption = dropdownCtrl.selectedOption === elems.length -1 ?
286+
dropdownCtrl.selectedOption : dropdownCtrl.selectedOption + 1;
289287
}
290-
288+
break;
291289
}
292-
break;
293290
case (38): { // Up
294-
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === 0 ? 0 : dropdownCtrl.selectedOption-1);
291+
dropdownCtrl.selectedOption = dropdownCtrl.selectedOption === 0 ?
292+
0 : dropdownCtrl.selectedOption - 1;
293+
break;
295294
}
296-
break;
297295
}
298296
elems[dropdownCtrl.selectedOption].focus();
299297
}
@@ -307,7 +305,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
307305
return {
308306
require: '?^dropdown',
309307
link: function(scope, element, attrs, dropdownCtrl) {
310-
if ( !dropdownCtrl ) {
308+
if (!dropdownCtrl) {
311309
return;
312310
}
313311

@@ -318,7 +316,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
318316
var toggleDropdown = function(event) {
319317
event.preventDefault();
320318

321-
if ( !element.hasClass('disabled') && !attrs.disabled ) {
319+
if (!element.hasClass('disabled') && !attrs.disabled) {
322320
scope.$apply(function() {
323321
dropdownCtrl.toggle();
324322
});

Diff for: src/dropdown/test/dropdown.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ describe('dropdownToggle', function() {
175175
});
176176

177177
// pr/issue 3274
178-
it('should not raise $digest:inprog if dismissed during a digest cycle', function () {
178+
it('should not raise $digest:inprog if dismissed during a digest cycle', function() {
179179
clickDropdownToggle();
180180
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
181181

182-
$rootScope.$apply(function () {
182+
$rootScope.$apply(function() {
183183
$document.click();
184184
});
185185

@@ -235,7 +235,7 @@ describe('dropdownToggle', function() {
235235
function dropdown() {
236236

237237
// Simulate URL rewriting behavior
238-
$document.on('click', 'a[href="#something"]', function () {
238+
$document.on('click', 'a[href="#something"]', function() {
239239
$rootScope.$broadcast('$locationChangeSuccess');
240240
$rootScope.$apply();
241241
});
@@ -481,7 +481,7 @@ describe('dropdownToggle', function() {
481481
expect(elm2.hasClass(dropdownConfig.openClass)).toBe(true);
482482
});
483483

484-
it('should not close on $locationChangeSuccess if auto-close="disabled"', function () {
484+
it('should not close on $locationChangeSuccess if auto-close="disabled"', function() {
485485
var elm1 = dropdown('disabled');
486486
expect(elm1.hasClass(dropdownConfig.openClass)).toBe(false);
487487
clickDropdownToggle(elm1);

0 commit comments

Comments
 (0)