<commit_before>class GroupsMemberPermissionsView extends JView

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

    options.cssClass = "member-related"

    super options, data

    @searchWrapper = new KDCustomHTMLView
      tagName  : 'section'
      cssClass : 'searchbar'

    @search = new KDHitEnterInputView
      placeholder  : "Search..."
      name         : "searchInput"
      cssClass     : "header-search-input"
      type         : "text"
      callback     : =>
        @emit 'SearchInputChanged', @search.getValue()
        @search.focus()
      keyup        : =>
        return unless @search.getValue() is ""
        @emit 'SearchInputChanged', ''

    @searchIcon = new KDCustomHTMLView
      tagName  : 'span'
      cssClass : 'icon search'

    @searchWrapper.addSubView @search
    @searchWrapper.addSubView @searchIcon

    @_searchValue = null

    @listController = new KDListViewController
      itemClass             : GroupsMemberPermissionsListItemView
      lazyLoadThreshold     : .99
    @listWrapper    = @listController.getView()

    @listController.getListView().on 'ItemWasAdded', (view)=>
      view.on 'RolesChanged', @memberRolesChange.bind this, view
      view.on 'OwnershipChanged', @bound "refresh"

    @listController.on 'LazyLoadThresholdReached', @bound 'continueLoadingTeasers'

    @on 'teasersLoaded', =>
      unless @listController.scrollView.hasScrollBars()
        @continueLoadingTeasers()

    @on 'SearchInputChanged', (value)=>
      @_searchValue = value
      if value isnt ""
        @timestamp = new Date
        @skip = 0
        @listController.removeAllItems()
        @fetchSomeMembers()
      else @refresh()

    KD.singletons.groupsController.on "MemberJoinedGroup", (data) =>
      @refresh()
    # CtF not very sure about this. just fetched it from ActivityTicker for lazy loading
    @once 'viewAppended', =>
      @$('.kdscrollview').height window.innerHeight - 120

  fetchRoles:(callback=->)->
    groupData = @getData()
    list = @listController.getListView()
    list.getOptions().group = groupData
    groupData.fetchRoles (err, roles)=>
      return warn err if err
      list.getOptions().roles = roles

  fetchSomeMembers:(selector={})->
    @listController.showLazyLoader no
    options =
      limit : 30
      sort  : { timestamp: -1 }
    # return
    if @_searchValue
      {JAccount} = KD.remote.api
      options = {limit : 30, skip: @skip}
      JAccount.byRelevance @_searchValue, options, (err, members)=> @populateMembers err, members
    else
      @getData().fetchMembers selector, options, (err, members)=> @populateMembers err, members

  populateMembers:(err, members)->
    return warn err if err
    @listController.hideLazyLoader()
    @skip += members.length

    if members.length > 0
      @timestamp = new Date members.last.timestamp_
      ids = (member._id for member in members)
      @getData().fetchUserRoles ids, (err, userRoles)=>
        return warn err if err
        userRolesHash = {}
        for userRole in userRoles
          userRolesHash[userRole.targetId] ?= []
          userRolesHash[userRole.targetId].push userRole.as


        list = @listController.getListView()
        list.getOptions().userRoles ?= []
        list.getOptions().userRoles = _.extend(
          list.getOptions().userRoles, userRolesHash
        )

        nicknames = (member.profile.nickname for member in members)
        @getData().fetchUserStatus nicknames, (err, users)=>
          userStatusHash = {}
          for user in users
            userStatusHash[user.username] = user.status

          listOptions = list.getOptions()
          listOptions.userStatus ?= []
          listOptions.userStatus = _.extend(listOptions.userStatus, userStatusHash)
          @listController.instantiateListItems members
          if members.length < 30
            @continueLoadingTeasers()

  refresh:->
    @listController.removeAllItems()
    @timestamp = new Date
    @skip = 0
    @fetchRoles()
    @fetchSomeMembers()

  continueLoadingTeasers:->
    @listController.showLazyLoader no
    @fetchSomeMembers {timestamp: $lt: @timestamp.getTime()}

  memberRolesChange:(view, member, roles)->
    @getData().changeMemberRoles member.getId(), roles, (err)=>
      view.updateRoles roles  unless err

  pistachio:->
    """
    {{> @searchWrapper}}
    {{> @listWrapper}}
    """
<commit_msg>Groups: populateMembers method is divided for readability<commit_after>class GroupsMemberPermissionsView extends JView

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

    options.cssClass = "member-related"

    super options, data

    @searchWrapper = new KDCustomHTMLView
      tagName  : 'section'
      cssClass : 'searchbar'

    @search = new KDHitEnterInputView
      placeholder  : "Search..."
      name         : "searchInput"
      cssClass     : "header-search-input"
      type         : "text"
      callback     : =>
        @emit 'SearchInputChanged', @search.getValue()
        @search.focus()
      keyup        : =>
        return unless @search.getValue() is ""
        @emit 'SearchInputChanged', ''

    @searchIcon = new KDCustomHTMLView
      tagName  : 'span'
      cssClass : 'icon search'

    @searchWrapper.addSubView @search
    @searchWrapper.addSubView @searchIcon

    @_searchValue = null

    @listController = new KDListViewController
      itemClass             : GroupsMemberPermissionsListItemView
      lazyLoadThreshold     : .99
    @listWrapper    = @listController.getView()

    @listController.getListView().on 'ItemWasAdded', (view)=>
      view.on 'RolesChanged', @memberRolesChange.bind this, view
      view.on 'OwnershipChanged', @bound "refresh"

    @listController.on 'LazyLoadThresholdReached', @bound 'continueLoadingTeasers'

    @on 'teasersLoaded', =>
      unless @listController.scrollView.hasScrollBars()
        @continueLoadingTeasers()

    @on 'SearchInputChanged', (value)=>
      @_searchValue = value
      if value isnt ""
        @timestamp = new Date
        @skip = 0
        @listController.removeAllItems()
        @fetchSomeMembers()
      else @refresh()

    KD.singletons.groupsController.on "MemberJoinedGroup", (data) =>
      @refresh()
    # CtF not very sure about this. just fetched it from ActivityTicker for lazy loading
    @once 'viewAppended', =>
      @$('.kdscrollview').height window.innerHeight - 120

  fetchRoles:(callback=->)->
    groupData = @getData()
    list = @listController.getListView()
    list.getOptions().group = groupData
    groupData.fetchRoles (err, roles)=>
      return warn err if err
      list.getOptions().roles = roles

  fetchSomeMembers:(selector={})->
    @listController.showLazyLoader no
    options =
      limit : 30
      sort  : { timestamp: -1 }
    # return
    if @_searchValue
      {JAccount} = KD.remote.api
      options = {limit : 30, skip: @skip}
      JAccount.byRelevance @_searchValue, options, (err, members)=> @populateMembers err, members
    else
      @getData().fetchMembers selector, options, (err, members)=> @populateMembers err, members

  populateMembers:(err, members)->
    instantiateItems = (err) =>
      return warn err  if err
      @listController.instantiateListItems members
      @emit 'teasersLoaded' if members.length is 10

    return warn err if err
    @listController.hideLazyLoader()
    @skip += members.length
    if members.length > 0
      @fetchUserRoles members, (err) =>
        return warn err  if err
        #no need to fetch user status for all group admins
        if @getData().slug is "koding"
          @fetchUserStatus members, instantiateItems
        else
          instantiateItems()

  fetchUserRoles:(members, callback) ->
    ids = (member._id for member in members)
    @getData().fetchUserRoles ids, (err, userRoles)=>
      return callback err  if err
      userRolesHash = {}
      for userRole in userRoles
        userRolesHash[userRole.targetId] ?= []
        userRolesHash[userRole.targetId].push userRole.as

      list = @listController.getListView()
      list.getOptions().userRoles ?= {}
      {userRoles} = list.getOptions()
      userRoles = _.extend(
        userRoles, userRolesHash
      )
      callback null

  fetchUserStatus: (members, callback) ->
    nicknames = (member.profile.nickname for member in members)
    @getData().fetchUserStatus nicknames, (err, users)=>
      return callback err  if err
      userStatusHash = {}
      for user in users
        userStatusHash[user.username] = user.status

      list = @listController.getListView()
      list.getOptions().userStatus ?= {}
      {userStatus} = list.getOptions()
      userStatus = _.extend(userStatus, userStatusHash)
      callback null

  refresh:->
    @listController.removeAllItems()
    @timestamp = new Date
    @skip = 0
    @fetchRoles()
    @fetchSomeMembers()

  continueLoadingTeasers:->
    @listController.showLazyLoader no
    @fetchSomeMembers {timestamp: $lt: @timestamp.getTime()}

  memberRolesChange:(view, member, roles)->
    @getData().changeMemberRoles member.getId(), roles, (err)=>
      view.updateRoles roles  unless err

  pistachio:->
    """
    {{> @searchWrapper}}
    {{> @listWrapper}}
    """

{'fraction_non_alphanumeric': 0.11068743871881469, 'fraction_numerical': 0.0032683298834295673, 'mean_word_length': 2.7777777777777777, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 57, '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': '7586812', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2601, 'n_words': 624}