<commit_before>{View} = require 'space-pen'
Pane = require 'pane'
$ = require 'jquery'
telepath = require 'telepath'

# Private: Manages the list of panes within a {RootView}
module.exports =
class PaneContainer extends View
  registerDeserializer(this)

  ### Internal ###
  @acceptsDocuments: true

  @deserialize: (state) ->
    container = new PaneContainer(state)
    container.removeEmptyPanes()
    container

  @content: ->
    @div id: 'panes'

  initialize: (state) ->
    @destroyedItemStates = []

    if state instanceof telepath.Document
      @state = state
      @setRoot(deserialize(@state.get('root')))
    else
      @state = site.createDocument(deserializer: 'PaneContainer')

    @subscribe @state, 'changed', ({key, newValue, site}) =>
      return if site is @state.site.id
      if key is 'root'
        if newValue?
          @setRoot(deserialize(newValue))
        else
          @setRoot(null)

    @on 'pane:attached', (event, pane) =>
      @triggerActiveItemChange() if @getActivePane() is pane

    @on 'pane:removed', (event, pane) =>
      @triggerActiveItemChange() unless @getActivePane()?

    @on 'pane:became-active', =>
      @triggerActiveItemChange()

    @on 'pane:active-item-changed', (event, item) =>
      @triggerActiveItemChange() if @getActivePaneItem() is item

  triggerActiveItemChange: ->
    @trigger 'pane-container:active-pane-item-changed', [@getActivePaneItem()]

  serialize: ->
    state = @state.clone()
    state.set('root', @getRoot()?.serialize())
    state

  getState: -> @state

  ### Public ###

  focusNextPane: ->
    panes = @getPanes()
    if panes.length > 1
      currentIndex = panes.indexOf(@getFocusedPane())
      nextIndex = (currentIndex + 1) % panes.length
      panes[nextIndex].focus()
      true
    else
      false

  focusPreviousPane: ->
    panes = @getPanes()
    if panes.length > 1
      currentIndex = panes.indexOf(@getFocusedPane())
      previousIndex = currentIndex - 1
      previousIndex = panes.length - 1 if previousIndex < 0<commit_msg>Use `subscribe` instead of `on`<commit_after>{View} = require 'space-pen'
Pane = require 'pane'
$ = require 'jquery'
telepath = require 'telepath'

# Private: Manages the list of panes within a {RootView}
module.exports =
class PaneContainer extends View
  registerDeserializer(this)

  ### Internal ###
  @acceptsDocuments: true

  @deserialize: (state) ->
    container = new PaneContainer(state)
    container.removeEmptyPanes()
    container

  @content: ->
    @div id: 'panes'

  initialize: (state) ->
    @destroyedItemStates = []

    if state instanceof telepath.Document
      @state = state
      @setRoot(deserialize(@state.get('root')))
    else
      @state = site.createDocument(deserializer: 'PaneContainer')

    @subscribe @state, 'changed', ({key, newValue, site}) =>
      return if site is @state.site.id
      if key is 'root'
        if newValue?
          @setRoot(deserialize(newValue))
        else
          @setRoot(null)

    @subscribe this, 'pane:attached', (event, pane) =>
      @triggerActiveItemChange() if @getActivePane() is pane

    @subscribe this, 'pane:removed', (event, pane) =>
      @triggerActiveItemChange() unless @getActivePane()?

    @subscribe this, 'pane:became-active', =>
      @triggerActiveItemChange()

    @subscribe this, 'pane:active-item-changed', (event, item) =>
      @triggerActiveItemChange() if @getActivePaneItem() is item

  triggerActiveItemChange: ->
    @trigger 'pane-container:active-pane-item-changed', [@getActivePaneItem()]

  serialize: ->
    state = @state.clone()
    state.set('root', @getRoot()?.serialize())
    state

  getState: -> @state

  ### Public ###

  focusNextPane: ->
    panes = @getPanes()
    if panes.length > 1
      currentIndex = panes.indexOf(@getFocusedPane())
      nextIndex = (currentIndex + 1) % panes.length
      panes[nextIndex].focus()
      true
    else
      false

  focusPreviousPane: ->
    panes = @getPanes()
    if panes.length > 1
      currentIndex = panes.indexOf(@getFocusedPane())
      previousIndex = currentIndex - 1
      previousIndex = panes.length - 1 if previousIndex < 0
{'fraction_non_alphanumeric': 0.1336227689339122, 'fraction_numerical': 0.002894356005788712, 'mean_word_length': 3.2016210739614994, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '3594738', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1268, 'n_words': 303}