<commit_before>app = FactlinkApp

class window.ChannelsController extends Backbone.Factlink.BaseController

  routes: ['getChannelFacts', 'getChannelFact', 'getChannelActivities', 'getChannelFactForActivity']

  onShow:   -> @channel_views = new Backbone.Factlink.DetachedViewCache
  onClose:  -> @channel_views.cleanup()
  onAction: -> @unbindFrom @permalink_event if @permalink_event?

  loadChannel: (username, channel_id, callback) ->
    channel = Channels.get(channel_id)

    mp_track("mp_page_view", {mp_page: window.location.href})

    withChannel = (channel) ->
      channel.set(new_facts: false)
      callback(channel)

    if (channel)
      withChannel(channel)
    else
      channel = new Channel({created_by:{username: username}, id: channel_id})
      channel.fetch
        success: (model, response) -> withChannel(model)

  commonChannelViews: (channel) ->
    window.currentChannel = channel

    @showRelatedChannels channel
    @showUserProfile channel.user()
    @showChannelSideBar window.Channels, channel, channel.user()

  showRelatedChannels: (channel)->
    if channel.get('is_normal')
      app.leftBottomRegion.show(new RelatedChannelsView(model: channel))
    else
      app.leftBottomRegion.close()

  showChannelSideBar: (channels, currentChannel, user)->
    window.Channels.setUsernameAndRefresh(user.get('username'))
    channelCollectionView = new ChannelsView(collection: channels, model: user)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActiveChannel(currentChannel)

  showUserProfile: (user)->
    unless user.is_current_user()
      userView = new UserView(model: user)
      app.leftTopRegion.show(userView)
    else
      app.leftTopRegion.close()

  getChannelFacts: (username, channel_id) ->
    app.mainRegion.show(@channel_views)

    @loadChannel username, channel_id, (channel) =>
      @commonChannelViews(channel)
      @makePermalinkEvent(channel.url())

      @restoreChannelView channel_id, => new ChannelView(model: channel)

      @channel_views.clear()

  getChannelActivities: (username, channel_id) ->
    app.mainRegion.show(@channel_views)

    @loadChannel username, channel_id, (channel) =>
      @commonChannelViews(channel)
      @makePermalinkEvent(channel.url() + '/activities')

      @restoreChannelView channel_id, =>
        activities = new ChannelActivities([],{ channel: channel })
        new ChannelActivitiesView(model: channel, collection: activities)

      @channel_views.clear()

  getChannelFactForActivity: (username, channel_id, fact_id) ->
    @getChannelFact(username, channel_id, fact_id, true)

  getChannelFact: (username, channel_id, fact_id, for_stream=false) ->
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)

    channel = `undefined`
    fact = `undefined`

    with_both = =>
      return_to_url = channel.url()
      return_to_url = return_to_url + "/activities" if for_stream

      title_view = new ExtendedFactTitleView(
                                      model: fact,
                                      return_to_url: return_to_url,
                                      return_to_text: channel.get('title') )
      @main.titleRegion.show( title_view )

    callback_with_both = _.after 2, with_both

    @loadChannel username, channel_id, ( ch ) =>
      channel = ch
      @commonChannelViews( ch )
      callback_with_both()

    fact = new Fact(id: fact_id)
    fact.fetch
      success: (model, response) =>
        window.efv = new ExtendedFactView(model: model)
        @main.contentRegion.show(efv)
        callback_with_both()

  restoreChannelView: (channel_id, new_callback) ->
    view = @channel_views.switchCacheView(channel_id)
    $('body').scrollTo(@lastChannelStatus.scrollTop) if view == @lastChannelStatus?.view
    delete @lastChannelStatus
    @channel_views.renderCacheView(channel_id, new_callback()) if not view?

  makePermalinkEvent: (baseUrl) ->
    @permalink_event = @bindTo FactlinkApp.vent, 'factlink_permalink_clicked', (e, fact) =>
      @lastChannelStatus =
        view: @channel_views.currentView()
        scrollTop: $('body').scrollTop()

      navigate_to = baseUrl + "/facts/" + fact.id
      Backbone.history.navigate navigate_to, true

      e.preventDefault()
      e.stopPropagation()<commit_msg>Clear cached views in a sensible spot<commit_after>app = FactlinkApp

class window.ChannelsController extends Backbone.Factlink.BaseController

  routes: ['getChannelFacts', 'getChannelFact', 'getChannelActivities', 'getChannelFactForActivity']

  onShow:   -> @channel_views = new Backbone.Factlink.DetachedViewCache
  onClose:  -> @channel_views.cleanup()
  onAction: -> @unbindFrom @permalink_event if @permalink_event?

  loadChannel: (username, channel_id, callback) ->
    channel = Channels.get(channel_id)

    mp_track("mp_page_view", {mp_page: window.location.href})

    withChannel = (channel) ->
      channel.set(new_facts: false)
      callback(channel)

    if (channel)
      withChannel(channel)
    else
      channel = new Channel({created_by:{username: username}, id: channel_id})
      channel.fetch
        success: (model, response) -> withChannel(model)

  commonChannelViews: (channel) ->
    window.currentChannel = channel

    @showRelatedChannels channel
    @showUserProfile channel.user()
    @showChannelSideBar window.Channels, channel, channel.user()

  showRelatedChannels: (channel)->
    if channel.get('is_normal')
      app.leftBottomRegion.show(new RelatedChannelsView(model: channel))
    else
      app.leftBottomRegion.close()

  showChannelSideBar: (channels, currentChannel, user)->
    window.Channels.setUsernameAndRefresh(user.get('username'))
    channelCollectionView = new ChannelsView(collection: channels, model: user)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActiveChannel(currentChannel)

  showUserProfile: (user)->
    unless user.is_current_user()
      userView = new UserView(model: user)
      app.leftTopRegion.show(userView)
    else
      app.leftTopRegion.close()

  getChannelFacts: (username, channel_id) ->
    app.mainRegion.show(@channel_views)

    @loadChannel username, channel_id, (channel) =>
      @commonChannelViews(channel)
      @makePermalinkEvent(channel.url())

      @restoreChannelView channel_id, => new ChannelView(model: channel)

  getChannelActivities: (username, channel_id) ->
    app.mainRegion.show(@channel_views)

    @loadChannel username, channel_id, (channel) =>
      @commonChannelViews(channel)
      @makePermalinkEvent(channel.url() + '/activities')

      @restoreChannelView channel_id, =>
        activities = new ChannelActivities([],{ channel: channel })
        new ChannelActivitiesView(model: channel, collection: activities)

  getChannelFactForActivity: (username, channel_id, fact_id) ->
    @getChannelFact(username, channel_id, fact_id, true)

  getChannelFact: (username, channel_id, fact_id, for_stream=false) ->
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)

    channel = `undefined`
    fact = `undefined`

    with_both = =>
      return_to_url = channel.url()
      return_to_url = return_to_url + "/activities" if for_stream

      title_view = new ExtendedFactTitleView(
                                      model: fact,
                                      return_to_url: return_to_url,
                                      return_to_text: channel.get('title') )
      @main.titleRegion.show( title_view )

    callback_with_both = _.after 2, with_both

    @loadChannel username, channel_id, ( ch ) =>
      channel = ch
      @commonChannelViews( ch )
      callback_with_both()

    fact = new Fact(id: fact_id)
    fact.fetch
      success: (model, response) =>
        window.efv = new ExtendedFactView(model: model)
        @main.contentRegion.show(efv)
        callback_with_both()

  restoreChannelView: (channel_id, new_callback) ->
    view = @channel_views.switchCacheView(channel_id)
    $('body').scrollTo(@lastChannelStatus.scrollTop) if view == @lastChannelStatus?.view
    delete @lastChannelStatus
    @channel_views.renderCacheView(channel_id, new_callback()) if not view?
    @channel_views.clear()

  makePermalinkEvent: (baseUrl) ->
    @permalink_event = @bindTo FactlinkApp.vent, 'factlink_permalink_clicked', (e, fact) =>
      @lastChannelStatus =
        view: @channel_views.currentView()
        scrollTop: $('body').scrollTop()

      navigate_to = baseUrl + "/facts/" + fact.id
      Backbone.history.navigate navigate_to, true

      e.preventDefault()
      e.stopPropagation()
{'fraction_non_alphanumeric': 0.1087536231884058, 'fraction_numerical': 0.00023188405797101448, 'mean_word_length': 3.5423907319641916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820278', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2499, 'n_words': 486}