From 7b8b310651069bc5acef1a6df0f65c97342c89e5 Mon Sep 17 00:00:00 2001 From: Treecat Date: Fri, 3 Mar 2023 14:57:55 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14512=20refact:=20=E8=B0=83=E6=95=B4=20b?= =?UTF-8?q?ehavior=20=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/behavior/0.behavior.js | 17 +++++++++++++ src/base/behavior/behavior.highlight.js | 34 +++++++++++++++++++++++++ src/base/behavior/behavior.redmark.js | 25 ++++++++++++++++++ src/base/behavior/index.js | 26 +++++++++++++++++++ src/base/combination/group.button.js | 2 +- 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/base/behavior/0.behavior.js create mode 100644 src/base/behavior/behavior.highlight.js create mode 100644 src/base/behavior/behavior.redmark.js create mode 100644 src/base/behavior/index.js diff --git a/src/base/behavior/0.behavior.js b/src/base/behavior/0.behavior.js new file mode 100644 index 000000000..99ef2f633 --- /dev/null +++ b/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() { + + } +} diff --git a/src/base/behavior/behavior.highlight.js b/src/base/behavior/behavior.highlight.js new file mode 100644 index 000000000..bd60ac46e --- /dev/null +++ b/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); + } + }); + } +} diff --git a/src/base/behavior/behavior.redmark.js b/src/base/behavior/behavior.redmark.js new file mode 100644 index 000000000..9e770195a --- /dev/null +++ b/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); + } + }); + } +} diff --git a/src/base/behavior/index.js b/src/base/behavior/index.js new file mode 100644 index 000000000..52d5f6499 --- /dev/null +++ b/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 +}; diff --git a/src/base/combination/group.button.js b/src/base/combination/group.button.js index 99437247c..a88c79b1f 100644 --- a/src/base/combination/group.button.js +++ b/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.