DrMartiner
11/12/2013 - 5:15 AM

Directive, who set\unset toggle class at the element, when the function-condition changed

Directive, who set\unset toggle class at the element, when the function-condition changed

<!-- ... -->
<button ng-toggle-class="animate" ng-toggle-condition="isBtnShow()" ng-toggle-delay="1000">Attention!</button>
<!-- ... -->
toggleClass = angular.module('jobMapApp')
    .directive 'ngToggleClass', () ->
        return {
            restrict: 'A'
            scope:
                condition: '&ngToggleCondition'
            link: (scope, element, attrs) ->
                scope.timerId = null
                scope.$watch () ->
                    return scope.condition()
                , (newValue) ->
                    if newValue
                        scope.timerId = setInterval () ->
                            element.toggleClass attrs.ngToggleClass
                        , attrs.ngToggleDelay
                    else
                        if scope.timerId
                            clearInterval scope.timerId
        }