UNPKG

@kelvininc/ui-components

Version:
90 lines (85 loc) 5.03 kB
'use strict'; require('./components-D2lyDQ_a.js'); require('./lib-config-QLtHwxiM.js'); require('./action-button.types-BYOe6st0.js'); var absoluteTimePickerDropdown_types = require('./absolute-time-picker-dropdown.types-mPwO9zQk.js'); require('./icon.types-B8lvUrX_.js'); require('./summary-card.types-W26sTdH1.js'); require('./toaster.types-DlSCye8T.js'); require('./tree-item.types-C2yRoORC.js'); require('./tag-alarm.types-BeZw-7PT.js'); require('./wizard.types-DTbE-B6d.js'); var date_helper = require('./date.helper-DwNTBYV0.js'); var relativeTimePicker_config = require('./relative-time-picker.config-BBTpZn3r.js'); /** Date range builders */ const buildOptionRange = (option, timeZone) => { switch (option.comparisonConfig) { case absoluteTimePickerDropdown_types.ERelativeTimeComparisonConfig.StartDateEndDate: return buildStartDateEndDateConfigRange(option, timeZone); case absoluteTimePickerDropdown_types.ERelativeTimeComparisonConfig.StartDate || absoluteTimePickerDropdown_types.ERelativeTimeComparisonConfig.EndDate: return buildSingleDateConfigRange(option, timeZone); case absoluteTimePickerDropdown_types.ERelativeTimeComparisonConfig.AbsoluteAmountOfUnits: return buildAbsoluteAmountOfUnitsConfigRange(option, timeZone); default: return buildRelativeAmountOfUnitsConfigRange(option, timeZone); } }; // Build date from config with start and end date const buildStartDateEndDateConfigRange = (option, timeZone) => { const startDateTime = date_helper.newTimezoneDateFromFormat(timeZone, option.startDate.dateFormat, option.startDate.date); const endDateTime = date_helper.newTimezoneDateFromFormat(timeZone, option.endDate.dateFormat, option.endDate.date); return buildDayjsRange(startDateTime, endDateTime); }; // Build date from config with start or end date const buildSingleDateConfigRange = (option, timeZone) => { const nowDateTime = date_helper.newTimezoneDate(timeZone); const calculatedDate = date_helper.newTimezoneDateFromFormat(timeZone, option.startDate.dateFormat, option.startDate.date); return buildDayjsRange(nowDateTime, calculatedDate); }; // Build date with both start and end date relative to now timestamp const buildAbsoluteAmountOfUnitsConfigRange = (option, timeZone) => { const nowDateTime = date_helper.newTimezoneDate(timeZone); const { unit: startDateUnit, amount: startDateAmount, unitReference: startUnitReference } = option.startDate; const { unit: endDateUnit, amount: endDateAmount, unitReference: endUnitReference } = option.endDate; const startDateTime = calculateDateWithUnitReference(date_helper.calculateDate(nowDateTime, startDateAmount, startDateUnit), startDateUnit, startUnitReference); const endDateTime = calculateDateWithUnitReference(date_helper.calculateDate(nowDateTime, endDateAmount, endDateUnit), endDateUnit, endUnitReference); return buildDayjsRange(startDateTime, endDateTime); }; // Build date with start date relative to now timestamp const buildRelativeAmountOfUnitsConfigRange = (option, timeZone) => { const nowDateTime = date_helper.newTimezoneDate(timeZone); const { amount, unit, unitReference } = option.startDate; const calculatedDate = calculateDateWithUnitReference(date_helper.calculateDate(nowDateTime, amount, unit), unit, unitReference); return buildDayjsRange(nowDateTime, calculatedDate); }; const calculateDateWithUnitReference = (date, unit, unitReference) => { if (!unitReference) { return date; } return unitReference === absoluteTimePickerDropdown_types.EUnitReference.StartOfUnit ? date.startOf(unit) : date.endOf(unit); }; /** * Builds the range of the start and end date and returns the range array, ordering the dates if needed * @param dateA date (start or end) * @param dateB date (start or end) * @returns range containing start date and end date ordered */ const buildDayjsRange = (dateA, dateB) => { return date_helper.isDateTimeBefore(dateA, dateB) ? [dateA, dateB] : [dateB, dateA]; }; const buildTimestampRange = ([startDate, endDate]) => [startDate.valueOf(), endDate.valueOf()]; const getRelativeTimeOption = (key, options = relativeTimePicker_config.DEFAULT_RELATIVE_TIME_OPTIONS_GROUPS) => options.flat().find(option => option.value === key); var relativeTime_helper = /*#__PURE__*/Object.freeze({ __proto__: null, buildAbsoluteAmountOfUnitsConfigRange: buildAbsoluteAmountOfUnitsConfigRange, buildDayjsRange: buildDayjsRange, buildOptionRange: buildOptionRange, buildRelativeAmountOfUnitsConfigRange: buildRelativeAmountOfUnitsConfigRange, buildSingleDateConfigRange: buildSingleDateConfigRange, buildStartDateEndDateConfigRange: buildStartDateEndDateConfigRange, buildTimestampRange: buildTimestampRange, getRelativeTimeOption: getRelativeTimeOption }); exports.buildOptionRange = buildOptionRange; exports.buildTimestampRange = buildTimestampRange; exports.relativeTime_helper = relativeTime_helper;