Last active
August 29, 2015 14:02
-
-
Save charandas/ed6bf36215888c4a8cba to your computer and use it in GitHub Desktop.
Wrapper around angular-foundation $modal service
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
angular.module('services').factory('ModalService', | |
function ($modal, $timeout) { | |
/** | |
* Provides a thin wrapper around a $modalInstance to help | |
* navigate around double-dismiss cases. | |
* @param the $modalInstance to wrap | |
* @constructor | |
*/ | |
function Modal(instance) { | |
this.instance = instance; | |
} | |
/** | |
* Set the timeout handler that is | |
* registered to dismiss the modal. | |
*/ | |
Modal.prototype.setTimeout = function(timeout) { | |
this.timeout = timeout; | |
}; | |
/** | |
* Relay dismiss call to $modalInstance | |
* @param reason | |
*/ | |
Modal.prototype.dismiss = function(reason) { | |
if(this.timeout) { | |
$timeout.cancel(this.timeout); | |
this.timeout = undefined; | |
} | |
return this.instance.dismiss(reason); | |
}; | |
/** | |
* Relay close call to $modalInstance | |
* @param result | |
*/ | |
Modal.prototype.close = function(result) { | |
if(this.timeout) { | |
$timeout.cancel(this.timeout); | |
this.timeout = undefined; | |
} | |
return this.instance.close(result); | |
}; | |
var open = function(params) { | |
var modalInstance = $modal.open({ | |
templateUrl: params.templateUrl ? params.templateUrl : 'views/core/directives/foundation/modal-default.html', | |
controller: ModalInstanceCtrl, | |
backdrop: 'static', | |
windowClass: 'notification', | |
resolve: { | |
heading: function () { | |
return params.heading; | |
}, | |
messages: function () { | |
return params.messages; | |
}, | |
size: function () { | |
return params.size; | |
}, | |
extraParams: function () { | |
return params.extraParams; | |
} | |
} | |
}); | |
var newModal = new Modal(modalInstance); | |
if (params.timeout) { | |
newModal.setTimeout($timeout(function() { | |
newModal.dismiss('timeout'); | |
}, params.timeout)); | |
} | |
return newModal; | |
}; | |
return { | |
open: open | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment