Skip to content

Instantly share code, notes, and snippets.

@msbrime
Last active November 2, 2016 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msbrime/64cbe88f2229da811cc35c673deb313e to your computer and use it in GitHub Desktop.
Save msbrime/64cbe88f2229da811cc35c673deb313e to your computer and use it in GitHub Desktop.
A modal implementation with an adapter for Bootstrap
var Modal = (function () {
var adapter;
function init(modalAdapter) {
adapter = modalAdapter;
return {
show: show,
hide: hide,
beforeShow: beforeShow,
onShow: onShow,
beforeClose: beforeClose,
onClose: onClose
};
}
function show() {
adapter.show();
}
function hide() {
adapter.hide();
}
function beforeShow(fn) {
adapter.registerHandler('beforeShow', fn);
}
function onShow(fn) {
adapter.registerHandler('onShow', fn);
}
function beforeClose(fn) {
adapter.registerHandler('beforeClose', fn);
}
function onClose(fn) {
adapter.registerHandler('onClose', fn);
}
return {
init: init
};
})();
/**
* A modal adapter implementation for bootstrap modals
*/
var BootstrapModalAdapter = (function () {
var
selector,
modalInstance,
events = {
'onShow': 'shown.bs.modal',
'beforeShow': 'show.bs.modal',
'onClose': 'hidden.bs.modal',
'beforeClose': 'hide.bs.modal'
};
function init(elementSelector) {
selector = elementSelector;
modalInstance = $(elementSelector);
return {
show: show,
hide: hide,
registerHandler: registerHandler
};
}
function registerHandler(event, fn) {
modalInstance.on(events[event], function (e) {
fn(e, modalInstance);
});
}
function show() {
modalInstance.modal('show');
}
function hide() {
modalInstance.modal('hide');
}
return {
init: init
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment