<commit_before>app = FactlinkApp

class window.ProfileController extends Backbone.Factlink.BaseController

  routes: ['showProfile', 'showNotificationSettings', 'showFact']

  profile_options: (username) ->
    title: 'About ' + username
    active_tab: 'show'
    mainRegion: (user) =>
      @makePermalinkEvent( user.get('username') )

      new ProfileView
        model: user
        collection: window.Channels.orderedByAuthority()
        created_facts_view: @getFactsView user.created_facts()

  notification_options: (username)->
    title: 'Notification Settings'
    active_tab: 'notification-settings'
    mainRegion: (user) ->
      new NotificationSettingsView model: user

  # ACTIONS
  showProfile: (username) -> @showPage username, @profile_options(username)
  showNotificationSettings: (username) -> @showPage username, @notification_options(username)

  showFact: (slug, fact_id)->
    app.closeAllContentRegions()
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)

    fact = new Fact(id: fact_id)
    fact.fetch
      success: (model, response) => @withFact(model)


  # HELPERS
  showPage: (username, options) ->
    app.leftBottomRegion.close()
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)
    @getUser username,
      onInit: (user) =>
        @setChannelListing(username)
        @main.showTitle(options.title)
      onFetch: (user) =>
        @showUserLarge(user)
        @main.tabsRegion.show(@getUserTabs(user, options.active_tab))
        @main.contentRegion.show(options.mainRegion(user))

  switchToPage: (username, user, path, options)->
    @main.setTitle options.title
    @main.tabsRegion.currentView.activate(options.active_tab)
    @main.contentRegion.show(options.mainRegion(user))
    Backbone.history.navigate path, false

  getUserTabs: (user, active_tab) ->
    usertabs = new UserTabsView(model: user, active_tab: active_tab)
    username = user.get('username')
    userjson = user.toJSON()
    usertabs.on 'showProfile',       => @switchToPage(username, user, userjson.link , @profile_options(username))
    usertabs.on 'showNotifications', => @switchToPage(username, user,userjson.notifications_settings_path, @notification_options(username))

  getUser: (username, options) ->
    user = new User(username: username)
    options.onInit(user)
    user.fetch
      success: -> options.onFetch(user)
      forProfile: true

  getFactsView: (channel) ->
    facts_view = new FactsView(
        collection: new ChannelFacts([],
          channel: channel
        ),
        model: channel
      )

  withFact: (fact)->
    window.efv = new ExtendedFactView(model: fact)
    @main.contentRegion.show(efv)

    username = fact.get('created_by').username

    title_view = new ExtendedFactTitleView(
                        model: fact,
                        return_to_url: username,
                        return_to_text: username.capitalize() )

    @main.titleRegion.show( title_view )

    @showChannelListing(fact.get('created_by').username)

  showChannelListing: (username)->
    changed = window.Channels.setUsernameAndRefresh(username)
    channelCollectionView = new ChannelsView(collection: window.Channels)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActive('profile')


  showUserLarge: (user) ->
    userLargeView = new UserLargeView(model: user);
    app.leftTopRegion.show(userLargeView);

  setChannelListing: (username) ->
    changed = window.Channels.setUsernameAndRefresh(username)
    channelCollectionView = new ChannelsView(collection: window.Channels)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActive('profile')


  makePermalinkEvent: (baseUrl) ->
    @permalink_event = @bindTo FactlinkApp.vent, 'factlink_permalink_clicked', (e, fact) =>
      navigate_to = fact.get('url')
      Backbone.history.navigate navigate_to, true

      e.preventDefault()
      e.stopPropagation()
<commit_msg>Made code a bit nicer<commit_after>app = FactlinkApp

class window.ProfileController extends Backbone.Factlink.BaseController

  routes: ['showProfile', 'showNotificationSettings', 'showFact']

  # ACTIONS
  showProfile: (username) -> @showPage username, @profile_options(username)
  showNotificationSettings: (username) -> @showPage username, @notification_options(username)

  showFact: (slug, fact_id)->
    app.closeAllContentRegions()
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)

    fact = new Fact(id: fact_id)
    fact.fetch
      success: (model, response) => @withFact(model)

  # HELPERS
  profile_options: (username) ->
    title: 'About ' + username
    active_tab: 'show'
    mainRegion: (user) =>
      @makePermalinkEvent( user.get('username') )

      new ProfileView
        model: user
        collection: window.Channels.orderedByAuthority()
        created_facts_view: @getFactsView user.created_facts()

  notification_options: (username)->
    title: 'Notification Settings'
    active_tab: 'notification-settings'
    mainRegion: (user) ->
      new NotificationSettingsView model: user

  showPage: (username, options) ->
    app.leftBottomRegion.close()
    @main = new TabbedMainRegionLayout();
    app.mainRegion.show(@main)
    @getUser username,
      onInit: (user) =>
        @setChannelListing(username)
        @main.showTitle(options.title)
      onFetch: (user) =>
        @showUserLarge(user)
        @main.tabsRegion.show(@getUserTabs(user, options.active_tab))
        @main.contentRegion.show(options.mainRegion(user))

  switchToPage: (username, user, path, options)->
    @main.setTitle options.title
    @main.tabsRegion.currentView.activate(options.active_tab)
    @main.contentRegion.show(options.mainRegion(user))
    Backbone.history.navigate path, false

  getUserTabs: (user, active_tab) ->
    usertabs = new UserTabsView(model: user, active_tab: active_tab)
    username = user.get('username')
    userjson = user.toJSON()
    usertabs.on 'showProfile',       => @switchToPage(username, user, userjson.link , @profile_options(username))
    usertabs.on 'showNotifications', => @switchToPage(username, user,userjson.notifications_settings_path, @notification_options(username))

  getUser: (username, options) ->
    user = new User(username: username)
    options.onInit(user)
    user.fetch
      success: -> options.onFetch(user)
      forProfile: true

  getFactsView: (channel) ->
    collection = new ChannelFacts [], channel: channel
    facts_view = new FactsView
        collection: collection,
        model: channel

  withFact: (fact)->
    window.efv = new ExtendedFactView(model: fact)
    @main.contentRegion.show(efv)

    username = fact.get('created_by').username

    title_view = new ExtendedFactTitleView(
                        model: fact,
                        return_to_url: username,
                        return_to_text: username.capitalize() )

    @main.titleRegion.show( title_view )

    @showChannelListing(fact.get('created_by').username)

  showChannelListing: (username)->
    changed = window.Channels.setUsernameAndRefresh(username)
    channelCollectionView = new ChannelsView(collection: window.Channels)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActive('profile')


  showUserLarge: (user) ->
    userLargeView = new UserLargeView(model: user);
    app.leftTopRegion.show(userLargeView);

  setChannelListing: (username) ->
    changed = window.Channels.setUsernameAndRefresh(username)
    channelCollectionView = new ChannelsView(collection: window.Channels)
    app.leftMiddleRegion.show(channelCollectionView)
    channelCollectionView.setActive('profile')

  makePermalinkEvent: (baseUrl) ->
    @permalink_event = @bindTo FactlinkApp.vent, 'factlink_permalink_clicked', (e, fact) =>
      navigate_to = fact.get('url')
      Backbone.history.navigate navigate_to, true

      e.preventDefault()
      e.stopPropagation()

{'fraction_non_alphanumeric': 0.11297805642633228, 'fraction_numerical': 0.0, 'mean_word_length': 3.7279193835210434, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1083207', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2326, 'n_words': 438}