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

Commit

Permalink
feat(modal): add modal-open class to body on modal open
Browse files Browse the repository at this point in the history
Closes #1254
  • Loading branch information
pkozlowski-opensource committed Dec 21, 2013
1 parent 709e679 commit e76512f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion misc/test-lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ beforeEach(function() {
this.addMatchers({
toHaveClass: function(cls) {
this.message = function() {
return "Expected '" + angular.mock.dump(this.actual) + "' to have class '" + cls + "'.";
return "Expected '" + this.actual + "'" + (this.isNot ? ' not ' : ' ') + "to have class '" + cls + "'.";
};

return this.actual.hasClass(cls);
Expand Down
6 changes: 5 additions & 1 deletion src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ angular.module('ui.bootstrap.modal', [])
.factory('$modalStack', ['$document', '$compile', '$rootScope', '$$stackedMap',
function ($document, $compile, $rootScope, $$stackedMap) {

var OPENED_MODAL_CLASS = 'modal-open';

var backdropjqLiteEl, backdropDomEl;
var backdropScope = $rootScope.$new(true);
var openedWindows = $$stackedMap.createNew();
Expand All @@ -129,13 +131,15 @@ angular.module('ui.bootstrap.modal', [])

function removeModalWindow(modalInstance) {

var body = $document.find('body').eq(0);
var modalWindow = openedWindows.get(modalInstance).value;

//clean up the stack
openedWindows.remove(modalInstance);

//remove window DOM element
modalWindow.modalDomEl.remove();
body.toggleClass(OPENED_MODAL_CLASS, openedWindows.length() > 0);

//remove backdrop if no longer needed
if (backdropDomEl && backdropIndex() == -1) {
Expand Down Expand Up @@ -185,7 +189,7 @@ angular.module('ui.bootstrap.modal', [])
var modalDomEl = $compile(angularDomEl)(modal.scope);
openedWindows.top().value.modalDomEl = modalDomEl;
body.append(modalDomEl);

body.addClass(OPENED_MODAL_CLASS);
};

$modalStack.close = function (modalInstance, result) {
Expand Down
19 changes: 19 additions & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('$modal', function () {
var body = $document.find('body');
body.find('div.modal').remove();
body.find('div.modal-backdrop').remove();
body.removeClass('modal-open');
});

function open(modalOptions) {
Expand Down Expand Up @@ -435,5 +436,23 @@ describe('$modal', function () {

expect($document).toHaveBackdrop();
});

it('should add "modal-open" class when a modal gets opened', function () {

var body = $document.find('body');
expect(body).not.toHaveClass('modal-open');

var modal1 = open({template: '<div>Content1</div>'});
expect(body).toHaveClass('modal-open');

var modal2 = open({template: '<div>Content1</div>'});
expect(body).toHaveClass('modal-open');

dismiss(modal1);
expect(body).toHaveClass('modal-open');

dismiss(modal2);
expect(body).not.toHaveClass('modal-open');
});
});
});

0 comments on commit e76512f

Please sign in to comment.