salix/front/core/lib/template.js

47 lines
1.6 KiB
JavaScript

import ngModule from '../module';
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) {
if ($attrs.field) {
let split = $attrs.field.split('.');
let len = split.length;
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]`);
let i = len - 1;
let field = split[i--];
let entity = i >= 0 ? split[i--] : 'model';
let ctrl = i >= 0 ? split[i--] : '$ctrl';
if ($attrs.model === undefined)
$attrs.model = `${ctrl}.${entity}.${field}`;
if ($attrs.rule === undefined && len >= 2)
$attrs.rule = `${entity}.${field}`;
if ($attrs.label === undefined && len >= 2)
$attrs.label = `${entity}.${field}`;
if ($attrs.name === undefined)
$attrs.name = field;
}
if ($attrs.focus !== undefined)
$attrs.focus = 'vn-focus';
}
}
Template.$inject = ['vnInterpolate'];
ngModule.service('vnTemplate', Template);