<commit_before>class MemberListSortButtonsView extends Backbone.View
    el: ".member-list-sort-buttons"
    template: _.template $.trim $("#member-list-sort-button-template").html()

    initialize: ->
        @listenTo member_list_view, 'sort-changed', @sort_changed

    events:
        'click button': 'handle_sort_button'

    handle_sort_button: (ev) ->
        btn = $(ev.currentTarget)
        ascending = true
        if btn.hasClass('active') and btn.hasClass('ascending')
            ascending = false

        field = $(btn).data "col"
        member_list_view.set_sort_order field, ascending

    sort_changed: (field, ascending) ->
        @$el.find('button').removeClass('active').removeClass('ascending').find('i').remove()
        active_btn = @$el.find("[data-col='#{field.id}']")
        active_btn.addClass 'active'
        if ascending
            icon = 'arrow-up'
            active_btn.addClass 'ascending'
        else
            icon = 'arrow-down'
        active_btn.append $("<i class='typcn typcn-#{icon}'></i>")

    render: ->
        @$el.empty()
        for f in MEMBER_LIST_FIELDS
            button_el = $(@template f)
            if member_list_view.active_sort_field.id == f.id
                button_el.addClass 'active'
            @$el.append button_el
        return @

class MemberListItemView extends Backbone.View
    template: _.template $("#member-list-item-template").html()

    render: (current_stat) ->
        attr = @model.toJSON()
        attr.party = @model.get_party(party_list)
        attr.current_stat = current_stat
        html = $($.trim(@template attr))
        @$el = html
        @el = @$el[0]
        return @

class MemberListView extends Backbone.View
    el: "ul.member-list"
    spinner_el: ".spinner-container"
    search_el: "main .text-search"

    initialize: ->
        @spinner_el = $(@spinner_el)
        @spinner_el.spin top: 0

        @children = {}

        @collection = new MemberList
        @listenTo @collection, "add", @append_item
        @listenTo @collection, "reset", @render

        @index = new lunr.Index
        @index.field "name"
        @index.field "party"
        @index.field "district"
        @index.field "party_short"
        @index.field "titles"
        @index.ref "id"

        @search_el = $(@search_el)
        @search_el.input @_filter_listing

        @_setup_sort()

        @collection.fetch
            reset: true
            data:
                thumbnail_dim: "104x156"
                current: true
                stats: true
                limit: 500
            processData: true

    _setup_sort: =>
        namesort = (a, b) -> a.model.attributes['name'].localeCompare b.model.attributes['name']
        statsort = (field) ->
            (aw, bw) ->
                a = aw.model.attributes.stats[field]
                b = bw.model.attributes.stats[field]
                if a == b
                    return namesort(aw, bw)
                if a > b
                    return -1
                else
                    return 1
        attrsort = (field) ->
            (aw, bw) ->
                a = aw.model.attributes[field]
                b = bw.model.attributes[field]
                if a == b
                    return namesort(aw, bw)
                if a > b
                    return -1
                else
                    return 1

        @_sort_funcs =
            name: namesort
            recent_activity: statsort('recent_activity')
            attendance: statsort('attendance')
            party_agree: statsort('party_agree')
            session_agree: statsort('party_agree')
            term_count: statsort('term_count')
            age: attrsort('age')

        @_sort_fields = []
        for field in MEMBER_LIST_FIELDS
            # Copy the original object
            f = $.extend {}, field
            f.sort_func = @_sort_funcs[f.id]
            @_sort_fields.push f

        @_raw_sort_func = null
        @sort_order = 1
        @sort_func = (a, b) =>
            @sort_order*@_raw_sort_func(a, b)

        # Just to have something in the sort order,
        # this will be overriden once we get data
        @set_sort_order "name"

    _calculate_rankings: (collection) =>
        activity_scores = (model.attributes.stats['recent_activity'] \
            for model in collection.models)
        activity_scores = _.sortBy activity_scores, (x) -> x
        percentile = (v) ->
            activity_scores.indexOf(v)/(activity_scores.length-1)
        for model in collection.models
            stats = model.attributes.stats
            stats['activity_ranking'] = percentile stats['recent_activity']

    set_sort_order: (field, ascending=true) =>
            for f in @_sort_fields
                if f.id == field<commit_msg>Make activity default sorting to prevent loading weirdness<commit_after>class MemberListSortButtonsView extends Backbone.View
    el: ".member-list-sort-buttons"
    template: _.template $.trim $("#member-list-sort-button-template").html()

    initialize: ->
        @listenTo member_list_view, 'sort-changed', @sort_changed

    events:
        'click button': 'handle_sort_button'

    handle_sort_button: (ev) ->
        btn = $(ev.currentTarget)
        ascending = true
        if btn.hasClass('active') and btn.hasClass('ascending')
            ascending = false

        field = $(btn).data "col"
        member_list_view.set_sort_order field, ascending

    sort_changed: (field, ascending) ->
        @$el.find('button').removeClass('active').removeClass('ascending').find('i').remove()
        active_btn = @$el.find("[data-col='#{field.id}']")
        active_btn.addClass 'active'
        if ascending
            icon = 'arrow-up'
            active_btn.addClass 'ascending'
        else
            icon = 'arrow-down'
        active_btn.append $("<i class='typcn typcn-#{icon}'></i>")

    render: ->
        @$el.empty()
        for f in MEMBER_LIST_FIELDS
            button_el = $(@template f)
            if member_list_view.active_sort_field.id == f.id
                button_el.addClass 'active'
            @$el.append button_el
        return @

class MemberListItemView extends Backbone.View
    template: _.template $("#member-list-item-template").html()

    render: (current_stat) ->
        attr = @model.toJSON()
        attr.party = @model.get_party(party_list)
        attr.current_stat = current_stat
        html = $($.trim(@template attr))
        @$el = html
        @el = @$el[0]
        return @

class MemberListView extends Backbone.View
    el: "ul.member-list"
    spinner_el: ".spinner-container"
    search_el: "main .text-search"

    initialize: ->
        @spinner_el = $(@spinner_el)
        @spinner_el.spin top: 0

        @children = {}

        @collection = new MemberList
        @listenTo @collection, "add", @append_item
        @listenTo @collection, "reset", @render

        @index = new lunr.Index
        @index.field "name"
        @index.field "party"
        @index.field "district"
        @index.field "party_short"
        @index.field "titles"
        @index.ref "id"

        @search_el = $(@search_el)
        @search_el.input @_filter_listing

        @_setup_sort()

        @collection.fetch
            reset: true
            data:
                thumbnail_dim: "104x156"
                current: true
                stats: true
                limit: 500
            processData: true

    _setup_sort: =>
        namesort = (a, b) -> a.model.attributes['name'].localeCompare b.model.attributes['name']
        statsort = (field) ->
            (aw, bw) ->
                a = aw.model.attributes.stats[field]
                b = bw.model.attributes.stats[field]
                if a == b
                    return namesort(aw, bw)
                if a > b
                    return -1
                else
                    return 1
        attrsort = (field) ->
            (aw, bw) ->
                a = aw.model.attributes[field]
                b = bw.model.attributes[field]
                if a == b
                    return namesort(aw, bw)
                if a > b
                    return -1
                else
                    return 1

        @_sort_funcs =
            name: namesort
            recent_activity: statsort('recent_activity')
            attendance: statsort('attendance')
            party_agree: statsort('party_agree')
            session_agree: statsort('party_agree')
            term_count: statsort('term_count')
            age: attrsort('age')

        @_sort_fields = []
        for field in MEMBER_LIST_FIELDS
            # Copy the original object
            f = $.extend {}, field
            f.sort_func = @_sort_funcs[f.id]
            @_sort_fields.push f

        @_raw_sort_func = null
        @sort_order = 1
        @sort_func = (a, b) =>
            @sort_order*@_raw_sort_func(a, b)

        # Just to have something in the sort order,
        # this will be overriden once we get data
        @set_sort_order "recent_activity"

    _calculate_rankings: (collection) =>
        activity_scores = (model.attributes.stats['recent_activity'] \
            for model in collection.models)
        activity_scores = _.sortBy activity_scores, (x) -> x
        percentile = (v) ->
            activity_scores.indexOf(v)/(activity_scores.length-1)
        for model in collection.models
            stats = model.attributes.stats
            stats['activity_ranking'] = percentile stats['recent_activity']

    set_sort_order: (field, ascending=true) =>
            for f in @_sort_fields
                if f.id == field
{'fraction_non_alphanumeric': 0.11267605633802817, 'fraction_numerical': 0.0035211267605633804, 'mean_word_length': 1.7591428571428571, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, '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': '2107616', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2740, 'n_words': 614}