<commit_before>_                       = require 'underscore'
Backbone                = require 'backbone'
Artist                  = require '../../../models/artist.coffee'
Artists                 = require '../../../collections/artists.coffee'
Gene                    = require '../../../models/gene.coffee'
Genes                   = require '../../../collections/genes.coffee'
sd                      = require('sharify').data
FillwidthView           = require '../../../components/fillwidth_row/view.coffee'
CurrentUser             = require '../../../models/current_user.coffee'
FollowButton            = require './follow_button.coffee'
itemTemplate            = -> require('../templates/following_item.jade') arguments...
hintTemplate            = -> require('../templates/empty.jade') arguments...
FollowArtists           = require '../../../collections/follow_artists.coffee'
FollowGenes             = require '../../../collections/follow_genes.coffee'

module.exports.FollowingView = class FollowingView extends Backbone.View

  initialize: (options) ->
    @followItems  = @model  # More readable alias
    @pageNum      = 0       # Last page loaded
    @itemsPerPage = sd.ITEMS_PER_PAGE or 5
    @setupCurrentUser()
    @setupFollowingItems()

  setupCurrentUser: ->
    @currentUser = CurrentUser.orNull()
    @currentUser?.initializeDefaultArtworkCollection()
    @artworkCollection = @currentUser?.defaultArtworkCollection()

  setupFollowingItems: ->
    @followItems.syncFollows? [], success: (col) =>
      @showEmptyHint() unless @followItems.length > 0
      if @followItems.length > @itemsPerPage
        $(window).bind 'scroll.following', _.throttle(@infiniteScroll, 150)
      @loadNextPage()

  loadNextPage: ->
    end = @itemsPerPage * (@pageNum + 1)
    start = end - @itemsPerPage
    return unless @followItems.length > start

    showingItems = @followItems.slice start, end
    _.each showingItems, (item) =>
      @appendItemSkeleton(item)
      @showItemContent(item)
    ++@pageNum

  infiniteScroll: =>
    $(window).unbind('.following') unless @pageNum * @itemsPerPage < @followItems.length
    fold = $(window).height() + $(window).scrollTop()
    $lastItem = @$('.following > .item:last')
    @loadNextPage() unless fold < $lastItem.offset()?.top + $lastItem.height()

  showEmptyHint: () ->
    @$('.following').append $( hintTemplate type: sd.TYPE )

  # Append the item with name, spinner, etc (without content) to the container
  #
  # So that we can display some stuff to users asap.
  # @param {Object} followItem an item from the followItems collection 
  appendItemSkeleton: (followItem) ->
    @$('.following').append $( itemTemplate item: followItem.getItem() )

  # Display item content
  #
  # The function assumes the skeleton (container) of this item already exists.
  # @param {Object} followItem an item from a followItems collection 
  showItemContent: (followItem) =>
    item = followItem.getItem() # Actual item model, e.g. an Artist
    item.fetchArtworks success: (artworks) =>
      $container = @$("##{sd.TYPE} ##{item.get('id')}")
      $followButton = $container.find(".follow-button")
      $artworks = $container.find('.artworks')
      view = new FillwidthView
        artworkCollection: @artworkCollection
        fetchOptions: { 'filter[]': 'for_sale' }
        collection: artworks
        empty: (-> @$el.parent().remove() )
        el: $artworks
      view.render()
      new FollowButton
        followItemCollection: @followItems
        model: item
        el: $followButton
      _.defer ->
        view.hideFirstRow()
        view.removeHiddenItems()

module.exports.init = ->
  dict = artists: FollowArtists, genes: FollowGenes
  followItemCollection = if dict[sd.TYPE]? then new dict[sd.TYPE]() else []
  new FollowingView model: followItemCollection, el: $('body')
<commit_msg>Remove unnecessary nested selector with id.<commit_after>_                       = require 'underscore'
Backbone                = require 'backbone'
Artist                  = require '../../../models/artist.coffee'
Artists                 = require '../../../collections/artists.coffee'
Gene                    = require '../../../models/gene.coffee'
Genes                   = require '../../../collections/genes.coffee'
sd                      = require('sharify').data
FillwidthView           = require '../../../components/fillwidth_row/view.coffee'
CurrentUser             = require '../../../models/current_user.coffee'
FollowButton            = require './follow_button.coffee'
itemTemplate            = -> require('../templates/following_item.jade') arguments...
hintTemplate            = -> require('../templates/empty.jade') arguments...
FollowArtists           = require '../../../collections/follow_artists.coffee'
FollowGenes             = require '../../../collections/follow_genes.coffee'

module.exports.FollowingView = class FollowingView extends Backbone.View

  initialize: (options) ->
    @followItems  = @model  # More readable alias
    @pageNum      = 0       # Last page loaded
    @itemsPerPage = sd.ITEMS_PER_PAGE or 5
    @setupCurrentUser()
    @setupFollowingItems()

  setupCurrentUser: ->
    @currentUser = CurrentUser.orNull()
    @currentUser?.initializeDefaultArtworkCollection()
    @artworkCollection = @currentUser?.defaultArtworkCollection()

  setupFollowingItems: ->
    @followItems.syncFollows? [], success: (col) =>
      @showEmptyHint() unless @followItems.length > 0
      if @followItems.length > @itemsPerPage
        $(window).bind 'scroll.following', _.throttle(@infiniteScroll, 150)
      @loadNextPage()

  loadNextPage: ->
    end = @itemsPerPage * (@pageNum + 1)
    start = end - @itemsPerPage
    return unless @followItems.length > start

    showingItems = @followItems.slice start, end
    _.each showingItems, (item) =>
      @appendItemSkeleton(item)
      @showItemContent(item)
    ++@pageNum

  infiniteScroll: =>
    $(window).unbind('.following') unless @pageNum * @itemsPerPage < @followItems.length
    fold = $(window).height() + $(window).scrollTop()
    $lastItem = @$('.following > .item:last')
    @loadNextPage() unless fold < $lastItem.offset()?.top + $lastItem.height()

  showEmptyHint: () ->
    @$('.following').append $( hintTemplate type: sd.TYPE )

  # Append the item with name, spinner, etc (without content) to the container
  #
  # So that we can display some stuff to users asap.
  # @param {Object} followItem an item from the followItems collection 
  appendItemSkeleton: (followItem) ->
    @$('.following').append $( itemTemplate item: followItem.getItem() )

  # Display item content
  #
  # The function assumes the skeleton (container) of this item already exists.
  # @param {Object} followItem an item from a followItems collection 
  showItemContent: (followItem) =>
    item = followItem.getItem() # Actual item model, e.g. an Artist
    item.fetchArtworks success: (artworks) =>
      $container = @$("##{item.get('id')}")
      $followButton = $container.find(".follow-button")
      $artworks = $container.find('.artworks')
      view = new FillwidthView
        artworkCollection: @artworkCollection
        fetchOptions: { 'filter[]': 'for_sale' }
        collection: artworks
        empty: (-> @$el.parent().remove() )
        el: $artworks
      view.render()
      new FollowButton
        followItemCollection: @followItems
        model: item
        el: $followButton
      _.defer ->
        view.hideFirstRow()
        view.removeHiddenItems()

module.exports.init = ->
  dict = artists: FollowArtists, genes: FollowGenes
  followItemCollection = if dict[sd.TYPE]? then new dict[sd.TYPE]() else []
  new FollowingView model: followItemCollection, el: $('body')

{'fraction_non_alphanumeric': 0.1386804657179819, 'fraction_numerical': 0.0018111254851228978, 'mean_word_length': 2.7293777134587556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '2212044', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2133, 'n_words': 514}