<commit_before>define [
    '../../core'
    '../../controls'
    '../../button'
    'tpl!templates/controls/date-range-input.html'
], (c, controls, button, templates...) ->
    
    templates = c._.object ['date'], templates

    class DateControl extends controls.Control 
        template: templates.date

        events: ->
            'changeDate .datepicker': 'change'
            'keyup .datepicker': 'change'
            'change .btn-select': 'change'
            'click .range-help-button': 'toggleHelpText'

        initialize: (options) ->
            @model = options.model

        ui:
            inOutSelect: '.btn-select'

        onRender: ->
            @inOutSelect = new button.ButtonSelect
                collection: [
                    value: 'between'
                    label: 'between'
                    selected: true
                ,
                    value: 'not_between'
                    label: 'not between'
                ]

            @inOutSelect.render().$el.prependTo(@$el)
            @$('.help-block').hide()
            @$('.range-from').datepicker({'autoclose': true})
            @$('.range-to').datepicker({'autoclose': true})

        toggleHelpText: (event) ->
            @$('.help-block').toggle()
            event.preventDefault()

        isBetweenSelected: ->
            return @inOutSelect.getSelection() == 'between'

        getField: ->
            return @model.id

        getOperator: ->
            from = @$('.range-from').val()
            to = @$('.range-to').val()
            operator = ''

            if from != "" and to != ""
                operator = if @isBetweenSelected() then 'range' else '-range'
            else if from != ""
                operator = if @isBetweenSelected() then 'gte' else 'lte'
            else if to != ""
                operator = if @isBetweenSelected() then 'lte' else 'gte'
            else
                operator = if @isBetweenSelected() then 'range' else '-range'
          
            return operator

        getValue: ->
            from_text = @$('.range-from').val()
            from_date = @$('.range-from').data('datepicker').getFormattedDate()

            to_text = @$('.range-to').val()
            to_date = @$('.range-to').data('datepicker').getFormattedDate()
            
            value = []
            
            if from_text != "" and to_text != ""
                value = [from_date, to_date]
            else if from_text != ""
                value = from_date
            else if to_text != ""
                value = to_date
            # If both fields are emtpy, return null to invalidate this range
            else
                value = null 
             
            return value

        setOperator: (operator) ->
            @operator = operator

            if operator == '-range'
                @inOutSelect.setSelection("not_between")
            else
                @inOutSelect.setSelection("between")

        setValue: (value) ->
            switch @operator
                when 'range', '-range'
                    if value[0]?
                        @$('.range-from').data('datepicker').setDate(
                            new Date(value[0]))
                    else
                        @$('.range-from').val('')
                    if value[1]?
                        @$('.range-to').data('datepicker').setDate(
                            new Date(value[1]))
                    else
                        @$('.range-to').val('')
                when 'gte'
                    @$('.range-from').data('datepicker').setDate(
                        new Date(value))
                when 'lte'
                    @$('.range-to').data('datepicker').setDate(
                        new Date(value))
<commit_msg>Properly set the UI values based on the operator and value<commit_after>define [
    '../../core'
    '../../controls'
    '../../button'
    'tpl!templates/controls/date-range-input.html'
], (c, controls, button, templates...) ->
    
    templates = c._.object ['date'], templates

    class DateControl extends controls.Control 
        template: templates.date

        events: ->
            'changeDate .datepicker': 'change'
            'keyup .datepicker': 'change'
            'change .btn-select': 'change'
            'click .range-help-button': 'toggleHelpText'

        initialize: (options) ->
            super(options)
            @model = options.model

        ui:
            inOutSelect: '.btn-select'

        onRender: ->
            @inOutSelect = new button.ButtonSelect
                collection: [
                    value: 'between'
                    label: 'between'
                    selected: true
                ,
                    value: 'not_between'
                    label: 'not between'
                ]

            @inOutSelect.render().$el.prependTo(@$el)
            @$('.help-block').hide()
            @$('.range-from').datepicker({'autoclose': true})
            @$('.range-to').datepicker({'autoclose': true})

            operator = @context.get('operator')
            value = @context.get('value')
          
            if operator == '-range'
                @inOutSelect.setSelection("not_between")
            else
                @inOutSelect.setSelection("between")

            switch operator
                when 'range', '-range'
                    if value[0]?
                        @$('.range-from').data('datepicker').setDate(
                            new Date(value[0]))
                    else
                        @$('.range-from').val('')
                    if value[1]?
                        @$('.range-to').data('datepicker').setDate(
                            new Date(value[1]))
                    else
                        @$('.range-to').val('')
                when 'gte'
                    @$('.range-from').data('datepicker').setDate(
                        new Date(value))
                when 'lte'
                    @$('.range-to').data('datepicker').setDate(
                        new Date(value))
        toggleHelpText: (event) ->
            @$('.help-block').toggle()
            event.preventDefault()

        isBetweenSelected: ->
            return @inOutSelect.getSelection() == 'between'

        getField: ->
            return @model.id

        getOperator: ->
            from = @$('.range-from').val()
            to = @$('.range-to').val()
            operator = ''

            if from != "" and to != ""
                operator = if @isBetweenSelected() then 'range' else '-range'
            else if from != ""
                operator = if @isBetweenSelected() then 'gte' else 'lte'
            else if to != ""
                operator = if @isBetweenSelected() then 'lte' else 'gte'
            else
                operator = if @isBetweenSelected() then 'range' else '-range'
          
            return operator

        getValue: ->
            from_text = @$('.range-from').val()
            from_date = @$('.range-from').data('datepicker').getFormattedDate()

            to_text = @$('.range-to').val()
            to_date = @$('.range-to').data('datepicker').getFormattedDate()
            
            value = []
            
            if from_text != "" and to_text != ""
                value = [from_date, to_date]
            else if from_text != ""
                value = from_date
            else if to_text != ""
                value = to_date
            # If both fields are emtpy, return null to invalidate this range
            else
                value = null 
             
            return value


{'fraction_non_alphanumeric': 0.1392223382045929, 'fraction_numerical': 0.0010438413361169101, 'mean_word_length': 1.2314410480349345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '815767', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1869, 'n_words': 415}