Last active
November 2, 2016 09:03
-
-
Save msbrime/64cbe88f2229da811cc35c673deb313e to your computer and use it in GitHub Desktop.
A modal implementation with an adapter for Bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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