framework/js/old/common/utils/patchMithril.js

46 lines
971 B
JavaScript
Raw Normal View History

import Component from '../Component';
export default function patchMithril(global) {
const mo = global.m;
2020-04-17 17:57:55 +08:00
const m = function (comp, ...args) {
if (comp.prototype && comp.prototype instanceof Component) {
let children = args.slice(1);
if (children.length === 1 && Array.isArray(children[0])) {
2020-04-17 17:57:55 +08:00
children = children[0];
}
return comp.component(args[0], children);
}
const node = mo.apply(this, arguments);
if (node.attrs.bidi) {
m.bidi(node, node.attrs.bidi);
}
if (node.attrs.route) {
node.attrs.href = node.attrs.route;
node.attrs.config = m.route;
delete node.attrs.route;
}
return node;
};
2020-04-17 17:57:55 +08:00
Object.keys(mo).forEach((key) => (m[key] = mo[key]));
/**
* Redraw only if not in the middle of a computation (e.g. a route change).
*
* @return {void}
*/
2020-04-17 17:57:55 +08:00
m.lazyRedraw = function () {
m.startComputation();
m.endComputation();
};
global.m = m;
}