Skip to content

Instantly share code, notes, and snippets.

@lbenie
Last active February 3, 2016 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lbenie/0bb722802ed7eb6b28f2 to your computer and use it in GitHub Desktop.
Save lbenie/0bb722802ed7eb6b28f2 to your computer and use it in GitHub Desktop.
/**
* Opens a modal
* @param {Object} scope - an object to be merged with modal's scope
* @param {String} modalClass - (optional) class(es) to be applied to the modal
* @param {String} size - (optional) size to be applied to the modal
* @param {String} templateUrl - (optional) template to be applied to the modal
* @param {String} controller - (optional) controller to be applied to the modal
* @return {Object} - the instance $uibModal.open() returns
*/
function openModal(scope = {}, modalClass = 'modal-default', size = 'sm', templateUrl = 'components/modal/modal.html', controller = '') {
var modalScope = $rootScope.$new();
angular.extend(modalScope, scope);
return $uibModal.open({
templateUrl: templateUrl,
windowClass: modalClass,
scope: modalScope,
size: size,
controller: controller
});
}
return {
/* Confirmation modals */
confirm: {
/**
* Create a function to open a delete confirmation modal (ex. ng-click='myModalFn(name, arg1, arg2...)')
* @param {Function} del - callback, ran when delete is confirmed
* @return {Function} - the function to open the modal (ex. myModalFn)
*/
delete(del = angular.noop) {
/**
* Open a delete confirmation modal
* @param {String} name - name or info to show on modal
* @param {All} - any additional args are passed straight to del callback
*/
return function() {
var args = Array.prototype.slice.call(arguments),
name = args.shift(),
deleteModal;
deleteModal = openModal({
modal: {
dismissable: true,
title: 'Confirm Delete',
html: '<p>Are you sure you want to delete <strong>' + name + '</strong> ?</p>',
buttons: [{
classes: 'btn-danger',
text: 'Delete',
click: function(e) {
deleteModal.close(e);
}
}, {
classes: 'btn-default',
text: 'Cancel',
click: function(e) {
deleteModal.dismiss(e);
}
}]
}
}, 'myModalClass', 'mySize', 'myTemplate.html', 'myController.js');
deleteModal.result.then(function(event) {
del.apply(event, args);
});
};
}
}
}
@Luidog
Copy link

Luidog commented Nov 24, 2015

Thank you very much. This makes so much sense now that I see it.

@lbenie
Copy link
Author

lbenie commented Nov 25, 2015

@Luidog

it's my pleasure

@haverchuck
Copy link

THANK YOU!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment