Browse Source

Pull request #3456: Es6 refact: 修复 behavior、router、z-tree、config 等问题

Merge in VISUAL/fineui from ~TREECAT/fineui:es6 to es6

* commit '7e4d44e8cc6cabe25e58a455a9e12528b73232f8':
  KERNEL-14316 fix: config 找不到依赖
  KERNEL-14512 refact: 使用 babel 打包出 ESM 模块
  KERNEL-14512 refact: 删除ES6脚本,防止被滥用
  KERNEL-14512 refact: 修复一些细节问题
  KERNEL-14512 fix: 修改 z-tree 严格模式下的问题
  无 jira fix:有的地方没有模块
  KERNEL-14512 refact: router
  KERNEL-14512 refact: 调整 behavior 的位置
es6
treecat-罗群 1 year ago
parent
commit
d4f3a3996f
  1. 3
      demo/js/base/tree/demo.part_tree.js
  2. 2
      demo/js/case/triggers/demo.select_text_trigger.js
  3. 6
      demo/js/core/abstract/demo.virtual_list.js
  4. 19
      es6.babel.js
  5. 538
      es6.js
  6. 172
      es6.xtype.js
  7. 11
      package.json
  8. 52
      rollup.config.js
  9. 17
      src/base/behavior/0.behavior.js
  10. 34
      src/base/behavior/behavior.highlight.js
  11. 25
      src/base/behavior/behavior.redmark.js
  12. 26
      src/base/behavior/index.js
  13. 2
      src/base/combination/group.button.js
  14. 12
      src/bundle.js
  15. 2
      src/case/ztree/jquery.ztree.excheck-3.5.js
  16. 2
      src/core/1.lodash.js
  17. 72
      src/core/decorator.js
  18. 2
      src/core/index.js
  19. 89
      src/core/platform/web/config.js
  20. 2
      src/core/system.js
  21. 7
      src/index.js
  22. 464
      src/router/0.router.js
  23. 3431
      src/router/router.js
  24. 1
      src/widget/index.js
  25. 2
      template/index.html
  26. 6
      webpack/attachments.js
  27. 18
      webpack/webpack.common.js

3
demo/js/base/tree/demo.part_tree.js

@ -1,4 +1,5 @@
import { shortcut, Widget, TreeView, isNull } from "@/core";
import { shortcut, Widget, isNull } from "@/core";
import { TreeView } from "@/case";
@shortcut()
export class Func extends Widget {

2
demo/js/case/triggers/demo.select_text_trigger.js

@ -1,4 +1,4 @@
import { shortcut, Widget } from "@/core";
import { shortcut, Widget, createWidget } from "@/core";
@shortcut()
export class Func extends Widget {

6
demo/js/core/abstract/demo.virtual_list.js

@ -1,4 +1,4 @@
import { shortcut, Widget } from "@/core";
import { shortcut, Widget, map, extend } from "@/core";
@shortcut()
export class Func extends Widget {
@ -13,9 +13,9 @@ export class Func extends Widget {
extend({}, item, {
type: "bi.label",
height: 30,
text: `${i + 1}.${item.text}`,
text: `${i + 1}.${item.text}`
})
),
)
};
}
}

19
es6.babel.js

@ -0,0 +1,19 @@
module.exports = {
plugins: [
[
"@babel/plugin-proposal-decorators",
{
legacy: true
}
],
[
"module-resolver",
{
root: ["./src"],
alias: {
"@": "./src"
}
}
]
]
};

538
es6.js

@ -1,538 +0,0 @@
const fs = require("fs");
const path = require("path");
const prettier = require("prettier");
const { exec } = require("child_process");
const { search, initDepts, depts } = require("./es6.xtype");
const lodash = require("lodash");
const DEPTS = depts;
// const XTYPE_ONLY = false;
// const THIS_REPLACE = false;
const ConflictImport = [];
const CircularDependency = [];
function objHaveFunction(obj) {
if (obj === null) {
return false;
}
return Object.keys(obj).some(key => {
const value = obj[key];
if (typeof value === "object") {
return objHaveFunction(value);
} else if (typeof value === "function") {
return true;
}
return false;
});
}
function parserImport(code) {
const reg = /import {([\s\S]*?)} from "(.*?)";/g;
const importMap = {};
let regResult = reg.exec(code);
while (regResult) {
importMap[regResult[2]] = regResult[1]
.split(",")
.map(el => el.trim())
.filter(el => el);
regResult = reg.exec(code);
}
return importMap;
}
async function saveAndFixCode(path, code) {
let _code = code;
if (!code) {
_code = fs.readFileSync(path).toString();
}
const prettierCode = prettier.format(_code, {
tabWidth: 4,
parser: "babel",
printWidth: 120,
});
fs.writeFileSync(path, prettierCode);
new Promise(res => {
exec(`yarn eslint --fix ${path}`, () => {
res();
});
});
}
function divideFile(srcName) {
const targetPath = srcName.match(/.*\//g);
const sourceCode = fs.readFileSync(srcName).toString();
const splitSourceCode = sourceCode.match(/[\.\s]{1}[^\s\.]+?\s=\sBI\.inherit\([^]*?BI\.shortcut.*\;/g);
const newFileNames = [];
for (let i = 0; i < splitSourceCode.length; i++) {
// 去除开头的 空格 或者 .
const newCode = splitSourceCode[i].slice(1);
// 匹配第一个等号之前的组件名
const componentName = /BI\.shortcut\("(.*)"/.exec(newCode)[1];
// 文件名转化 ButtonIcon => demo.button.icon.js
const demoFileName = componentName + '.js';
newFileNames.push(demoFileName);
// 代码 componentName 前面加上 BI.
const fileCode = 'BI.' + newCode;
// 规范最后一行的组件为 BI.Button
const targetComponet = /(BI\..*)\s=/.exec(fileCode)[1];
// 最后一行的内容
const replaceContext = /BI\.shortcut.*;/.exec(fileCode)[0];
// 替换
const finalCode = fileCode.replace(replaceContext,`BI.shortcut("${componentName}", ${targetComponet});`)
// 创建新文件
fs.writeFileSync(targetPath + demoFileName, finalCode);
}
return newFileNames;
}
// const target = [];
// 加载模块
const loader = {
// G: { "@/core": { shortcut: true } },
load(srcName, module) {
const G = loader.G;
// if (target.indexOf(module) >= 0) {
// G["@/core"][module] = true;
// return true;
// }
if (module.startsWith('"bi.')) {
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
const clzName = depts[module].clzName;
G[key][clzName] = true;
}
return !!key;
} else {
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
G[key][module] = true;
}
return !!key;
}
},
};
async function handleFile(srcName) {
await saveAndFixCode(srcName);
// 全局状态回归
let G = (loader.G = {});
const sourceCode = fs.readFileSync(srcName).toString();
if (sourceCode.match(/shortcut/g).length > 1) {
console.log('该文件存在多处BI.shorcut, 需要拆分...');
const newTargets = divideFile(srcName);
newTargets.forEach(name => console.log(name));
return;
}
const result = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(sourceCode);
if (!result) {
// console.log(`已经es6过,替换 xtype => ${srcName}`);
if (!/export class/.test(sourceCode)) {
// console.log("忽略文件", srcName);
return;
}
// 处理 xtype
const noXtypeCode = sourceCode.replace(/type:\s?"bi\.(.*?)"/g, v => {
const matchedSentence = v.replace(/type:\s?/, "");
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `type: ${clzName}.xtype`;
} else {
console.log(`xtype 替换失败 ${matchedSentence} `);
return v;
}
});
// 识别 import
const importMap = parserImport(noXtypeCode);
// 合并原来的 import 到 G
lodash.forEach(importMap, (depts, module) => {
depts.forEach(dept => {
if (!G[module]) {
G[module] = {};
}
G[module][dept] = true;
});
});
// 合并包
const crossPackages = fs.readdirSync("src").map(el => `@/${el}`);
lodash.forEach(G, (depts, module) => {
crossPackages.forEach(crosspackage => {
if (module.indexOf(crosspackage.replace(/^@\//, "")) >= 0) {
if (!G[crosspackage]) {
G[crosspackage] = {};
}
Object.assign(G[crosspackage], depts);
}
});
});
const tmpG = {};
lodash.forEach(G, (depts, module) => {
const flag = lodash.some(
crossPackages,
crosspackage =>
module.indexOf(crosspackage) >= 0 && !module.startsWith("@"),
);
if (!flag) {
tmpG[module] = depts;
}
});
let circle = false;
// 处理循环依赖,base: ["@/case", "@/base", "@/widget"] 等于 base 不能直接引数组内的包
const forbiddenCrossRules = {
base: ["@/case", "@/base", "@/widget"],
"case": ["@/case", "@/widget"],
widget: ["@/widget"],
component: ["@/component"],
core: ["@/core", "@base", "@/widget", "@/case"],
};
const forbiddenKeys = [];
const circleG = {};
lodash.forEach(G, (depts, module) => {
// 找出 rule
const packages = Object.keys(forbiddenCrossRules);
let key = packages.filter(
_package => srcName.indexOf(_package) >= 0,
);
if (key.length !== 1) {
throw new Error(
"理论不可能出现这个问题,需要 treecat 优化下找包逻辑1",
);
}
key = key[0];
const rule = forbiddenCrossRules[key];
if (lodash.includes(rule, module)) {
circle = true;
const deptsArr = Object.keys(depts);
if (deptsArr.filter(dept => !DEPTS[dept]).length > 0) {
throw new Error(
"理论不可能出现这个问题,需要 treecat 优化下找包逻辑2",
);
}
deptsArr
.filter(dept => DEPTS[dept])
.forEach(dept => {
const value = `@${DEPTS[dept].replace(path.resolve("src"), "").replace(/\\/g, "/").replace(/\.js$/, "")}`;
if (!tmpG[value]) {
tmpG[value] = {};
}
tmpG[value][dept] = true;
});
forbiddenKeys.push(module);
}
});
Object.assign(tmpG, circleG);
forbiddenKeys.forEach(key => {
delete tmpG[key];
});
// 较验手工 import 错误
const map = {};
let conflict = false;
lodash.forEach(tmpG, (depts, module) => {
lodash.forEach(depts, (_, _import) => {
if (map[_import] && map[_import] !== module) {
conflict = true;
}
map[_import] = module;
});
});
conflict && ConflictImport.push(srcName);
circle && CircularDependency.push(srcName);
G = tmpG;
const noImportCode = noXtypeCode.replace(
/import {([\s\S]*?)} from "(.*?)";/g,
"",
);
let I = "";
Object.keys(G).forEach(key => {
let moduleKey = key;
if (moduleKey === path.basename(srcName).replace(/.js$/g, "")) {
return;
}
let i = "";
Object.keys(G[moduleKey]).forEach(key => {
i += `${key}, `;
});
// 必须以 . 开头
const moduleInValid = /^[^@.]/.test(moduleKey);
if (moduleInValid) {
moduleKey = `./${moduleKey}`;
}
I += `import {${i}} from '${moduleKey}'\n`;
});
const code = `${I}\n${noImportCode}`;
await saveAndFixCode(srcName, code);
return;
}
G["@/core"] = { shortcut: true };
const clzName = result[1];
if (!clzName) {
console.log(`${srcName} 不需要 es6 化`);
return;
}
const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1];
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
const collection = {
"static": {},
attr: {},
};
// eslint-disable-next-line no-unused-vars
const BI = {
[clzName]: clzName,
inherit(_, options) {
collection.methods = Object.keys(options)
.filter(key => typeof options[key] === "function")
.map(key => options[key]);
Object.keys(options)
.filter(key => typeof options[key] !== "function")
.forEach(key => {
collection.attr[key] = options[key];
});
return collection.static;
},
extend(targetClz, o) {
Object.assign(collection.static, o);
},
shortcut(xtype) {
collection.xtype = xtype;
},
};
// eslint-disable-next-line no-eval
eval(sourceCode);
let M = "";
let E = "";
let I = "";
let A = "";
loader.load(srcName, superName);
Object.keys(collection.attr).forEach(key => {
const value = collection.attr[key];
if (typeof value === "function" || typeof value === "number") {
A += `\t${key} = ${value}\n`;
} else if (typeof value === "string") {
A += `\t${key} = "${value}"\n`;
} else if (typeof value === "object") {
if (objHaveFunction(value)) {
throw new Error("G");
} else {
A += `\t${key} = ${JSON.stringify(value)}\n`;
}
}
});
// 静态方法
Object.keys(collection.static).forEach(key => {
// console.log(key, collection.static[key], typeof collection.static[key])
const value = collection.static[key];
if (typeof value === "function" || typeof value === "number") {
E += `\tstatic ${key} = ${value}\n`;
} else if (typeof value === "string") {
E += `\tstatic ${key} = "${value}"\n`;
} else if (typeof value === "object") {
if (objHaveFunction(value)) {
throw new Error("G");
} else {
E += `\tstatic ${key} = ${JSON.stringify(value)}\n`;
}
}
});
// 对函数进行替换
collection.methods.forEach(el => {
let f = `${el.toString().replace(/^function/, el.name)}\n`;
// 换 BI.Button.superclass,就说能不能跑吧
for (let i = 0; i < 100; i++) {
f = f.replace(`BI.${clzName}.superclass`, "super");
}
// 换 super._defaultConfig
f = f.replace(
/super\._defaultConfig\.apply\(this,\sarguments\)/g,
"super._defaultConfig(...arguments)",
);
// 换 super.xxx.apply
f = f.replace(/super\.(.*?)\.apply\(this,\sarguments\)/g, a => {
const f = /super\.(.*?)\.apply\(this,\sarguments\)/.exec(a);
return `super.${f[1]}(...arguments)`;
});
// 尝试换掉所有的 BI.xxx. BI.xxx( BI.xxx[空白]
f = f.replace(/BI\.(.*?)(\.|\(|\s|,)/g, matchedSentence => {
const match = /BI\.(.*?)(\.|\(|\s|,)/.exec(matchedSentence);
const target = match[1];
const end = match[2];
// 尝试加载 target
const loadSuccess = loader.load(srcName, target);
if (loadSuccess) {
return target + end;
} else {
console.log(`BI 变量替换失败 BI.${target}`);
return matchedSentence;
}
});
// 尝试对 xtype 进行替换
f = f.replace(/"bi\.(.*?)"/g, matchedSentence => {
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `${clzName}.xtype`;
} else {
// console.log(`(没事) xtype 替换失败 ${matchedSentence} `);
return matchedSentence;
}
});
M += `${f}\n`;
});
if (!collection.xtype) {
delete G["@/core"].shortcut;
}
Object.keys(G).forEach(key => {
let moduleKey = key;
if (moduleKey === path.basename(srcName).replace(/.js$/g, "")) {
return;
}
let i = "";
Object.keys(G[moduleKey]).forEach(key => {
i += `${key}, `;
});
// 必须以 . 开头
const moduleInValid = /^[^@.]/.test(moduleKey);
if (moduleInValid) {
moduleKey = `./${moduleKey}`;
}
I += `import {${i}} from '${moduleKey}'\n`;
});
const outputCode = `
${I}
${collection.xtype ? "@shortcut()" : ""}
export class ${clzName} extends ${superName} {
${collection.xtype ? `static xtype = "${collection.xtype}"` : ""}
${A}
${E}
${M}
}
`;
await saveAndFixCode(srcName, outputCode);
return clzName;
}
async function traverse(srcName) {
if (srcName.indexOf("__test__") >= 0) return;
if (srcName.endsWith(".js")) {
try {
return await handleFile(srcName);
} catch (error) {
console.log(`文件处理失败 ${srcName} \n`);
console.error(error);
return;
}
} else {
const stat = fs.statSync(srcName);
const flag = stat.isDirectory();
if (flag) {
const files = fs.readdirSync(srcName);
// let indexContent = "";
for (let i = 0; i < files.length; i++) {
const file = files[i];
await traverse(path.resolve(srcName, file));
// const clzName = await traverse(path.resolve(srcName, file));
// const moduleName = path.basename(srcName).replace(/.js$/, "");
// if (clzName) {
// indexContent += `export { ${clzName} } from '${moduleName}'\n`;
// }
}
}
}
}
const srcName = process.argv[2];
initDepts().then(async () => {
await traverse(srcName);
// 对数据处理
ConflictImport.forEach(el => {
console.log(`导入冲突 ${el}`);
});
CircularDependency.forEach(el => {
console.log(`出现循环依赖(已经fixed) ${el}`);
});
});

172
es6.xtype.js

@ -1,172 +0,0 @@
const fs = require("fs");
const path = require("path");
const lodash = require("lodash");
const depts = {};
async function handle(filename) {
if (path.extname(filename) !== ".js") {
return;
}
const code = fs.readFileSync(filename).toString();
const inheritRegResult = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(code);
if (inheritRegResult) {
const clzName = inheritRegResult[1];
depts[clzName] = filename;
} else {
// 一个文件夹里面可能有多个 xtype
const reg = /export\s+class\s+(.*?)([\s|{])([\w\W]*?)static xtype\s?=\s?((["|'])(.*?)(\5))/g;
Array.from(code.matchAll(reg)).forEach(res => {
const xtype = res[4];
depts[xtype] = {
clzName: res[1],
clzPath: filename,
};
});
// 同时找一下 export
if (path.basename(filename) !== "index.js") {
const regs = [
/export function (.*?)\(/g,
/export const (.*?) =/g,
/export class (.*?) /g,
/export \{([\w\W]*?)\}/g,
];
regs.forEach((reg, index) => {
Array.from(code.matchAll(reg)).forEach(el => {
depts[el[1]] = filename;
if (index === 3) {
el[1].split(",").map(el => el.trim()).forEach(key => {
depts[key] = filename;
});
}
});
});
}
}
}
function isExist(filePath) {
return fs.existsSync(filePath);
}
async function bfs(filename) {
const stat = fs.statSync(filename);
const isDir = stat.isDirectory();
if (isDir) {
const files = fs.readdirSync(filename);
for (let i = 0; i < files.length; i++) {
const file = files[i];
await bfs(path.resolve(filename, file));
}
} else {
await handle(filename);
}
}
async function initDepts() {
// dfs 构建依赖关系
await bfs(path.resolve("src"));
const m = {};
lodash.forEach(depts, value => {
if (typeof value === "object") {
m[value.clzName] = value.clzPath;
}
});
Object.assign(depts, m);
}
function search(src, module) {
let clzName = module;
let clzPath = depts[module];
if (!depts[clzName]) {
return "";
}
if (clzName.indexOf("\"") >= 0) {
clzName = depts[module].clzName;
clzPath = depts[module].clzPath;
}
const dstName = path.basename(clzPath).replace(/.js$/g, "");
const dstPath = path.normalize(clzPath).split("src")[1].split("\\").join("/").split("/");
const srcPath = path.normalize(src).split("src")[1].split("\\").join("/").split("/");
// console.log("src", src);
// console.log("dst", depts[clzName]);
dstPath.shift();
dstPath.pop();
srcPath.shift();
srcPath.pop();
const findDstIndexPath = (dstArr, startIndex) => {
let i = startIndex;
while (!isExist(path.resolve("src", dstArr.slice(0, i + 1).join("/"), "index.js")) && i < dstArr.length) {
i++;
}
if (i < dstArr.length) {
return dstArr.slice(startIndex, i + 1).join("/");
} else {
return `${dstArr.slice(startIndex).join("/")}/${dstName}`;
}
};
// 不同包
if (dstPath[0] !== srcPath[0]) {
return `@/${dstPath[0]}`;
}
// 同包
let i = 0;
while (dstPath[i] === srcPath[i] && i < dstPath.length && i < srcPath.length) {
i++;
}
// i 代表同名的位置
i--;
// 没有匹配完
if (i < srcPath.length) {
let result = "";
// 回溯,向上找,回到目录 i
for (let j = srcPath.length - 1; j > i; j--) {
result += "../";
}
// 匹配过的下一个位置
i++;
if (i >= dstPath.length) {
// 越界
return `${result}${dstName}`;
} else {
// 看看这个目录下有没有 index
return result + findDstIndexPath(dstPath, i);
}
} else if (i === srcPath.length) {
if (i === dstPath.length) {
return dstName;
} else if (i < dstPath.length) {
return findDstIndexPath(dstPath, i);
}
}
}
// search(process.argv[2], "Text").then(res => {
// console.log(res);
// });
exports.initDepts = initDepts;
exports.search = search;
exports.depts = depts;

11
package.json

@ -3,11 +3,16 @@
"version": "2.0.20230208163847",
"description": "fineui",
"main": "dist/fineui_without_conflict.min.js",
"module": "dist/esm/index.js",
"sideEffects": [
"dist/esm/core/**"
],
"types": "dist/lib/index.d.ts",
"bin": {
"fui-cli": "./bin/cli/cli.js"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.17.4",
"@fui/babel-preset-fineui": "^3.0.0",
"@fui/eslint-plugin": "^1.0.19",
@ -21,6 +26,7 @@
"@typescript-eslint/parser": "2.33.0",
"autoprefixer": "9.6.1",
"babel-loader": "8.0.6",
"babel-plugin-module-resolver": "^5.0.0",
"chai": "4.2.0",
"concat": "1.0.3",
"core-js": "3.3.2",
@ -66,10 +72,12 @@
"webpack:prod": "node --max_old_space_size=4096 node_modules/webpack/bin/webpack -p --progress --config=webpack/webpack.prod.js --mode production && npm run biCss && npm run jsyCss",
"webpack:css": "node --max_old_space_size=4096 node_modules/webpack/bin/webpack -p --progress --config=webpack/webpack.css.js --mode production",
"start": "node server.js",
"build": "npm run webpack:prod && tsc",
"build": "npm run esm && npm run webpack:prod && tsc",
"postbuild": "node ./lib/postbuild/postbuild.js",
"test": "karma start",
"dev": "npm run webpack:dev",
"esm": "babel src -d dist\\esm --config-file .\\es6.babel.js",
"esm:dev": "babel src -d dist\\esm --config-file .\\es6.babel.js -w",
"prepublishToPrivate": "npm run build && node ./lib/prepublish/prepublish.js",
"publishToPrivate": "npm publish",
"postpublishToPrivate": " node ./lib/postpublish/postpublish.js",
@ -91,6 +99,7 @@
"author": "fanruan",
"license": "MIT",
"dependencies": {
"@popperjs/core": "2.11.6",
"@types/yargs": "17.0.13",
"jquery": "3.6.3",
"yargs": "17.6.2"

52
rollup.config.js

@ -2,38 +2,64 @@ const babel = require("@rollup/plugin-babel");
const alias = require("@rollup/plugin-alias");
const resolve = require("@rollup/plugin-node-resolve");
const commonjs = require("@rollup/plugin-commonjs");
const sizes = require("rollup-plugin-sizes");
const path = require("path");
/**
* todo: 删除根目录下的 babel.config.js然后移入到这个文件
*/
const input = "src/index.js";
module.exports = [
{
input,
input: path.resolve(__dirname, "src/index.js"),
output: [
{
file: "dist/fineui.esm.js",
format: "esm",
sourcemap: true,
},
}
],
plugins: [
alias({
entries: [
{ find: "@", replacement: path.resolve(__dirname, "src") },
],
{ find: "@", replacement: path.resolve(__dirname, "src") }
]
}),
resolve(),
babel({
babelHelpers: "inline",
plugins: [
["@babel/plugin-proposal-decorators", { legacy: true }],
["@babel/plugin-proposal-decorators", { legacy: true }]
]
}),
commonjs()
]
},
{
input: path.resolve(__dirname, "src/bundle.js"),
output: [
{
file: "dist/fineui.min.js",
sourcemap: true,
format: "umd"
}
],
plugins: [
alias({
entries: [
{ find: "@", replacement: path.resolve(__dirname, "src") }
]
}),
resolve(),
babel({
babelHelpers: "inline",
plugins: [
["@babel/plugin-proposal-decorators", { legacy: true }]
]
}),
// babel({
// babelHelpers: "runtime",
// presets: [babelConfig]
// }),
commonjs(),
],
},
sizes(),
// terser()
]
}
];

17
src/base/behavior/0.behavior.js

@ -0,0 +1,17 @@
/**
* guy
* 行为控件
*/
import { OB, extend } from "@/core";
export class Behavior extends OB {
_defaultConfig() {
return extend(super._defaultConfig(...arguments), {
rule: () => true,
});
}
doBehavior() {
}
}

34
src/base/behavior/behavior.highlight.js

@ -0,0 +1,34 @@
/**
* guy
*/
import { isFunction, each } from "@/core";
import { Single } from "../single/0.single";
import { Behavior } from "./0.behavior";
export class HighlightBehavior extends Behavior {
doBehavior(items) {
const args = Array.prototype.slice.call(arguments, 1),
{ rule } = this.options;
each(items, (i, item) => {
if (item instanceof Single) {
const rules = rule(item.getValue(), item);
function doBe(run) {
if (run === true) {
item.doHighLight && item.doHighLight(...args);
} else {
item.unHighLight && item.unHighLight(...args);
}
}
if (isFunction(rules)) {
rules(doBe);
} else {
doBe(rules);
}
} else {
item.doBehavior && item.doBehavior(...args);
}
});
}
}

25
src/base/behavior/behavior.redmark.js

@ -0,0 +1,25 @@
/**
* guy
* 标红行为
*/
import { Behavior } from "./0.behavior";
import { each } from "@/core";
import { Single } from "../single/0.single";
export class RedMarkBehavior extends Behavior {
doBehavior(items) {
const args = Array.prototype.slice.call(arguments, 1),
{ rule } = this.options;
each(items, (i, item) => {
if (item instanceof Single) {
if (rule(item.getValue(), item)) {
item.doRedMark && item.doRedMark(...args);
} else {
item.doRedMark && item.unRedMark(...args);
}
} else {
item.doBehavior && item.doBehavior(...args);
}
});
}
}

26
src/base/behavior/index.js

@ -0,0 +1,26 @@
import { HighlightBehavior } from "./behavior.highlight";
import { RedMarkBehavior } from "./behavior.redmark";
export const BehaviorFactory = {
createBehavior (key, options) {
let Behavior;
switch (key) {
case "highlight":
Behavior = HighlightBehavior;
break;
case "redmark":
Behavior = RedMarkBehavior;
break;
default:
}
return new Behavior(options);
},
};
export { Behavior } from "./0.behavior";
export {
HighlightBehavior,
RedMarkBehavior
};

2
src/base/combination/group.button.js

@ -26,10 +26,10 @@ import {
deepContains,
has,
any,
BehaviorFactory,
Events
} from "@/core";
import { TextButton } from "../single";
import { BehaviorFactory } from "../behavior";
/**
* Created by GUY on 2015/6/26.

12
src/bundle.js

@ -1,4 +1,14 @@
import * as fineui from "./index";
import { shortcut, provider } from "@/core/5.inject";
import { _global } from "@/core/0.foundation";
import * as Decorators from "@/core/decorator";
Object.assign(BI, fineui, { shortcut, provider });
_global.BI = _global.BI || {};
_global.BI.Decorators = Decorators;
_global.Fix = fineui.Fix;
_global._ = fineui._;
_global.$ = fineui.$;
Object.assign(_global.BI, fineui, { shortcut, provider });

2
src/case/ztree/jquery.ztree.excheck-3.5.js

@ -489,7 +489,7 @@ import $ from "jquery";
r = consts.radio,
checkboxType = setting.check.chkboxType;
var notEffectByOtherNode = checkboxType.Y === "" && checkboxType.N === "";
fullStyle = "";
var fullStyle = "";
if (node.chkDisabled === true) {
fullStyle = c.DISABLED;
} else if (node.halfCheck) {

2
src/core/1.lodash.js

@ -10356,6 +10356,6 @@
freeExports._ = lodash;
} else {
// Export to the global object.
BI._ = lodash;
// BI._ = lodash;
}
}.call(this));

72
src/core/decorator.js

@ -1,6 +1,11 @@
// export * from "../../typescript/core/decorator/decorator.ts";
import { shortcut as biShortcut, provider as biProvider } from "./5.inject";
import {
shortcut as biShortcut,
provider as biProvider,
model as biModel,
Models
} from "./5.inject";
/**
* 注册widget
@ -19,3 +24,68 @@ export function provider() {
biProvider(Target.xtype, Target);
};
}
/**
* 注册model
*/
export function model() {
return function decorator(Target) {
biModel(Target.xtype, Target);
};
}
/**
* 类注册_store属性
* @param Model model类
* @param opts 额外条件
*/
export function store(Model, opts) {
return function classDecorator(constructor) {
return class extends constructor {
_store() {
const props = opts.props ? opts.props.apply(this) : undefined;
return Models.getModel(Model.xtype, props);
}
};
};
}
/**
* 注册mixin
* ie8下不能使用
*/
export function mixin() {
return function decorator(Target) {
const mixin = {};
Object.getOwnPropertyNames(Target.prototype).forEach(name => {
if (name === "constructor") {
return;
}
mixin[name] = Target.prototype[name];
});
Fix.mixin(Target.xtype, mixin);
};
}
/**
* 类注册mixins属性
* ie8下不能使用
* @param Mixins
*/
export function mixins(...Mixins) {
return function classDecorator(constructor) {
const mixins = [];
Mixins.forEach(mixin => {
mixin.xtype && mixins.push(mixin.xtype);
});
return class extends constructor {
mixins = mixins;
};
};
}

2
src/core/index.js

@ -22,7 +22,7 @@ export * from "./5.inject";
export * from "./6.plugin";
export * from "./system";
export * from "./action";
export * from "./behavior";
// export * from "./behavior";
// export * from "./controller";
export * from "./func";
export * from "./structure";

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

@ -3,6 +3,21 @@ import { Providers } from "../../5.inject";
import { Plugin } from "../../6.plugin";
import { isSupportCss3, isIE, getIEVersion } from "../../platform/web";
import { HorizontalAlign, VerticalAlign } from "../../constant";
import { FlexCenterLayout, FlexHorizontalLayout, VerticalLayout,
FlexVerticalLayout, TdLayout, InlineLayout, TableAdaptLayout,
FloatHorizontalFillLayout, ResponsiveInlineLayout,
InlineCenterAdaptLayout, FlexVerticalCenterAdapt,
InlineVerticalAdaptLayout, HorizontalAutoLayout,
TableAdaptLayout, FlexHorizontalCenter,
InlineHorizontalAdaptLayout, TableLayout,
AutoVerticalTapeLayout, VTapeLayout,
HorizontalFillLayout, VerticalFillLayout,
FlexLeftRightVerticalAdaptLayout, ResponsiveFlexWrapperHorizontalLayout,
FlexWrapperHorizontalLayout, ResponsiveFlexHorizontalLayout,
FlexWrapperVerticalLayout
} from "@/core/wrapper/layout/index";
import { SystemProvider } from "@/core/system";
// 工程配置
// 注册布局
@ -54,25 +69,25 @@ configWidget("bi.horizontal", ob => {
// return extend({}, ob, {type: "bi.table_adapt"});
// }
if (supportFlex) {
return extend({}, ob, { type: "bi.flex_horizontal" });
return extend({}, ob, { type: FlexHorizontalLayout.xtype });
}
return extend({
scrollx: true,
}, ob, { type: "bi.inline" });
}, ob, { type: InlineLayout.xtype });
});
configWidget("bi.vertical", ob => {
if (ob.horizontalAlign === HorizontalAlign.Left || ob.horizontalAlign === HorizontalAlign.Right) {
if (isSupportFlex()) {
return extend({}, ob, { type: "bi.flex_vertical" });
return extend({}, ob, { type: FlexVerticalLayout.xtype });
}
return extend({}, ob, {
horizontalAlign: HorizontalAlign.Stretch,
type: "bi.vertical",
type: VerticalLayout.xtype,
items: map(ob.items, (i, item) => {
return {
type: "bi.inline",
type: InlineLayout.xtype,
horizontalAlign: ob.horizontalAlign,
items: [item],
};
@ -83,7 +98,7 @@ configWidget("bi.vertical", ob => {
if (isSupportFlex()) {
return extend({
horizontalAlign: HorizontalAlign.Stretch,
}, ob, { type: "bi.flex_vertical" });
}, ob, { type: FlexVerticalLayout.xtype });
}
}
@ -113,15 +128,15 @@ configWidget("bi.inline", ob => {
if ((ob.scrollable !== true && ob.scrollx !== true) || ob.horizontalAlign === HorizontalAlign.Stretch) {
return extend({
verticalAlign: VerticalAlign.Top,
}, ob, { type: "bi.horizontal_float_fill" });
}, ob, { type: FloatHorizontalFillLayout.xtype });
}
return extend({
horizontalAlign: HorizontalAlign.Stretch,
}, ob, { type: "bi.table_adapt" });
}, ob, { type: TableAdaptLayout.xtype });
}
if (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_inline" });
if (Providers.getProvider(SystemProvider.xtype).getResponsiveMode()) {
return extend({}, ob, { type: ResponsiveInlineLayout.xtype });
}
return ob;
@ -131,10 +146,10 @@ configWidget("bi.center_adapt", ob => {
// var isAdapt = !ob.horizontalAlign || ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
// if (!isAdapt || justOneItem) {
if (supportFlex) {
return extend({}, ob, { type: "bi.flex_center_adapt" });
return extend({}, ob, { type: FlexCenterLayout.xtype });
}
return extend({}, ob, { type: "bi.inline_center_adapt" });
return extend({}, ob, { type: InlineCenterAdaptLayout.xtype });
// }
// return ob;
});
@ -143,10 +158,10 @@ configWidget("bi.vertical_adapt", ob => {
// var isAdapt = ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
// if (!isAdapt || justOneItem) {
if (supportFlex) {
return extend({}, ob, { type: "bi.flex_vertical_center_adapt" });
return extend({}, ob, { type: FlexVerticalCenterAdapt.xtype });
}
return extend({}, ob, { type: "bi.inline_vertical_adapt" });
return extend({}, ob, { type: InlineVerticalAdaptLayout.xtype });
// }
// return ob;
});
@ -156,7 +171,7 @@ configWidget("bi.horizontal_adapt", ob => {
const isAdapt = !ob.horizontalAlign || ob.horizontalAlign === HorizontalAlign.Center || ob.horizontalAlign === HorizontalAlign.Stretch;
const verticalAlignTop = !ob.verticalAlign || ob.verticalAlign === VerticalAlign.TOP;
if (verticalAlignTop && justOneItem) {
return extend({}, ob, { type: "bi.horizontal_auto" });
return extend({}, ob, { type: HorizontalAutoLayout.xtype });
}
const supportFlex = isSupportFlex();
// 在横向自适应场景下我们需要使用table的自适应撑出滚动条的特性(flex处理不了这种情况)
@ -164,26 +179,26 @@ configWidget("bi.horizontal_adapt", ob => {
if (isAdapt) {
return extend({
horizontalAlign: HorizontalAlign.Center,
}, ob, { type: "bi.table_adapt" });
}, ob, { type: TableAdaptLayout.xtype });
}
if (supportFlex) {
return extend({
horizontalAlign: HorizontalAlign.Center,
scrollx: false,
}, ob, { type: "bi.flex_horizontal" });
}, ob, { type: FlexHorizontalLayout.xtype });
}
return extend({
horizontalAlign: HorizontalAlign.Center,
}, ob, { type: "bi.table_adapt" });
}, ob, { type: TableAdaptLayout.xtype });
});
configWidget("bi.horizontal_float", ob => {
if (isSupportFlex()) {
return extend({}, ob, { type: "bi.flex_horizontal_adapt" });
return extend({}, ob, { type: FlexHorizontalCenter.xtype });
}
if (ob.items && ob.items.length <= 1) {
return extend({}, ob, { type: "bi.inline_horizontal_adapt" });
return extend({}, ob, { type: InlineHorizontalAdaptLayout.xtype });
}
return ob;
@ -195,17 +210,17 @@ configWidget("bi.horizontal_fill", ob => {
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
scrollx: false,
}, ob, { type: "bi.flex_horizontal" });
}, ob, { type: FlexHorizontalLayout.xtype });
}
if ((ob.horizontalAlign && ob.horizontalAlign !== HorizontalAlign.Stretch) || (ob.scrollable === true || ob.scrollx === true)) {
// 宽度不受限,要用table布局
return extend({
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
}, ob, { type: "bi.table_adapt" });
}, ob, { type: TableLayout.xtype });
}
return extend({}, ob, { type: "bi.horizontal_float_fill" });
return extend({}, ob, { type: FloatHorizontalFillLayout.xtype });
});
configWidget("bi.vertical_fill", ob => {
if (isSupportFlex()) {
@ -213,12 +228,12 @@ configWidget("bi.vertical_fill", ob => {
horizontalAlign: HorizontalAlign.Stretch,
verticalAlign: VerticalAlign.Stretch,
scrolly: false,
}, ob, { type: "bi.flex_vertical" });
}, ob, { type: FlexVerticalLayout });
}
if (ob.scrollable === true || ob.scrollx === true || ob.scrolly === true) {
// 有滚动条,降级到table布局处理
return extend({}, ob, {
type: "bi.td",
type: TdLayout.xtype,
items: map(ob.items, (i, item) => [item]),
});
}
@ -236,19 +251,19 @@ configWidget("bi.vertical_fill", ob => {
}
if (hasAuto) {
// 有自动高的时候
return extend({}, ob, { type: "bi.vtape_auto" });
return extend({}, ob, { type: AutoVerticalTapeLayout.xtype });
}
return extend({}, ob, { type: "bi.vtape" });
return extend({}, ob, { type: VTapeLayout.xtype });
});
configWidget("bi.horizontal_sticky", ob => {
if (!isSupportSticky) {
return extend({ scrollx: true }, ob, { type: "bi.horizontal_fill" });
return extend({ scrollx: true }, ob, { type: HorizontalFillLayout.xtype });
}
});
configWidget("bi.vertical_sticky", ob => {
if (!isSupportSticky) {
return extend({ scrolly: true }, ob, { type: "bi.vertical_fill" });
return extend({ scrolly: true }, ob, { type: VerticalFillLayout.xtype });
}
});
@ -256,7 +271,7 @@ configWidget("bi.left_right_vertical_adapt", ob => {
if (isSupportFlex()) {
// IE下其实也是可以使用flex布局的,只要排除掉出现滚动条的情况
// if (!isIE() || (ob.scrollable !== true && ob.scrolly !== true)) {
return extend({}, ob, { type: "bi.flex_left_right_vertical_adapt" });
return extend({}, ob, { type: FlexLeftRightVerticalAdaptLayout.xtype });
// }
}
@ -265,28 +280,28 @@ 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 (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_flex_scrollable_horizontal" });
if (Providers.getProvider(SystemProvider.xtype).getResponsiveMode()) {
return extend({}, ob, { type: ResponsiveFlexWrapperHorizontalLayout.xtype });
}
return extend({}, ob, { type: "bi.flex_scrollable_horizontal" });
return extend({}, ob, { type: FlexWrapperHorizontalLayout.xtype });
}
}
if (Providers.getProvider("bi.provider.system").getResponsiveMode()) {
return extend({}, ob, { type: "bi.responsive_flex_horizontal" });
if (Providers.getProvider(SystemProvider.xtype).getResponsiveMode()) {
return extend({}, ob, { type: ResponsiveFlexHorizontalLayout.xtype });
}
});
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 extend({}, ob, { type: "bi.flex_scrollable_vertical" });
return extend({}, ob, { type: FlexWrapperVerticalLayout.xtype });
}
}
});
configWidget("bi.table", ob => {
if (!isSupportGrid()) {
return extend({}, ob, { type: "bi.td" });
return extend({}, ob, { type: TdLayout.xtype });
}
return ob;

2
src/core/system.js

@ -4,7 +4,7 @@
* Created by windy on 2021/6/30
*/
import { prepares } from "./0.foundation";
import { deepExtend, extend, inherit, object } from "./2.base";
import { deepExtend, extend } from "./2.base";
import { OB } from "./3.ob";
import { Providers } from "./5.inject";
import { provider } from "./decorator";

7
src/index.js

@ -1,8 +1,13 @@
import "./polyfill";
import _jquery from "jquery";
export * from "./core";
export * from "./base";
export * from "./case";
export * from "./widget";
export * from "./component";
export * from "./fix";
export * from "./router";
export const jQuery = _jquery;
export const $ = _jquery;

464
src/router/0.router.js

@ -1,26 +1,37 @@
(function () {
var Events = {
import { _ } from "@/core";
const Events = {
// Bind an event to a `callback` function. Passing `"all"` will bind
// the callback to all events fired.
on: function (name, callback, context) {
if (!eventsApi(this, "on", name, [callback, context]) || !callback) return this;
on(name, callback, context) {
if (!eventsApi(this, "on", name, [callback, context]) || !callback) {
return this;
}
this._events || (this._events = {});
var events = this._events[name] || (this._events[name] = []);
events.push({callback: callback, context: context, ctx: context || this});
const events = this._events[name] || (this._events[name] = []);
events.push({
callback,
context,
ctx: context || this
});
return this;
},
// Bind an event to only be triggered a single time. After the first time
// the callback is invoked, it will be removed.
once: function (name, callback, context) {
if (!eventsApi(this, "once", name, [callback, context]) || !callback) return this;
var self = this;
var once = BI._.once(function () {
once(name, callback, context) {
if (!eventsApi(this, "once", name, [callback, context]) || !callback) {
return this;
}
const self = this;
var once = _.once(function () {
self.off(name, once);
callback.apply(this, arguments);
});
once._callback = callback;
return this.on(name, once, context);
},
@ -28,21 +39,27 @@
// callbacks with that function. If `callback` is null, removes all
// callbacks for the event. If `name` is null, removes all bound
// callbacks for all events.
off: function (name, callback, context) {
if (!this._events || !eventsApi(this, "off", name, [callback, context])) return this;
off(name, callback, context) {
if (
!this._events ||
!eventsApi(this, "off", name, [callback, context])
) {
return this;
}
// Remove all callbacks for all events.
if (!name && !callback && !context) {
this._events = void 0;
return this;
}
var names = name ? [name] : BI._.keys(this._events);
for (var i = 0, length = names.length; i < length; i++) {
const names = name ? [name] : _.keys(this._events);
for (let i = 0, length = names.length; i < length; i++) {
name = names[i];
// Bail out if there are no events stored.
var events = this._events[name];
const events = this._events[name];
if (!events) continue;
// Remove all callbacks for this event.
@ -52,13 +69,14 @@
}
// Find any remaining events.
var remaining = [];
for (var j = 0, k = events.length; j < k; j++) {
var event = events[j];
const remaining = [];
for (let j = 0, k = events.length; j < k; j++) {
const event = events[j];
if (
callback && callback !== event.callback &&
callback !== event.callback._callback ||
context && context !== event.context
(callback &&
callback !== event.callback &&
callback !== event.callback._callback) ||
(context && context !== event.context)
) {
remaining.push(event);
}
@ -75,7 +93,7 @@
return this;
},
un: function () {
un() {
this.off.apply(this, arguments);
},
@ -83,151 +101,172 @@
// passed the same arguments as `trigger` is, apart from the event name
// (unless you're listening on `"all"`, which will cause your callback to
// receive the true name of the event as the first argument).
trigger: function (name) {
trigger(name) {
if (!this._events) return this;
var args = slice.call(arguments, 1);
const args = slice.call(arguments, 1);
if (!eventsApi(this, "trigger", name, args)) return this;
var events = this._events[name];
var allEvents = this._events.all;
const events = this._events[name];
const allEvents = this._events.all;
if (events) triggerEvents(events, args);
if (allEvents) triggerEvents(allEvents, arguments);
return this;
},
fireEvent: function () {
fireEvent() {
this.trigger.apply(this, arguments);
},
// Inversion-of-control versions of `on` and `once`. Tell *this* object to
// listen to an event in another object ... keeping track of what it's
// listening to.
listenTo: function (obj, name, callback) {
var listeningTo = this._listeningTo || (this._listeningTo = {});
var id = obj._listenId || (obj._listenId = BI._.uniqueId("l"));
listenTo(obj, name, callback) {
const listeningTo = this._listeningTo || (this._listeningTo = {});
const id = obj._listenId || (obj._listenId = _.uniqueId("l"));
listeningTo[id] = obj;
if (!callback && typeof name === "object") callback = this;
obj.on(name, callback, this);
return this;
},
listenToOnce: function (obj, name, callback) {
listenToOnce(obj, name, callback) {
if (typeof name === "object") {
for (var event in name) this.listenToOnce(obj, event, name[event]);
for (const event in name) {
this.listenToOnce(obj, event, name[event]);
}
return this;
}
if (eventSplitter.test(name)) {
var names = name.split(eventSplitter);
for (var i = 0, length = names.length; i < length; i++) {
const names = name.split(eventSplitter);
for (let i = 0, length = names.length; i < length; i++) {
this.listenToOnce(obj, names[i], callback);
}
return this;
}
if (!callback) return this;
var once = BI._.once(function () {
var once = _.once(function () {
this.stopListening(obj, name, once);
callback.apply(this, arguments);
});
once._callback = callback;
return this.listenTo(obj, name, once);
},
// Tell this object to stop listening to either specific events ... or
// to every object it's currently listening to.
stopListening: function (obj, name, callback) {
var listeningTo = this._listeningTo;
stopListening(obj, name, callback) {
let listeningTo = this._listeningTo;
if (!listeningTo) return this;
var remove = !name && !callback;
const remove = !name && !callback;
if (!callback && typeof name === "object") callback = this;
if (obj) (listeningTo = {})[obj._listenId] = obj;
for (var id in listeningTo) {
for (const id in listeningTo) {
obj = listeningTo[id];
obj.off(name, callback, this);
if (remove || BI._.isEmpty(obj._events)) delete this._listeningTo[id];
if (remove || _.isEmpty(obj._events)) {
delete this._listeningTo[id];
}
return this;
}
};
return this;
}
};
// Regular expression used to split event strings.
var eventSplitter = /\s+/;
// Regular expression used to split event strings.
var eventSplitter = /\s+/;
// Implement fancy features of the Events API such as multiple event
// names `"change blur"` and jQuery-style event maps `{change: action}`
// in terms of the existing API.
var eventsApi = function (obj, action, name, rest) {
// Implement fancy features of the Events API such as multiple event
// names `"change blur"` and jQuery-style event maps `{change: action}`
// in terms of the existing API.
var eventsApi = function (obj, action, name, rest) {
if (!name) return true;
// Handle event maps.
if (typeof name === "object") {
for (var key in name) {
for (const key in name) {
obj[action].apply(obj, [key, name[key]].concat(rest));
}
return false;
}
// Handle space separated event names.
if (eventSplitter.test(name)) {
var names = name.split(eventSplitter);
for (var i = 0, length = names.length; i < length; i++) {
const names = name.split(eventSplitter);
for (let i = 0, length = names.length; i < length; i++) {
obj[action].apply(obj, [names[i]].concat(rest));
}
return false;
}
return true;
};
// A difficult-to-believe, but optimized internal dispatch function for
// triggering events. Tries to keep the usual cases speedy (most internal
// BI events have 3 arguments).
var triggerEvents = function (events, args) {
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2];
};
// A difficult-to-believe, but optimized internal dispatch function for
// triggering events. Tries to keep the usual cases speedy (most internal
// BI events have 3 arguments).
var triggerEvents = function (events, args) {
let ev,
i = -1,
l = events.length,
a1 = args[0],
a2 = args[1],
a3 = args[2];
switch (args.length) {
case 0:
while (++i < l) (ev = events[i]).callback.call(ev.ctx);
return;
case 1:
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1);
return;
case 2:
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2);
return;
case 3:
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
while (++i < l) {
(ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
}
return;
default:
while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args);
return;
}
};
};
// BI.Router
// ---------------
// BI.Router
// ---------------
// Routers map faux-URLs to actions, and fire events when routes are
// matched. Creating a new one sets its `routes` hash, if not set statically.
var Router = BI.Router = function (options) {
// Routers map faux-URLs to actions, and fire events when routes are
// matched. Creating a new one sets its `routes` hash, if not set statically.
export const Router = function (options) {
options || (options = {});
if (options.routes) this.routes = options.routes;
this._bindRoutes();
this._init.apply(this, arguments);
};
};
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var optionalParam = /\((.*?)\)/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
// Set up all inheritable **BI.Router** properties and methods.
BI._.extend(Router.prototype, Events, {
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
const optionalParam = /\((.*?)\)/g;
const namedParam = /(\(\?)?:\w+/g;
const splatParam = /\*\w+/g;
const escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
// Set up all inheritable **BI.Router** properties and methods.
_.extend(Router.prototype, Events, {
// _init is an empty function by default. Override it with your own
// initialization logic.
_init: function () {
},
_init() {},
// Manually bind a single named route to a callback. For example:
//
@ -235,44 +274,47 @@
// ...
// });
//
route: function (route, name, callback) {
if (!BI._.isRegExp(route)) route = this._routeToRegExp(route);
if (BI._.isFunction(name)) {
route(route, name, callback) {
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (_.isFunction(name)) {
callback = name;
name = "";
}
if (!callback) callback = this[name];
var router = this;
BI.history.route(route, function (fragment) {
var args = router._extractParameters(route, fragment);
const router = this;
history.route(route, (fragment) => {
const args = router._extractParameters(route, fragment);
if (router.execute(callback, args, name) !== false) {
router.trigger.apply(router, ["route:" + name].concat(args));
router.trigger.apply(router, [`route:${name}`].concat(args));
router.trigger("route", name, args);
BI.history.trigger("route", router, name, args);
history.trigger("route", router, name, args);
}
});
return this;
},
// Execute a route handler with the provided parameters. This is an
// excellent place to do pre-route setup or post-route cleanup.
execute: function (callback, args, name) {
execute(callback, args, name) {
if (callback) callback.apply(this, args);
},
// Simple proxy to `BI.history` to save a fragment into the history.
navigate: function (fragment, options) {
BI.history.navigate(fragment, options);
navigate(fragment, options) {
history.navigate(fragment, options);
return this;
},
// Bind all defined routes to `BI.history`. We have to reverse the
// order of the routes here to support behavior where the most general
// routes can be defined at the bottom of the route map.
_bindRoutes: function () {
_bindRoutes() {
if (!this.routes) return;
this.routes = BI._.result(this, "routes");
var route, routes = BI._.keys(this.routes);
this.routes = _.result(this, "routes");
let route,
routes = _.keys(this.routes);
while ((route = routes.pop()) != null) {
this.route(route, this.routes[route]);
}
@ -280,25 +322,28 @@
// Convert a route string into a regular expression, suitable for matching
// against the current location hash.
_routeToRegExp: function (route) {
route = route.replace(escapeRegExp, "\\$&")
_routeToRegExp(route) {
route = route
.replace(escapeRegExp, "\\$&")
.replace(optionalParam, "(?:$1)?")
.replace(namedParam, function (match, optional) {
return optional ? match : "([^/?]+)";
})
.replace(namedParam, (match, optional) =>
optional ? match : "([^/?]+)"
)
.replace(splatParam, "([^?]*?)");
return new RegExp("^" + route + "(?:\\?([\\s\\S]*))?$");
return new RegExp(`^${route}(?:\\?([\\s\\S]*))?$`);
},
// Given a route, and a URL fragment that it matches, return the array of
// extracted decoded parameters. Empty or unmatched parameters will be
// treated as `null` to normalize cross-browser behavior.
_extractParameters: function (route, fragment) {
var params = route.exec(fragment).slice(1);
return BI._.map(params, function (param, i) {
_extractParameters(route, fragment) {
const params = route.exec(fragment).slice(1);
return _.map(params, (param, i) => {
// Don't decode the search params.
if (i === params.length - 1) return param || null;
var resultParam = null;
let resultParam = null;
if (param) {
try {
resultParam = decodeURIComponent(param);
@ -306,84 +351,86 @@
resultParam = param;
}
}
return resultParam;
});
}
});
});
// History
// ----------------
// History
// ----------------
// Handles cross-browser history management, based on either
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
// and URL fragments. If the browser supports neither (old IE, natch),
// falls back to polling.
var History = function () {
// Handles cross-browser history management, based on either
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
// and URL fragments. If the browser supports neither (old IE, natch),
// falls back to polling.
const History = function () {
this.handlers = [];
this.checkUrl = BI._.bind(this.checkUrl, this);
this.checkUrl = _.bind(this.checkUrl, this);
// Ensure that `History` can be used outside of the browser.
if (typeof window !== "undefined") {
this.location = _global.location;
this.history = _global.history;
}
};
// Cached regex for stripping a leading hash/slash and trailing space.
var routeStripper = /^[#\/]|\s+$/g;
};
// Cached regex for stripping leading and trailing slashes.
var rootStripper = /^\/+|\/+$/g;
// Cached regex for stripping a leading hash/slash and trailing space.
const routeStripper = /^[#\/]|\s+$/g;
// Cached regex for stripping urls of hash.
var pathStripper = /#.*$/;
// Cached regex for stripping leading and trailing slashes.
const rootStripper = /^\/+|\/+$/g;
// Has the history handling already been started?
History.started = false;
// Cached regex for stripping urls of hash.
const pathStripper = /#.*$/;
// Set up all inheritable **BI.History** properties and methods.
BI._.extend(History.prototype, Events, {
// Has the history handling already been started?
History.started = false;
// Set up all inheritable **BI.History** properties and methods.
_.extend(History.prototype, Events, {
// The default interval to poll for hash changes, if necessary, is
// twenty times a second.
interval: 50,
// Are we at the app root?
atRoot: function () {
var path = this.location.pathname.replace(/[^\/]$/, "$&/");
atRoot() {
const path = this.location.pathname.replace(/[^\/]$/, "$&/");
return path === this.root && !this.getSearch();
},
// In IE6, the hash fragment and search params are incorrect if the
// fragment contains `?`.
getSearch: function () {
var match = this.location.href.replace(/#.*/, "").match(/\?.+/);
getSearch() {
const match = this.location.href.replace(/#.*/, "").match(/\?.+/);
return match ? match[0] : "";
},
// Gets the true hash value. Cannot use location.hash directly due to bug
// in Firefox where location.hash will always be decoded.
getHash: function (window) {
var match = (window || this).location.href.match(/#(.*)$/);
getHash(window) {
const match = (window || this).location.href.match(/#(.*)$/);
return match ? match[1] : "";
},
// Get the pathname and search params, without the root.
getPath: function () {
var path = this.location.pathname + this.getSearch();
getPath() {
let path = this.location.pathname + this.getSearch();
try {
path = decodeURI(path);
} catch(e) {
}
var root = this.root.slice(0, -1);
} catch (e) {}
const root = this.root.slice(0, -1);
if (!path.indexOf(root)) path = path.slice(root.length);
return path.charAt(0) === "/" ? path.slice(1) : path;
},
// Get the cross-browser normalized URL fragment from the path or hash.
getFragment: function (fragment) {
getFragment(fragment) {
if (fragment == null) {
if (this._hasPushState || !this._wantsHashChange) {
fragment = this.getPath();
@ -391,73 +438,91 @@
fragment = this.getHash();
}
}
return fragment.replace(routeStripper, "");
},
// Start the hash change handling, returning `true` if the current URL matches
// an existing route, and `false` otherwise.
start: function (options) {
if (History.started) throw new Error("BI.history has already been started");
start(options) {
if (History.started) {
throw new Error("BI.history has already been started");
}
History.started = true;
// Figure out the initial configuration. Do we need an iframe?
// Is pushState desired ... is it available?
this.options = BI._.extend({root: "/"}, this.options, options);
this.options = _.extend({ root: "/" }, this.options, options);
this.root = this.options.root;
this._wantsHashChange = this.options.hashChange !== false;
this._hasHashChange = "onhashchange" in window;
this._wantsPushState = !!this.options.pushState;
this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState);
this._hasPushState = !!(
this.options.pushState &&
this.history &&
this.history.pushState
);
this.fragment = this.getFragment();
// Normalize root to always include a leading and trailing slash.
this.root = ("/" + this.root + "/").replace(rootStripper, "/");
this.root = `/${this.root}/`.replace(rootStripper, "/");
// Transition from hashChange to pushState or vice versa if both are
// requested.
if (this._wantsHashChange && this._wantsPushState) {
// If we've started off with a route from a `pushState`-enabled
// browser, but we're currently in a browser that doesn't support it...
if (!this._hasPushState && !this.atRoot()) {
var root = this.root.slice(0, -1) || "/";
this.location.replace(root + "#" + this.getPath());
const root = this.root.slice(0, -1) || "/";
this.location.replace(`${root}#${this.getPath()}`);
// Return immediately as browser will do redirect to new url
return true;
// Or if we've started out with a hash-based route, but we're currently
// in a browser where it could be `pushState`-based instead...
} else if (this._hasPushState && this.atRoot()) {
this.navigate(this.getHash(), {replace: true});
this.navigate(this.getHash(), { replace: true });
}
}
// Proxy an iframe to handle location events if the browser doesn't
// support the `hashchange` event, HTML5 history, or the user wants
// `hashChange` but not `pushState`.
if (!this._hasHashChange && this._wantsHashChange && (!this._wantsPushState || !this._hasPushState)) {
var iframe = document.createElement("iframe");
if (
!this._hasHashChange &&
this._wantsHashChange &&
(!this._wantsPushState || !this._hasPushState)
) {
const iframe = document.createElement("iframe");
iframe.src = "javascript:0";
iframe.style.display = "none";
iframe.tabIndex = -1;
var body = document.body;
const body = document.body;
// Using `appendChild` will throw on IE < 9 if the document is not ready.
this.iframe = body.insertBefore(iframe, body.firstChild).contentWindow;
this.iframe = body.insertBefore(
iframe,
body.firstChild
).contentWindow;
this.iframe.document.open().close();
this.iframe.location.hash = "#" + this.fragment;
this.iframe.location.hash = `#${this.fragment}`;
}
// Add a cross-platform `addEventListener` shim for older browsers.
var addEventListener = _global.addEventListener || function (eventName, listener) {
return attachEvent("on" + eventName, listener);
const addEventListener =
_global.addEventListener ||
function (eventName, listener) {
return attachEvent(`on${eventName}`, listener);
};
// Depending on whether we're using pushState or hashes, and whether
// 'onhashchange' is supported, determine how we check the URL state.
if (this._hasPushState) {
addEventListener("popstate", this.checkUrl, false);
} else if (this._wantsHashChange && this._hasHashChange && !this.iframe) {
} else if (
this._wantsHashChange &&
this._hasHashChange &&
!this.iframe
) {
addEventListener("hashchange", this.checkUrl, false);
} else if (this._wantsHashChange) {
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
@ -468,16 +533,22 @@
// Disable BI.history, perhaps temporarily. Not useful in a real app,
// but possibly useful for unit testing Routers.
stop: function () {
stop() {
// Add a cross-platform `removeEventListener` shim for older browsers.
var removeEventListener = _global.removeEventListener || function (eventName, listener) {
return detachEvent("on" + eventName, listener);
const removeEventListener =
_global.removeEventListener ||
function (eventName, listener) {
return detachEvent(`on${eventName}`, listener);
};
// Remove window listeners.
if (this._hasPushState) {
removeEventListener("popstate", this.checkUrl, false);
} else if (this._wantsHashChange && this._hasHashChange && !this.iframe) {
} else if (
this._wantsHashChange &&
this._hasHashChange &&
!this.iframe
) {
removeEventListener("hashchange", this.checkUrl, false);
}
@ -494,14 +565,17 @@
// Add a route to be tested when the fragment changes. Routes added later
// may override previous routes.
route: function (route, callback) {
this.handlers.unshift({route: route, callback: callback});
route(route, callback) {
this.handlers.unshift({ route, callback });
},
// check route is Exist. if exist, return the route
checkRoute: function (route) {
for (var i = 0; i < this.handlers.length; i++) {
if (this.handlers[i].route.toString() === Router.prototype._routeToRegExp(route).toString()) {
checkRoute(route) {
for (let i = 0; i < this.handlers.length; i++) {
if (
this.handlers[i].route.toString() ===
Router.prototype._routeToRegExp(route).toString()
) {
return this.handlers[i];
}
}
@ -510,10 +584,10 @@
},
// remove a route match in routes
unRoute: function (route) {
var index = BI._.findIndex(this.handlers, function (handler) {
return handler.route.test(route);
});
unRoute(route) {
const index = _.findIndex(this.handlers, (handler) =>
handler.route.test(route)
);
if (index > -1) {
this.handlers.splice(index, 1);
}
@ -521,14 +595,13 @@
// Checks the current URL to see if it has changed, and if it has,
// calls `loadUrl`, normalizing across the hidden iframe.
checkUrl: function (e) {
var current = this.getFragment();
checkUrl(e) {
let current = this.getFragment();
try {
// getFragment 得到的值是编码过的,而this.fragment是没有编码过的
// 英文路径没有问题,遇上中文和空格有问题了
current = decodeURIComponent(current);
} catch(e) {
}
} catch (e) {}
// If the user pressed the back button, the iframe's hash will have
// changed and we should use that for comparison.
if (current === this.fragment && this.iframe) {
@ -543,11 +616,13 @@
// Attempt to load the current URL fragment. If a route succeeds with a
// match, returns `true`. If no defined routes matches the fragment,
// returns `false`.
loadUrl: function (fragment) {
loadUrl(fragment) {
fragment = this.fragment = this.getFragment(fragment);
return BI._.some(this.handlers, function (handler) {
return _.some(this.handlers, (handler) => {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
@ -560,44 +635,51 @@
// The options object can contain `trigger: true` if you wish to have the
// route callback be fired (not usually desirable), or `replace: true`, if
// you wish to modify the current URL without adding an entry to the history.
navigate: function (fragment, options) {
navigate(fragment, options) {
if (!History.started) return false;
if (!options || options === true) options = {trigger: !!options};
if (!options || options === true) options = { trigger: !!options };
// Normalize the fragment.
fragment = this.getFragment(fragment || "");
// Don't include a trailing slash on the root.
var root = this.root;
let root = this.root;
if (fragment === "" || fragment.charAt(0) === "?") {
root = root.slice(0, -1) || "/";
}
var url = root + fragment;
const url = root + fragment;
// Strip the hash and decode for matching.
fragment = fragment.replace(pathStripper, "")
fragment = fragment.replace(pathStripper, "");
try {
fragment = decodeURI(fragment);
} catch(e) {
}
} catch (e) {}
if (this.fragment === fragment) return;
this.fragment = fragment;
// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.history[options.replace ? "replaceState" : "pushState"]({}, document.title, url);
this.history[options.replace ? "replaceState" : "pushState"](
{},
document.title,
url
);
// If hash changes haven't been explicitly disabled, update the hash
// fragment to store history.
} else if (this._wantsHashChange) {
this._updateHash(this.location, fragment, options.replace);
if (this.iframe && (fragment !== this.getHash(this.iframe))) {
if (this.iframe && fragment !== this.getHash(this.iframe)) {
// Opening and closing the iframe tricks IE7 and earlier to push a
// history entry on hash-tag change. When replace is true, we don't
// want this.
if (!options.replace) this.iframe.document.open().close();
this._updateHash(this.iframe.location, fragment, options.replace);
this._updateHash(
this.iframe.location,
fragment,
options.replace
);
}
// If you've told us that you explicitly don't want fallback hashchange-
@ -610,18 +692,16 @@
// Update the hash location, either replacing the current entry, or adding
// a new one to the browser history.
_updateHash: function (location, fragment, replace) {
_updateHash(location, fragment, replace) {
if (replace) {
var href = location.href.replace(/(javascript:|#).*$/, "");
location.replace(href + "#" + fragment);
const href = location.href.replace(/(javascript:|#).*$/, "");
location.replace(`${href}#${fragment}`);
} else {
// Some browsers require that `hash` contains a leading #.
location.hash = "#" + fragment;
location.hash = `#${fragment}`;
}
}
});
});
// Create the default BI.history.
BI.history = new History;
}());
// Create the default BI.history.
export const history = new History();

3431
src/router/router.js

File diff suppressed because it is too large Load Diff

1
src/widget/index.js

@ -1,4 +1,5 @@
export { Collapse } from "./collapse/collapse";
export { SelectTreeExpander } from "./selecttree/selecttree.expander";
export { SelectTreeCombo } from "./selecttree/selecttree.combo";
export { SingleTreeCombo } from "./singletree/singletree.combo";
export { MultiTreeCombo } from "./multitree/multi.tree.combo";

2
template/index.html

@ -38,7 +38,7 @@
<div id="wrapper"></div>
</body>
<script src="../dist/router.js"></script>
<!-- <script src="../dist/router.js"></script> -->
<script>
BI.pixUnit = "rem";
BI.pixRatio = 10;

6
webpack/attachments.js

@ -18,9 +18,9 @@ const basicAttachmentMap = {
core: sync([
"src/less/core/**/*.less",
"src/less/theme/**/*.less",
lodashJs,
jqueryJs,
popperJs,
// lodashJs,
// jqueryJs,
// popperJs,
"src/core/conflict.js",
"src/bundle.js",
// "src/core/**/*.js",

18
webpack/webpack.common.js

@ -63,15 +63,15 @@ module.exports = {
},
],
},
{
test: /\.js$/,
include: [path.resolve(__dirname, '../', attachments.lodash)],
use: [
{
loader: 'script-loader',
},
],
},
// {
// test: /\.js$/,
// include: [path.resolve(__dirname, '../', attachments.lodash)],
// use: [
// {
// loader: 'script-loader',
// },
// ],
// },
{
test: path.resolve(__dirname, '../', attachments.fix),
use: [

Loading…
Cancel
Save