<commit_before>debug = require('debug')('sidebar:controller')
kd = require 'kd'
EnvironmentFlux = require 'app/flux/environment'
remote = require 'app/remote'

module.exports = class SidebarController extends kd.Controller

  constructor: (options = {}) ->

    super options

    @invitedId = null
    @leavingId = null

    @selected =
      stackId: null
      machineId: null

    @visibility =
      stacks: {}
      drafts: {}

    @managed = {}

    @bindNotificationHandlers()
    @setStateFromStorage()


  bindNotificationHandlers: ->

    { notificationController, computeController } = kd.singletons

    computeController.ready =>
      notificationController
        .on 'MachineShare:Added', @bound 'onAddSharedMachine'
        .on 'MachineShare:Removed', @bound 'onRemoveSharedMachine'


  setStateFromStorage: ->

    { computeController } = kd.singletons

    computeController.ready =>
      for machine in computeController.storage.get('machines')
        if not (machine.isMine() or machine.isApproved())
          return @setInvited machine.getId()


  onAddSharedMachine: ({ machine }) ->

    debug 'onAddSharedMachine', { machine }

    @setInvited machine.getId()


  onRemoveSharedMachine: ({ machine }) ->

    debug 'onRemoveSharedMachine', { machine }

    @setInvited null


  subscribeChange: (handler) ->

    changeHandler = => handler @getState()

    @on 'change', changeHandler

    # call the handler for the first time
    kd.utils.defer changeHandler

    return {
      cancel: => @off 'change', changeHandler
    }


  setSelected: (type, id) ->

    @selected[type] = id
    @emit 'change'


  setInvited: (machineId) ->

    @invitedId = machineId
    @emit 'change'


  setLeaving: (machineId) ->

    @leavingId = machineId
    @emit 'change'


  loadVisibilityFilters: ->

    fetchVisibility().then (filters) => @setVisibility filters


  setVisibility: (type, id, state) ->

    fetchVisibility()
      .then (filters) =>
        filters[type][id] = state
        return filters

    new Promise (resolve) ->
      appStorageController.storage('Sidebar')
        .fetchValue 'visibility', (filters) =>
          @setVisibilityFilters filters
          resolve filters


  addManaged: (id) ->

    debug 'add managed vm', { id }

    @managed[id] = id
    @emit 'change'


  removeManaged: (id) ->

    debug 'remove managed vm', { id }

    delete @managed[id]
    @emit 'change'


  getManaged: -><commit_msg>sidebar/ctrl: start tracking visibility filter states<commit_after>debug = require('debug')('sidebar:controller')
kd = require 'kd'
EnvironmentFlux = require 'app/flux/environment'
remote = require 'app/remote'

module.exports = class SidebarController extends kd.Controller

  @FilterType = {
    Hidden: 'hidden'
    Visible: 'visible'
  }

  constructor: (options = {}) ->

    super options

    @invitedId = null
    @leavingId = null

    @selected =
      stackId: null
      machineId: null

    @visibility =
      stacks: {}
      drafts: {}

    @managed = {}

    # kd.singletons.mainController.ready =>
    @bindNotificationHandlers()
    @setStateFromStorage()
    @loadVisibilityFilters()


  bindNotificationHandlers: ->

    { notificationController, computeController } = kd.singletons

    computeController.ready =>
      notificationController
        .on 'MachineShare:Added', @bound 'onAddSharedMachine'
        .on 'MachineShare:Removed', @bound 'onRemoveSharedMachine'


  setStateFromStorage: ->

    { computeController } = kd.singletons

    computeController.ready =>
      for machine in computeController.storage.get('machines')
        if not (machine.isMine() or machine.isApproved())
          return @setInvited machine.getId()


  onAddSharedMachine: ({ machine }) ->

    debug 'onAddSharedMachine', { machine }

    @setInvited machine.getId()


  onRemoveSharedMachine: ({ machine }) ->

    debug 'onRemoveSharedMachine', { machine }

    @setInvited null


  subscribeChange: (handler) ->

    changeHandler = => handler @getState()

    @on 'change', changeHandler

    # call the handler for the first time
    kd.utils.defer changeHandler

    return {
      cancel: => @off 'change', changeHandler
    }


  setSelected: (type, id) ->

    @selected[type] = id
    @emit 'change'


  setInvited: (machineId) ->

    @invitedId = machineId
    @emit 'change'


  setLeaving: (machineId) ->

    @leavingId = machineId
    @emit 'change'


  loadVisibilityFilters: ->

    fetchVisibility().then (filters) =>
      if filters
        @visibility = filters
        @emit 'change'


  saveVisibility: (type, id, state) ->

    fetchVisibility()
      .then (filters) ->
        filters[type][id] = state
        return filters

      .then (filters) ->
        return saveVisibility(filters)

      .then (filters) =>
        @visibility = filters
        return filters

      .then (filters) =>
        @emit 'change'
        return filters


  makeVisible: (type, id) ->

    debug 'make visible', { type, id }

    { FilterType } = SidebarController

    @saveVisibility type, id, FilterType.Visible


  makeHidden: (type, id) ->

    debug 'make hidden', { type, id }

    { FilterType } = SidebarController

    @saveVisibility type, id, FilterType.Hidden


  isVisible: (type, id) ->

    { FilterType } = SidebarController

    isVisible = if visibility = @visibility[type]?[id]?
    then visibility is SidebarController.FilterType
    else yes

    debug 'is visible', isVisible

    return isVisible


  addManaged: (id) ->

    debug 'add managed vm', { id }

    @managed[id] = id
    @emit 'change'


  removeManaged: (id) ->

    debug 'remove managed vm', { id }

    delete @managed[id]
    @emit 'change'


  getManaged: ->
{'fraction_non_alphanumeric': 0.11655052264808362, 'fraction_numerical': 0.0, 'mean_word_length': 2.7919418758256276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 52, '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': '4110158', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1615, 'n_words': 410}