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

feat(dropdown): add alignment option #1924

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('ui.bootstrap.dropdown', [])
angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])

.constant('dropdownConfig', {
openClass: 'open'
Expand Down Expand Up @@ -42,7 +42,7 @@ angular.module('ui.bootstrap.dropdown', [])
};
}])

.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate) {
.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', '$position', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate, $position) {
var self = this,
scope = $scope.$new(), // create a child scope so we are not polluting original one
openClass = dropdownConfig.openClass,
Expand Down Expand Up @@ -78,11 +78,21 @@ angular.module('ui.bootstrap.dropdown', [])
}
};

scope.positionMenuElement = function() {
if ( self.menuElement && self.menuAlign ) {
var pos = $position.alignElements(self.toggleElement, self.menuElement, self.menuAlign);
if (angular.isDefined( pos.left )) {
self.menuElement.css('left', pos.left + 'px');
}
}
};

scope.$watch('isOpen', function( isOpen ) {
$animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass);

if ( isOpen ) {
scope.focusToggleElement();
scope.positionMenuElement();
dropdownService.open( scope );
} else {
dropdownService.close( scope );
Expand All @@ -106,11 +116,27 @@ angular.module('ui.bootstrap.dropdown', [])
restrict: 'CA',
controller: 'DropdownController',
link: function(scope, element, attrs, dropdownCtrl) {

dropdownCtrl.init( element );
}
};
})

.directive('dropdownMenu', function() {
return {
restrict: 'CA',
require: '?^dropdown',
link: function(scope, element, attrs, dropdownCtrl) {
if ( !dropdownCtrl ) {
return;
}

dropdownCtrl.menuElement = element;
dropdownCtrl.menuAlign = attrs.dropdownAlign;
}
};
})

.directive('dropdownToggle', function() {
return {
restrict: 'CA',
Expand Down
48 changes: 48 additions & 0 deletions src/position/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,54 @@ angular.module('ui.bootstrap.position', [])
break;
}

return targetElPos;
},

/**
* Provides coordinates for aligning horizontally the targetEl
* to the hostEl. Allowed values for positionStr are "left", "right",
* "left-auto", "right-auto" and "center"
*/
alignElements: function (hostEl, targetEl, positionStr, appendToBody) {

positionStr = positionStr || 'left';

var hostElPos,
hostElOffset,
targetElWidth,
targetElPos;

hostElPos = appendToBody ? this.offset(hostEl) : this.position(hostEl);
hostElOffset = this.offset(hostEl);

targetElWidth = targetEl.prop('offsetWidth');

var shiftHorizontally = {
left: function () {
return hostElPos.left;
},
// Like left but prevents target element to overflow on the right
'left-auto': function () {
var clientWidth = $document[0].documentElement.clientWidth;
return Math.min(hostElPos.left, clientWidth - targetElWidth);
},
right: function () {
return hostElPos.left + hostElPos.width - targetElWidth;
},
// Like left but prevents target element to overflow on the left
'right-auto': function () {
return Math.max(-hostElOffset.left, hostElPos.left + hostElPos.width - targetElWidth);
},
center: function () {
return hostElPos.left + hostElPos.width / 2 - targetElWidth / 2;
}
};

targetElPos = {
top: hostElPos.top + hostElPos.height,
left: shiftHorizontally[positionStr]()
};

return targetElPos;
}
};
Expand Down