Skip to content

Instantly share code, notes, and snippets.

@almozavr
Last active Aug 29, 2015
Embed
What would you like to do?
Modal which preserves unique url via query, best with with routeProvider's reloadOnSearch: false
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