<commit_before>BrowserWindow = null # Defer require until actually used
RendererIpc = require 'ipc'

{$, View} = require 'atom'
_ = require 'underscore-plus'
TabView = require './tab-view'

module.exports =
class TabBarView extends View
  @content: ->
    @ul tabindex: -1, class: "list-inline tab-bar inset-panel"

  initialize: (@pane) ->
    @command 'tabs:close-tab', => @closeTab()
    @command 'tabs:close-other-tabs', => @closeOtherTabs()
    @command 'tabs:close-tabs-to-right', => @closeTabsToRight()
    @command 'tabs:close-saved-tabs', => @closeSavedTabs()
    @command 'tabs:close-all-tabs', => @closeAllTabs()

    @on 'dragstart', '.sortable', @onDragStart
    @on 'dragend', '.sortable', @onDragEnd
    @on 'dragleave', @onDragLeave
    @on 'dragover', @onDragOver
    @on 'drop', @onDrop

    @paneContainer = @pane.getContainer()
    @addTabForItem(item) for item in @pane.getItems()

    @subscribe @paneContainer, 'pane:removed', (e, pane) =>
      @unsubscribe() if pane is @pane

    @subscribe @pane, 'pane:item-added', (e, item, index) =>
      @addTabForItem(item, index)
      true

    @subscribe @pane, 'pane:item-moved', (e, item, index) =>
      @moveItemTabToIndex(item, index)
      true

    @subscribe @pane, 'pane:item-removed', (e, item) =>
      @removeTabForItem(item)
      true

    @subscribe @pane, 'pane:active-item-changed', =>
      @updateActiveTab()
      true

    @subscribe atom.config.observe 'tabs.tabScrolling', => @updateTabScrolling()
    @subscribe atom.config.observe 'tabs.tabScrollingThreshold', => @updateTabScrollingThreshold()

    @updateActiveTab()

    @on 'mousedown', '.tab', ({target, which, ctrlKey}) =>
      tab = $(target).closest('.tab')[0]
      if which is 3 or (which is 1 and ctrlKey is true)
        @find('.right-clicked').removeClass('right-clicked')
        tab.classList.add('right-clicked')
        false
      else if which is 2
        @pane.destroyItem(tab.item)
        false

    @on 'dblclick', ({target}) =>
      if target is @[0]
        @pane.trigger('application:new-file')
        false

    @on 'click', '.tab .close-icon', ({target}) =>
      tab = $(target).closest('.tab')[0]
      @pane.destroyItem(tab.item)
      false

    @on 'click', '.tab', ({target, which}) =>
      tab = $(target).closest('.tab')[0]
      if which is 1 and not target.classList.contains('close-icon')
        @pane.activateItem(tab.item)
        @pane.focus()
        false

    RendererIpc.on('tab:dropped', @onDropOnOtherWindow)

    @pane.prepend(this)

  unsubscribe: ->
    RendererIpc.removeListener('tab:dropped', @onDropOnOtherWindow)
    super

  addTabForItem: (item, index) ->
    tabView = new TabView()
    tabView.initialize(item)
    @insertTabAtIndex(tabView, index)

  moveItemTabToIndex: (item, index) ->
    tab = @tabForItem(item)
    tab.remove()
    @insertTabAtIndex(tab, index)

  insertTabAtIndex: (tab, index) ->
    followingTab = @tabAtIndex(index) if index?
    if followingTab
      $(tab).insertBefore(followingTab)
    else
      @append(tab)
    tab.updateTitle()

  removeTabForItem: (item) ->
    @tabForItem(item).destroy()
    tab.updateTitle() for tab in @getTabs()
    return

  getTabs: ->
    @children('.tab').toArray()

  tabAtIndex: (index) ->
    @children(".tab:eq(#{index})")[0]

  tabForItem: (item) ->
    _.detect @getTabs(), (tab) -> tab.item is item

  setActiveTab: (tabView) ->
    if tabView? and not tabView.classList.contains('active')
      @find(".tab.active").removeClass('active')
      tabView.classList.add('active')

  updateActiveTab: ->
    @setActiveTab(@tabForItem(@pane.activeItem))

  closeTab: (tab) ->
    tab ?= @children('.right-clicked')[0]
    @pane.destroyItem(tab.item)

  closeOtherTabs: ->
    tabs = @getTabs()
    active = @children('.right-clicked')[0]
    return unless active?
    @closeTab tab for tab in tabs when tab isnt active

  closeTabsToRight: ->
    tabs = @getTabs()
    active = @children('.right-clicked')[0]
    index = tabs.indexOf(active)
    return if index is -1
    @closeTab tab for tab, i in tabs when i > index

  closeSavedTabs: ->
    for tab in @getTabs()
      @closeTab(tab) unless tab.item.isModified?()

  closeAllTabs: ->
    @closeTab(tab) for tab in @getTabs()
<commit_msg>Use querySelector to find active tab<commit_after>BrowserWindow = null # Defer require until actually used
RendererIpc = require 'ipc'

{$, View} = require 'atom'
_ = require 'underscore-plus'
TabView = require './tab-view'

module.exports =
class TabBarView extends View
  @content: ->
    @ul tabindex: -1, class: "list-inline tab-bar inset-panel"

  initialize: (@pane) ->
    @command 'tabs:close-tab', => @closeTab()
    @command 'tabs:close-other-tabs', => @closeOtherTabs()
    @command 'tabs:close-tabs-to-right', => @closeTabsToRight()
    @command 'tabs:close-saved-tabs', => @closeSavedTabs()
    @command 'tabs:close-all-tabs', => @closeAllTabs()

    @on 'dragstart', '.sortable', @onDragStart
    @on 'dragend', '.sortable', @onDragEnd
    @on 'dragleave', @onDragLeave
    @on 'dragover', @onDragOver
    @on 'drop', @onDrop

    @paneContainer = @pane.getContainer()
    @addTabForItem(item) for item in @pane.getItems()

    @subscribe @paneContainer, 'pane:removed', (e, pane) =>
      @unsubscribe() if pane is @pane

    @subscribe @pane, 'pane:item-added', (e, item, index) =>
      @addTabForItem(item, index)
      true

    @subscribe @pane, 'pane:item-moved', (e, item, index) =>
      @moveItemTabToIndex(item, index)
      true

    @subscribe @pane, 'pane:item-removed', (e, item) =>
      @removeTabForItem(item)
      true

    @subscribe @pane, 'pane:active-item-changed', =>
      @updateActiveTab()
      true

    @subscribe atom.config.observe 'tabs.tabScrolling', => @updateTabScrolling()
    @subscribe atom.config.observe 'tabs.tabScrollingThreshold', => @updateTabScrollingThreshold()

    @updateActiveTab()

    @on 'mousedown', '.tab', ({target, which, ctrlKey}) =>
      tab = $(target).closest('.tab')[0]
      if which is 3 or (which is 1 and ctrlKey is true)
        @find('.right-clicked').removeClass('right-clicked')
        tab.classList.add('right-clicked')
        false
      else if which is 2
        @pane.destroyItem(tab.item)
        false

    @on 'dblclick', ({target}) =>
      if target is @[0]
        @pane.trigger('application:new-file')
        false

    @on 'click', '.tab .close-icon', ({target}) =>
      tab = $(target).closest('.tab')[0]
      @pane.destroyItem(tab.item)
      false

    @on 'click', '.tab', ({target, which}) =>
      tab = $(target).closest('.tab')[0]
      if which is 1 and not target.classList.contains('close-icon')
        @pane.activateItem(tab.item)
        @pane.focus()
        false

    RendererIpc.on('tab:dropped', @onDropOnOtherWindow)

    @pane.prepend(this)

  unsubscribe: ->
    RendererIpc.removeListener('tab:dropped', @onDropOnOtherWindow)
    super

  addTabForItem: (item, index) ->
    tabView = new TabView()
    tabView.initialize(item)
    @insertTabAtIndex(tabView, index)

  moveItemTabToIndex: (item, index) ->
    tab = @tabForItem(item)
    tab.remove()
    @insertTabAtIndex(tab, index)

  insertTabAtIndex: (tab, index) ->
    followingTab = @tabAtIndex(index) if index?
    if followingTab
      $(tab).insertBefore(followingTab)
    else
      @append(tab)
    tab.updateTitle()

  removeTabForItem: (item) ->
    @tabForItem(item).destroy()
    tab.updateTitle() for tab in @getTabs()
    return

  getTabs: ->
    @children('.tab').toArray()

  tabAtIndex: (index) ->
    @children(".tab:eq(#{index})")[0]

  tabForItem: (item) ->
    _.detect @getTabs(), (tab) -> tab.item is item

  setActiveTab: (tabView) ->
    if tabView? and not tabView.classList.contains('active')
      @element.querySelector('.tab.active')?.classList.remove('active')
      tabView.classList.add('active')

  updateActiveTab: ->
    @setActiveTab(@tabForItem(@pane.activeItem))

  closeTab: (tab) ->
    tab ?= @children('.right-clicked')[0]
    @pane.destroyItem(tab.item)

  closeOtherTabs: ->
    tabs = @getTabs()
    active = @children('.right-clicked')[0]
    return unless active?
    @closeTab tab for tab in tabs when tab isnt active

  closeTabsToRight: ->
    tabs = @getTabs()
    active = @children('.right-clicked')[0]
    index = tabs.indexOf(active)
    return if index is -1
    @closeTab tab for tab, i in tabs when i > index

  closeSavedTabs: ->
    for tab in @getTabs()
      @closeTab(tab) unless tab.item.isModified?()

  closeAllTabs: ->
    @closeTab(tab) for tab in @getTabs()

{'fraction_non_alphanumeric': 0.16006062019118675, 'fraction_numerical': 0.0032641641408253674, 'mean_word_length': 3.2177974434611603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '82089', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2880, 'n_words': 635}