<commit_before>_ = require 'underscore'
Backbone = require 'backbone'
Popover = require 'popover'
{ API_URL } = require('sharify').data
listTemplate = -> require('./artist_suggestions.jade') arguments...
rowTemplate = -> require('./artist_suggestion.jade') arguments...
Artists = require '../../collections/artists.coffee'
analyticsHooks = require '../../lib/analytics_hooks.coffee'

module.exports = class ArtistSuggestions extends Backbone.View
  
  initialize: (options) ->
    { @following } = options

  # Follow the artist, and if there are any remaining swap the next one in
  followAndMaybeSwap: (e) =>
    e.preventDefault()
    id = $(e.currentTarget).data('artist-id')
    @following.follow id
    analyticsHooks.trigger 'follow-widget:follow', id
    $li = $(e.currentTarget).parent().parent('li')
    $li.find('.follow-button').attr 'data-state', 'following'
    return unless (nextArtist = @remainingArtists?.shift())
    _.delay (=> @replaceTextAndFade($li, nextArtist)), 500   # give a bit of time to see the 'Following' message

  replaceTextAndFade: ($li, artist) ->
    html = rowTemplate artist: artist
    $li.find('.artist-suggestion-row').replaceWith html
    $newRow = $li.find('.artist-suggestion-row')
    $newRow.css 'opacity', 0
    $newRow.fadeTo('slow', 1)
    @$popoverEl.find(@followButtonSelector(artist.get('id'))).on 'click', @followAndMaybeSwap

  remove: =>
    analyticsHooks.trigger 'follow-widget:closed'
    @popover.remove()

  followButtonSelector: (artistId) ->
    "a[data-artist-id='#{artistId}']"

  renderSuggestedArtists: ->
    artists = new Artists
    artists.url = "#{API_URL}/api/v1/me/suggested/artists"
    artists.fetch
      data:
        exclude_followed_artists: true
        artist_id: @model.id
        exclude_artists_without_artworks: true
      success: =>
        return unless artists.length > 3
        @initialArtists = artists.models[0...3]
        @remainingArtists = artists.models[3...-1]
        html = listTemplate
          artists: @initialArtists
        position = @bestPosition(@$el[0])
        @popover = new Popover<commit_msg>Correct sort for follow artists<commit_after>_ = require 'underscore'
Backbone = require 'backbone'
Popover = require 'popover'
{ API_URL } = require('sharify').data
listTemplate = -> require('./artist_suggestions.jade') arguments...
rowTemplate = -> require('./artist_suggestion.jade') arguments...
Artist = require '../../models/artist.coffee'
analyticsHooks = require '../../lib/analytics_hooks.coffee'

module.exports = class ArtistSuggestions extends Backbone.View
  
  initialize: (options) ->
    { @following } = options

  # Follow the artist, and if there are any remaining swap the next one in
  followAndMaybeSwap: (e) =>
    e.preventDefault()
    id = $(e.currentTarget).data('artist-id')
    @following.follow id
    analyticsHooks.trigger 'follow-widget:follow', id
    $li = $(e.currentTarget).parent().parent('li')
    $li.find('.follow-button').attr 'data-state', 'following'
    return unless (nextArtist = @remainingArtists?.shift())
    _.delay (=> @replaceTextAndFade($li, nextArtist)), 500   # give a bit of time to see the 'Following' message

  replaceTextAndFade: ($li, artist) ->
    html = rowTemplate artist: artist
    $li.find('.artist-suggestion-row').replaceWith html
    $newRow = $li.find('.artist-suggestion-row')
    $newRow.css 'opacity', 0
    $newRow.fadeTo('slow', 1)
    @$popoverEl.find(@followButtonSelector(artist.get('id'))).on 'click', @followAndMaybeSwap

  remove: =>
    analyticsHooks.trigger 'follow-widget:closed'
    @popover.remove()

  followButtonSelector: (artistId) ->
    "a[data-artist-id='#{artistId}']"

  renderSuggestedArtists: ->
    artists = new Backbone.Collection [], model: Artist
    artists.url = "#{API_URL}/api/v1/me/suggested/artists"
    artists.fetch
      data:
        exclude_followed_artists: true
        artist_id: @model.id
        exclude_artists_without_artworks: true
      success: =>
        return unless artists.length > 3
        @initialArtists = artists.models[0...3]
        @remainingArtists = artists.models[3...-1]
        html = listTemplate
          artists: @initialArtists
        position = @bestPosition(@$el[0])
        @popover = new Popover
{'fraction_non_alphanumeric': 0.14533928152148393, 'fraction_numerical': 0.005635125616341864, 'mean_word_length': 3.498416050686378, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1784255', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1336, 'n_words': 279}