<commit_before>class App.OnlineNotificationWidget extends App.Controller
  elements:
    '.js-toggleNavigation': 'toggle'

  constructor: ->
    super

    @bind 'OnlineNotification::changed', =>
      @delay(
        => @fetch()
        1200
        'online-notification-changed'
      )

    # rebuild widget on auth
    @bind 'auth', (user) =>
      if !user
        @el.find('.js-counter').text('')
      else
        if !@access()
          @el.find('.js-counter').text('')
          return
        @createContainer()

    if @access()
      @createContainer()
      @subscribeId = App.OnlineNotification.subscribe( @updateContent )

  release: =>
    @removeContainer()
    App.OnlineNotification.unsubscribe( @subscribeId )

  access: ->
    return false if !@Session.get()
    return true if @isRole('Agent')
    return true if @isRole('Admin')
    return false

  counterUpdate: (count) =>
    if !count
      @el.find('.js-counter').text('')
      return
    
    @el.find('.js-counter').text(count)

  markAllAsRead: =>
    @counterUpdate(0)
    @ajax(
      id:   'markAllAsRead'
      type: 'POST'
      url:  @apiPath + '/online_notifications/mark_all_as_read'
      data: JSON.stringify( '' )
      processData: true
    )

  removeClickCatcher: () =>
    return if !@clickCatcher
    @clickCatcher.remove()
    @clickCatcher = null

  onShow: =>
    @updateContent()

    # set height of notification popover
    notificationsContainer  = $('.js-notificationsContainer')
    heightApp               = $('#app').height()
    heightPopoverSpacer     = 22
    heightPopoverHeader     = notificationsContainer.find('.popover-notificationsHeader').outerHeight(true)
    heightPopoverContent    = notificationsContainer.find('.popover-content').prop('scrollHeight')
    heightPopoverContentNew = heightPopoverContent
    if (heightPopoverHeader + heightPopoverContent + heightPopoverSpacer) > heightApp
      heightPopoverContentNew = heightApp - heightPopoverHeader - heightPopoverSpacer
      notificationsContainer.addClass('is-overflowing')
    else
      notificationsContainer.removeClass('is-overflowing')
    
    notificationsContainer.find('.popover-content').css('height', "#{heightPopoverContentNew}px")

    # close notification list on click
    $('.js-notificationsContainer').on('click', (e) =>
      @hidePopover()
    )

    # mark all notifications as read
    $('.js-markAllAsRead').on('click', (e) =>
      e.preventDefault()
      @markAllAsRead()
    )

    # add clickCatcher
    @clickCatcher = new App.clickCatcher
      holder:      @el.offsetParent()
      callback:    @hidePopover
      zIndexScale: 4

  onHide: =>
    @removeClickCatcher()

  hidePopover: =>
    @toggle.popover('hide')

  fetch: =>
    load = (items) =>
      App.OnlineNotification.refresh( items, { clear: true } )
      @updateContent()
    App.OnlineNotification.fetchFull(load)

  updateContent: =>
    items = App.OnlineNotification.search( sortBy: 'created_at', order: 'DESC' )
    counter = 0
    for item in items
      if !item.seen
        counter = counter + 1
    @counterUpdate(counter)

    # update title
    $('.js-notificationsContainer .popover-title').html(
      App.i18n.translateInline( 'Notifications' ) + " <span class='popover-notificationsCounter'>#{counter}</span>"
    )

    # update content
    items = @prepareForObjectList(items)
    $('.js-notificationsContainer .popover-content').html(
      $( App.view('widget/online_notification_content')(items: items) )
    )
<commit_msg>Only show "mark all as read" if min. one notification is available.<commit_after>class App.OnlineNotificationWidget extends App.Controller
  elements:
    '.js-toggleNavigation': 'toggle'

  constructor: ->
    super

    @bind 'OnlineNotification::changed', =>
      @delay(
        => @fetch()
        1200
        'online-notification-changed'
      )

    # rebuild widget on auth
    @bind 'auth', (user) =>
      if !user
        @el.find('.js-counter').text('')
      else
        if !@access()
          @el.find('.js-counter').text('')
          return
        @createContainer()

    if @access()
      @createContainer()
      @subscribeId = App.OnlineNotification.subscribe( @updateContent )

  release: =>
    @removeContainer()
    App.OnlineNotification.unsubscribe( @subscribeId )

  access: ->
    return false if !@Session.get()
    return true if @isRole('Agent')
    return true if @isRole('Admin')
    return false

  counterUpdate: (count) =>
    if !count
      @el.find('.js-counter').text('')
      return

    @el.find('.js-counter').text(count)

  markAllAsRead: =>
    @counterUpdate(0)
    @ajax(
      id:   'markAllAsRead'
      type: 'POST'
      url:  @apiPath + '/online_notifications/mark_all_as_read'
      data: JSON.stringify( '' )
      processData: true
    )

  removeClickCatcher: () =>
    return if !@clickCatcher
    @clickCatcher.remove()
    @clickCatcher = null

  onShow: =>
    @updateContent()

    # set height of notification popover
    notificationsContainer  = $('.js-notificationsContainer')
    heightApp               = $('#app').height()
    heightPopoverSpacer     = 22
    heightPopoverHeader     = notificationsContainer.find('.popover-notificationsHeader').outerHeight(true)
    heightPopoverContent    = notificationsContainer.find('.popover-content').prop('scrollHeight')
    heightPopoverContentNew = heightPopoverContent
    if (heightPopoverHeader + heightPopoverContent + heightPopoverSpacer) > heightApp
      heightPopoverContentNew = heightApp - heightPopoverHeader - heightPopoverSpacer
      notificationsContainer.addClass('is-overflowing')
    else
      notificationsContainer.removeClass('is-overflowing')

    notificationsContainer.find('.popover-content').css('height', "#{heightPopoverContentNew}px")

    # close notification list on click
    $('.js-notificationsContainer').on('click', (e) =>
      @hidePopover()
    )

    # mark all notifications as read
    $('.js-markAllAsRead').on('click', (e) =>
      e.preventDefault()
      @markAllAsRead()
    )

    # add clickCatcher
    @clickCatcher = new App.clickCatcher
      holder:      @el.offsetParent()
      callback:    @hidePopover
      zIndexScale: 4

  onHide: =>
    @removeClickCatcher()

  hidePopover: =>
    @toggle.popover('hide')

  fetch: =>
    load = (items) =>
      App.OnlineNotification.refresh( items, { clear: true } )
      @updateContent()
    App.OnlineNotification.fetchFull(load)

  updateContent: =>
    items = App.OnlineNotification.search( sortBy: 'created_at', order: 'DESC' )
    counter = 0
    for item in items
      if !item.seen
        counter = counter + 1
    @counterUpdate(counter)

    # update title
    $('.js-notificationsContainer .popover-title').html(
      App.i18n.translateInline( 'Notifications' ) + " <span class='popover-notificationsCounter'>#{counter}</span>"
    )

    # show mark all as read if needed
    if counter is 0
      $('.js-notificationsContainer .js-markAllAsRead').addClass('hidden')
    else
      $('.js-notificationsContainer .js-markAllAsRead').removeClass('hidden')

    # update content
    items = @prepareForObjectList(items)
    $('.js-notificationsContainer .popover-content').html(
      $( App.view('widget/online_notification_content')(items: items) )
    )

{'fraction_non_alphanumeric': 0.1311722062645329, 'fraction_numerical': 0.003419504855696895, 'mean_word_length': 2.973913043478261, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3635752', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2108, 'n_words': 413}