Skip to content

Instantly share code, notes, and snippets.

@almozavr
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almozavr/8b21a3ac5adca43742e4 to your computer and use it in GitHub Desktop.
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
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