<commit_before>class WebTermView extends KDView
  viewAppended: ->
    @container = new KDView
      cssClass : "console"
    @addSubView @container
    
    @sessionBox = new KDView
      cssClass: "kddialogview"
      position:
        top: 10
        left: 10
    @sessionBox.hide()
    @addSubView @sessionBox
    
    label = new KDLabelView
      title: "Select session:"
    @sessionBox.addSubView label

    @sessionList = new KDListView
      subItemClass: WebTermSessionItem
    @sessionBox.addSubView @sessionList
    
    label = new KDLabelView
      title: "Create session with name:"
    @sessionBox.addSubView label
    
    sessionNameInput = new KDInputView
      label: label
      defaultValue: (new Date).format "yyyy-mm-dd HH:MM:ss"
    @sessionBox.addSubView sessionNameInput
    
    createSessionButton = new KDButtonView
      title: "Create"
      callback: =>
        @sessionBox.hide()
        @terminal.createSession sessionNameInput.getValue()
        @setKeyView()
    @sessionBox.addSubView createSessionButton
    
    @listenWindowResize()
    
    $(window).bind "focus", =>
      @terminal.setFocused true
    
    $(window).bind "blur", =>
      @terminal.setFocused false
    
    KD.whoami().tellKite
      kiteName: 'webterm',
      method: 'connectionInitializationDummy'
    
    window.setTimeout =>
      @terminal = new WebTerm.Terminal @container.$(), (sessions) =>
        keys = Object.keys sessions
        keys.sort (a, b) ->
          if sessions[a] < sessions[b]
            -1
          else if sessions[a] > sessions[b]
            1
          else
            0
        @sessionList.empty()
        for key in keys
          @sessionList.addItem
            id: parseInt(key)
            name: sessions[key]
            mainView: this
        @sessionBox.show()
      
      KD.whoami().tellKite
        kiteName: 'webterm',
        method: 'createServer',
        withArgs: @terminal.clientInterface
      , (remote) =>
        @terminal.server = remote
        @terminal.showSessions()
    , 3000
  
  click: ->
    @setKeyView()
    
  keyDown: (event) ->
    @terminal.keyDown event
  
  keyPress: (event) ->
    @terminal.keyPress event
  
  keyUp: (event) ->
    @terminal.keyUp event
  
  _windowDidResize: (event) ->
    @terminal.windowDidResize()

class WebTermSessionItem extends KDListItemView
  constructor: (options = {},data) ->
    super options, data
  
  partial: (data) ->
    link = $(document.createElement("a"))
    link.text data.name
    link.attr "href", "#"
    link.bind "click", (event) =>
      data.mainView.sessionBox.hide()
      data.mainView.terminal.joinSession data.id
      event.preventDefault()
    link<commit_msg>key view focus/blur support for webterm<commit_after>class WebTermView extends KDView
  viewAppended: ->
    @container = new KDView
      cssClass : "console"
    @addSubView @container
    
    @sessionBox = new KDView
      cssClass: "kddialogview"
      position:
        top: 10
        left: 10
    @sessionBox.hide()
    @addSubView @sessionBox
    
    label = new KDLabelView
      title: "Select session:"
    @sessionBox.addSubView label

    @sessionList = new KDListView
      subItemClass: WebTermSessionItem
    @sessionBox.addSubView @sessionList
    
    label = new KDLabelView
      title: "Create session with name:"
    @sessionBox.addSubView label
    
    sessionNameInput = new KDInputView
      label: label
      defaultValue: (new Date).format "yyyy-mm-dd HH:MM:ss"
    @sessionBox.addSubView sessionNameInput
    
    createSessionButton = new KDButtonView
      title: "Create"
      callback: =>
        @sessionBox.hide()
        @terminal.createSession sessionNameInput.getValue()
        @setKeyView()
    @sessionBox.addSubView createSessionButton
    
    @listenWindowResize()
    
    @on "ReceivedClickElsewhere", =>
      @terminal.setFocused false
      @getSingleton('windowController').removeLayer @

    # $(window).bind "focus", =>
    #   @terminal.setFocused true
    
    # $(window).bind "blur", =>
    #   @terminal.setFocused false
    
    KD.whoami().tellKite
      kiteName: 'webterm',
      method: 'connectionInitializationDummy'
    
    window.setTimeout =>
      @terminal = new WebTerm.Terminal @container.$(), (sessions) =>
        keys = Object.keys sessions
        keys.sort (a, b) ->
          if sessions[a] < sessions[b]
            -1
          else if sessions[a] > sessions[b]
            1
          else
            0
        @sessionList.empty()
        for key in keys
          @sessionList.addItem
            id: parseInt(key)
            name: sessions[key]
            mainView: this
        @sessionBox.show()

      KD.whoami().tellKite
        kiteName: 'webterm',
        method: 'createServer',
        withArgs: @terminal.clientInterface
      , (remote) =>
        @terminal.server = remote
        @terminal.showSessions()
    , 3000
  
  setKeyView:->
    @getSingleton('windowController').addLayer @
    # checking existence of terminal because of the initial timeout
    # this may run before it is created
    @terminal.setFocused true if @terminal
    super

  click: ->
    @setKeyView()
    
  keyDown: (event) ->
    @terminal.keyDown event
  
  keyPress: (event) ->
    @terminal.keyPress event
  
  keyUp: (event) ->
    @terminal.keyUp event
  
  _windowDidResize: (event) ->
    @terminal.windowDidResize()

class WebTermSessionItem extends KDListItemView
  constructor: (options = {},data) ->
    super options, data
  
  partial: (data) ->
    link = $(document.createElement("a"))
    link.text data.name
    link.attr "href", "#"
    link.bind "click", (event) =>
      data.mainView.sessionBox.hide()
      data.mainView.terminal.joinSession data.id
      event.preventDefault()
    link
{'fraction_non_alphanumeric': 0.105, 'fraction_numerical': 0.003793103448275862, 'mean_word_length': 2.2718556119571347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '5561637', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1718, 'n_words': 390}