<commit_before>class ManageSharedView extends KDView

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

    options.cssClass = KD.utils.curry 'users-view', options.cssClass

    super options, data

    {@machine} = @getOptions()

    @addSubView @inputView = new KDView
      cssClass          : 'input-view'

    @inputView.addSubView @input = new KDHitEnterInputView
      type              : 'text'
      attributes        :
        spellcheck      : no

    @usersController    = new KDListViewController
      viewOptions       :
        type            : 'user'
        wrapper         : yes
        itemClass       : UserItem
        itemOptions     :
          machineId     : @machine._id

    @addSubView @userListView = @usersController.getView()
    @inputView.hide()

    @usersController.getListView()
      .on 'KickUserRequested', @bound 'kickUser'

    @addSubView @loader = new KDLoaderView
      cssClass          : 'in-progress'
      size              :
        width           : 14
        height          : 14
      loaderOptions     :
        color           : '#333333'
      showLoader        : yes

    @addSubView @warning = new KDCustomHTMLView
      cssClass          : 'warning hidden'
      click             : -> @hide()

    @listUsers()


  listUsers: ->

    @loader.show()

    @machine.jMachine.reviveUsers permanentOnly: yes, (err, users)=>
      warn err  if err?

      users ?= []
      @usersController.replaceAllItems users
      @userListView[if users.length > 0 then 'show' else 'hide']()

      @loader.hide()


  kickUser: (userItem)->

    @loader.show()
    @warning.hide()

    {profile:{nickname}} = userItem.getData()

    @machine.jMachine.shareWith
      target    : [nickname]
      permanent : yes
      asUser    : no
    , (err)=>

      @loader.hide()

      if err
        @warning.setTooltip title: err.message
        @warning.show()

      else
        if @usersController.itemsOrdered.length is 1
        then @listUsers()
        else @usersController.removeItem userItem


  toggleInput:->

    @inputView.toggleClass 'hidden'

    {windowController} = KD.singletons

    windowController.addLayer @input
    @input.setFocus()

    @input.off  "ReceivedClickElsewhere"
    @input.once "ReceivedClickElsewhere", (event)=>
      return  if $(event.target).hasClass 'toggle'
      @emit "UserInputCancelled"
      @inputView.hide()
      @warning.hide()<commit_msg>MachineShareView: use autoCompleteController to fetch account list<commit_after>class ManageSharedView extends KDView

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

    options.cssClass = KD.utils.curry 'users-view', options.cssClass

    super options, data

    {@machine} = @getOptions()

    @_users = []

    @autoComplete = new KDAutoCompleteController
      name                : 'userController'
      placeholder         : 'Type a username...'
      itemClass           : ActivityAutoCompleteUserItemView
      itemDataPath        : 'profile.nickname'
      outputWrapper       : new KDView cssClass: 'hidden'
      listWrapperCssClass : 'private-message hidden'
      submitValuesAsText  : yes
      dataSource          : @bound 'fetchAccounts'

    @addSubView @inputView = @autoComplete.getView()
    @inputView.setClass 'input-view'


    @usersController    = new KDListViewController
      viewOptions       :
        type            : 'user'
        wrapper         : yes
        itemClass       : UserItem
        itemOptions     :
          machineId     : @machine._id

    @addSubView @userListView = @usersController.getView()
    @inputView.hide()

    @usersController.getListView()
      .on 'KickUserRequested', @bound 'kickUser'

    @addSubView @loader = new KDLoaderView
      cssClass          : 'in-progress'
      size              :
        width           : 14
        height          : 14
      loaderOptions     :
        color           : '#333333'
      showLoader        : yes

    @addSubView @warning = new KDCustomHTMLView
      cssClass          : 'warning hidden'
      click             : -> @hide()

    @listUsers()


  listUsers: ->

    @loader.show()

    @machine.jMachine.reviveUsers permanentOnly: yes, (err, users)=>
      warn err  if err?

      users ?= []
      @usersController.replaceAllItems users
      @userListView[if users.length > 0 then 'show' else 'hide']()

      @loader.hide()


  kickUser: (userItem)->

    @loader.show()
    @warning.hide()

    {profile:{nickname}} = userItem.getData()

    @machine.jMachine.shareWith
      target    : [nickname]
      permanent : yes
      asUser    : no
    , (err)=>

      @loader.hide()

      if err
        @warning.setTooltip title: err.message
        @warning.show()

      else
        if @usersController.itemsOrdered.length is 1
        then @listUsers()
        else @usersController.removeItem userItem


  toggleInput:->

    @inputView.toggleClass 'hidden'

    {windowController} = KD.singletons

    windowController.addLayer @inputView
    @inputView.setFocus()

    @inputView.off  "ReceivedClickElsewhere"
    @inputView.once "ReceivedClickElsewhere", (event)=>
      return  if $(event.target).hasClass 'toggle'
      @emit "UserInputCancelled"
      @inputView.hide()
      @warning.hide()


  fetchAccounts: ({inputValue}, callback) ->

    KD.singletons.search.searchAccounts inputValue
      .filter (it) => it.profile.nickname not in @_users
      .then callback
      .timeout 1e4
      .catch Promise.TimeoutError, callback.bind this, []
{'fraction_non_alphanumeric': 0.102349298852668, 'fraction_numerical': 0.004735020943361865, 'mean_word_length': 2.0664433277498606, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4314072', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1544, 'n_words': 347}