Blame view

online-office/src/controllers/cellDatePickerCtrl.js 3.67 KB
wangtao authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
import menuButton from './menuButton';
import formula from '../global/formula';
import Store from '../store';
import flatpickr from 'flatpickr'
import dayjs from "dayjs";
import { update, datenum_local } from '../global/format';
import { setCellValue, setCellFormat } from '../global/api';

const fitFormat = (formatStr) => {
    let dateFormat = formatStr.replace(/y/g, 'Y');
    dateFormat = dateFormat.replace(/d/g, 'D');
    dateFormat = dateFormat.replace(/h/g, 'H');

    dateFormat = dateFormat.replace(/上午\/下午/g, 'A');
    dateFormat = dateFormat.replace(/上午/g, 'A');
    dateFormat = dateFormat.replace(/下午/g, 'A');

    dateFormat = dateFormat.replace(/AM\/PM/g, 'A');
    dateFormat = dateFormat.replace(/AM/g, 'A');
    dateFormat = dateFormat.replace(/PM/g, 'A');
    dateFormat = dateFormat.replace(/\"/g, '');

    if (dateFormat.includes('A')) {
        dateFormat = dateFormat.replace(/H/g, 'h');
    }
    return dateFormat
}

const cellDatePickerCtrl = {
    cellFocus: function (r, c, cell) {
        let row = Store.visibledatarow[r],
            row_pre = r == 0 ? 0 : Store.visibledatarow[r - 1];
        let col = Store.visibledatacolumn[c],
            col_pre = c == 0 ? 0 : Store.visibledatacolumn[c - 1];

        let margeset = menuButton.mergeborer(Store.flowdata, r, c);
        let type = cell.ct.fa || 'YYYY-MM-DD';
        let defaultDate = update('yyyy-MM-dd hh:mm:ss', cell.v);
        let dateFormat = fitFormat(type);
        let enableTime = false;
        let noCalendar = false;
        let enableSeconds = false;
        let time_24hr = true;
        let hasChineseTime = false;


        if (!!margeset) {
            row = margeset.row[1];
            row_pre = margeset.row[0];

            col = margeset.column[1];
            col_pre = margeset.column[0];
        }

        $(".cell-date-picker").show().css({
            width: col - col_pre + 1,
            height: row - row_pre + 1,
            left: col_pre,
            top: row_pre
        })

        if (/[上午下午]/.test(type)) {
            hasChineseTime = true
        }
        if (/[Hhms]/.test(dateFormat)) {
            enableTime = true;
        }
        if (!/[YMD]/.test(dateFormat)) {
            noCalendar = true;
        }
        if (/s/.test(dateFormat)) {
            enableSeconds = true;
        }
        if (/A/.test(dateFormat)) {
            time_24hr = false;
        }

        const fp = flatpickr('#luckysheet-input-box', {
            allowInput: false,
            noCalendar,
            enableSeconds,
            enableTime,
            dateFormat,
            time_24hr,
            defaultDate,
            onClose() {
                setTimeout(() => {
                    fp.destroy()
                }, 0);
            },
            parseDate: (datestr, format) => {
                return dayjs(datestr).toDate();
            },
            formatDate: (date, format, locale) => {
                if (hasChineseTime) {
                    return dayjs(date).format(format).replace('AM', '上午').replace('PM', '下午')
                }
                return dayjs(date).format(format);
            },
            onChange: function (selectedDates, dateStr) {
                let currentVal = datenum_local(new Date(selectedDates))
                $("#luckysheet-rich-text-editor").html(dateStr);
                setCellValue(r, c, currentVal, { isRefresh: false })
                setCellFormat(r, c, 'ct', cell.ct)
                if (!enableTime) {
                    formula.updatecell(Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[1]);
                }
            }
        });

        $("#luckysheet-input-box").click();
    },
}

export default cellDatePickerCtrl;