2015-08-26 14:52:29 +08:00
|
|
|
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) {
|
2015-08-26 14:52:29 +08:00
|
|
|
if (comp.prototype && comp.prototype instanceof Component) {
|
2017-11-05 13:42:26 +08:00
|
|
|
let children = args.slice(1);
|
|
|
|
if (children.length === 1 && Array.isArray(children[0])) {
|
2020-04-17 17:57:55 +08:00
|
|
|
children = children[0];
|
2017-11-05 13:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return comp.component(args[0], children);
|
2015-08-26 14:52:29 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:36:37 +08:00
|
|
|
const node = mo.apply(this, arguments);
|
|
|
|
|
|
|
|
if (node.attrs.bidi) {
|
|
|
|
m.bidi(node, node.attrs.bidi);
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:39:38 +08:00
|
|
|
if (node.attrs.route) {
|
|
|
|
node.attrs.href = node.attrs.route;
|
|
|
|
node.attrs.config = m.route;
|
|
|
|
|
|
|
|
delete node.attrs.route;
|
|
|
|
}
|
|
|
|
|
2015-09-18 11:36:37 +08:00
|
|
|
return node;
|
|
|
|
};
|
2015-08-26 14:52:29 +08:00
|
|
|
|
2020-04-17 17:57:55 +08:00
|
|
|
Object.keys(mo).forEach((key) => (m[key] = mo[key]));
|
2015-08-26 14:52:29 +08:00
|
|
|
|
2015-08-31 10:34:51 +08:00
|
|
|
/**
|
|
|
|
* 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 () {
|
2015-08-31 10:34:51 +08:00
|
|
|
m.startComputation();
|
|
|
|
m.endComputation();
|
|
|
|
};
|
|
|
|
|
2015-08-26 14:52:29 +08:00
|
|
|
global.m = m;
|
|
|
|
}
|