<commit_before>PopoverScreenView = require 'lib/popover_screen_view'
ComboBox    = require 'views/widgets/combobox'
Event       = require 'models/event'

tFormat                 = 'HH:mm'
dFormat                 = 'DD/MM/YYYY'
inputDateDTPickerFormat = 'dd/mm/yyyy'
allDayDateFieldFormat   = 'YYYY-MM-DD'

defTimePickerOpts       =
    template: false
    minuteStep: 5
    showMeridian: false

defDatePickerOps        =
    language: window.app.locale
    fontAwesome: true
    autoclose: true
    pickerPosition: 'bottom-right'
    keyboardNavigation: false
    format: inputDateDTPickerFormat
    minView: 2
    viewSelect: 4

module.exports = class MainPopoverScreen extends PopoverScreenView

    # Override generic template title.
    templateTitle: require 'views/templates/popover_screens/main_title'
    templateContent: require 'views/templates/popover_screens/main'


    events:
        'keyup':                'onKeyUp'
        'change select':        'onKeyUp'
        'change input':         'onKeyUp'
        'click .add':           'onAddClicked'
        'click .advanced-link': 'onAdvancedClicked'
        'click .remove':        -> @switchToScreen('delete')
        'click .duplicate':     'onDuplicateClicked'


        'changeTime.timepicker .input-start':    'onSetStart'
        'changeTime.timepicker .input-end-time': 'onSetEnd'
        'changeDate .input-end-date':            'onSetEnd'
        'click .input-allday':                   'toggleAllDay'

        'input .input-desc':                     'onSetDesc'
        'input .input-place':                    'onSetPlace'

        'keydown [data-tabindex-next]':          'onTab'
        'keydown [data-tabindex-prev]':          'onTab'

        # Screen switches.
        'click .input-people': -> @switchToScreen('guests')
        'click .input-details-trigger': -> @switchToScreen('details')
        'click .input-alert': -> @switchToScreen('alert')
        'click .input-repeat': -> @switchToScreen('repeat')


    initialize: ->
        @listenTo @model, 'change', @refresh


    getRenderData: ->
        # A new event's calendar is the first calendar in alphabetical order
        # It fallbacks to the default calendar name if anything goes wrong
        firstCalendar = app.calendars?.at(0)?.get 'name'
        defaultCalendar = t 'default calendar name'
        if @model.isNew()
            currentCalendar = firstCalendar or defaultCalendar
        else
            currentCalendar = @model.get('tags')?[0] or defaultCalendar

        return data = _.extend super(),
            tFormat:     tFormat
            dFormat:     dFormat
            calendar:    currentCalendar
            allDay:      @model.isAllDay()
            sameDay:     @model.isSameDay()
            start:       @model.getStartDateObject()
            end:         @model.getEndDateObject()
                               .add((if @model.isAllDay() then -1 else 0), 'd')
            alerts: @model.get('alarms')
            guestsButtonText: @getGuestsButtonText()
            buttonText: @getButtonText()
            getReccuringSummary: =>
                rrule = RRule.fromString @model.get('rrule')
                # Handle localization.
                locale = moment.localeData()
                language =
                    dayNames: locale._weekdays
                    monthNames: locale._months

                return rrule.toText(window.t, language)

    afterRender: ->
        # Cache jQuery selectors.
        @$container   = @$ '.popover-content-wrapper'
        @addButton    = @$ '.btn.add'
        @removeButton = @$ '.remove'
        @spinner = @$ '.remove-spinner'
        @duplicateButton = @$ '.duplicate'
        @$optionalFields = @$ '[data-optional="true"]'
        @$moreDetailsButton = @$ '.advanced-link'

        # If this is a new unsaved event, hide the irrelevant buttons.
        if @model.isNew()
            @removeButton.hide()
            @duplicateButton.hide()

        # Apply the expanded status if it has been previously set.
        if window.popoverExtended
            @expandPopover()

        timepickerEvents =
            'focus': ->
                $(@).timepicker 'highlightHour'
            'timepicker.next': ->
                $("[tabindex=#{+$(@).attr('tabindex') + 1}]").focus()
            'timepicker.prev': ->
                $("[tabindex=#{+$(@).attr('tabindex') - 1}]").focus()


        @$('input[type="time"]').attr('type', 'text')
                                .timepicker defTimePickerOpts
                                .delegate timepickerEvents

        # Chrome is really bad with HTML5 form so it always get an error of
        # validation. As a result we don't use a type=date, but a type=text.
        @$('.input-date').datetimepicker defDatePickerOps

        @$('[tabindex=1]').focus()
        @calendar = new ComboBox
            el: @$ '.calendarcombo'
            small: true
            source: app.calendars.toAutoCompleteSource()
            current: @model.getCalendar()?.get('name')

        @calendar.on 'edition-complete', (value) => @model.setCalendar value

        @refresh()

    onLeaveScreen: ->
        value = @$('.input-details').val()
        @model.set 'details', value


    refresh: ->
        # We must apply a delta when an event is all-day long, because it starts
        # one day, and finishes the day after (if its duration is one day). But
        # we don't want to display the day afteras endDate, but the start day.
        #
        # e.g.: you set a all-day long event on 15/01/2015. The event starts at<commit_msg>Fixed: if all optional fields had value, the 'more details' button was still shown<commit_after>PopoverScreenView = require 'lib/popover_screen_view'
ComboBox    = require 'views/widgets/combobox'
Event       = require 'models/event'

tFormat                 = 'HH:mm'
dFormat                 = 'DD/MM/YYYY'
inputDateDTPickerFormat = 'dd/mm/yyyy'
allDayDateFieldFormat   = 'YYYY-MM-DD'

defTimePickerOpts       =
    template: false
    minuteStep: 5
    showMeridian: false

defDatePickerOps        =
    language: window.app.locale
    fontAwesome: true
    autoclose: true
    pickerPosition: 'bottom-right'
    keyboardNavigation: false
    format: inputDateDTPickerFormat
    minView: 2
    viewSelect: 4

module.exports = class MainPopoverScreen extends PopoverScreenView

    # Override generic template title.
    templateTitle: require 'views/templates/popover_screens/main_title'
    templateContent: require 'views/templates/popover_screens/main'


    events:
        'keyup':                'onKeyUp'
        'change select':        'onKeyUp'
        'change input':         'onKeyUp'
        'click .add':           'onAddClicked'
        'click .advanced-link': 'onAdvancedClicked'
        'click .remove':        -> @switchToScreen('delete')
        'click .duplicate':     'onDuplicateClicked'


        'changeTime.timepicker .input-start':    'onSetStart'
        'changeTime.timepicker .input-end-time': 'onSetEnd'
        'changeDate .input-end-date':            'onSetEnd'
        'click .input-allday':                   'toggleAllDay'

        'input .input-desc':                     'onSetDesc'
        'input .input-place':                    'onSetPlace'

        'keydown [data-tabindex-next]':          'onTab'
        'keydown [data-tabindex-prev]':          'onTab'

        # Screen switches.
        'click .input-people': -> @switchToScreen('guests')
        'click .input-details-trigger': -> @switchToScreen('details')
        'click .input-alert': -> @switchToScreen('alert')
        'click .input-repeat': -> @switchToScreen('repeat')


    initialize: ->
        @listenTo @model, 'change', @refresh


    getRenderData: ->
        # A new event's calendar is the first calendar in alphabetical order
        # It fallbacks to the default calendar name if anything goes wrong
        firstCalendar = app.calendars?.at(0)?.get 'name'
        defaultCalendar = t 'default calendar name'
        if @model.isNew()
            currentCalendar = firstCalendar or defaultCalendar
        else
            currentCalendar = @model.get('tags')?[0] or defaultCalendar

        return data = _.extend super(),
            tFormat:     tFormat
            dFormat:     dFormat
            calendar:    currentCalendar
            allDay:      @model.isAllDay()
            sameDay:     @model.isSameDay()
            start:       @model.getStartDateObject()
            end:         @model.getEndDateObject()
                               .add((if @model.isAllDay() then -1 else 0), 'd')
            alerts: @model.get('alarms')
            guestsButtonText: @getGuestsButtonText()
            buttonText: @getButtonText()
            getReccuringSummary: =>
                rrule = RRule.fromString @model.get('rrule')
                # Handle localization.
                locale = moment.localeData()
                language =
                    dayNames: locale._weekdays
                    monthNames: locale._months

                return rrule.toText(window.t, language)

    afterRender: ->
        # Cache jQuery selectors.
        @$container   = @$ '.popover-content-wrapper'
        @addButton    = @$ '.btn.add'
        @removeButton = @$ '.remove'
        @spinner = @$ '.remove-spinner'
        @duplicateButton = @$ '.duplicate'
        @$optionalFields = @$ '[data-optional="true"]'
        @$moreDetailsButton = @$ '.advanced-link'

        # If this is a new unsaved event, hide the irrelevant buttons.
        if @model.isNew()
            @removeButton.hide()
            @duplicateButton.hide()

        # Apply the expanded status if it has been previously set.
        if window.popoverExtended
            @expandPopover()

        timepickerEvents =
            'focus': ->
                $(@).timepicker 'highlightHour'
            'timepicker.next': ->
                $("[tabindex=#{+$(@).attr('tabindex') + 1}]").focus()
            'timepicker.prev': ->
                $("[tabindex=#{+$(@).attr('tabindex') - 1}]").focus()


        @$('input[type="time"]').attr('type', 'text')
                                .timepicker defTimePickerOpts
                                .delegate timepickerEvents

        # Chrome is really bad with HTML5 form so it always get an error of
        # validation. As a result we don't use a type=date, but a type=text.
        @$('.input-date').datetimepicker defDatePickerOps

        @$('[tabindex=1]').focus()
        @calendar = new ComboBox
            el: @$ '.calendarcombo'
            small: true
            source: app.calendars.toAutoCompleteSource()
            current: @model.getCalendar()?.get('name')

        @calendar.on 'edition-complete', (value) => @model.setCalendar value

        @refresh()

        # If all the optional fields are shown by default (they all have a
        # value), then hide the "more details" button.
        if @$("[aria-hidden=true]").length is 0
            @$moreDetailsButton.hide()


    onLeaveScreen: ->
        value = @$('.input-details').val()
        @model.set 'details', value


    refresh: ->
        # We must apply a delta when an event is all-day long, because it starts
        # one day, and finishes the day after (if its duration is one day). But
        # we don't want to display the day afteras endDate, but the start day.
        #
        # e.g.: you set a all-day long event on 15/01/2015. The event starts at
{'fraction_non_alphanumeric': 0.1163357715081853, 'fraction_numerical': 0.0033960292580982234, 'mean_word_length': 1.9186785260482846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '4219575', 'n_tokens_mistral': 3270, 'n_tokens_neox': 3127, 'n_words': 793}