<commit_before>Backbone = require 'backbone'
mediator = require '../../../lib/mediator.coffee'
SaveControlsModal = require './modal.coffee'
CurrentUser = require '../../../models/current_user.coffee'
{ track } = require '../../../lib/analytics.coffee'
{ ArtworkCollection } = ArtworkCollections = require '../../../collections/artwork_collections.coffee'

module.exports = class SaveControls extends Backbone.View

  initialize: (options) ->
    @user = CurrentUser.orNull()
    @savedArtworks = new ArtworkCollection id: 'saved-artwork', user_id: @user.get('id')
    { @collections } = options
    @collections ?= new ArtworkCollections [], user: @user

  showSignupModal: ->
    track.funnel 'Triggered sign up form via save button'
    mediator.trigger 'open:auth',
      mode: 'register'
      copy: 'Sign up to save artworks'
      destination: "#{@model.href()}/save"
    true

  events:
    'click .overlay-button-save:not([data-state=saved]), .circle-icon-button-save': 'save'
    'click .overlay-button-save[data-state=saved], .circle-icon-button-save[data-state=saved]': 'remove'
    'click .save-controls-two-btn-add-to-collection, .circle-icon-button-add': 'openCollectionModal'

  save: (e) ->
    e?.preventDefault()
    return @showSignupModal() unless @user
    @savedArtworks.saveArtwork @model
    @$('.overlay-button-save, .circle-icon-button-save').attr 'data-state', 'saved'

  remove: (e) ->
    e?.preventDefault()
    @savedArtworks.removeArtwork @model
    @$('.overlay-button-save, .circle-icon-button-save').attr 'data-state', null

  openCollectionModal: (e) ->
    e?.preventDefault()
    return @showSignupModal() unless @user
    new SaveControlsModal width: 500, model: @model, collections: @collections<commit_msg>Stop propagation of click events<commit_after>Backbone = require 'backbone'
mediator = require '../../../lib/mediator.coffee'
SaveControlsModal = require './modal.coffee'
CurrentUser = require '../../../models/current_user.coffee'
{ track } = require '../../../lib/analytics.coffee'
{ ArtworkCollection } = ArtworkCollections = require '../../../collections/artwork_collections.coffee'

module.exports = class SaveControls extends Backbone.View

  initialize: (options) ->
    @user = CurrentUser.orNull()
    @savedArtworks = new ArtworkCollection id: 'saved-artwork', user_id: @user.get('id')
    { @collections } = options
    @collections ?= new ArtworkCollections [], user: @user

  showSignupModal: ->
    track.funnel 'Triggered sign up form via save button'
    mediator.trigger 'open:auth',
      mode: 'register'
      copy: 'Sign up to save artworks'
      destination: "#{@model.href()}/save"
    true

  events:
    'click .overlay-button-save:not([data-state=saved]), .circle-icon-button-save': 'save'
    'click .overlay-button-save[data-state=saved], .circle-icon-button-save[data-state=saved]': 'remove'
    'click .save-controls-two-btn-add-to-collection, .circle-icon-button-add': 'openCollectionModal'

  save: (e) ->
    e?.preventDefault()
    e?.stopPropagation()
    return @showSignupModal() unless @user
    @savedArtworks.saveArtwork @model
    @$('.overlay-button-save, .circle-icon-button-save').attr 'data-state', 'saved'

  remove: (e) ->
    e?.preventDefault()
    e?.stopPropagation()
    @savedArtworks.removeArtwork @model
    @$('.overlay-button-save, .circle-icon-button-save').attr 'data-state', null

  openCollectionModal: (e) ->
    e?.preventDefault()
    e?.stopPropagation()
    return @showSignupModal() unless @user
    new SaveControlsModal width: 500, model: @model, collections: @collections
{'fraction_non_alphanumeric': 0.1633221850613155, 'fraction_numerical': 0.0016722408026755853, 'mean_word_length': 3.75364238410596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2765589', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1111, 'n_words': 234}