<commit_before>kd                        = require 'kd'
KDView                    = kd.View
KDSelectBox               = kd.SelectBox
KDCustomHTMLView          = kd.CustomHTMLView
KDHitEnterInputView       = kd.HitEnterInputView
whoami                    = require 'app/util/whoami'
remote                    = require('app/remote').getInstance()
MemberItemView            = require './memberitemview'
KodingListController      = require 'app/kodinglist/kodinglistcontroller'
TeamMembersListController = require './teammemberslistcontroller'


module.exports = class TeamMembersCommonView extends KDView

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

    options.cssClass                 = 'members-commonview'
    options.itemLimit               ?= 10
    options.fetcherMethod          or= 'fetchMembersWithEmail'
    options.listViewItemOptions    or= {}
    options.listViewItemClass      or= null
    options.searchInputPlaceholder or= 'Find by name/username'
    options.showSearchFieldAtFirst or= no
    options.sortOptions            or= [
      { title: 'Screen name',   value: 'fullname' }
      { title: 'Nickname',      value: 'nickname' }
    ]

    super options, data

    @page = 0

    @createSearchView()
    @createListController()


  createSearchView: ->

    { sortOptions, showSearchFieldAtFirst } = @getOptions()

    @searchContainer = new KDCustomHTMLView
      cssClass : 'search'
      partial  : '<span class="label">Sort by</span>'

    @searchContainer.hide()  unless showSearchFieldAtFirst

    @addSubView @searchContainer

    @searchContainer.addSubView @sortSelectBox = new KDSelectBox
      defaultValue  : sortOptions.first.value
      selectOptions : sortOptions
      callback      : @bound 'search'

    @searchContainer.addSubView @searchInput = new KDHitEnterInputView
      type        : 'text'
      placeholder : @getOptions().searchInputPlaceholder
      callback    : @bound 'search'

    @searchContainer.addSubView @searchClear = new KDCustomHTMLView
      tagName     : 'span'
      partial     : 'clear'
      cssClass    : 'clear-search hidden'
      click       : =>
        @searchInput.setValue ''
        @search()
        @searchClear.hide()


  createListController: ->

    { noItemFoundText, listViewItemOptions, fetcherMethod, listViewItemClass, memberType, defaultMemberRole } = @getOptions()
    group = @getData()

    @listController       = new TeamMembersListController
      memberType          : memberType
      defaultMemberRole   : defaultMemberRole
      noItemFoundText     : noItemFoundText
      itemClass           : listViewItemClass or MemberItemView
      viewOptions         :
        wrapper           : yes
        itemOptions       : listViewItemOptions
      fetcherMethod       : (query, fetchOptions, callback) ->
        group[fetcherMethod] query, fetchOptions, callback
    , group

    @buildListController()


  buildListController: ->

    @addSubView @listController.getView()

    @listController.on 'LazyLoadThresholdReached', =>
      if @searchInput.getValue()
        unless @isFetching
          @isFetching = yes
          @page++
          @search no, yes
      else
        @fetchMembers()

    @listController
      .on 'CalculateAndFetchMoreIfNeeded',  @bound 'calculateAndFetchMoreIfNeeded'
      .on 'ShowSearchContainer',            @searchContainer.bound 'show'
      .on 'HideSearchContainer',            @searchContainer.bound 'hide'
      .on 'ErrorHappened',                  @bound 'handleError'


  fetchMembers: ->

    return if @isFetching

    @isFetching = yes

    { fetcherMethod, itemLimit } = @getOptions()

    group   = @getData()
    options =
      limit : itemLimit
      sort  : { timestamp: -1 } # timestamp is at relationship collection
      skip  : @listController.filterStates.skip

    # fetch members as jAccount
    group[fetcherMethod] {}, options, (err, members) =>

      return @handleError err  if err

      @listController.addListItems members
      @isFetching = no


  handleError: (err) ->

    @page = 0

    if err?.message?.indexOf('No account found') > -1
      @search yes
    else
      @listController.lazyLoader.hide()
      kd.warn err


  calculateAndFetchMoreIfNeeded: ->

    listCtrl = @listController

    viewHeight = listCtrl.getView().getHeight()<commit_msg>Call hideLazyLoader method of list controller<commit_after>kd                        = require 'kd'
KDView                    = kd.View
KDSelectBox               = kd.SelectBox
KDCustomHTMLView          = kd.CustomHTMLView
KDHitEnterInputView       = kd.HitEnterInputView
whoami                    = require 'app/util/whoami'
remote                    = require('app/remote').getInstance()
MemberItemView            = require './memberitemview'
KodingListController      = require 'app/kodinglist/kodinglistcontroller'
TeamMembersListController = require './teammemberslistcontroller'


module.exports = class TeamMembersCommonView extends KDView

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

    options.cssClass                 = 'members-commonview'
    options.itemLimit               ?= 10
    options.fetcherMethod          or= 'fetchMembersWithEmail'
    options.listViewItemOptions    or= {}
    options.listViewItemClass      or= null
    options.searchInputPlaceholder or= 'Find by name/username'
    options.showSearchFieldAtFirst or= no
    options.sortOptions            or= [
      { title: 'Screen name',   value: 'fullname' }
      { title: 'Nickname',      value: 'nickname' }
    ]

    super options, data

    @page = 0

    @createSearchView()
    @createListController()


  createSearchView: ->

    { sortOptions, showSearchFieldAtFirst } = @getOptions()

    @searchContainer = new KDCustomHTMLView
      cssClass : 'search'
      partial  : '<span class="label">Sort by</span>'

    @searchContainer.hide()  unless showSearchFieldAtFirst

    @addSubView @searchContainer

    @searchContainer.addSubView @sortSelectBox = new KDSelectBox
      defaultValue  : sortOptions.first.value
      selectOptions : sortOptions
      callback      : @bound 'search'

    @searchContainer.addSubView @searchInput = new KDHitEnterInputView
      type        : 'text'
      placeholder : @getOptions().searchInputPlaceholder
      callback    : @bound 'search'

    @searchContainer.addSubView @searchClear = new KDCustomHTMLView
      tagName     : 'span'
      partial     : 'clear'
      cssClass    : 'clear-search hidden'
      click       : =>
        @searchInput.setValue ''
        @search()
        @searchClear.hide()


  createListController: ->

    { noItemFoundText, listViewItemOptions, fetcherMethod, listViewItemClass, memberType, defaultMemberRole } = @getOptions()
    group = @getData()

    @listController       = new TeamMembersListController
      memberType          : memberType
      defaultMemberRole   : defaultMemberRole
      noItemFoundText     : noItemFoundText
      itemClass           : listViewItemClass or MemberItemView
      viewOptions         :
        wrapper           : yes
        itemOptions       : listViewItemOptions
      fetcherMethod       : (query, fetchOptions, callback) ->
        group[fetcherMethod] query, fetchOptions, callback
    , group

    @buildListController()


  buildListController: ->

    @addSubView @listController.getView()

    @listController.on 'LazyLoadThresholdReached', =>
      if @searchInput.getValue()
        unless @isFetching
          @isFetching = yes
          @page++
          @search no, yes
      else
        @fetchMembers()

    @listController
      .on 'CalculateAndFetchMoreIfNeeded',  @bound 'calculateAndFetchMoreIfNeeded'
      .on 'ShowSearchContainer',            @searchContainer.bound 'show'
      .on 'HideSearchContainer',            @searchContainer.bound 'hide'
      .on 'ErrorHappened',                  @bound 'handleError'


  fetchMembers: ->

    return if @isFetching

    @isFetching = yes

    { fetcherMethod, itemLimit } = @getOptions()

    group   = @getData()
    options =
      limit : itemLimit
      sort  : { timestamp: -1 } # timestamp is at relationship collection
      skip  : @listController.filterStates.skip

    # fetch members as jAccount
    group[fetcherMethod] {}, options, (err, members) =>

      return @handleError err  if err

      @listController.addListItems members
      @isFetching = no


  handleError: (err) ->

    @page = 0

    if err?.message?.indexOf('No account found') > -1
      @search yes
    else
      @listController.hideLazyLoader()
      kd.warn err


  calculateAndFetchMoreIfNeeded: ->

    listCtrl = @listController

    viewHeight = listCtrl.getView().getHeight()
{'fraction_non_alphanumeric': 0.0882862088863243, 'fraction_numerical': 0.0013848817080207732, 'mean_word_length': 2.4239431054919005, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, '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': '4937699', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2321, 'n_words': 522}