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

Commit

Permalink
Added z-index setting on modal and backdrops
Browse files Browse the repository at this point in the history
To support multiple modals with different sizes, added code in
directives to set the z-index of each div based on the modal stack
depth. Starts initial backdrop at 1040 (bootstrap's 'modal' class value)
and the modal itself at 1050. Subsequent modals are offset by 20 with
each additional modal.
  • Loading branch information
jmwolfe committed Sep 2, 2013
1 parent c0215c8 commit 96fb17c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module('ui.bootstrap.modal', [])

return {
add: function (key, value) {
value.stackIndex = stack.length;
stack.push({
key: key,
value: value
Expand Down Expand Up @@ -61,6 +62,9 @@ angular.module('ui.bootstrap.modal', [])
//trigger CSS transitions
$timeout(function () {
scope.animate = true;
var modal = $modalStack.getTop();
var styleData = 'z-index: ' + (1040 + 20*modal.window.stackIndex) + ';';
element[0].setAttribute('style', styleData);
});

scope.close = function (evt) {
Expand All @@ -76,7 +80,7 @@ angular.module('ui.bootstrap.modal', [])
};
}])

.directive('modalWindow', ['$timeout', function ($timeout) {
.directive('modalWindow', ['$timeout', '$modalStack',function ($timeout,$modalStack) {
return {
restrict: 'EA',
scope: {},
Expand All @@ -86,6 +90,9 @@ angular.module('ui.bootstrap.modal', [])
link: function (scope, element, attrs) {
//trigger CSS transitions
$timeout(function () {
var modal = $modalStack.getTop();
var styleData = 'z-index: ' + (1050 + 20 * modal.window.stackIndex) + ';';
element[0].setAttribute('style', styleData);
scope.animate = true;
});
}
Expand Down
36 changes: 34 additions & 2 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,27 @@ describe('$modal', function () {
};

return backdropDomEls.length === 1;
},

toHaveOneDivWithCertainClassAndZindexInStyleAttr: function(divclass, expectedZindex) {

var domEls = this.actual.find('body > div.'+divclass);
this.message = function() {
return "Expected '" + angular.mock.dump(domEls) + "' to be a div element with class '"+divclass+"' and style containing z-index: "+expectedZindex+".";
};
var divsAtLevel = 0;
for(var i=0; i < domEls.length; i++)
{
var domEl = domEls[i];
var style = domEl.getAttribute('style');
if (style.indexOf('z-index: ' + expectedZindex) != -1) {
divsAtLevel++;
}
}

return divsAtLevel === 1;
}

});
}));

Expand Down Expand Up @@ -364,7 +384,7 @@ describe('$modal', function () {

describe('multiple modals', function () {

it('it should allow opening of multiple modals', function () {
it('should allow opening of multiple modals', function () {

var modal1 = open({template: '<div>Modal1</div>'});
var modal2 = open({template: '<div>Modal2</div>'});
Expand All @@ -377,7 +397,19 @@ describe('$modal', function () {
dismiss(modal1);
expect($document).toHaveModalsOpen(0);
});


it('should occlude any open child modals', function() {
var modal1 = open({template: '<div>Modal1</div>'});
$timeout.flush(); // to call the directives in their natural order
var modal2 = open({template: '<div>Modal2</div>'});
$timeout.flush();

expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal-backdrop','1040');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal','1050');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal-backdrop','1060');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal','1070');
});

it('should not close any modals on ESC if the topmost one does not allow it', function () {

var modal1 = open({template: '<div>Modal1</div>'});
Expand Down
7 changes: 7 additions & 0 deletions src/modal/test/stackedMap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ describe('stacked map', function () {
expect(stackedMap.get('foo')).toBeUndefined();
});

it('should add the stackIndex to the value object', function() {
stackedMap.add('foo', { name: 'foo_value'});
stackedMap.add('bar', { name: 'bar_value'});
expect(stackedMap.get('foo').value.stackIndex).toEqual(0);
expect(stackedMap.get('bar').value.stackIndex).toEqual(1);
});

it('should get topmost element', function () {

stackedMap.add('foo', 'foo_value');
Expand Down

0 comments on commit 96fb17c

Please sign in to comment.