<commit_before>_ = require 'underscore'
Backbone = require 'backbone'
SearchBarView = require '../../search_bar/client/view.coffee'
AuthModalView = require '../../auth_modal/view.coffee'
SettingsView = require '../../settings/client/view.coffee'
AuthRouter = require './auth_router.coffee'
mediator = require '../../../lib/mediator.coffee'
Notifications = require "../../../collections/notifications.coffee"
SmallFeedView = require '../../feed/client/small_feed_view.coffee'
NewChannelView = require '../../new_channel/client/new_channel_view.coffee'
sd = require('sharify').data

template = -> require('./templates/index.jade') arguments...

module.exports = class HeaderView extends Backbone.View

  events:
    'tap #layout-header__search__input'   : 'setActive'
    'blur #layout-header__search__input'  : 'unsetActive'
    'tap .header--icon'                   : 'setActive'
    'tap .btn-login'                      : 'login'
    'tap .btn-signup'                     : 'signup'
    'tap .dropdown--menu__trigger'        : 'toggleDropdown'
    'tap .dropdown__link--settings'       : 'openSettings'

  initialize: (options) ->
    mediator.on 'open:auth', @openAuth, @
    mediator.on 'body:click', @closeDropdown, @
    mediator.on 'search:loaded', @closeDropdown, @
    mediator.on 'notifications:synced', @maybeSetNotifications, @
    mediator.on 'notifications:cleared', @unsetNotifications, @
    mediator.on 'new:channel', @openChannelToggle, @
    mediator.on 'current_user:refreshed', @render, @
    mediator.shared.state.on 'change', @toggle, @

    @postRender()

  toggle: ->
    if mediator.shared.state.get('isDraggingBlocks')
      @$el.hide()
    else
      @$el.show()

  setActive: (e)->
    unless $('body').hasClass('is-mobile')
      @$el.addClass 'is-active'
      @$('#layout-header__search__input').focus()

  unsetActive: (e)->
    @$el.removeClass 'is-active'

  clearNotifications: (e)->
    @notifications.markRead()
    @$('.user-avatar').removeClass 'has-notifications'

  maybeSetNotifications: ->
    if (count = @notifications.getNumberUnread()) > 0
      @$('.user-avatar, .dropdown__link--notifications').addClass 'has-notifications'
      @$('.notifications--count').text count

  unsetNotifications: ->
    @$('.user-avatar, .dropdown__link--notifications').removeClass 'has-notifications'
    @$('.notifications--count').text ""

  openChannelToggle: ->
    @$('.new-channel-dropdown').addClass 'dropdown--is_active'

  toggleDropdown: (e)->
    $el = $(e.currentTarget).parent()
    if !@$('.dropdown--is_active').is($el)
      @$('.dropdown--is_active').removeClass 'dropdown--is_active'

    ac = $el.toggleClass('dropdown--is_active')

    if $el.hasClass 'dropdown--is_active'
      $el.find('input').focus()

    if $(window).width() < 410
      $('.container').toggleClass 'transparent'

  closeDropdown: (e)->
    if !e or (!@$el.is(e.target) and @$el.has(e.target).length is 0 and !$(e.target).hasClass 'trigger-mediator')
      $('.dropdown--is_active').removeClass 'dropdown--is_active'

  signup: (e) ->
    e.preventDefault()
    mediator.trigger 'open:auth', mode: 'signup'

  login: (e) ->
    e.preventDefault()
    mediator.trigger 'open:auth', mode: 'login'

  openAuth: (options) ->
    @modal = new AuthModalView _.extend({ width: '500px' }, options)<commit_msg>sets notification count to 0 when marked read (closes #138)<commit_after>_ = require 'underscore'
Backbone = require 'backbone'
SearchBarView = require '../../search_bar/client/view.coffee'
AuthModalView = require '../../auth_modal/view.coffee'
SettingsView = require '../../settings/client/view.coffee'
AuthRouter = require './auth_router.coffee'
mediator = require '../../../lib/mediator.coffee'
Notifications = require "../../../collections/notifications.coffee"
SmallFeedView = require '../../feed/client/small_feed_view.coffee'
NewChannelView = require '../../new_channel/client/new_channel_view.coffee'
sd = require('sharify').data

template = -> require('./templates/index.jade') arguments...

module.exports = class HeaderView extends Backbone.View

  events:
    'tap #layout-header__search__input'   : 'setActive'
    'blur #layout-header__search__input'  : 'unsetActive'
    'tap .header--icon'                   : 'setActive'
    'tap .btn-login'                      : 'login'
    'tap .btn-signup'                     : 'signup'
    'tap .dropdown--menu__trigger'        : 'toggleDropdown'
    'tap .dropdown__link--settings'       : 'openSettings'

  initialize: (options) ->
    mediator.on 'open:auth', @openAuth, @
    mediator.on 'body:click', @closeDropdown, @
    mediator.on 'search:loaded', @closeDropdown, @
    mediator.on 'notifications:synced', @maybeSetNotifications, @
    mediator.on 'notifications:cleared', @unsetNotifications, @
    mediator.on 'new:channel', @openChannelToggle, @
    mediator.on 'current_user:refreshed', @render, @
    mediator.shared.state.on 'change', @toggle, @

    @postRender()

  toggle: ->
    if mediator.shared.state.get('isDraggingBlocks')
      @$el.hide()
    else
      @$el.show()

  setActive: (e)->
    unless $('body').hasClass('is-mobile')
      @$el.addClass 'is-active'
      @$('#layout-header__search__input').focus()

  unsetActive: (e)->
    @$el.removeClass 'is-active'

  clearNotifications: (e)->
    @notifications.markRead()
    @$('.user-avatar').removeClass 'has-notifications'

  maybeSetNotifications: ->
    if (count = @notifications.getNumberUnread()) > 0
      @$('.user-avatar, .dropdown__link--notifications').addClass 'has-notifications'
      @$('.notifications--count').text count

  unsetNotifications: ->
    @$('.user-avatar, .dropdown__link--notifications').removeClass 'has-notifications'
    @$('.notifications--count').text "0"

  openChannelToggle: ->
    @$('.new-channel-dropdown').addClass 'dropdown--is_active'

  toggleDropdown: (e)->
    $el = $(e.currentTarget).parent()
    if !@$('.dropdown--is_active').is($el)
      @$('.dropdown--is_active').removeClass 'dropdown--is_active'

    ac = $el.toggleClass('dropdown--is_active')

    if $el.hasClass 'dropdown--is_active'
      $el.find('input').focus()

    if $(window).width() < 410
      $('.container').toggleClass 'transparent'

  closeDropdown: (e)->
    if !e or (!@$el.is(e.target) and @$el.has(e.target).length is 0 and !$(e.target).hasClass 'trigger-mediator')
      $('.dropdown--is_active').removeClass 'dropdown--is_active'

  signup: (e) ->
    e.preventDefault()
    mediator.trigger 'open:auth', mode: 'signup'

  login: (e) ->
    e.preventDefault()
    mediator.trigger 'open:auth', mode: 'login'

  openAuth: (options) ->
    @modal = new AuthModalView _.extend({ width: '500px' }, options)
{'fraction_non_alphanumeric': 0.17813946543228312, 'fraction_numerical': 0.0031357324175003733, 'mean_word_length': 3.332470892626132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '1992257', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2127, 'n_words': 381}