Skip to content

Commit

Permalink
Add modal directive (no tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Oct 7, 2012
1 parent 0848182 commit 3523360
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions directives/modal/modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
angular.module('ui.bootstrap').directive('bsModal', ['$parse',function($parse) {
var backdropEl;
var defaultOpts = {
backdrop: true,
escape: true
};
return {
restrict: 'ECA',
link: function(scope, elm, attrs) {
var opts = angular.extend(defaultOpts, scope.$eval(attrs.uiOptions || attrs.bsOptions || attrs.options));
var shownAttr = attrs.bsModal || attrs.ngModel || attrs.ngShow || attrs.uiShow;
var model = $parse(shownAttr);
elm.addClass('modal');

if (opts.backdrop && !backdropEl) {
backdropEl = angular.element('<div class="modal-backdrop"></div>');
backdropEl.css('display','none');
angular.element(document.getElementsByTagName('body')[0]).append(backdropEl);
}

function setShown(shown) {
scope.$apply(function() {
model.assign(scope, shown);
});
}

function escapeClose(evt) {
if (evt.which === 27) { setShown(false); }
}
function clickClose() {
setShown(false);
}

function close() {
if (opts.escape) { body.unbind('keyup', escapeClose); console.log('escp'); }
if (opts.backdrop) {
backdropEl.css('display', 'none');
backdropEl.unbind('click', clickClose);
}
elm.css('display', 'none');
}
function open() {
if (opts.escape) { body.bind('keyup', escapeClose); }
if (opts.backdrop) {
backdropEl.css('display', 'block');
backdropEl.bind('click', clickClose);
}
elm.css('display', 'block');
}

scope.$watch(shownAttr, function(isShown, oldShown) {
if (isShown !== oldShown) {
if (isShown) {
open();
} else {
close();
}
}
});
}
};
}]);

0 comments on commit 3523360

Please sign in to comment.