Browse Source

KERNEL-14124 refactor: 优化优化

es6
Zhenfei.Li 1 year ago
parent
commit
434491ddf2
  1. 82
      src/core/func/date.js
  2. 22
      src/core/func/function.js
  3. 152
      src/core/platform/web/config.js

82
src/core/func/date.js

@ -1,5 +1,7 @@
/** Constants used for time computations */
import { getDate, getTime, parseInt } from "../2.base";
import { getDate, getTime, parseInt, i18nText } from "../2.base";
import { StartOfWeek } from "../constant";
import { isKhtml } from "../platform/web";
const SECOND = 1000;
const MINUTE = 60 * SECOND;
@ -41,7 +43,7 @@ export const Date = {
* @returns {String}
*/
export function getTimezone(date) {
return date.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, "");
return date.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, "");
}
/**
@ -94,7 +96,7 @@ export function getDayOfYear(date) {
export function getWeekNumber(date) {
const d = getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
const week = d.getDay();
const startOfWeek = BI.StartOfWeek % 7;
const startOfWeek = StartOfWeek % 7;
let middleDay = (startOfWeek + 3) % 7;
middleDay = middleDay || 7;
// 偏移到周周首之前需要多少天
@ -125,7 +127,7 @@ export function getOffsetDate(date, offset) {
export function getOffsetQuarter(date, n) {
const dt = getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
let day = dt.getDate();
const monthDay = BI.getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10) * 3, 1));
const monthDay = getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10) * 3, 1));
if (day > monthDay) {
day = monthDay;
}
@ -205,7 +207,7 @@ export function getOffsetMonth(date, n) {
*/
export function getWeekStartDate(date) {
const w = date.getDay();
const startOfWeek = BI.StartOfWeek % 7;
const startOfWeek = StartOfWeek % 7;
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)]);
}
@ -217,61 +219,61 @@ export function getWeekStartDate(date) {
*/
export function getWeekEndDate(date) {
const w = date.getDay();
const startOfWeek = BI.StartOfWeek % 7;
const startOfWeek = StartOfWeek % 7;
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)] + 6);
}
export function getFullDayName(index) {
return [
BI.i18nText("BI-Basic_Sunday"),
BI.i18nText("BI-Basic_Monday"),
BI.i18nText("BI-Basic_Tuesday"),
BI.i18nText("BI-Basic_Wednesday"),
BI.i18nText("BI-Basic_Thursday"),
BI.i18nText("BI-Basic_Friday"),
BI.i18nText("BI-Basic_Saturday"),
BI.i18nText("BI-Basic_Sunday")
i18nText("BI-Basic_Sunday"),
i18nText("BI-Basic_Monday"),
i18nText("BI-Basic_Tuesday"),
i18nText("BI-Basic_Wednesday"),
i18nText("BI-Basic_Thursday"),
i18nText("BI-Basic_Friday"),
i18nText("BI-Basic_Saturday"),
i18nText("BI-Basic_Sunday")
][index];
}
export function getShortDayName(index) {
return [
BI.i18nText("BI-Basic_Simple_Sunday"),
BI.i18nText("BI-Basic_Simple_Monday"),
BI.i18nText("BI-Basic_Simple_Tuesday"),
BI.i18nText("BI-Basic_Simple_Wednesday"),
BI.i18nText("BI-Basic_Simple_Thursday"),
BI.i18nText("BI-Basic_Simple_Friday"),
BI.i18nText("BI-Basic_Simple_Saturday"),
BI.i18nText("BI-Basic_Simple_Sunday")
i18nText("BI-Basic_Simple_Sunday"),
i18nText("BI-Basic_Simple_Monday"),
i18nText("BI-Basic_Simple_Tuesday"),
i18nText("BI-Basic_Simple_Wednesday"),
i18nText("BI-Basic_Simple_Thursday"),
i18nText("BI-Basic_Simple_Friday"),
i18nText("BI-Basic_Simple_Saturday"),
i18nText("BI-Basic_Simple_Sunday")
][index];
}
export function getMonthName(index) {
return [
BI.i18nText("BI-Basic_January"),
BI.i18nText("BI-Basic_February"),
BI.i18nText("BI-Basic_March"),
BI.i18nText("BI-Basic_April"),
BI.i18nText("BI-Basic_May"),
BI.i18nText("BI-Basic_June"),
BI.i18nText("BI-Basic_July"),
BI.i18nText("BI-Basic_August"),
BI.i18nText("BI-Basic_September"),
BI.i18nText("BI-Basic_October"),
BI.i18nText("BI-Basic_November"),
BI.i18nText("BI-Basic_December")
i18nText("BI-Basic_January"),
i18nText("BI-Basic_February"),
i18nText("BI-Basic_March"),
i18nText("BI-Basic_April"),
i18nText("BI-Basic_May"),
i18nText("BI-Basic_June"),
i18nText("BI-Basic_July"),
i18nText("BI-Basic_August"),
i18nText("BI-Basic_September"),
i18nText("BI-Basic_October"),
i18nText("BI-Basic_November"),
i18nText("BI-Basic_December")
][index];
}
export function getQuarterName(index) {
return [
"",
BI.i18nText("BI-Quarter_1"),
BI.i18nText("BI-Quarter_2"),
BI.i18nText("BI-Quarter_3"),
BI.i18nText("BI-Quarter_4")
i18nText("BI-Quarter_1"),
i18nText("BI-Quarter_2"),
i18nText("BI-Quarter_3"),
i18nText("BI-Quarter_4")
][index];
}
@ -341,7 +343,7 @@ export function print(date, str) {
s["%Q"] = qr;
let re = /%./g;
BI.isKhtml = BI.isKhtml || function () {
isKhtml = isKhtml || function () {
if (!_global.navigator) {
return false;
}
@ -371,7 +373,7 @@ export function print(date, str) {
}
}
if (!BI.isKhtml()) {
if (!isKhtml()) {
return str.replace(re, par => s[par] || par);
}
const a = str.match(re);

22
src/core/func/function.js

@ -2,9 +2,10 @@
* 基本的函数
* Created by GUY on 2015/6/24.
*/
import { every, isKey, isArray, toUpperCase, each, stripEL, isNotNull, isNull, isObject } from "../2.base";
import { every, isKey, isArray, toUpperCase, each, stripEL, isNotNull, isNull, isObject, flatten, isFunction } from "../2.base";
import { makeFirstPY } from "../utils/chinesePY";
import { CODE_INDEX } from "@/third/sort.gb2312";
import { MAX } from "../constant";
/**
* 创建唯一的名字
@ -17,6 +18,7 @@ export function createDistinctName(array, name) {
let idx = 1;
name = name || "";
while (true) {
// eslint-disable-next-line no-loop-func
if (every(array, (i, item) => isKey(item) ? item !== name : item.name !== name)) {
break;
}
@ -46,7 +48,7 @@ export function getGBWidth(str) {
*/
export function getSearchResult(items, keyword, param) {
const array = isArray(items);
items = array ? BI.flatten(items) : items;
items = array ? flatten(items) : items;
param || (param = "text");
if (!isKey(keyword)) {
return {
@ -104,15 +106,15 @@ export function getSearchResult(items, keyword, param) {
* @return {any[]}
*/
export function getSortedResult(items, key = null) {
const getTextOfItem = BI.isFunction(key) ? key :
const getTextOfItem = isFunction(key) ? key :
function (item, key) {
if (BI.isNotNull(key)) {
if (isNotNull(key)) {
return item[key];
}
if (BI.isNotNull(item.text)) {
if (isNotNull(item.text)) {
return item.text;
}
if (BI.isNotNull(item.value)) {
if (isNotNull(item.value)) {
return item.value;
}
@ -122,13 +124,13 @@ export function getSortedResult(items, key = null) {
return items.sort((item1, item2) => {
const str1 = getTextOfItem(item1, key);
const str2 = getTextOfItem(item2, key);
if (BI.isNull(str1) && BI.isNull(str2)) {
if (isNull(str1) && isNull(str2)) {
return 0;
}
if (BI.isNull(str1)) {
if (isNull(str1)) {
return -1;
}
if (BI.isNull(str2)) {
if (isNull(str2)) {
return 1;
}
if (str1 === str2) {
@ -140,7 +142,7 @@ export function getSortedResult(items, key = null) {
const char2 = str2[i];
if (char1 !== char2) {
// 找不到的字符都往后面放
return (BI.isNull(CODE_INDEX[char1]) ? BI.MAX : CODE_INDEX[char1]) - (BI.isNull(CODE_INDEX[char2]) ? BI.MAX : CODE_INDEX[char2]);
return (isNull(CODE_INDEX[char1]) ? MAX : CODE_INDEX[char1]) - (isNull(CODE_INDEX[char2]) ? MAX : CODE_INDEX[char2]);
}
}

152
src/core/platform/web/config.js

@ -1,4 +1,8 @@
import { extend, map, each, isNull } from "../../2.base";
import { Providers } from "../../5.inject";
import { Plugin } from "../../6.plugin";
import { isSupportCss3, isIE, getIEVersion } from "../../platform/web";
import { HorizontalAlign, VerticalAlign } from "../../constant";
// 工程配置
// 注册布局
@ -10,7 +14,7 @@ let _isSupportFlex, _isSupportGrid;
function isSupportFlex() {
if (!_isSupportFlex) {
_isSupportFlex = !!(BI.isSupportCss3 && BI.isSupportCss3("flex"));
_isSupportFlex = !!(isSupportCss3 && isSupportCss3("flex"));
}
return _isSupportFlex;
@ -18,7 +22,7 @@ function isSupportFlex() {
function isSupportGrid() {
if (!_isSupportGrid) {
_isSupportGrid = !!(BI.isSupportCss3 && BI.isSupportCss3("grid"));
_isSupportGrid = !!(isSupportCss3 && isSupportCss3("grid"));
}
return _isSupportGrid;
@ -46,27 +50,27 @@ configWidget("bi.horizontal", ob => {
const supportFlex = isSupportFlex();
// // 在横向自适应场景下我们需要使用table的自适应撑出滚动条的特性(flex处理不了这种情况)
// // 主要出现在center_adapt或者horizontal_adapt的场景,或者主动设置horizontalAlign的场景
// if (ob.horizontalAlign === BI.HorizontalAlign.Center || ob.horizontalAlign === BI.HorizontalAlign.Stretch) {
// return BI.extend({}, ob, {type: "bi.table_adapt"});
// if (ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch) {
// return extend({}, ob, {type: "bi.table_adapt"});
// }
if (supportFlex) {
return BI.extend({}, ob, { type: "bi.flex_horizontal" });
return extend({}, ob, { type: "bi.flex_horizontal" });
}
return BI.extend({
return extend({
scrollx: true,
}, ob, { type: "bi.inline" });
});
configWidget("bi.vertical", ob => {
if (ob.horizontalAlign === BI.HorizontalAlign.Left || ob.horizontalAlign === BI.HorizontalAlign.Right) {
if (ob.horizontalAlign === HorizontalAlign.Left || ob.horizontalAlign === HorizontalAlign.Right) {
if (isSupportFlex()) {
return BI.extend({}, ob, { type: "bi.flex_vertical" });
return extend({}, ob, { type: "bi.flex_vertical" });
}
return BI.extend({}, ob, {
horizontalAlign: BI.HorizontalAlign.Stretch,
return extend({}, ob, {
horizontalAlign: HorizontalAlign.Stretch,
type: "bi.vertical",
items: BI.map(ob.items, (i, item) => {
items: map(ob.items, (i, item) => {
return {
type: "bi.inline",
horizontalAlign: ob.horizontalAlign,
@ -75,10 +79,10 @@ configWidget("bi.vertical", ob => {
}),
});
}
if (ob.verticalAlign === BI.VerticalAlign.Stretch) {
if (ob.verticalAlign === VerticalAlign.Stretch) {
if (isSupportFlex()) {
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Stretch,
return extend({
horizontalAlign: HorizontalAlign.Stretch,
}, ob, { type: "bi.flex_vertical" });
}
}
@ -94,10 +98,10 @@ configWidget("bi.inline", ob => {
}
} else {
let hasAuto = false, hasFill = false;
BI.each(ob.items, (i, item) => {
each(ob.items, (i, item) => {
if (item.width === "fill") {
hasFill = true;
} else if (BI.isNull(item.width) || item.width === "" || item.width === "auto") {
} else if (isNull(item.width) || item.width === "" || item.width === "auto") {
hasAuto = true;
}
});
@ -106,80 +110,80 @@ configWidget("bi.inline", ob => {
if (hasAutoAndFillColumnSize) {
// 宽度是不是受限
if ((ob.scrollable !== true && ob.scrollx !== true) || ob.horizontalAlign === BI.HorizontalAlign.Stretch) {
return BI.extend({
verticalAlign: BI.VerticalAlign.Top,
if ((ob.scrollable !== true && ob.scrollx !== true) || ob.horizontalAlign === HorizontalAlign.Stretch) {
return extend({
verticalAlign: VerticalAlign.Top,
}, ob, { type: "bi.horizontal_float_fill" });
}
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Stretch,
return extend({
horizontalAlign: HorizontalAlign.Stretch,
}, ob, { type: "bi.table_adapt" });
}
if (BI.Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return BI.extend({}, ob, { type: "bi.responsive_inline" });
if (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_inline" });
}
return ob;
});
configWidget("bi.center_adapt", ob => {
const supportFlex = isSupportFlex();
// var isAdapt = !ob.horizontalAlign || ob.horizontalAlign === BI.HorizontalAlign.Center || ob.horizontalAlign === BI.HorizontalAlign.Stretch;
// var isAdapt = !ob.horizontalAlign || ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
// if (!isAdapt || justOneItem) {
if (supportFlex) {
return BI.extend({}, ob, { type: "bi.flex_center_adapt" });
return extend({}, ob, { type: "bi.flex_center_adapt" });
}
return BI.extend({}, ob, { type: "bi.inline_center_adapt" });
return extend({}, ob, { type: "bi.inline_center_adapt" });
// }
// return ob;
});
configWidget("bi.vertical_adapt", ob => {
const supportFlex = isSupportFlex();
// var isAdapt = ob.horizontalAlign === BI.HorizontalAlign.Center || ob.horizontalAlign === BI.HorizontalAlign.Stretch;
// var isAdapt = ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
// if (!isAdapt || justOneItem) {
if (supportFlex) {
return BI.extend({}, ob, { type: "bi.flex_vertical_center_adapt" });
return extend({}, ob, { type: "bi.flex_vertical_center_adapt" });
}
return BI.extend({}, ob, { type: "bi.inline_vertical_adapt" });
return extend({}, ob, { type: "bi.inline_vertical_adapt" });
// }
// return ob;
});
configWidget("bi.horizontal_adapt", ob => {
const justOneItem = (ob.items && ob.items.length <= 1);
const isAdapt = !ob.horizontalAlign || ob.horizontalAlign === BI.HorizontalAlign.Center || ob.horizontalAlign === BI.HorizontalAlign.Stretch;
const verticalAlignTop = !ob.verticalAlign || ob.verticalAlign === BI.VerticalAlign.TOP;
const isAdapt = !ob.horizontalAlign || ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
const verticalAlignTop = !ob.verticalAlign || ob.verticalAlign === VerticalAlign.TOP;
if (verticalAlignTop && justOneItem) {
return BI.extend({}, ob, { type: "bi.horizontal_auto" });
return extend({}, ob, { type: "bi.horizontal_auto" });
}
const supportFlex = isSupportFlex();
// 在横向自适应场景下我们需要使用table的自适应撑出滚动条的特性(flex处理不了这种情况)
// 主要出现在center_adapt或者horizontal_adapt的场景,或者主动设置horizontalAlign的场景
if (isAdapt) {
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Center,
return extend({
horizontalAlign: HorizontalAlign.Center,
}, ob, { type: "bi.table_adapt" });
}
if (supportFlex) {
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Center,
return extend({
horizontalAlign: HorizontalAlign.Center,
scrollx: false,
}, ob, { type: "bi.flex_horizontal" });
}
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Center,
return extend({
horizontalAlign: HorizontalAlign.Center,
}, ob, { type: "bi.table_adapt" });
});
configWidget("bi.horizontal_float", ob => {
if (isSupportFlex()) {
return BI.extend({}, ob, { type: "bi.flex_horizontal_adapt" });
return extend({}, ob, { type: "bi.flex_horizontal_adapt" });
}
if (ob.items && ob.items.length <= 1) {
return BI.extend({}, ob, { type: "bi.inline_horizontal_adapt" });
return extend({}, ob, { type: "bi.inline_horizontal_adapt" });
}
return ob;
@ -187,35 +191,35 @@ configWidget("bi.horizontal_float", ob => {
configWidget("bi.horizontal_fill", ob => {
if (isSupportFlex()) {
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Stretch,
verticalAlign: BI.VerticalAlign.Stretch,
return extend({
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
scrollx: false,
}, ob, { type: "bi.flex_horizontal" });
}
if ((ob.horizontalAlign && ob.horizontalAlign !== BI.HorizontalAlign.Stretch) || (ob.scrollable === true || ob.scrollx === true)) {
if ((ob.horizontalAlign && ob.horizontalAlign !== HorizontalAlign.Stretch) || (ob.scrollable === true || ob.scrollx === true)) {
// 宽度不受限,要用table布局
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Stretch,
verticalAlign: BI.VerticalAlign.Stretch,
return extend({
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
}, ob, { type: "bi.table_adapt" });
}
return BI.extend({}, ob, { type: "bi.horizontal_float_fill" });
return extend({}, ob, { type: "bi.horizontal_float_fill" });
});
configWidget("bi.vertical_fill", ob => {
if (isSupportFlex()) {
return BI.extend({
horizontalAlign: BI.HorizontalAlign.Stretch,
verticalAlign: BI.VerticalAlign.Stretch,
return extend({
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
scrolly: false,
}, ob, { type: "bi.flex_vertical" });
}
if (ob.scrollable === true || ob.scrollx === true || ob.scrolly === true) {
// 有滚动条,降级到table布局处理
return BI.extend({}, ob, {
return extend({}, ob, {
type: "bi.td",
items: BI.map(ob.items, (i, item) => [item]),
items: map(ob.items, (i, item) => [item]),
});
}
let hasAuto = false;
@ -224,35 +228,35 @@ configWidget("bi.vertical_fill", ob => {
hasAuto = true;
}
} else {
BI.each(ob.items, (i, item) => {
if (BI.isNull(item.height) || item.height === "") {
each(ob.items, (i, item) => {
if (isNull(item.height) || item.height === "") {
hasAuto = true;
}
});
}
if (hasAuto) {
// 有自动高的时候
return BI.extend({}, ob, { type: "bi.vtape_auto" });
return extend({}, ob, { type: "bi.vtape_auto" });
}
return BI.extend({}, ob, { type: "bi.vtape" });
return extend({}, ob, { type: "bi.vtape" });
});
configWidget("bi.horizontal_sticky", ob => {
if (!isSupportSticky) {
return BI.extend({ scrollx: true }, ob, { type: "bi.horizontal_fill" });
return extend({ scrollx: true }, ob, { type: "bi.horizontal_fill" });
}
});
configWidget("bi.vertical_sticky", ob => {
if (!isSupportSticky) {
return BI.extend({ scrolly: true }, ob, { type: "bi.vertical_fill" });
return extend({ scrolly: true }, ob, { type: "bi.vertical_fill" });
}
});
configWidget("bi.left_right_vertical_adapt", ob => {
if (isSupportFlex()) {
// IE下其实也是可以使用flex布局的,只要排除掉出现滚动条的情况
// if (!BI.isIE() || (ob.scrollable !== true && ob.scrolly !== true)) {
return BI.extend({}, ob, { type: "bi.flex_left_right_vertical_adapt" });
// if (!isIE() || (ob.scrollable !== true && ob.scrolly !== true)) {
return extend({}, ob, { type: "bi.flex_left_right_vertical_adapt" });
// }
}
@ -261,55 +265,55 @@ configWidget("bi.left_right_vertical_adapt", ob => {
configWidget("bi.flex_horizontal", ob => {
if (ob.scrollable === true || ob.scrollx === true || ob.scrolly === true) {
if (ob.hgap > 0 || ob.lgap > 0 || ob.rgap > 0) {
if (BI.Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return BI.extend({}, ob, { type: "bi.responsive_flex_scrollable_horizontal" });
if (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_flex_scrollable_horizontal" });
}
return BI.extend({}, ob, { type: "bi.flex_scrollable_horizontal" });
return extend({}, ob, { type: "bi.flex_scrollable_horizontal" });
}
}
if (BI.Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return BI.extend({}, ob, { type: "bi.responsive_flex_horizontal" });
if (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_flex_horizontal" });
}
});
configWidget("bi.flex_vertical", ob => {
if (ob.scrollable === true || ob.scrollx === true || ob.scrolly === true) {
if (ob.hgap > 0 || ob.lgap > 0 || ob.rgap > 0) {
return BI.extend({}, ob, { type: "bi.flex_scrollable_vertical" });
return extend({}, ob, { type: "bi.flex_scrollable_vertical" });
}
}
});
configWidget("bi.table", ob => {
if (!isSupportGrid()) {
return BI.extend({}, ob, { type: "bi.td" });
return extend({}, ob, { type: "bi.td" });
}
return ob;
});
configWidget("bi.radio", ob => {
if (BI.isIE() && BI.getIEVersion() <= 9) {
return BI.extend({}, ob, { type: "bi.image_radio" });
if (isIE() && getIEVersion() <= 9) {
return extend({}, ob, { type: "bi.image_radio" });
}
return ob;
});
configWidget("bi.checkbox", ob => {
if (BI.isIE() && BI.getIEVersion() <= 9) {
return BI.extend({}, ob, { type: "bi.image_checkbox" });
if (isIE() && getIEVersion() <= 9) {
return extend({}, ob, { type: "bi.image_checkbox" });
}
return ob;
});
configWidget("bi.half_icon_button", ob => {
if (BI.isIE() && BI.getIEVersion() < 9) {
if (isIE() && getIEVersion() < 9) {
return ob;
}
return BI.extend({}, ob, { type: "bi.half_button" });
return extend({}, ob, { type: "bi.half_button" });
});

Loading…
Cancel
Save