<commit_before>/*jslint nomen: true*/
/*global define*/
define([
    'jquery', 'underscore', 'orotranslation/js/translator', 'moment', 'oroui/js/tools',
    'orolocale/js/formatter/datetime', 'orolocale/js/locale-settings', 'oroui/js/app/views/base/view'
],
function ($, _, __, moment, tools, datetimeFormatter, localeSettings, BaseView) {
    'use strict';

    var ChangeDurationView = BaseView.extend({

        /**
         * Form row selector with fronted and backend inputs
         */
        formRowSelector: 'div.controls',

        /**
         * Frontend date input selector
         */
        dateHolderSelector: 'input.hasDatepicker',

        /**
         * Frontend time input selector
         */
        timeHolderSelector: 'input.ui-timepicker-input',

        /**
         * Backend start datetime input selector
         */
        startSelector: null,

        /**
         * Backend end datetime input selector
         */
        endSelector: null,

        /**
         * Original difference between start datetime and end datetime in seconds
         */
        diff: 0,

        /**
         * Format of date/datetime that original input accepts
         */
        backendFormat: datetimeFormatter.backendFormats.datetime,

        /**
         * Use native pickers of proper HTML-inputs
         */
        nativeMode: false,

        /**
         * Format of time that native date input accepts
         */
        nativeTimeFormat: 'HH:mm',

        /**
         * Format of date that native date input accepts
         */
        nativeDateFormat: 'YYYY-MM-DD',

        /**
         * @constructor
         */
        initialize: function (options) {
            this.nativeMode = tools.isMobile();
            this.startSelector = options.startSelector;
            this.endSelector = options.endSelector;
            this.storeDiff();
            this.bindEvents();
        },

        /**
         * Stores original difference between start datetime and end datetime in seconds
         */
        storeDiff: function() {
            this.diff = this.getTimestamp($(this.endSelector).val()) - this.getTimestamp($(this.startSelector).val());
        },

        /**
         * Binds event listeners which keeps original duration and fix end timestamp
         */
        bindEvents: function() {
            var self = this;
            $(this.startSelector).on('change', function() {
                self.applyEndTimestamp(self.getTimestamp($(self.startSelector).val()) + self.diff);
            });

            $(this.endSelector).on('change', function() {
                var start = self.getTimestamp($(self.startSelector).val());
                if (self.getTimestamp($(self.endSelector).val()) < start) {
                    self.applyEndTimestamp(start);
                }
            });
        },

        /**
         * Applies timestamp to frontend and backend inputs
         *
         * @param endTimestamp
         */
        applyEndTimestamp: function(endTimestamp) {
            var momentInstance = moment.utc(endTimestamp, 'X');

            $(this.endSelector).val(momentInstance.format(this.getBackendFormat()));

            momentInstance.add(localeSettings.getTimeZoneShift(), 'm');
            var endDateHolder = $(this.endSelector).parents(this.formRowSelector).find(this.dateHolderSelector)[0];
            $(endDateHolder).val(momentInstance.format(this.getDateFormat()));

            var endTimeHolder = $(this.endSelector).parents(this.formRowSelector).find(this.timeHolderSelector)[0];
            $(endTimeHolder).val(momentInstance.format(this.getTimeFormat()));
        },

        /**
         * Calculates unix timestamp
         *
         * @param value
         * @returns {*}
         */
        getTimestamp: function(value) {
            return moment.utc(value, this.getBackendFormat(), true).unix();
        },

        /**
         * Defines backend format for datetime field
         *
         * @returns {string}
         */
        getBackendFormat: function() {
            return _.isArray(this.backendFormat) ? this.backendFormat[0] : this.backendFormat;
        },<commit_msg>BAP-7667: Calendar Event - End Date/Time should update when Start Date/Time is updated. - End date is not updated when start date is not valid. - Fixed end date less than start date in some cases.<commit_after>/*jslint nomen: true*/
/*global define*/
define([
    'jquery', 'underscore', 'orotranslation/js/translator', 'moment', 'oroui/js/tools',
    'orolocale/js/formatter/datetime', 'orolocale/js/locale-settings', 'oroui/js/app/views/base/view'
],
function ($, _, __, moment, tools, datetimeFormatter, localeSettings, BaseView) {
    'use strict';

    var ChangeDurationView = BaseView.extend({

        /**
         * Form row selector with fronted and backend inputs
         */
        formRowSelector: 'div.controls',

        /**
         * Frontend date input selector
         */
        dateHolderSelector: 'input.hasDatepicker',

        /**
         * Frontend time input selector
         */
        timeHolderSelector: 'input.ui-timepicker-input',

        /**
         * Backend start datetime input selector
         */
        startSelector: null,

        /**
         * Backend end datetime input selector
         */
        endSelector: null,

        /**
         * Original difference between start datetime and end datetime in seconds
         */
        diff: 0,

        /**
         * Format of date/datetime that original input accepts
         */
        backendFormat: datetimeFormatter.backendFormats.datetime,

        /**
         * Use native pickers of proper HTML-inputs
         */
        nativeMode: false,

        /**
         * Format of time that native date input accepts
         */
        nativeTimeFormat: 'HH:mm',

        /**
         * Format of date that native date input accepts
         */
        nativeDateFormat: 'YYYY-MM-DD',

        /**
         * @constructor
         */
        initialize: function (options) {
            this.nativeMode = tools.isMobile();
            this.startSelector = options.startSelector;
            this.endSelector = options.endSelector;
            this.storeDiff();
            this.bindEvents();
        },

        /**
         * Stores original difference between start datetime and end datetime in seconds
         */
        storeDiff: function() {
            this.diff = this.getTimestamp($(this.endSelector).val()) - this.getTimestamp($(this.startSelector).val());
        },

        /**
         * Binds event listeners which keeps original duration and fix end timestamp
         */
        bindEvents: function() {
            var self = this;
            $(this.startSelector).on('change', function() {
                var value = $(self.startSelector).val();
                if (self.isValid(value)) {
                    self.applyEndTimestamp(self.getTimestamp(value) + self.diff);
                }
            });

            $(this.endSelector).on('change', function() {
                var startValue = $(self.startSelector).val();
                var endValue = $(self.endSelector).val();
                if (self.isValid(startValue) && self.isValid(endValue)) {
                    var start = self.getTimestamp(startValue);
                    if (self.getTimestamp(endValue) < start) {
                        self.applyEndTimestamp(start);
                    }
                }
            });
        },

        /**
         * Applies timestamp to frontend and backend inputs
         *
         * @param endTimestamp
         */
        applyEndTimestamp: function(endTimestamp) {
            var momentInstance = moment.utc(endTimestamp, 'X');

            $(this.endSelector).val(momentInstance.format(this.getBackendFormat()));
            $(this.endSelector).trigger('change');

            momentInstance.add(localeSettings.getTimeZoneShift(), 'm');
            var endDateHolder = $(this.endSelector).parents(this.formRowSelector).find(this.dateHolderSelector)[0];
            $(endDateHolder).val(momentInstance.format(this.getDateFormat()));
            $(endDateHolder).trigger('change');

            var endTimeHolder = $(this.endSelector).parents(this.formRowSelector).find(this.timeHolderSelector)[0];
            $(endTimeHolder).val(momentInstance.format(this.getTimeFormat()));
            $(endTimeHolder).trigger('change');
        },

        /**
         * Returns whether the value is valid date string
         *
         * @param value
         * @returns {*|Boolean}
         */
        isValid: function(value) {
            return moment.utc(value, this.getBackendFormat(), true).isValid();
        },

        /**
         * Calculates unix timestamp
         *
         * @param value
         * @returns {*}
         */
        getTimestamp: function(value) {
            return moment.utc(value, this.getBackendFormat(), true).unix();
        },

        /**
         * Defines backend format for datetime field
         *
         * @returns {string}
         */
        getBackendFormat: function() {
            return _.isArray(this.backendFormat) ? this.backendFormat[0] : this.backendFormat;
        },
{'fraction_non_alphanumeric': 0.11886976291003573, 'fraction_numerical': 0.0012991230919129587, 'mean_word_length': 1.8302696078431373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3876633', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2284, 'n_words': 505}