UNPKG

2.12 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Base');
7
8module.exports = class ViewModel extends Base {
9
10 static getExtendedClassProperties () {
11 return [
12 'ATTR_HINTS',
13 'ATTR_LABELS',
14 'ATTR_VALUE_LABELS'
15 ];
16 }
17
18 static getConstants () {
19 return {
20 ATTR_HINTS: {},
21 ATTR_LABELS: {},
22 ATTR_VALUE_LABELS: {}
23 }
24 }
25
26 static getAttrValueLabels (name) {
27 return this.ATTR_VALUE_LABELS[name];
28 }
29
30 static getAttrValueLabel (name, value) {
31 return this.ATTR_VALUE_LABELS[name] && this.ATTR_VALUE_LABELS[name][value];
32 }
33
34 constructor (config) {
35 super(config);
36 this.module = this.view.module;
37 this.controller = this.view.controller;
38 this.data = this.data || {};
39 this.data._viewModel = this;
40 }
41
42 async getTemplateData () {
43 return Object.assign(this.data, await this.resolveTemplateData());
44 }
45
46 resolveTemplateData () {
47 return {}; // to override
48 }
49
50 prepareModels () {
51 // to override
52 }
53
54 getAttrLabel (name) {
55 return Object.prototype.hasOwnProperty.call(this.ATTR_LABELS, name)
56 ? this.ATTR_LABELS[name]
57 : this.generateAttrLabel(name);
58 }
59
60 getAttrHint (name) {
61 return ObjectHelper.getValue(name, this.ATTR_HINTS, '');
62 }
63
64 generateAttrLabel (name) {
65 this.ATTR_LABELS[name] = StringHelper.camelToWords(StringHelper.camelize(name));
66 return this.ATTR_LABELS[name];
67 }
68
69 format () {
70 return this.controller.format(...arguments);
71 }
72
73 translate () {
74 return this.controller.translate(...arguments);
75 }
76
77 translateMessageMap () {
78 return this.controller.translateMessageMap(...arguments);
79 }
80};
81module.exports.init();
82
83const ObjectHelper = require('../helper/ObjectHelper');
84const StringHelper = require('../helper/StringHelper');
\No newline at end of file