Zhenfei.Li
2 years ago
8 changed files with 330 additions and 298 deletions
@ -1,123 +1,127 @@
|
||||
BI.Plugin = BI.Plugin || {}; |
||||
!(function () { |
||||
var _WidgetsPlugin = {}; |
||||
var _ObjectPlugin = {}; |
||||
var _ConfigPlugin = {}; |
||||
var _ConfigRenderPlugin = {}; |
||||
var _GlobalWidgetConfigFns = []; |
||||
var __GlobalObjectConfigFns = []; |
||||
BI.defaults(BI.Plugin, { |
||||
const _WidgetsPlugin = {}; |
||||
const _ObjectPlugin = {}; |
||||
const _ConfigPlugin = {}; |
||||
const _ConfigRenderPlugin = {}; |
||||
let _GlobalWidgetConfigFns = []; |
||||
let __GlobalObjectConfigFns = []; |
||||
|
||||
getWidget: function (type, options) { |
||||
if (_GlobalWidgetConfigFns.length > 0) { |
||||
var fns = _GlobalWidgetConfigFns.slice(0); |
||||
for (var i = fns.length - 1; i >= 0; i--) { |
||||
fns[i](type, options); |
||||
} |
||||
export const Plugin = { |
||||
getWidget (type, options) { |
||||
if (_GlobalWidgetConfigFns.length > 0) { |
||||
const fns = _GlobalWidgetConfigFns.slice(0); |
||||
for (let i = fns.length - 1; i >= 0; i--) { |
||||
fns[i](type, options); |
||||
} |
||||
} |
||||
|
||||
var res; |
||||
if (_ConfigPlugin[type]) { |
||||
for (var i = _ConfigPlugin[type].length - 1; i >= 0; i--) { |
||||
if (res = _ConfigPlugin[type][i](options)) { |
||||
options = res; |
||||
} |
||||
let res; |
||||
if (_ConfigPlugin[type]) { |
||||
for (let i = _ConfigPlugin[type].length - 1; i >= 0; i--) { |
||||
res = _ConfigPlugin[type][i](options); |
||||
if (res) { |
||||
options = res; |
||||
} |
||||
} |
||||
// Deprecated
|
||||
if (_WidgetsPlugin[type]) { |
||||
for (var i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { |
||||
if (res = _WidgetsPlugin[type][i](options)) { |
||||
return res; |
||||
} |
||||
} |
||||
// Deprecated
|
||||
if (_WidgetsPlugin[type]) { |
||||
for (let i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { |
||||
res = _WidgetsPlugin[type][i](options); |
||||
if (res) { |
||||
return res; |
||||
} |
||||
} |
||||
return options; |
||||
}, |
||||
} |
||||
|
||||
return options; |
||||
}, |
||||
|
||||
config: function (widgetConfigFn, objectConfigFn) { |
||||
_GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]); |
||||
__GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]); |
||||
}, |
||||
config (widgetConfigFn, objectConfigFn) { |
||||
_GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]); |
||||
__GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]); |
||||
}, |
||||
|
||||
configWidget: function (type, fn, opt) { |
||||
// opt.single: true 最后一次注册有效
|
||||
if (!_ConfigPlugin[type] || (opt && opt.single)) { |
||||
_ConfigPlugin[type] = []; |
||||
} |
||||
_ConfigPlugin[type].push(fn); |
||||
}, |
||||
configWidget (type, fn, opt) { |
||||
// opt.single: true 最后一次注册有效
|
||||
if (!_ConfigPlugin[type] || (opt && opt.single)) { |
||||
_ConfigPlugin[type] = []; |
||||
} |
||||
_ConfigPlugin[type].push(fn); |
||||
}, |
||||
|
||||
getRender: function (type, rendered) { |
||||
var res; |
||||
if (_ConfigRenderPlugin[type]) { |
||||
for (var i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { |
||||
if (res = _ConfigRenderPlugin[type][i](rendered)) { |
||||
rendered = res; |
||||
} |
||||
getRender (type, rendered) { |
||||
let res; |
||||
if (_ConfigRenderPlugin[type]) { |
||||
for (let i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { |
||||
res = _ConfigRenderPlugin[type][i](rendered); |
||||
if (res) { |
||||
rendered = res; |
||||
} |
||||
} |
||||
return rendered; |
||||
}, |
||||
} |
||||
|
||||
return rendered; |
||||
}, |
||||
|
||||
configRender: function (type, fn) { |
||||
if (!_ConfigRenderPlugin[type]) { |
||||
_ConfigRenderPlugin[type] = []; |
||||
} |
||||
_ConfigRenderPlugin[type].push(fn); |
||||
}, |
||||
configRender (type, fn) { |
||||
if (!_ConfigRenderPlugin[type]) { |
||||
_ConfigRenderPlugin[type] = []; |
||||
} |
||||
_ConfigRenderPlugin[type].push(fn); |
||||
}, |
||||
|
||||
// Deprecated
|
||||
registerWidget: function (type, fn) { |
||||
if (!_WidgetsPlugin[type]) { |
||||
_WidgetsPlugin[type] = []; |
||||
} |
||||
if (_WidgetsPlugin[type].length > 0) { |
||||
console.log("组件已经注册过了!"); |
||||
} |
||||
_WidgetsPlugin[type].push(fn); |
||||
}, |
||||
// Deprecated
|
||||
registerWidget (type, fn) { |
||||
if (!_WidgetsPlugin[type]) { |
||||
_WidgetsPlugin[type] = []; |
||||
} |
||||
if (_WidgetsPlugin[type].length > 0) { |
||||
console.log("组件已经注册过了!"); |
||||
} |
||||
_WidgetsPlugin[type].push(fn); |
||||
}, |
||||
|
||||
// Deprecated
|
||||
relieveWidget: function (type) { |
||||
delete _WidgetsPlugin[type]; |
||||
}, |
||||
// Deprecated
|
||||
relieveWidget (type) { |
||||
delete _WidgetsPlugin[type]; |
||||
}, |
||||
|
||||
getObject: function (type, object) { |
||||
if (__GlobalObjectConfigFns.length > 0) { |
||||
var fns = __GlobalObjectConfigFns.slice(0); |
||||
for (var i = fns.length - 1; i >= 0; i--) { |
||||
fns[i](type, object); |
||||
} |
||||
getObject (type, object) { |
||||
if (__GlobalObjectConfigFns.length > 0) { |
||||
const fns = __GlobalObjectConfigFns.slice(0); |
||||
for (let i = fns.length - 1; i >= 0; i--) { |
||||
fns[i](type, object); |
||||
} |
||||
} |
||||
|
||||
if (_ObjectPlugin[type]) { |
||||
var res; |
||||
for (var i = 0, len = _ObjectPlugin[type].length; i < len; i++) { |
||||
if (res = _ObjectPlugin[type][i](object)) { |
||||
object = res; |
||||
} |
||||
let res; |
||||
if (_ObjectPlugin[type]) { |
||||
for (let i = 0, len = _ObjectPlugin[type].length; i < len; i++) { |
||||
res = _ObjectPlugin[type][i](object); |
||||
if (res) { |
||||
object = res; |
||||
} |
||||
} |
||||
return res || object; |
||||
}, |
||||
|
||||
hasObject: function (type) { |
||||
return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; |
||||
}, |
||||
} |
||||
|
||||
return res || object; |
||||
}, |
||||
|
||||
registerObject: function (type, fn) { |
||||
if (!_ObjectPlugin[type]) { |
||||
_ObjectPlugin[type] = []; |
||||
} |
||||
if (_ObjectPlugin[type].length > 0) { |
||||
console.log("对象已经注册过了!"); |
||||
} |
||||
_ObjectPlugin[type].push(fn); |
||||
}, |
||||
hasObject (type) { |
||||
return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; |
||||
}, |
||||
|
||||
relieveObject: function (type) { |
||||
delete _ObjectPlugin[type]; |
||||
registerObject (type, fn) { |
||||
if (!_ObjectPlugin[type]) { |
||||
_ObjectPlugin[type] = []; |
||||
} |
||||
if (_ObjectPlugin[type].length > 0) { |
||||
console.log("对象已经注册过了!"); |
||||
} |
||||
}); |
||||
})(); |
||||
_ObjectPlugin[type].push(fn); |
||||
}, |
||||
|
||||
relieveObject (type) { |
||||
delete _ObjectPlugin[type]; |
||||
}, |
||||
}; |
||||
|
@ -1,9 +1,21 @@
|
||||
// export * from "../../typescript/core/decorator/decorator.ts";
|
||||
|
||||
import { shortcut as biShortcut, provider as biProvider } from "./5.inject"; |
||||
|
||||
/** |
||||
* 注册widget |
||||
*/ |
||||
import { shortcut as biShortcut } from "./5.inject"; |
||||
export function shortcut() { |
||||
return function decorator(Target) { |
||||
biShortcut(Target.xtype, Target); |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* 注册provider |
||||
*/ |
||||
export function provider() { |
||||
return function decorator(Target) { |
||||
biProvider(Target.xtype, Target); |
||||
}; |
||||
} |
||||
|
@ -1,58 +1,59 @@
|
||||
BI.Fragment = function () { |
||||
}; |
||||
import { isNotNull, isArray, isFunction, isKey, extend } from "./2.base"; |
||||
|
||||
BI.h = function (type, props, children) { |
||||
if (children != null) { |
||||
if (!BI.isArray(children)) { |
||||
export function Fragment () {} |
||||
|
||||
export function h (type, props, children) { |
||||
if (isNotNull(children)) { |
||||
if (!isArray(children)) { |
||||
children = [children]; |
||||
} |
||||
} else { |
||||
children = []; |
||||
} |
||||
if (arguments.length > 3) { |
||||
for (var i = 3; i < arguments.length; i++) { |
||||
if (BI.isArray(arguments[i])) { |
||||
for (let i = 3; i < arguments.length; i++) { |
||||
if (isArray(arguments[i])) { |
||||
children = children.concat(arguments[i]); |
||||
} else { |
||||
children.push(arguments[i]); |
||||
} |
||||
} |
||||
} |
||||
if (type === BI.Fragment) { |
||||
if (type === Fragment) { |
||||
return children; |
||||
} |
||||
if (BI.isFunction(type)) { |
||||
if (isFunction(type)) { |
||||
type = type.xtype || type; |
||||
} |
||||
if (type === "el") { |
||||
return BI.extend({ |
||||
el: children[0] |
||||
return extend({ |
||||
el: children[0], |
||||
}, props); |
||||
} |
||||
if (type === "left") { |
||||
return BI.extend({ |
||||
left: children |
||||
return extend({ |
||||
left: children, |
||||
}, props); |
||||
} |
||||
if (type === "right") { |
||||
return BI.extend({ |
||||
right: children |
||||
return extend({ |
||||
right: children, |
||||
}, props); |
||||
} |
||||
if (children.length === 1) { |
||||
if (BI.isKey(children[0])) { |
||||
return BI.extend({ |
||||
type: type |
||||
if (isKey(children[0])) { |
||||
return extend({ |
||||
type, |
||||
}, { text: children[0] }, props); |
||||
} |
||||
if (BI.isFunction(children[0])) { |
||||
return BI.extend({ |
||||
type: type |
||||
if (isFunction(children[0])) { |
||||
return extend({ |
||||
type, |
||||
}, { items: children[0] }, props); |
||||
} |
||||
} |
||||
|
||||
return BI.extend({ |
||||
type: type |
||||
return extend({ |
||||
type, |
||||
}, children.length > 0 ? { items: children } : {}, props); |
||||
}; |
||||
} |
||||
|
@ -1,51 +1,54 @@
|
||||
!(function () { |
||||
BI.useInWorker = function () { |
||||
function createWatcher (model, keyOrFn, cb, options) { |
||||
if (BI.isPlainObject(cb)) { |
||||
options = cb; |
||||
cb = cb.handler; |
||||
} |
||||
options = options || {}; |
||||
return Fix.watch(model, keyOrFn, cb, BI.extend(options, { |
||||
store: model |
||||
})); |
||||
import { isPlainObject, extend, each, isArray } from "./2.base"; |
||||
import { Models } from "./5.inject"; |
||||
|
||||
export function useInWorker () { |
||||
function createWatcher (model, keyOrFn, cb, options) { |
||||
if (isPlainObject(cb)) { |
||||
options = cb; |
||||
cb = cb.handler; |
||||
} |
||||
options = options || {}; |
||||
|
||||
return Fix.watch(model, keyOrFn, cb, extend(options, { |
||||
store: model, |
||||
})); |
||||
} |
||||
|
||||
var models = {}, watches = {}; |
||||
addEventListener("message", function (e) { |
||||
var data = e.data; |
||||
switch (data.eventType) { |
||||
case "action": |
||||
models[data.name][data.action].apply(models[data.name], data.args); |
||||
break; |
||||
case "destroy": |
||||
BI.each(watches[data.name], function (i, unwatches) { |
||||
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches]; |
||||
BI.each(unwatches, function (j, unwatch) { |
||||
unwatch(); |
||||
}); |
||||
}); |
||||
delete models[data.name]; |
||||
delete watches[data.name]; |
||||
break; |
||||
case "create": |
||||
var store = models[data.name] = BI.Models.getModel(data.type, data.options); |
||||
watches[data.name] = []; |
||||
BI.each(data.watches, function (i, key) { |
||||
watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) { |
||||
postMessage(BI.extend({}, data, { |
||||
eventType: "watch", |
||||
currentWatchType: key |
||||
}, {args: [newValue, oldValue]})); |
||||
})); |
||||
}); |
||||
postMessage(BI.extend({}, data, { |
||||
eventType: "create" |
||||
}, {msg: store.model})); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
}, false); |
||||
}; |
||||
}()); |
||||
const models = {}, watches = {}; |
||||
addEventListener("message", e => { |
||||
const data = e.data; |
||||
let store; |
||||
switch (data.eventType) { |
||||
case "action": |
||||
models[data.name][data.action](...data.args); |
||||
break; |
||||
case "destroy": |
||||
each(watches[data.name], (i, unwatches) => { |
||||
unwatches = isArray(unwatches) ? unwatches : [unwatches]; |
||||
each(unwatches, (j, unwatch) => { |
||||
unwatch(); |
||||
}); |
||||
}); |
||||
delete models[data.name]; |
||||
delete watches[data.name]; |
||||
break; |
||||
case "create": |
||||
store = models[data.name] = Models.getModel(data.type, data.options); |
||||
watches[data.name] = []; |
||||
each(data.watches, (i, key) => { |
||||
watches[data.name].push(createWatcher(store.model, key, (newValue, oldValue) => { |
||||
postMessage(extend({}, data, { |
||||
eventType: "watch", |
||||
currentWatchType: key, |
||||
}, { args: [newValue, oldValue] })); |
||||
})); |
||||
}); |
||||
postMessage(extend({}, data, { |
||||
eventType: "create", |
||||
}, { msg: store.model })); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
}, false); |
||||
} |
||||
|
Loading…
Reference in new issue