chowa
Version:
UI component library based on React
207 lines (206 loc) • 7.54 kB
JavaScript
/**
* @license chowa v1.1.3
*
* Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const moment = require("moment");
const utils_1 = require("../utils");
function isDisabledDate(date, disabledDate, granularity = 'day') {
if (!disabledDate) {
return false;
}
const { begin, end, before, after } = disabledDate;
let flag = false;
if (moment.isMoment(begin) && moment.isMoment(end)) {
flag = date.isBetween(begin, end, granularity)
|| date.isSame(begin, granularity)
|| date.isSame(end, granularity);
}
if (!flag && moment.isMoment(before)) {
flag = date.isBefore(before, granularity);
}
if (!flag && moment.isMoment(after)) {
flag = date.isAfter(after, granularity);
}
return flag;
}
exports.isDisabledDate = isDisabledDate;
function isRangeDate(date, rangeDate, granularity = 'day') {
if (!rangeDate) {
return false;
}
const { begin, end } = rangeDate;
if (moment.isMoment(begin) && moment.isMoment(end)) {
return date.isBetween(begin, end, granularity);
}
return false;
}
exports.isRangeDate = isRangeDate;
function hasActiveMoment(mom, values, granularity = 'day') {
if (!Array.isArray(values)) {
return false;
}
return values.some((value) => utils_1.isSameMoment(value, mom, granularity));
}
exports.hasActiveMoment = hasActiveMoment;
function compileRowEvents(events, rowMoms, granularity) {
const ret = [];
const amount = rowMoms.length;
const firstMom = rowMoms[0];
const lastMom = rowMoms[amount - 1];
const rowEvents = [];
if (!Array.isArray(events)) {
return ret;
}
events.forEach((event, key) => {
const { start: orginStart, finish: originFinish, type, content, category } = event;
const start = orginStart.clone();
const finish = originFinish.clone();
if (granularity === 'day') {
start.hour(0).minute(0).second(0);
finish.hour(0).minute(0).second(0);
}
else {
start.date(5).hour(0).minute(0).second(0);
finish.date(5).hour(0).minute(0).second(0);
}
if (start.isBetween(firstMom, lastMom, granularity)
|| finish.isBetween(firstMom, lastMom, granularity)
|| start.isSame(firstMom, granularity)
|| finish.isSame(lastMom, granularity)
|| start.isSame(lastMom, granularity)
|| finish.isSame(firstMom, granularity)) {
const index = start.isBefore(firstMom, granularity)
? 0
: start.diff(firstMom, granularity);
const span = finish.isAfter(lastMom, granularity)
? amount - index
: start.isBefore(firstMom, granularity)
? finish.diff(firstMom, granularity) + 1
: finish.diff(start, granularity) + 1;
rowEvents.push({
eventIndex: key,
index,
span,
type: utils_1.isExist(type) ? type : 'info',
begin: start.isSame(rowMoms[index], granularity),
end: finish.isSame(rowMoms[index + span - 1], granularity),
content,
category,
start: orginStart,
finish: originFinish
});
}
else if (start.isBefore(firstMom, granularity) && finish.isAfter(lastMom, granularity)) {
rowEvents.push({
eventIndex: key,
index: 0,
span: amount,
type: utils_1.isExist(type) ? type : 'info',
begin: false,
end: false,
content,
category,
start: orginStart,
finish: originFinish
});
}
});
rowEvents.forEach((rowEvent) => {
const appended = ret.some((row) => {
const lastIndex = row.reduce((pre, current) => {
const endIndex = current.index + current.span - 1;
return pre > endIndex ? pre : endIndex;
}, 0);
if (lastIndex < rowEvent.index) {
row.push(rowEvent);
return true;
}
return false;
});
if (!appended) {
ret.push([rowEvent]);
}
});
return ret;
}
exports.compileRowEvents = compileRowEvents;
function compileColumnEvents(events, mom) {
const ret = [];
const rangeGranularity = 'second';
const granularity = 'minute';
const amount = 24;
const colEvents = [];
if (!Array.isArray(events)) {
return ret;
}
const firstMom = mom.clone().hour(0).minute(0).second(0);
const lastMom = mom.clone().hour(24).minute(0).second(0);
events.forEach((event, key) => {
const { start, finish, type, content, category } = event;
if (start.isBetween(firstMom, lastMom, rangeGranularity)
|| finish.isBetween(firstMom, lastMom, rangeGranularity)
|| start.isSame(firstMom, rangeGranularity)
|| finish.isSame(lastMom, rangeGranularity)) {
const index = start.isBefore(firstMom, granularity)
? 0
: start.diff(firstMom, granularity) / 60;
const span = finish.isAfter(lastMom, granularity)
? amount - index
: (start.isBefore(firstMom, granularity)
? finish.diff(firstMom, granularity)
: finish.diff(start, granularity)) / 60;
const diffStart = mom.clone().hour(0).minute(index * 60).second(0);
const diffFinish = mom.clone().hour(0).minute(Math.round(index + span) * 60).second(0);
colEvents.push({
eventIndex: key,
index,
span,
type: utils_1.isExist(type) ? type : 'info',
begin: start.isSame(diffStart, granularity) || diffStart.isBefore(start, granularity),
end: finish.isSame(diffFinish, granularity) || diffFinish.isAfter(finish, granularity),
content,
category,
start,
finish
});
}
else if (start.isBefore(firstMom, granularity) && finish.isAfter(lastMom, granularity)) {
colEvents.push({
eventIndex: key,
index: 0,
span: amount,
type: utils_1.isExist(type) ? type : 'info',
begin: false,
end: false,
content,
category,
start,
finish
});
}
});
colEvents.forEach((colEvent) => {
const appended = ret.some((col) => {
const lastIndex = col.reduce((pre, current) => {
const endIndex = current.index + current.span - 1;
return pre > endIndex ? pre : endIndex;
}, 0);
if (lastIndex < colEvent.index) {
col.push(colEvent);
return true;
}
return false;
});
if (!appended) {
ret.push([colEvent]);
}
});
return ret;
}
exports.compileColumnEvents = compileColumnEvents;