salix/front/core/lib/template.js

41 lines
1.3 KiB
JavaScript

import ngModule from '../module';
export default class Template {
getNormalized(template, $attrs, defaults) {
this.normalizeInputAttrs($attrs);
return this.get(template, $attrs, defaults);
}
normalizeInputAttrs($attrs) {
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';
if ($attrs.field) {
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)
$attrs.model = `${ctrl}.${entity}.${fieldName}`;
if ($attrs.rule === undefined && len >= 2)
$attrs.rule = `${entity}.${fieldName}`;
if ($attrs.label === undefined && len >= 2)
$attrs.label = `${entity}.${fieldName}`;
}
if ($attrs.name === undefined)
$attrs.name = fieldName;
if ($attrs.focus !== undefined)
$attrs.focus = 'vn-focus';
}
}
ngModule.service('vnTemplate', Template);