nornj
Version:
A powerful template engine that can works with React, JSX enhancement or alternative tools.
313 lines (243 loc) • 8.05 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.styleProps = styleProps;
exports.getData = getData;
exports.getAccessorData = getAccessorData;
exports.getElement = getElement;
exports.getElementRefer = getElementRefer;
exports.getElementName = getElementName;
exports.addArgs = addArgs;
exports.newContext = newContext;
exports.fixPropName = fixPropName;
exports.assignStrProps = assignStrProps;
exports.exRet = exRet;
exports.tmplWrap = tmplWrap;
exports.template = template;
var _core = _interopRequireDefault(require("../core"));
var tools = _interopRequireWildcard(require("../utils/tools"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var REGEX_NUM = /^(-?([0-9]+[.]?[0-9]+)|[0-9])$/; //提取style内参数
function styleProps(obj) {
//If the parameter is a style object,then direct return.
if (tools.isObject(obj) || tools.isArray(obj) || tools.isNumber(obj)) {
return obj;
} //参数为字符串
var pattern = /([^\s:]+)[\s]?:[\s]?([^;]+)[;]?/g;
var matchArr, ret;
while (matchArr = pattern.exec(obj)) {
var key = matchArr[1];
var value = matchArr[2];
if (!ret) {
ret = {};
} //Convert to lowercase when style name is all capital.
if (/^[A-Z-]+$/.test(key)) {
key = key.toLowerCase();
} //将连字符转为驼峰命名
key = tools.camelCase(key);
ret[key] = REGEX_NUM.test(value) ? Number(value) : value;
}
return ret;
} //Get value from multiple datas
function getData(prop, data, hasSource) {
var value, obj;
if (!data) {
data = this.data;
}
for (var i = 0, l = data.length; i < l; i++) {
obj = data[i];
if (obj) {
value = obj[prop];
if (value !== undefined) {
if (hasSource) {
return {
source: obj,
value: value,
prop: prop,
_njSrc: true
};
}
return value;
}
}
}
}
function getAccessorData(fn, context) {
if (fn == null) {
return fn;
}
if (fn._njTmpl) {
//模板函数
return fn.call(context);
} else {
//普通函数
return fn.call(context.$this, context);
}
}
function getElement(name, global, nameO, context, subName) {
var element;
if (!context.icp) {
element = global.cp[name];
} else {
element = getData(nameO, context.icp);
if (!element) {
element = global.cp[name];
}
}
if (subName != null && element) {
element = element[subName];
}
return element ? element : nameO;
}
function getElementRefer(refer, name, global, nameO, context) {
return refer != null ? tools.isString(refer) ? getElement(refer.toLowerCase(), global, refer, context) : refer : getElement(name, global, nameO, context);
}
function getElementName(refer, name) {
return refer != null && refer !== '' ? refer : name;
}
function addArgs(props, dataRefer) {
var args = props.args;
if (args) {
for (var i = args.length; i--;) {
dataRefer.unshift(args[i]);
}
}
} //Rebuild local variables in the new context
function newContext(context, params) {
if (!params) {
return context;
}
return tools.assign({}, context, {
data: params.data ? tools.arrayPush(params.data, context.data) : context.data,
parent: params.newParent ? context : context.parent,
index: params.index != null ? params.index : context.index,
item: params.item != null ? params.item : context.item
});
} //修正属性名
function fixPropName(name) {
switch (name) {
case 'class':
name = 'className';
break;
case 'for':
name = 'htmlFor';
break;
}
return name;
} //合并字符串属性
function assignStrProps(target) {
var ret = '';
for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
params[_key - 1] = arguments[_key];
}
var retObj = tools.assign.apply(tools, [target].concat(params));
for (var k in retObj) {
var v = retObj[k];
ret += ' ' + k + (k !== v ? '="' + v + '"' : ' ');
}
return ret;
} //创建扩展标签子节点函数
function exRet(global, context, fn) {
return function (param) {
return fn(global, context, param);
};
}
function _getLocalComponents(localConfigs) {
var icp;
if (localConfigs && localConfigs.components) {
icp = localConfigs.components;
if (!tools.isArray(icp)) {
icp = [icp];
}
}
return icp;
} //构建可运行的模板函数
function tmplWrap(configs, main) {
return function (param1, param2) {
var ctx = this,
data = tools.arraySlice(arguments);
return main(configs, ctx && ctx._njCtx ? tools.assign({}, ctx, {
data: tools.arrayPush(data, ctx.data)
}) : {
data: data,
getData: getData,
get $this() {
return this.data[this.data.length - 1];
},
d: getData,
icp: _getLocalComponents(param1 && param1._njParam ? param2 : param1),
_njCtx: true
});
};
}
function levelSpace(context) {
if (context.level == null) {
return '';
}
var ret = '';
for (var i = 0; i < context.level; i++) {
ret += ' ';
}
return ret;
}
function firstNewline(context) {
return context.index == null ? '' : context.index == 0 ? '' : '\n';
}
function createElementApply(p) {
return _core["default"].createElement.apply(null, p);
}
function callFilter(filter) {
return filter.source ? filter.value.bind(filter.source) : filter;
} //创建模板函数
function template(fns, tmplKey) {
var configs = {
tmplKey: tmplKey,
us: fns.useString,
x: _core["default"].extensions,
f: _core["default"].filters,
np: tools.noop,
tf: tools.throwIf,
wn: tools.warn,
n: newContext,
c: getAccessorData,
sp: styleProps,
r: exRet,
e: getElement,
er: getElementRefer,
en: getElementName,
aa: addArgs,
an: tools.assign,
g: _core["default"].global,
cf: callFilter
};
if (!configs.us) {
configs.h = _core["default"].createElement;
configs.H = createElementApply;
configs.cp = _core["default"].components;
} else {
configs.ans = assignStrProps;
configs.es = _core["default"].escape;
configs.ls = levelSpace;
configs.fl = firstNewline;
}
tools.each(fns, function (v, k) {
if (k === 'main') {
//将每个主函数构建为可运行的模板函数
configs[k] = tmplWrap(configs, v);
tools.defineProps(configs[k], {
_njTmpl: {
value: true
}
});
configs['_' + k] = v;
} else if (k.indexOf('fn') === 0) {
//扩展标签函数
configs[k] = v;
}
}, false);
return configs;
}