<commit_before>htmlencode = require 'htmlencode'
globals = require 'globals'
groupifyLink = require '../util/groupifyLink'
nick = require '../util/nick'
kd = require 'kd'
KDCustomHTMLView = kd.CustomHTMLView
KDProgressBarView = kd.ProgressBarView
JView = require '../jview'
Machine = require '../providers/machine'


module.exports = class NavigationMachineItem extends JView

  {Running, Stopped} = Machine.State

  stateClasses  = ''
  stateClasses += "#{state.toLowerCase()} " for state in Object.keys Machine.State


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

    machine      = data
    @alias       = machine.slug or machine.label
    ideRoute     = "/IDE/#{@alias}/my-workspace"
    machineOwner = machine.getOwner()
    isMyMachine  = machine.isMine()
    channelId    = ''

    if not isMyMachine and globals.userWorkspaces
      globals.userWorkspaces.forEach (ws) ->
        return if channelId

        if ws.machineUId is machine.uid and ws.channelId
          channelId = ws.channelId

      ideRoute = if channelId
        "/IDE/#{channelId}"
      else
        "/IDE/#{machine.uid}/my-workspace"

    options.tagName    = 'a'
    options.cssClass   = "vm #{machine.status.state.toLowerCase()} #{machine.provider}"
    options.attributes =
      href             : groupifyLink ideRoute
      title            : "Open IDE for #{@alias}"

    unless machineOwner is nick()
      options.attributes.title += " (shared by @#{htmlencode.htmlDecode machineOwner})"

    super options, data

    @machine   = @getData()

    labelPartial = machine.label or @alias

    unless isMyMachine
      labelPartial = """
        #{labelPartial}
        <cite class='shared-by'>
          (@#{htmlencode.htmlDecode machineOwner})
        </cite>
      """

    @label     = new KDCustomHTMLView
      partial  : labelPartial

    @progress  = new KDProgressBarView
      cssClass : 'hidden'

    if @getData().isMine()
      @settingsIcon = new KDCustomHTMLView
        tagName     : 'span'
        click       : @bound 'handleMachineSettingsClick'
    else
      @settingsIcon = new KDCustomHTMLView cssClass: 'hidden'

    kd.singletons.computeController

      .on "public-#{@machine._id}", (event)=>
        @handleMachineEvent event

      # These are updating machine data on this instance indivudally
      # but since we have more data to update, I'm updating all machines
      # for now.
      #
      # .on "revive-#{@machine._id}", (machine)=>
      #   @machine = machine

      #   @label.updatePartial @machine.label
      #   @alias   = @machine.slug or @label
      #   newPath  = groupifyLink "/IDE/#{@alias}/my-workspace"

      #   @setAttributes
      #     href   : newPath
      #     title  : "Open IDE for #{@alias}"


  handleMachineSettingsClick: (event) ->

    machine    = @getData()
    { status } = machine
    { Building, Running } = Machine.State

    kd.utils.stopDOMEvent event

    if status?.state is Running
      kd.singletons.mainView.openMachineModal machine, this
    else return


  handleMachineEvent: (event) ->

    {percentage, status} = event

    # switch status
    #   when Machine.State.Terminated then @destroy()
    #  else @setState status

    @setState status

    if percentage?<commit_msg>NavigationItem: Handle routes for collaboration and shared VMs.<commit_after>htmlencode = require 'htmlencode'
globals = require 'globals'
groupifyLink = require '../util/groupifyLink'
nick = require '../util/nick'
kd = require 'kd'
KDCustomHTMLView = kd.CustomHTMLView
KDProgressBarView = kd.ProgressBarView
JView = require '../jview'
Machine = require '../providers/machine'


module.exports = class NavigationMachineItem extends JView

  {Running, Stopped} = Machine.State

  stateClasses  = ''
  stateClasses += "#{state.toLowerCase()} " for state in Object.keys Machine.State


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

    { machine, workspaces } = data

    @alias             = machine.slug or machine.label
    machineOwner       = machine.getOwner()
    isMyMachine        = machine.isMine()
    myMachineRoute     = "/IDE/#{@alias}/my-workspace"
    collabMachineRoute = "/IDE/#{workspaces.first.channelId}"
    sharedMachineRoute = "/IDE/#{machine.uid}/my-workspace"

    machineRoute = if machine.isCollaborationMachine then collabMachineRoute
    else if machine.isSharedMachine then sharedMachineRoute else myMachineRoute

    options.tagName    = 'a'
    options.cssClass   = "vm #{machine.status.state.toLowerCase()} #{machine.provider}"
    options.attributes =
      href             : groupifyLink machineRoute
      title            : "Open IDE for #{@alias}"

    unless isMyMachine
      options.attributes.title += " (shared by @#{htmlencode.htmlDecode machineOwner})"

    super options, data

    { @machine } = @getData()
    labelPartial = machine.label or @alias

    unless isMyMachine
      labelPartial = """
        #{labelPartial}
        <cite class='shared-by'>
          (@#{htmlencode.htmlDecode machineOwner})
        </cite>
      """

    @label     = new KDCustomHTMLView
      partial  : labelPartial

    @progress  = new KDProgressBarView
      cssClass : 'hidden'

    if @machine.isMine()
      @settingsIcon = new KDCustomHTMLView
        tagName     : 'span'
        click       : @bound 'handleMachineSettingsClick'
    else
      @settingsIcon = new KDCustomHTMLView cssClass: 'hidden'

    kd.singletons.computeController

      .on "public-#{@machine._id}", (event)=>
        @handleMachineEvent event

      # These are updating machine data on this instance indivudally
      # but since we have more data to update, I'm updating all machines
      # for now.
      #
      # .on "revive-#{@machine._id}", (machine)=>
      #   @machine = machine

      #   @label.updatePartial @machine.label
      #   @alias   = @machine.slug or @label
      #   newPath  = groupifyLink "/IDE/#{@alias}/my-workspace"

      #   @setAttributes
      #     href   : newPath
      #     title  : "Open IDE for #{@alias}"


  handleMachineSettingsClick: (event) ->

    { status } = @machine
    { Building, Running } = Machine.State

    kd.utils.stopDOMEvent event

    if status?.state is Running
      kd.singletons.mainView.openMachineModal @machine, this
    else return


  handleMachineEvent: (event) ->

    {percentage, status} = event

    # switch status
    #   when Machine.State.Terminated then @destroy()
    #  else @setState status

    @setState status

    if percentage?
{'fraction_non_alphanumeric': 0.11032138442521632, 'fraction_numerical': 0.0, 'mean_word_length': 2.5941143808995, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '5896396', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1848, 'n_words': 470}