Last active
August 29, 2015 14:19
-
-
Save almozavr/8b21a3ac5adca43742e4 to your computer and use it in GitHub Desktop.
Modal which preserves unique url via query, best with with routeProvider's reloadOnSearch: false
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('ModalUrl', [ | |
'$rootScope', '$modal', '$location', '$routeParams', '$window', '$timeout', | |
($rootScope, $modal, $location, $routeParams, $window, $timeout) -> | |
modalInstance = null | |
checkModal = () -> | |
modalParam = decodeURIComponent($routeParams.modal) | |
if (modalParam != 'undefined') | |
optionsParam = decodeURIComponent($routeParams.options) | |
if (optionsParam != 'undefined') | |
options = JSON.parse(optionsParam) | |
else | |
options = {} | |
modalInstance = $modal.open( | |
templateUrl: "modal/#{modalParam}" | |
controller: 'ModalUrlCtrl' | |
windowClass: options.windowClass | |
resolve: | |
options: () -> options | |
) | |
modalInstance.result.then(null, ()-> | |
for key in ['modal', 'options'] | |
delete $location.$$search[key] | |
$location.$$compose(); | |
$modalInstance = null | |
) | |
else | |
modalInstance.dismiss('finished') if modalInstance? | |
return | |
checkModal() | |
$rootScope.$on('$routeUpdate', (event) -> checkModal()) | |
showModal = (modalToShow, options) -> | |
explicitlyOpened = true | |
params = {modal: modalToShow, options: JSON.stringify(options)} | |
$timeout () -> $location.search(params) | |
{show: showModal} | |
]) | |
angular.module("controllers").controller('ModalUrlCtrl', [ | |
'$scope', '$modalInstance', 'options', ($scope, $modalInstance, options) -> | |
$scope.data = options.data | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment