UNPKG

qt-public-ui

Version:

新设计规范下业务组件库

213 lines (210 loc) 10.6 kB
import constants, { radioOptions, tabOptions } from './constant'; import moment from 'moment'; export var DATE_TYPE = 'YYYY-MM-DD'; export var DATE_TIME_TYPE = 'YYYY-MM-DD HH:mm'; var RadioKey = ['today', 'yesterday', 'recent7Day', 'recent15Day', 'recent15DayFromToday', 'recent7DayFromToday', 'recent30Day', 'recent60Day', 'recent90Day', 'recent30DayFromToday', 'recent48Hour', 'recent24Hour', 'recent1Hour', 'thisWeek', 'lastWeek', 'thisMonth', 'lastMonth']; var getDateRange = function getDateRange(key) { switch (key) { case 'recent24Hour': return [moment().subtract(23, 'h'), moment()]; case 'recent48Hour': return [moment().subtract(47, 'h'), moment()]; case 'recent1Hour': return [moment().subtract(59, 'm'), moment()]; case 'today': return [moment().startOf('d'), moment().endOf('d')]; case 'yesterday': return [moment().subtract(1, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent7Day': return [moment().subtract(7, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent7DayFromToday': return [moment().subtract(6, 'd').startOf('d'), moment().endOf('d')]; case 'recent15Day': return [moment().subtract(15, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent15DayFromToday': return [moment().subtract(14, 'd').startOf('d'), moment().endOf('d')]; case 'recent30Day': return [moment().subtract(30, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent30DayFromToday': return [moment().subtract(29, 'd').startOf('d'), moment().endOf('d')]; case 'recent60Day': return [moment().subtract(60, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent60DayFromToday': return [moment().subtract(59, 'd').startOf('d'), moment().endOf('d')]; case 'recent90Day': return [moment().subtract(90, 'd').startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'recent90DayFromToday': return [moment().subtract(89, 'd').startOf('d'), moment().endOf('d')]; case 'thisWeek': return [moment().isoWeekday(1).startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'lastWeek': return [moment().isoWeekday(-6).startOf('d'), moment().isoWeekday(0).endOf('d')]; case 'thisMonth': return [moment().set('date', 1).startOf('d'), moment().subtract(1, 'd').endOf('d')]; case 'lastMonth': return [moment().set('month', moment().get('month') - 1).set('date', 1).startOf('d'), moment().set('date', 1).subtract(1, 'd').endOf('d')]; default: return null; } }; var getFlatTabs = function getFlatTabs(tabs) { return tabs.map(function (tab) { return typeof tab === 'object' ? tab.key : tab; }); }; var isRadioType = function isRadioType(value) { return radioOptions.some(function (opt) { return opt.value === value; }); }; var isTabType = function isTabType(value) { return tabOptions.some(function (opt) { return opt.value === value; }); }; var hasHourShortcut = function hasHourShortcut(radios) { return radios.indexOf('recent48Hour') > -1 || radios.indexOf('recent24Hour') > -1 || radios.indexOf('recent1Hour') > -1; }; export var getValueByStatus = function getValueByStatus(status) { return status ? getDateRange(status) || [] : []; }; export var dateType2Date = function dateType2Date(initialValue) { return Array.isArray(initialValue) ? initialValue : getValueByStatus(initialValue); }; export var getInitialTab = function getInitialTab(startDate, endDate) { // 根据Calendar tab设置datetype 的函数 var _startDate = moment(startDate); var _endDate = moment(endDate); var diff = moment(_endDate).diff(moment(_startDate), 'days') + 1; // console.log('getInitialTab',diff,_startDate,_endDate) if (diff === 1) return 'unit_day'; if (diff === 7) { return _startDate.day() === 1 && _endDate.day() === 0 ? 'unit_week' : 'unit_7day'; } if (_startDate.format(DATE_TYPE) === _startDate.startOf('month').format(DATE_TYPE) && _endDate.format(DATE_TYPE) === _endDate.endOf('month').format(DATE_TYPE)) return 'unit_month'; if (diff === 15) return 'unit_15day'; if (diff === 30) return 'unit_30day'; if (diff === 60) return 'unit_60day'; if (diff === 90) return 'unit_90day'; return 'unit_custom'; }; export var getValidDateType = function getValidDateType(value, radios, tabs) { var getValidRadioStatus = function getValidRadioStatus(rValue) { // radios配置为默认空数组 if (!radios.length) { if (rValue === 'recent7DayFromToday') return getValidTabStatus('unit_7day'); if (rValue === 'recent15DayFromToday') return getValidTabStatus('unit_15day'); if (rValue === 'recent30DayFromToday') return getValidTabStatus('unit_30day'); if (rValue === 'recent60DayFromToday') return getValidTabStatus('unit_60day'); if (rValue === 'recent90DayFromToday') return getValidTabStatus('unit_90day'); return rValue; } // canlander映射回来的value type 属于 radio if (radios.includes(rValue)) return rValue; // canlander映射回来的value type 不属于 radio if (['today', 'yesterday'].includes(rValue)) return getValidTabStatus('unit_day'); if (['recent7Day', 'recent7DayFromToday'].includes(rValue)) return getValidTabStatus('unit_7day'); if (['recent15Day', 'recent15DayFromToday'].includes(rValue)) return getValidTabStatus('unit_15day'); if (['recent30Day', 'recent30DayFromToday'].includes(rValue)) return getValidTabStatus('unit_30day'); if (['recent60Day', 'recent60DayFromToday'].includes(rValue)) return getValidTabStatus('unit_60day'); if (['recent90Day', 'recent90DayFromToday'].includes(rValue)) return getValidTabStatus('unit_90day'); if (['recent30Day'].includes(rValue)) return getValidTabStatus('unit_30day'); // if (['lastWeek'].includes(rValue)) return getValidTabStatus('unit_week'); // if (['lastMonth'].includes(rValue)) return getValidTabStatus('unit_month'); return 'unit_custom'; }; var getValidTabStatus = function getValidTabStatus(tValue) { if (!tabs.length) return 'unit_custom'; if (tabs.includes(tValue)) return tValue; if (['unit_week'].includes(tValue)) return getValidTabStatus('unit_7day'); if (['unit_month'].includes(tValue)) return getValidTabStatus('unit_30day'); // if (['unit_day', 'unit_week', 'unit_month'].includes(tValue)) return tValue; return 'unit_custom'; }; return isRadioType(value) ? getValidRadioStatus(value) : getValidTabStatus(value); }; export var date2DateType = function date2DateType(initialValue, radios, tabs) { if (radios === void 0) { radios = []; } if (tabs === void 0) { tabs = []; } var flatTab = getFlatTabs(tabs); // initialValue不是数组表示点击的是Radio if (!Array.isArray(initialValue)) return initialValue ? getValidDateType(initialValue, radios, flatTab) : 'unit_custom'; // initialValue表示点击的是DatePicker var startDate = initialValue[0], endDate = initialValue[1]; var radioKey; var mapRadio = radios.length > 0 ? radios : RadioKey; mapRadio.some(function (key) { var _getDateRange, _getDateRange2, _getDateRange3, _getDateRange4; var isEqual = hasHourShortcut(radios) ? ((_getDateRange = getDateRange(key)) === null || _getDateRange === void 0 ? void 0 : _getDateRange[0].format(DATE_TIME_TYPE)) === startDate.format(DATE_TIME_TYPE) && ((_getDateRange2 = getDateRange(key)) === null || _getDateRange2 === void 0 ? void 0 : _getDateRange2[1].format(DATE_TIME_TYPE)) === endDate.format(DATE_TIME_TYPE) : ((_getDateRange3 = getDateRange(key)) === null || _getDateRange3 === void 0 ? void 0 : _getDateRange3[0].format(DATE_TYPE)) === startDate.format(DATE_TYPE) && ((_getDateRange4 = getDateRange(key)) === null || _getDateRange4 === void 0 ? void 0 : _getDateRange4[1].format(DATE_TYPE)) === endDate.format(DATE_TYPE); isEqual && (radioKey = key); return isEqual; }); var dateType = radioKey || getInitialTab(startDate, endDate); // console.log(radioKey,'radioKeyradioKeyradioKeyradioKey',radios,11111,getValidDateType(dateType, radios, flatTab)) return getValidDateType(dateType, radios, flatTab); }; export var getTempDateType = function getTempDateType(lastType, tabOpts) { if (tabOpts === void 0) { tabOpts = []; } if (isTabType(lastType)) return lastType; return tabOptions[0] ? tabOpts[0].value : 'unit_custom'; }; export var getRangeByDate = function getRangeByDate(date, tabStatus, index) { // console.log('getRangeByDate',date,tabStatus) if (tabStatus === 'unit_custom') return date; var _date = date; var start = moment(_date); var end = moment(_date); if (index === 0) { switch (tabStatus) { case 'unit_day': return [start.startOf('d'), end.endOf('d')]; case 'unit_7day': return [start.startOf('d'), end.add(6, 'days').endOf('d')]; case 'unit_15day': return [start.startOf('d'), end.add(14, 'days').endOf('d')]; case 'unit_30day': return [start.startOf('d'), end.add(29, 'days').endOf('d')]; case 'unit_60day': return [start.startOf('d'), end.add(59, 'days').endOf('d')]; case 'unit_90day': return [start.startOf('d'), end.add(89, 'days').endOf('d')]; case 'unit_week': return [start.startOf('week').startOf('d'), end.endOf('week').endOf('d')]; case 'unit_month': return [start.startOf('month').startOf('d'), end.endOf('month').endOf('d')]; default: return [start, end]; } } else { switch (tabStatus) { case 'unit_day': return [start.startOf('d'), end.endOf('d')]; case 'unit_7day': return [start.subtract(6, 'day').startOf('d'), end.endOf('d')]; case 'unit_15day': return [start.subtract(14, 'day').startOf('d'), end.endOf('d')]; case 'unit_30day': return [start.subtract(29, 'day').startOf('d'), end.endOf('d')]; case 'unit_60day': return [start.subtract(59, 'day').startOf('d'), end.endOf('d')]; case 'unit_90day': return [start.subtract(89, 'day').startOf('d'), end.endOf('d')]; case 'unit_week': return [start.startOf('week').startOf('d'), end.endOf('week').endOf('d')]; case 'unit_month': return [start.startOf('month').startOf('d'), end.endOf('month').endOf('d')]; default: return [start, end]; } } }; export var getValidUsePannels = function getValidUsePannels(usePannels) { return usePannels === undefined ? constants.getFullTabs() : usePannels; };