<commit_before>_ = require 'underscore'
Backbone = require 'backbone'
mediator = require '../../lib/mediator.coffee'
analytics = require '../../lib/analytics.coffee'

module.exports = class FollowButton extends Backbone.View
  events:
    'click': 'toggle'
    'touchstart': () -> @$el.removeClass "no-touch"

  initialize: (options) ->
    { @following, @notes, @modelName } = options

    return unless @following

    throw new Error('Requires @modelName') unless @modelName

    @listenTo @following, "add:#{@model.id}", @change
    @listenTo @following, "remove:#{@model.id}", @change

    @analyticsFollowMessage = options.analyticsFollowMessage or @defaultAnalyticsMessage('Followed')
    @analyticsUnfollowMessage = options.analyticsUnfollowMessage or @defaultAnalyticsMessage('Unfollowed')

    @$el.addClass "no-touch"

    @change()

  defaultAnalyticsMessage: (action) ->
    "#{action} #{@modelName} from #{window?.location.pathname}"

  change: ->
    state = if @following.isFollowing(@model.id) then 'following' else 'follow'
    @$el.attr 'data-state', state

  toggle: (e) ->
    @trigger 'click'

    unless @following
      analytics.track.funnel 'Triggered sign up form via follow button'
      mediator.trigger 'open:auth',
        mode: 'register'
        copy: "Sign up to follow #{@modelName}s"
        destination: "#{@model.href()}/follow"
      return false

    if @following.isFollowing @model.id
      @following.unfollow @model.id
      mediator.trigger 'follow-button:unfollow', @$el, @model
      analytics.track.click @analyticsUnfollowMessage, label: analytics.modelNameAndIdToLabel(@modelName, @model.id)
    else
      @following.follow @model.id, notes: (@notes or @analyticsFollowMessage)
      # Delay label change
      @$el.addClass 'is-clicked'
      setTimeout (=> @$el.removeClass 'is-clicked'), 1500
      mediator.trigger 'follow-button:follow', @$el, @model
      analytics.track.click @analyticsFollowMessage, label: analytics.modelNameAndIdToLabel(@modelName, @model.id)

    false<commit_msg>follow button can take a label argument<commit_after>_ = require 'underscore'
Backbone = require 'backbone'
mediator = require '../../lib/mediator.coffee'
analytics = require '../../lib/analytics.coffee'

module.exports = class FollowButton extends Backbone.View

  events:
    'click': 'toggle'
    'touchstart': () -> @$el.removeClass "no-touch"

  initialize: (options) ->
    { @following, @notes, @modelName } = options

    @label = if options.label then options.label else "#{@modelName}s"

    return unless @following

    throw new Error('Requires @modelName') unless @modelName

    @listenTo @following, "add:#{@model.id}", @change
    @listenTo @following, "remove:#{@model.id}", @change

    @analyticsFollowMessage = options.analyticsFollowMessage or @defaultAnalyticsMessage('Followed')
    @analyticsUnfollowMessage = options.analyticsUnfollowMessage or @defaultAnalyticsMessage('Unfollowed')

    @$el.addClass "no-touch"

    @change()

  defaultAnalyticsMessage: (action) ->
    "#{action} #{@modelName} from #{window?.location.pathname}"

  change: ->
    state = if @following.isFollowing(@model.id) then 'following' else 'follow'
    @$el.attr 'data-state', state

  toggle: (e) ->
    @trigger 'click'

    unless @following
      analytics.track.funnel 'Triggered sign up form via follow button'
      mediator.trigger 'open:auth',
        mode: 'register'
        copy: "Sign up to follow #{@label}"
        destination: "#{@model.href()}/follow"
      return false

    if @following.isFollowing @model.id
      @following.unfollow @model.id
      mediator.trigger 'follow-button:unfollow', @$el, @model
      analytics.track.click @analyticsUnfollowMessage, label: analytics.modelNameAndIdToLabel(@modelName, @model.id)
    else
      @following.follow @model.id, notes: (@notes or @analyticsFollowMessage)
      # Delay label change
      @$el.addClass 'is-clicked'
      setTimeout (=> @$el.removeClass 'is-clicked'), 1500
      mediator.trigger 'follow-button:follow', @$el, @model
      analytics.track.click @analyticsFollowMessage, label: analytics.modelNameAndIdToLabel(@modelName, @model.id)

    false
{'fraction_non_alphanumeric': 0.13821138211382114, 'fraction_numerical': 0.001912960306073649, 'mean_word_length': 3.847045191193511, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5611739', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1247, 'n_words': 296}