<commit_before>class Kandan.Helpers.Channels

  @options:
    autoScrollThreshold: 0.90
    maxActivities: 5

  @replaceCreateButton: ()->
    $tabNav = $(".create_channel").parent().parent()
    $createButton = $(".create_channel").parent().html()
    $tabNav.find(".create_channel").parent().remove()
    $tabNav.append("<li>"+$createButton+"</li>")

  @pastAutoScrollThreshold: (channelId)->
    currentPosition     = @currentScrollPosition channelId
    totalHeight         = $(document).height() - $(window).height()
    scrollPercentage    = (currentPosition) / (totalHeight)
    scrollPercentage > @options.autoScrollThreshold

  @scrollToLatestMessage: (channelId)->
    console.log("scrolling to last message");
    $(document).scrollTop($(document).height()+9000)

  @currentScrollPosition: (channelId)->
    console.log("current scroll position");
    $(document).scrollTop()

  @channel_activities_el: (channelId)->
    $("#channel-activities-#{channelId}")

  @channel_pagination_el: (channelId)->
    $("#channels-#{channelId} .pagination")

  @selected_tab: ()->
    $('#channels').tabs('option', 'selected')

  @getActiveChannelId: ()->
    if $(document).data('active_channel_id') == undefined
      return $("#channels .ui-tabs-panel")
        .eq(@selected_tab())
        .data('channel_id')
    else
      return $(document).data('active_channel_id')


  @confirmDeletion: ()->
    reply = confirm("Really delete the channel?")
    return reply


  @flushActivities: (channelId)->
    $channelActivities = $("#channel-activities-#{channelId}")
    if $channelActivities.children().length == @options.maxActivities + 1
      oldest = $channelActivities.children().first().data("activity_id")
      $channelActivities.children().first().remove()
      $channelActivities.prev().data("oldest", oldest)


  @confirmAndDeleteChannel: (channel, tabIndex)->
    return false if @confirmDeletion() == false
    channel.destroy({success: ()=>
      $("#kandan").tabs("remove", tabIndex)
    })


  @getChannelIdByTabIndex: (tabIndex)->
    console.log("getChannelIdByTabIndex", tabIndex)
    console.log($("#channels .ui-tabs-panel"))
    console.log($("#channels .ui-tabs-panel").eq(tabIndex))
    console.log($("#channels .ui-tabs-panel").eq(tabIndex).data('channel_id'))
    $("#channels .ui-tabs-panel")
      .eq(tabIndex)
      .data('channel_id')

  @getTabIndexByChannelId: (channelId)->
    $("#channels-#{channelId}").prevAll("div").length

  @deleteChannelById: (channelId)->
    if @channelExists(channelId)
      tabIndex = @getTabIndexByChannelId(channelId)
      @deleteChannelByTabIndex(tabIndex, true)

  @deleteChannelByTabIndex: (tabIndex, deleted)->
    # gotcha, 0 index being passed a natural index from the html views
    #tabIndex = tabIndex-1
    console.log("deleteChannelByTabIndex", tabIndex);
    deleted = deleted || false
    channelId = @getChannelIdByTabIndex(tabIndex)
    if channelId == 'undefined'
      throw "NO CHANNEL ID";
    console.log("deleteing channelId", channelId);
    channel = new Kandan.Models.Channel({id: channelId})
    return @confirmAndDeleteChannel(channel, tabIndex) if not deleted
    console.log "TAB INDEX", tabIndex
    $("#kandan").tabs("remove", tabIndex)


  @channelExists: (channelId)->
    return true if $("#channels-#{channelId}").length > 0
    false


  @createChannelArea: (channel)->
    channelArea = "#channels-#{channel.get('id')}"
    totalTabs = $("#channels").tabs("length")

    $("#channels").tabs('add', channelArea, "#{channel.get("name")}", totalTabs)
    Kandan.Helpers.Channels.replaceCreateButton()
    view = new Kandan.Views.ChannelPane({channel: channel})
    view.render $(channelArea)<commit_msg>deletes appear to be restored<commit_after>class Kandan.Helpers.Channels

  @options:
    autoScrollThreshold: 0.90
    maxActivities: 5

  @replaceCreateButton: ()->
    $tabNav = $(".create_channel").parent().parent()
    $createButton = $(".create_channel").parent().html()
    $tabNav.find(".create_channel").parent().remove()
    $tabNav.append("<li>"+$createButton+"</li>")

  @pastAutoScrollThreshold: (channelId)->
    currentPosition     = @currentScrollPosition channelId
    totalHeight         = $(document).height() - $(window).height()
    scrollPercentage    = (currentPosition) / (totalHeight)
    scrollPercentage > @options.autoScrollThreshold

  @scrollToLatestMessage: (channelId)->
    console.log("scrolling to last message");
    $(document).scrollTop($(document).height()+9000)

  @currentScrollPosition: (channelId)->
    console.log("current scroll position");
    $(document).scrollTop()

  @channel_activities_el: (channelId)->
    $("#channel-activities-#{channelId}")

  @channel_pagination_el: (channelId)->
    $("#channels-#{channelId} .pagination")

  @selected_tab: ()->
    $('#channels').tabs('option', 'selected')

  @getActiveChannelId: ()->
    if $(document).data('active_channel_id') == undefined
      return $("#channels .ui-tabs-panel")
        .eq(@selected_tab())
        .data('channel_id')
    else
      return $(document).data('active_channel_id')


  @confirmDeletion: ()->
    reply = confirm("Really delete the channel?")
    return reply


  @flushActivities: (channelId)->
    $channelActivities = $("#channel-activities-#{channelId}")
    if $channelActivities.children().length == @options.maxActivities + 1
      oldest = $channelActivities.children().first().data("activity_id")
      $channelActivities.children().first().remove()
      $channelActivities.prev().data("oldest", oldest)


  @confirmAndDeleteChannel: (channel, tabIndex)->
    return false if @confirmDeletion() == false
    channel.destroy({success: ()=>
      $("#kandan").tabs("remove", tabIndex)
    })


  @getChannelIdByTabIndex: (tabIndex)->
    console.log("getChannelIdByTabIndex", tabIndex)
    console.log($("#channels .ui-tabs-panel"))
    console.log($("#channels .ui-tabs-panel").eq(tabIndex))
    console.log($("#channels .ui-tabs-panel").eq(tabIndex).data('channel_id'))
    $("#channels .ui-tabs-panel")
      .eq(tabIndex)
      .data('channel_id')

  @getTabIndexByChannelId: (channelId)->
    $("#channels-#{channelId}").prevAll("div").length

  @deleteChannelById: (channelId)->
    if @channelExists(channelId)
      tabIndex = @getTabIndexByChannelId(channelId)
      @deleteChannelByTabIndex(tabIndex, true)

  @deleteChannelByTabIndex: (tabIndex, deleted)->
    # gotcha, 0 index being passed a natural index from the html views
    deleted = deleted || false
    channelId = @getChannelIdByTabIndex(tabIndex)
    if channelId == 'undefined'
      throw "NO CHANNEL ID";
    channel = new Kandan.Models.Channel({id: channelId})
    return @confirmAndDeleteChannel(channel, tabIndex) if not deleted


  @channelExists: (channelId)->
    return true if $("#channels-#{channelId}").length > 0
    false


  @createChannelArea: (channel)->
    channelArea = "#channels-#{channel.get('id')}"
    totalTabs = $("#channels").tabs("length")

    $("#channels").tabs('add', channelArea, "#{channel.get("name")}", totalTabs)
    Kandan.Helpers.Channels.replaceCreateButton()
    view = new Kandan.Views.ChannelPane({channel: channel})
    view.render $(channelArea)
{'fraction_non_alphanumeric': 0.16911764705882354, 'fraction_numerical': 0.003190899001109878, 'mean_word_length': 4.239098837209302, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, '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': '4552594', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2146, 'n_words': 378}