Skip to content

Instantly share code, notes, and snippets.

@IlanFrumer
Created February 5, 2014 13:24
Show Gist options
  • Save IlanFrumer/8823507 to your computer and use it in GitHub Desktop.
Save IlanFrumer/8823507 to your computer and use it in GitHub Desktop.
app.directive 'confirmBox', ($templateCache, $compile, $animate, $timeout)->
controller: ($element, $scope, $attrs)->
contents = $element.contents()
dialog = null
action = null
timeout = null
open: (options)->
action = options.action
html = $templateCache.get(options.template).trim()
dialog = angular.element(html)
$animate.addClass(contents,'ng-hide')
$animate.enter(dialog,$element)
$compile(dialog)($scope)
if options.timeout
timeout = $timeout ()=>
@close(false)
, options.timeout, false
return
close: (confirmed)->
$timeout.cancel(timeout)
$animate.leave(dialog)
$animate.removeClass(contents,'ng-hide')
if(confirmed)
$scope.$apply ()->
action()
dialog = null
action = null
return
app.directive 'confirmOpen', ()->
require: '^confirmBox'
scope:
confirmOpen : "&"
link: (scope,elm,attrs,ctrl)->
options = {
action: scope.confirmOpen
template: attrs.template
timeout: attrs.timeout
}
elm.bind 'click', ()->
ctrl.open(options)
app.directive 'confirmOk', ()->
require: '^confirmBox'
link: (scope,elm,attrs,ctrl)->
elm.bind 'click', ()-> ctrl.close(true)
app.directive 'confirmCancel', ()->
require: '^confirmBox'
link: (scope,elm,attrs,ctrl)->
elm.bind 'click', ()-> ctrl.close(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment