<commit_before>View = require './view'

Request = require '../models/Request'

class RequestsView extends View

    templateBase:   require './templates/requestsBase'
    templateFilter: require './templates/requestsFilter'

    # Figure out which template we'll use for the table based on the filter
    bodyTemplateMap:
        all:      require './templates/requestsAllBody'
        active:   require './templates/requestsActiveBody'
        cooldown: require './templates/requestsCooldownBody'
        paused:   require './templates/requestsPausedBody'
        pending:  require './templates/requestsPendingBody'
        cleaning: require './templates/requestsCleaningBody'

    removeRequestTemplate: require './templates/vex/removeRequest'

    # Which table views have sub-filters (daemon, scheduled, on-demand)
    haveSubfilter: ['all', 'active', 'paused', 'cooldown']

    events:
        'click [data-action="viewJSON"]': 'viewJson'
        'click [data-action="remove"]': 'removeRequest'
        'click [data-action="unpause"]': 'unpauseRequest'
        'click [data-action="starToggle"]': 'toggleStar'
        'click [data-action="run-now"]': 'runRequest'
        'click [data-requests-active-filter]': 'changeFilters'

        'change input[type="search"]': 'searchChange'
        'keyup input[type="search"]': 'searchChange'
        'input input[type="search"]': 'searchChange'

    initialize: ({@requestsFilter, @requestsSubFilter, @searchFilter}) ->
        @bodyTemplate = @bodyTemplateMap[@requestsFilter]

        # Set up collection
        collectionMap =
            all:      app.collections.requestsAll
            active:   app.collections.requestsActive
            cooldown: app.collections.requestsCooldown
            paused:   app.collections.requestsPaused
            pending:  app.collections.requestsPending
            cleaning: app.collections.requestsCleaning

        @collectionSynced = false
        @collection = collectionMap[@requestsFilter]
        @collection.on "sync", =>
            @collectionSynced = true
            @renderTable()
        # Initial fetch
        @collection.fetch()

    # Called by app on active view
    refresh: ->
        return @ if @$el.find('[data-sorted-direction]').length
        @collection.fetch()

    # Returns the array of requests that need to be rendered
    filterCollection: =>
        requests = _.pluck @collection.models, "attributes"

        # Only show requests that match the search query
        if @searchFilter
            requests = _.filter requests, (request) =>
                request.name.toLowerCase().indexOf(@searchFilter.toLowerCase()) isnt -1
        
        # Only show requests that match the clicky filters
        if @requestsFilter in @haveSubfilter
            requests = _.filter requests, (request) =>
                filter = @requestsSubFilter is 'all' then true else false

                if @requestsSubFilter.indexOf('daemon') isnt -1
                    filter = filter or request.daemon
                if @requestsSubFilter.indexOf('scheduled') isnt -1
                    filter = filter or request.scheduled
                if @requestsSubFilter.indexOf('on-demand') isnt -1
                    filter = filter or request.onDemand
                filter

        requests.reverse()

    render: =><commit_msg>Changed subfilter check<commit_after>View = require './view'

Request = require '../models/Request'

class RequestsView extends View

    templateBase:   require './templates/requestsBase'
    templateFilter: require './templates/requestsFilter'

    # Figure out which template we'll use for the table based on the filter
    bodyTemplateMap:
        all:      require './templates/requestsAllBody'
        active:   require './templates/requestsActiveBody'
        cooldown: require './templates/requestsCooldownBody'
        paused:   require './templates/requestsPausedBody'
        pending:  require './templates/requestsPendingBody'
        cleaning: require './templates/requestsCleaningBody'

    removeRequestTemplate: require './templates/vex/removeRequest'

    # Which table views have sub-filters (daemon, scheduled, on-demand)
    haveSubfilter: ['all', 'active', 'paused', 'cooldown']

    events:
        'click [data-action="viewJSON"]': 'viewJson'
        'click [data-action="remove"]': 'removeRequest'
        'click [data-action="unpause"]': 'unpauseRequest'
        'click [data-action="starToggle"]': 'toggleStar'
        'click [data-action="run-now"]': 'runRequest'
        'click [data-requests-active-filter]': 'changeFilters'

        'change input[type="search"]': 'searchChange'
        'keyup input[type="search"]': 'searchChange'
        'input input[type="search"]': 'searchChange'

    initialize: ({@requestsFilter, @requestsSubFilter, @searchFilter}) ->
        @bodyTemplate = @bodyTemplateMap[@requestsFilter]

        # Set up collection
        collectionMap =
            all:      app.collections.requestsAll
            active:   app.collections.requestsActive
            cooldown: app.collections.requestsCooldown
            paused:   app.collections.requestsPaused
            pending:  app.collections.requestsPending
            cleaning: app.collections.requestsCleaning

        @collectionSynced = false
        @collection = collectionMap[@requestsFilter]
        @collection.on "sync", =>
            @collectionSynced = true
            @renderTable()
        # Initial fetch
        @collection.fetch()

    # Called by app on active view
    refresh: ->
        return @ if @$el.find('[data-sorted-direction]').length
        @collection.fetch()

    # Returns the array of requests that need to be rendered
    filterCollection: =>
        requests = _.pluck @collection.models, "attributes"

        # Only show requests that match the search query
        if @searchFilter
            requests = _.filter requests, (request) =>
                request.name.toLowerCase().indexOf(@searchFilter.toLowerCase()) isnt -1
        
        # Only show requests that match the clicky filters
        if @requestsFilter in @haveSubfilter and @subFilter isnt 'all'
            requests = _.filter requests, (request) =>
                filter = false

                if @requestsSubFilter.indexOf('daemon') isnt -1
                    filter = filter or request.daemon
                if @requestsSubFilter.indexOf('scheduled') isnt -1
                    filter = filter or request.scheduled
                if @requestsSubFilter.indexOf('on-demand') isnt -1
                    filter = filter or request.onDemand
                filter

        requests.reverse()

    render: =>
{'fraction_non_alphanumeric': 0.10609920575453319, 'fraction_numerical': 0.0011988610819721264, 'mean_word_length': 2.4634146341463414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6945763', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1665, 'n_words': 440}