<commit_before>_ = require 'underscore'
Backbone = require 'backbone'
Filter = require './models/filter.coffee'
ArtworkColumns = require './collections/artwork_columns.coffee'
ArtworkColumnsView = require '../artwork_columns/view.coffee'
ArtworkTableView = require '../artwork_table/view.coffee'
analyticsHooks = require '../../lib/analytics_hooks.coffee'
BorderedPulldown = require '../bordered_pulldown/view.coffee'
mediator = require '../../lib/mediator.coffee'
splitTest = require '../../components/split_test/index.coffee'

template = -> require('./templates/index.jade') arguments...
filterTemplate = -> require('./templates/filter.jade') arguments...
headerTemplate = -> require('./templates/header.jade') arguments...

module.exports = class ArtworkFilterView extends Backbone.View
  events:
    'click .artwork-filter-select': 'selectCriteria'
    'click .artwork-filter-remove': 'removeCriteria'
    'click input[type="checkbox"]': 'toggleBoolean'
    'click #artwork-see-more': 'clickSeeMore'
    'click .bordered-pulldown-options a': 'selectCriteria'
    'click .artwork-filter-view-mode__toggle': 'changeViewMode'

  view: ->
    viewModes =
      grid: ArtworkColumnsView
      list: ArtworkTableView

    new viewModes[@viewMode.get('mode')] @params()

  params: ->
    viewModes =
      grid:
        numberOfColumns: 3
        gutterWidth: 40
        maxArtworkHeight: 400
        isOrdered: false
        seeMore: false
        allowDuplicates: true
        artworkSize: 'tall'
      list:
        show: true

    _.extend
      el: @$artworks
      collection: @artworks
    , viewModes[@viewMode.get('mode')]

  initialize: ({ @mode }) ->
    @artworks = new ArtworkColumns [], artistId: @model.id
    @filter = new Filter model: @model
    @viewMode = new Backbone.Model mode: @mode

    @listenTo @artworks, 'all', @handleArtworksState
    @listenTo @artworks, 'sync', @renderColumns
    @listenTo @filter, 'all', @handleFilterState
    @listenTo @filter, 'sync update:counts', @renderFilter
    @listenTo @filter, 'sync', @renderHeader
    @listenTo @filter.selected, 'change', @fetchArtworksFromBeginning
    @listenTo @filter.selected, 'change', @scrollToTop
    @listenTo @viewMode, 'change', @renderFilter
    @listenTo @viewMode, 'change', @renderHeader

    @render()

    @filter.fetchRoot
      success: (model, response, options) =>
        mediator.trigger 'artwork_filter:filter:sync', model
        @remove() unless model.get('total')
      error: =>
        @remove()
    @fetchArtworks()

  scrollToTop: ->
    @$htmlBody ?= $('html, body')
    visibleTop = @$el.offset().top - @$siteHeader.height()
    @$htmlBody.animate { scrollTop: visibleTop - 1 }, 500

  handleState: (el, eventName) ->
    if state = { request: 'loading', sync: 'loaded', error: 'loaded' }[eventName]
      el?.attr 'data-state', state
      state

  handleFilterState: (eventName) ->
    @handleState @$filter, eventName
<commit_msg>hide module when no artworks<commit_after>_ = require 'underscore'
Backbone = require 'backbone'
Filter = require './models/filter.coffee'
ArtworkColumns = require './collections/artwork_columns.coffee'
ArtworkColumnsView = require '../artwork_columns/view.coffee'
ArtworkTableView = require '../artwork_table/view.coffee'
analyticsHooks = require '../../lib/analytics_hooks.coffee'
BorderedPulldown = require '../bordered_pulldown/view.coffee'
mediator = require '../../lib/mediator.coffee'
splitTest = require '../../components/split_test/index.coffee'

template = -> require('./templates/index.jade') arguments...
filterTemplate = -> require('./templates/filter.jade') arguments...
headerTemplate = -> require('./templates/header.jade') arguments...

module.exports = class ArtworkFilterView extends Backbone.View
  events:
    'click .artwork-filter-select': 'selectCriteria'
    'click .artwork-filter-remove': 'removeCriteria'
    'click input[type="checkbox"]': 'toggleBoolean'
    'click #artwork-see-more': 'clickSeeMore'
    'click .bordered-pulldown-options a': 'selectCriteria'
    'click .artwork-filter-view-mode__toggle': 'changeViewMode'

  view: ->
    viewModes =
      grid: ArtworkColumnsView
      list: ArtworkTableView

    new viewModes[@viewMode.get('mode')] @params()

  params: ->
    viewModes =
      grid:
        numberOfColumns: 3
        gutterWidth: 40
        maxArtworkHeight: 400
        isOrdered: false
        seeMore: false
        allowDuplicates: true
        artworkSize: 'tall'
      list:
        show: true

    _.extend
      el: @$artworks
      collection: @artworks
    , viewModes[@viewMode.get('mode')]

  initialize: ({ @mode }) ->
    @artworks = new ArtworkColumns [], artistId: @model.id
    @filter = new Filter model: @model
    @viewMode = new Backbone.Model mode: @mode

    @listenTo @artworks, 'all', @handleArtworksState
    @listenTo @artworks, 'sync', @renderColumns
    @listenTo @filter, 'all', @handleFilterState
    @listenTo @filter, 'sync update:counts', @renderFilter
    @listenTo @filter, 'sync', @renderHeader
    @listenTo @filter.selected, 'change', @fetchArtworksFromBeginning
    @listenTo @filter.selected, 'change', @scrollToTop
    @listenTo @viewMode, 'change', @renderFilter
    @listenTo @viewMode, 'change', @renderHeader

    @render()

    @filter.fetchRoot
      success: (model, response, options) =>
        mediator.trigger 'artwork_filter:filter:sync', model
        @remove() unless (model.get('total').value > 0)
      error: =>
        @remove()
    @fetchArtworks()

  scrollToTop: ->
    @$htmlBody ?= $('html, body')
    visibleTop = @$el.offset().top - @$siteHeader.height()
    @$htmlBody.animate { scrollTop: visibleTop - 1 }, 500

  handleState: (el, eventName) ->
    if state = { request: 'loading', sync: 'loaded', error: 'loaded' }[eventName]
      el?.attr 'data-state', state
      state

  handleFilterState: (eventName) ->
    @handleState @$filter, eventName

{'fraction_non_alphanumeric': 0.14365387865472368, 'fraction_numerical': 0.0035490958255872907, 'mean_word_length': 3.490136570561457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '5612725', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1804, 'n_words': 407}