2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-02-06 17:01:04 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
export default class Template {
|
|
|
|
constructor(vnInterpolate) {
|
|
|
|
this.vnInterpolate = vnInterpolate;
|
|
|
|
}
|
|
|
|
get(template, $attrs, defaults) {
|
|
|
|
let scope = Object.assign({}, defaults, $attrs);
|
|
|
|
return template && this.vnInterpolate(template)(scope);
|
|
|
|
}
|
|
|
|
getNormalized(template, $attrs, defaults) {
|
|
|
|
this.normalizeInputAttrs($attrs);
|
|
|
|
return this.get(template, $attrs, defaults);
|
|
|
|
}
|
|
|
|
normalizeInputAttrs($attrs) {
|
2019-09-09 07:05:13 +00:00
|
|
|
const field = $attrs.field || $attrs.model;
|
|
|
|
const split = field.split('.');
|
|
|
|
const len = split.length;
|
|
|
|
|
|
|
|
let i = len - 1;
|
|
|
|
const fieldName = split[i--];
|
|
|
|
const entity = i >= 0 ? split[i--] : 'model';
|
|
|
|
const ctrl = i >= 0 ? split[i--] : '$ctrl';
|
2018-02-10 15:18:01 +00:00
|
|
|
|
2019-09-09 07:05:13 +00:00
|
|
|
if ($attrs.field) {
|
2018-02-10 15:18:01 +00:00
|
|
|
if (len == 0)
|
|
|
|
throw new Error(`Attribute 'field' can not be empty`);
|
|
|
|
if (len > 3)
|
|
|
|
throw new Error(`Attribute 'field' must have this syntax: [ctrl].[entity].[field]`);
|
|
|
|
|
|
|
|
if ($attrs.model === undefined)
|
2019-09-09 07:05:13 +00:00
|
|
|
$attrs.model = `${ctrl}.${entity}.${fieldName}`;
|
2018-02-10 15:18:01 +00:00
|
|
|
if ($attrs.rule === undefined && len >= 2)
|
2019-09-09 07:05:13 +00:00
|
|
|
$attrs.rule = `${entity}.${fieldName}`;
|
2018-02-10 15:18:01 +00:00
|
|
|
if ($attrs.label === undefined && len >= 2)
|
2019-09-09 07:05:13 +00:00
|
|
|
$attrs.label = `${entity}.${fieldName}`;
|
2018-02-10 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:05:13 +00:00
|
|
|
if ($attrs.name === undefined)
|
|
|
|
$attrs.name = fieldName;
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
if ($attrs.focus !== undefined)
|
|
|
|
$attrs.focus = 'vn-focus';
|
|
|
|
}
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
Template.$inject = ['vnInterpolate'];
|
|
|
|
|
|
|
|
ngModule.service('vnTemplate', Template);
|