 <% } %>
 """)
 
-AssignedToLightboxDirective = (lightboxService, lightboxListNavigationService) ->
+AssignedToLightboxDirective = (lightboxService, lightboxKeyboardNavigationService) ->
     link = ($scope, $el, $attrs) ->
         selectedUser = null
         selectedItem = null

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# File: modules/lightboxes.coffee
###

module = angular.module("taigaCommon")

bindOnce = @.taiga.bindOnce

class LightboxService extends taiga.Service
    open: (lightbox) ->
        lightbox.css('display', 'flex')

        setTimeout ( ->
            lightbox.addClass('open')
        ), 70

        $(document)
            .on 'keydown.lightbox', (e) =>
                code = if e.keyCode then e.keyCode else e.which

                if code == 27
                    @close(lightbox)

    close: (lightbox) ->
        $(document).off('.lightbox')
        lightbox
            .one "transitionend", () ->
                lightbox.css('display', 'none')
            .removeClass('open')

module.service("lightboxService", LightboxService)


LightboxDirective = (lightboxService) ->
    link = ($scope, $el, $attrs) ->
        $el.on "click", ".close", (event) ->
            event.preventDefault()

            lightboxService.close($el)

    return {
        restrict: "C",
        link: link
    }

module.directive("lightbox", ["lightboxService", LightboxDirective])

class LightboxListNavigationService
    stop: () ->
        $(document).off "keydown.list-navigation"

    init: ($el) ->
        $(document).on "keydown.list-navigation", (e) =>
            code = if e.keyCode then e.keyCode else e.which

            if code == 40 || code == 38 || code == 13
                e.preventDefault()

                active = $el.find('.active')

                if code == 13
                    active.trigger('click')

                if code == 40
                    if active.length
                        next = active.next('.watcher-single')

                        if next.length
                            active.removeClass('active')
                            next.addClass('active')
                    else
                        $el.find('.watcher-single:first').addClass('active')

                if code == 38
                    if active.length
                        prev = active.prev('.watcher-single')

                        if prev.length
                            active.removeClass('active')
                            prev.addClass('active')
                    else
                        $el.find('.watcher-single:last').addClass('active')

module.service("lightboxListNavigationService", LightboxListNavigationService)


#############################################################################
## Block Lightbox Directive
#############################################################################

BlockLightboxDirective = (lightboxService) ->
    link = ($scope, $el, $attrs, $model) ->
        title = $attrs.title
        $el.find("h2.title").text(title)
        $scope.$on "block", ->
            lightboxService.open($el)



        $el.on "click", ".button-green", (event) ->
            event.preventDefault()

            $scope.$apply ->
                $model.$modelValue.is_blocked = true
