<commit_before>kd                   = require 'kd'
KDView               = kd.View
KDSelectBox          = kd.SelectBox
KDListItemView       = kd.ListItemView
KDCustomHTMLView     = kd.CustomHTMLView
KDListViewController = kd.ListViewController
KDHitEnterInputView  = kd.HitEnterInputView

MemberItemView = require './memberitemview'


module.exports = class TeamMembersCommonView extends KDView

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

    options.noItemFoundWidget   or= new KDCustomHTMLView
    options.listViewItemClass   or= MemberItemView
    options.listViewItemOptions or= {}
    options.itemLimit            ?= 10

    super options, data

    @skip = 0

    @createSearchView()
    @createListController()
    @fetchMembers()


  createSearchView: ->

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

    @searchContainer.addSubView @sortSelectBox = new KDSelectBox
      defaultValue  : 'nickname'
      selectOptions : [
        { title     : 'Screen name',  value : 'fullname'  }
        { title     : 'Nickname',     value : 'nickname'  }
      ]
      callback      : (value) ->

    @searchContainer.addSubView @searchInput = new KDHitEnterInputView
      type        : 'text'
      placeholder : 'Find by name/username'
      callback    : @bound 'search'


  createListController: ->

    { listViewItemClass, noItemFoundWidget, listViewItemOptions } = @getOptions()

    @listController       = new KDListViewController
      viewOptions         :
        wrapper           : yes
        itemClass         : listViewItemClass
        itemOptions       : listViewItemOptions
      noItemFoundWidget   : noItemFoundWidget
      startWithLazyLoader : yes
      lazyLoadThreshold   : .99
      lazyLoaderOptions   :
        spinnerOptions    :
          size            : width: 28

    @addSubView @listController.getView()

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


  fetchMembers: ->

    return if @isFetching

    @isFetching = yes

    selector = @query or ''
    options  =
      limit  : @getOptions().itemLimit
      sort   : { timestamp: -1 }
      skip   : @skip

    @getData().searchMembers selector, options, (err, members) =>
      return kd.warn err  if err
      @listMembers members
      @isFetching = no


  listMembers: (members) ->

    unless members.length
      return @listController.lazyLoader.hide()

    @skip += members.length

    for member in members
      @listController.addItem member

    @listController.lazyLoader.hide()
    @searchContainer.show()


  search: ->

    @skip  = 0
    @query = @searchInput.getValue()

    @listController.removeAllItems()
    @listController.lazyLoader.show()
    @fetchMembers()
<commit_msg>TeamMembersCommonView: Error handling. Hide loader if there is an error.<commit_after>kd                   = require 'kd'
KDView               = kd.View
KDSelectBox          = kd.SelectBox
KDListItemView       = kd.ListItemView
KDCustomHTMLView     = kd.CustomHTMLView
KDListViewController = kd.ListViewController
KDHitEnterInputView  = kd.HitEnterInputView

MemberItemView = require './memberitemview'


module.exports = class TeamMembersCommonView extends KDView

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

    options.noItemFoundWidget   or= new KDCustomHTMLView
    options.listViewItemClass   or= MemberItemView
    options.listViewItemOptions or= {}
    options.itemLimit            ?= 10

    super options, data

    @skip = 0

    @createSearchView()
    @createListController()
    @fetchMembers()


  createSearchView: ->

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

    @searchContainer.addSubView @sortSelectBox = new KDSelectBox
      defaultValue  : 'nickname'
      selectOptions : [
        { title     : 'Screen name',  value : 'fullname'  }
        { title     : 'Nickname',     value : 'nickname'  }
      ]
      callback      : (value) ->

    @searchContainer.addSubView @searchInput = new KDHitEnterInputView
      type        : 'text'
      placeholder : 'Find by name/username'
      callback    : @bound 'search'


  createListController: ->

    { listViewItemClass, noItemFoundWidget, listViewItemOptions } = @getOptions()

    @listController       = new KDListViewController
      viewOptions         :
        wrapper           : yes
        itemClass         : listViewItemClass
        itemOptions       : listViewItemOptions
      noItemFoundWidget   : noItemFoundWidget
      startWithLazyLoader : yes
      lazyLoadThreshold   : .99
      lazyLoaderOptions   :
        spinnerOptions    :
          size            : width: 28

    @addSubView @listController.getView()

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


  fetchMembers: ->

    return if @isFetching

    @isFetching = yes

    selector = @query or ''
    options  =
      limit  : @getOptions().itemLimit
      sort   : { timestamp: -1 }
      skip   : @skip

    @getData().searchMembers selector, options, (err, members) =>
      if err
        @listController.lazyLoader.hide()
        return kd.warn err

      @listMembers members
      @isFetching = no


  listMembers: (members) ->

    unless members.length
      return @listController.lazyLoader.hide()

    @skip += members.length

    for member in members
      @listController.addItem member

    @listController.lazyLoader.hide()
    @searchContainer.show()


  search: ->

    @skip  = 0
    @query = @searchInput.getValue()

    @listController.removeAllItems()
    @listController.lazyLoader.show()
    @fetchMembers()

{'fraction_non_alphanumeric': 0.08888888888888889, 'fraction_numerical': 0.0031496062992125984, 'mean_word_length': 2.453776435045317, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '6125184', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1597, 'n_words': 360}