diff --git a/ui/lib/js/decorator.js b/ui/lib/js/decorator.js index 54b29c5e..a086892f 100644 --- a/ui/lib/js/decorator.js +++ b/ui/lib/js/decorator.js @@ -59,3 +59,27 @@ export function throttle(wait) { } } } + +/** + * 加载第三方sdk + * @param sdk + * @param interval + * @param name + * @returns {(function(*, *, *): void)|*} + */ +export function load(sdk, interval = 200, name = "") { + return function (target, n, descriptor) { + const origin = descriptor.value + let c = 0 + const loop = (that, args) => { + if (!!sdk) { + origin.apply(that, args) + } else if (c < 10) { + setTimeout(() => ++c && loop(that, args), interval) + } else throw new Error("无法加载" + name) + } + descriptor.value = function () { + loop(this, arguments) + } + } +} diff --git a/ui/lib/js/directives.js b/ui/lib/js/directives.js new file mode 100644 index 00000000..e9b4ce7f --- /dev/null +++ b/ui/lib/js/directives.js @@ -0,0 +1,28 @@ +const map = { + throttle: { + bind: function (el, obj) { + let timerId = null + let flag = true + + el.addEventListener('input', function () { + if (!flag) return + + flag = false + timerId && clearTimeout(timerId) + timerId = setTimeout(function () { + flag = true + obj.value() + }, 800) + }) + } + } +} + + +export default { + install(Vue) { + for (const key in map) { + Vue.directive(key, map[key]) + } + } +} diff --git a/ui/packages/index.js b/ui/packages/index.js index c5ebbee1..d63557c4 100644 --- a/ui/packages/index.js +++ b/ui/packages/index.js @@ -1,29 +1,15 @@ //本地仓库外部组件 // 存储组件列表 +import directives from "../lib/js/directives"; + let components = []; // 定义 install 方法,接收 Vue 作为参数。如果使用 use 注册插件,则所有的组件都将被注册 const install = function (Vue) { if (install.installed) return; // 遍历注册全局组件 let contexts = require.context('.', true, /[\\\/]Ai([^\\\/]+)\.vue$/); - Vue.directive('throttle', { - bind: function (el, obj) { - let timerId = null - let flag = true - - el.addEventListener('input', function () { - if (!flag) return - - flag = false - timerId && clearTimeout(timerId) - timerId = setTimeout(function () { - flag = true - obj.value() - }, 800) - }) - } - }) + Vue.use(directives) if (contexts) { contexts.keys().map((e) => { components.push(contexts(e).default);