<commit_before>class AvatarPopupGroupSwitcher extends AvatarPopup

  constructor:->
    @notLoggedInMessage = 'Login required to switch groups'
    super

  viewAppended:->

    super

    @pending             = 0
    @notPopulated        = yes
    @notPopulatedPending = yes
    groupsController     = KD.getSingleton "groupsController"
    router               = KD.getSingleton "router"


    @_popupList = new PopupList
      itemClass  : PopupGroupListItem

    @_popupListPending = new PopupList
      itemClass  : PopupGroupListItemPending

    # does not work yet
    # @_popupListPending.on 'PendingCountDecreased', @bound 'decreasePendingCount'
    # @_popupListPending.on 'UpdateGroupList',       @bound 'populateGroups'
    # KD.whoami().on        'NewPendingInvitation',  @bound 'populatePendingGroups'

    @listControllerPending = new KDListViewController
      lazyLoaderOptions   :
        partial           : ''
        spinnerOptions    :
          loaderOptions   :
            color         : '#6BB197'
          size            :
            width         : 32
      view                : @_popupListPending

    @listController = new KDListViewController
      lazyLoaderOptions   :
        partial           : ''
        spinnerOptions    :
          loaderOptions   :
            color         : '#6BB197'
          size            :
            width         : 32
      view                : @_popupList

    @listController.on "AvatarPopupShouldBeHidden", @bound 'hide'

    @avatarPopupContent.addSubView @invitesHeader = new KDView
      height   : "auto"
      cssClass : "sublink top hidden"
      partial  : "You have pending group invitations:"

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'span'
      cssClass   : 'icon help'
      tooltip    :
        title    : "Here you'll find the groups that you are a member of, clicking one of them will take you to a new browser tab."

    @addSubView @groupSubMenuWrapper = new KDCustomHTMLView
      partial  : '<div class="kdview content"></div>'
      cssClass : 'avatararea-popup notifications group-switcher submenu' # this is a hack, just to use the same position w/ notifications dropdown

    @groupSubMenuWrapper.setCss 'right', 293

    {entryPoint} = KD.config
    createGroupLink = new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Pricing/Team'
      cssClass   : 'bottom bb'
      partial    : 'Create a group'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Pricing/CreateGroup', entryPoint : 'koding'
        @hide()

    backToKoding = new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/'
      cssClass   : 'bottom bb'
      partial    : 'Go back to Koding'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        location.href = '/'

    groupsController.ready ->
      backToKoding.destroy()  if groupsController.getCurrentGroup().slug is 'koding'


    @groupSubMenuWrapper.addSubView createGroupLink, '.content'
    @groupSubMenuWrapper.addSubView backToKoding, '.content'
    @groupSubMenuWrapper.addSubView @listControllerPending.getView(), '.content'
    @groupSubMenuWrapper.addSubView @listController.getView(), '.content'


    submenuShown = no

    @avatarPopupContent.bindEvent 'mousemove'
    @avatarPopupContent.on 'mousemove', (event)=>
      return  if $(event.target).closest().is '.submenu'
      @groupSubMenuWrapper.unsetClass 'active'


    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '#'
      cssClass   : 'bottom'
      partial    : 'Your groups'
      bind       : 'mouseenter mousemove'
      mouseenter : (event)=>
        KD.utils.stopDOMEvent event
        submenuShown = yes
        @groupSubMenuWrapper.setClass 'active'
        @populateGroups()
      mousemove : KD.utils.stopDOMEvent

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Account'
      cssClass   : 'bottom separator'
      partial    : 'Account settings'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Account'
        @hide()


    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Environments'
      cssClass   : 'bottom'
      partial    : 'Environments'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Environments'
        @hide()


    @avatarPopupContent.addSubView dashboardLink = new KDCustomHTMLView
      tagName  : "a"
      cssClass : "bottom hidden"
      partial  : "Group dashboard"
      click    : (event) =>
        KD.utils.stopDOMEvent event
        KD.getSingleton("router").handleRoute "/Dashboard"
        @hide()

    # FIXME:
    groupsController.ready ->
      group = groupsController.getCurrentGroup()
      group.canEditGroup (err, success)=>
        return  unless success
        dashboardLink.show()

    cookieName = "kdproxy-usehttp"
    if $.cookie(cookieName) is "1"
      @avatarPopupContent.addSubView new KDCustomHTMLView
        tagName    : 'a'
        cssClass   : 'bottom'
        partial    : 'Switch back to secure (https) mode'
        click      : (event)=>
          KD.utils.stopDOMEvent event
          $.cookie cookieName, erase: yes
          window.location.reload()

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Logout'
      cssClass   : 'bottom'
      partial    : 'Logout'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Logout'
        @hide()

  populatePendingGroups:->
    @listControllerPending.removeAllItems()
    @listControllerPending.hideLazyLoader()

    return  unless KD.isLoggedIn()

    KD.whoami().fetchGroupsWithPendingInvitations (err, groups)=>
      if err then warn err
      else if groups?
        @pending = 0
        for group in groups when group
          @listControllerPending.addItem {group, roles:[], admin:no}
          @pending++
        @updatePendingCount()
        @notPopulatedPending = no


  populateGroups:->
    return  if not KD.isLoggedIn() or @isLoading

    @listController.removeAllItems()

    @isLoading = yes

    KD.whoami().fetchGroups null, (err, groups)=>
      if err then warn err
      else if groups?

        stack = []
        groups.forEach (group)->
          stack.push (cb)->
            group.group.fetchMyRoles (err, roles)->
              group.admin = unless err then 'admin' in roles else no
              cb err, group

        async.parallel stack, (err, results)=>
          @isLoading = no

          unless err
            results.sort (a, b)->
              return if a.admin is b.admin
              then a.group.slug > b.group.slug
              else not a.admin and b.admin

            index = null
            results.forEach (item, i)->
              index = i  if item.group.slug is 'koding'

            results.splice index, 1  if index?

            @listController.hideLazyLoader()
            @listController.instantiateListItems results

  decreasePendingCount:->
    @pending--
    @updatePendingCount()

  updatePendingCount:->
    @listControllerPending.emit 'PendingGroupsCountDidChange', @pending

  show:->
    super
    # in case user opens popup earlier than timed out initial population
    @populateGroups() if @notPopulated
    @populatePendingGroups() if @notPopulatedPending

  hide:->
    super
    @groupSubMenuWrapper.unsetClass 'active'

class PopupGroupListItem extends KDListItemView

  constructor:(options = {}, data)->
    options.tagName or= "li"
    super

    {group:{title, avatar, slug}, roles, admin} = @getData()

    roleClasses = roles.map((role)-> "role-#{role}").join ' '
    @setClass "role #{roleClasses}"

    @switchLink = new CustomLinkView
      title       : title
      href        : "/#{if slug is KD.defaultSlug then '' else slug+'/'}Activity"
      target      : slug
      icon        :
        cssClass  : 'new-page'
        placement : 'right'
        tooltip   :
          title   : "Opens in a new browser window."
          delayIn : 300

    @adminLink = if admin
      new CustomLinkView
        title       : ''
        href        : "/#{if slug is KD.defaultSlug then '' else slug+'/'}Dashboard"
        target      : slug
        cssClass    : 'admin-icon'
        iconOnly    : yes
        icon        :
          cssClass  : 'dashboard-page'
          placement : 'right'
          tooltip   :
            title   : "Opens admin dashboard in new browser window."
            delayIn : 300
    else new KDCustomHTMLView

  viewAppended: JView::viewAppended

  pistachio: ->
    """
    {{> @switchLink}}{{> @adminLink}}
    """

class PopupGroupListItemPending extends PopupGroupListItem

  constructor:(options = {}, data)->
    super

    {group} = @getData()
    @setClass 'role pending'

    @acceptButton = new KDButtonView
      style       : 'clean-gray'
      title       : 'Accept Invitation'
      icon        : yes
      iconOnly    : yes
      iconClass   : 'accept'
      tooltip     :
        title     : 'Accept Invitation'
      callback    : =>
        KD.whoami().acceptInvitation group, (err)=>
          if err then warn err
          else
            @destroy()
            @parent.emit 'PendingCountDecreased'
            @parent.emit 'UpdateGroupList'

    @ignoreButton = new KDButtonView
      style       : 'clean-gray'
      title       : 'Ignore Invitation'
      icon        : yes
      iconOnly    : yes
      iconClass   : 'ignore'
      tooltip     :
        title     : 'Ignore Invitation'
      callback    : =>
        KD.whoami().ignoreInvitation group, (err)=>
          if err then warn err
          else
            new KDNotificationView
              title    : 'Ignored!'
              content  : 'If you change your mind, you can request access to the group anytime.'
              duration : 2000
            @destroy()
            @parent.emit 'PendingCountDecreased'

  viewAppended: JView::viewAppended

  pistachio: ->
    """
    <div class='right-overflow'>
      <div class="buttons">
        {{> @acceptButton}}
        {{> @ignoreButton}}
      </div>
      {{> @switchLink}}
    </div>
    """
<commit_msg>AccountMenu: your groups submenu handling triggered now also on click<commit_after>class AvatarPopupGroupSwitcher extends AvatarPopup

  constructor:->
    @notLoggedInMessage = 'Login required to switch groups'
    super

  viewAppended:->

    super

    @pending             = 0
    @notPopulated        = yes
    @notPopulatedPending = yes
    groupsController     = KD.getSingleton "groupsController"
    router               = KD.getSingleton "router"


    @_popupList = new PopupList
      itemClass  : PopupGroupListItem

    @_popupListPending = new PopupList
      itemClass  : PopupGroupListItemPending

    # does not work yet
    # @_popupListPending.on 'PendingCountDecreased', @bound 'decreasePendingCount'
    # @_popupListPending.on 'UpdateGroupList',       @bound 'populateGroups'
    # KD.whoami().on        'NewPendingInvitation',  @bound 'populatePendingGroups'

    @listControllerPending = new KDListViewController
      lazyLoaderOptions   :
        partial           : ''
        spinnerOptions    :
          loaderOptions   :
            color         : '#6BB197'
          size            :
            width         : 32
      view                : @_popupListPending

    @listController = new KDListViewController
      lazyLoaderOptions   :
        partial           : ''
        spinnerOptions    :
          loaderOptions   :
            color         : '#6BB197'
          size            :
            width         : 32
      view                : @_popupList

    @listController.on "AvatarPopupShouldBeHidden", @bound 'hide'

    @avatarPopupContent.addSubView @invitesHeader = new KDView
      height   : "auto"
      cssClass : "sublink top hidden"
      partial  : "You have pending group invitations:"

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'span'
      cssClass   : 'icon help'
      tooltip    :
        title    : "Here you'll find the groups that you are a member of, clicking one of them will take you to a new browser tab."

    @addSubView @groupSubMenuWrapper = new KDCustomHTMLView
      partial  : '<div class="kdview content"></div>'
      cssClass : 'avatararea-popup notifications group-switcher submenu' # this is a hack, just to use the same position w/ notifications dropdown

    @groupSubMenuWrapper.setCss 'right', 293

    {entryPoint} = KD.config
    createGroupLink = new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Pricing/Team'
      cssClass   : 'bottom bb'
      partial    : 'Create a group'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Pricing/CreateGroup', entryPoint : 'koding'
        @hide()

    backToKoding = new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/'
      cssClass   : 'bottom bb'
      partial    : 'Go back to Koding'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        location.href = '/'

    groupsController.ready ->
      backToKoding.destroy()  if groupsController.getCurrentGroup().slug is 'koding'


    @groupSubMenuWrapper.addSubView createGroupLink, '.content'
    @groupSubMenuWrapper.addSubView backToKoding, '.content'
    @groupSubMenuWrapper.addSubView @listControllerPending.getView(), '.content'
    @groupSubMenuWrapper.addSubView @listController.getView(), '.content'


    submenuShown = no

    @avatarPopupContent.bindEvent 'mousemove'
    @avatarPopupContent.on 'mousemove', (event)=>
      return  if $(event.target).closest().is '.submenu'
      @groupSubMenuWrapper.unsetClass 'active'

    handleSubMenu = (event)=>
      KD.utils.stopDOMEvent event
      submenuShown = yes
      @groupSubMenuWrapper.setClass 'active'
      @populateGroups()

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '#'
      cssClass   : 'bottom'
      partial    : 'Your groups'
      bind       : 'mouseenter mousemove'
      mouseenter : handleSubMenu
      click      : handleSubMenu
      mousemove  : KD.utils.stopDOMEvent

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Account'
      cssClass   : 'bottom separator'
      partial    : 'Account settings'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Account'
        @hide()


    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Environments'
      cssClass   : 'bottom'
      partial    : 'Environments'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Environments'
        @hide()


    @avatarPopupContent.addSubView dashboardLink = new KDCustomHTMLView
      tagName  : "a"
      cssClass : "bottom hidden"
      partial  : "Group dashboard"
      click    : (event) =>
        KD.utils.stopDOMEvent event
        KD.getSingleton("router").handleRoute "/Dashboard"
        @hide()

    # FIXME:
    groupsController.ready ->
      group = groupsController.getCurrentGroup()
      group.canEditGroup (err, success)=>
        return  unless success
        dashboardLink.show()

    cookieName = "kdproxy-usehttp"
    if $.cookie(cookieName) is "1"
      @avatarPopupContent.addSubView new KDCustomHTMLView
        tagName    : 'a'
        cssClass   : 'bottom'
        partial    : 'Switch back to secure (https) mode'
        click      : (event)=>
          KD.utils.stopDOMEvent event
          $.cookie cookieName, erase: yes
          window.location.reload()

    @avatarPopupContent.addSubView new KDCustomHTMLView
      tagName    : 'a'
      attributes : href : '/Logout'
      cssClass   : 'bottom'
      partial    : 'Logout'
      click      : (event)=>
        KD.utils.stopDOMEvent event
        router.handleRoute '/Logout'
        @hide()

  populatePendingGroups:->
    @listControllerPending.removeAllItems()
    @listControllerPending.hideLazyLoader()

    return  unless KD.isLoggedIn()

    KD.whoami().fetchGroupsWithPendingInvitations (err, groups)=>
      if err then warn err
      else if groups?
        @pending = 0
        for group in groups when group
          @listControllerPending.addItem {group, roles:[], admin:no}
          @pending++
        @updatePendingCount()
        @notPopulatedPending = no


  populateGroups:->
    return  if not KD.isLoggedIn() or @isLoading

    @listController.removeAllItems()

    @isLoading = yes

    KD.whoami().fetchGroups null, (err, groups)=>
      if err then warn err
      else if groups?

        stack = []
        groups.forEach (group)->
          stack.push (cb)->
            group.group.fetchMyRoles (err, roles)->
              group.admin = unless err then 'admin' in roles else no
              cb err, group

        async.parallel stack, (err, results)=>
          @isLoading = no

          unless err
            results.sort (a, b)->
              return if a.admin is b.admin
              then a.group.slug > b.group.slug
              else not a.admin and b.admin

            index = null
            results.forEach (item, i)->
              index = i  if item.group.slug is 'koding'

            results.splice index, 1  if index?

            @listController.hideLazyLoader()
            @listController.instantiateListItems results

  decreasePendingCount:->
    @pending--
    @updatePendingCount()

  updatePendingCount:->
    @listControllerPending.emit 'PendingGroupsCountDidChange', @pending

  show:->
    super
    # in case user opens popup earlier than timed out initial population
    @populateGroups() if @notPopulated
    @populatePendingGroups() if @notPopulatedPending

  hide:->
    super
    @groupSubMenuWrapper.unsetClass 'active'

class PopupGroupListItem extends KDListItemView

  constructor:(options = {}, data)->
    options.tagName or= "li"
    super

    {group:{title, avatar, slug}, roles, admin} = @getData()

    roleClasses = roles.map((role)-> "role-#{role}").join ' '
    @setClass "role #{roleClasses}"

    @switchLink = new CustomLinkView
      title       : title
      href        : "/#{if slug is KD.defaultSlug then '' else slug+'/'}Activity"
      target      : slug
      icon        :
        cssClass  : 'new-page'
        placement : 'right'
        tooltip   :
          title   : "Opens in a new browser window."
          delayIn : 300

    @adminLink = if admin
      new CustomLinkView
        title       : ''
        href        : "/#{if slug is KD.defaultSlug then '' else slug+'/'}Dashboard"
        target      : slug
        cssClass    : 'admin-icon'
        iconOnly    : yes
        icon        :
          cssClass  : 'dashboard-page'
          placement : 'right'
          tooltip   :
            title   : "Opens admin dashboard in new browser window."
            delayIn : 300
    else new KDCustomHTMLView

  viewAppended: JView::viewAppended

  pistachio: ->
    """
    {{> @switchLink}}{{> @adminLink}}
    """

class PopupGroupListItemPending extends PopupGroupListItem

  constructor:(options = {}, data)->
    super

    {group} = @getData()
    @setClass 'role pending'

    @acceptButton = new KDButtonView
      style       : 'clean-gray'
      title       : 'Accept Invitation'
      icon        : yes
      iconOnly    : yes
      iconClass   : 'accept'
      tooltip     :
        title     : 'Accept Invitation'
      callback    : =>
        KD.whoami().acceptInvitation group, (err)=>
          if err then warn err
          else
            @destroy()
            @parent.emit 'PendingCountDecreased'
            @parent.emit 'UpdateGroupList'

    @ignoreButton = new KDButtonView
      style       : 'clean-gray'
      title       : 'Ignore Invitation'
      icon        : yes
      iconOnly    : yes
      iconClass   : 'ignore'
      tooltip     :
        title     : 'Ignore Invitation'
      callback    : =>
        KD.whoami().ignoreInvitation group, (err)=>
          if err then warn err
          else
            new KDNotificationView
              title    : 'Ignored!'
              content  : 'If you change your mind, you can request access to the group anytime.'
              duration : 2000
            @destroy()
            @parent.emit 'PendingCountDecreased'

  viewAppended: JView::viewAppended

  pistachio: ->
    """
    <div class='right-overflow'>
      <div class="buttons">
        {{> @acceptButton}}
        {{> @ignoreButton}}
      </div>
      {{> @switchLink}}
    </div>
    """

{'fraction_non_alphanumeric': 0.09455958549222798, 'fraction_numerical': 0.002782575321435425, 'mean_word_length': 2.0368589743589745, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 101, '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': '5214652', 'n_tokens_mistral': 6079, 'n_tokens_neox': 5872, 'n_words': 1483}