1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Base');
|
7 |
|
8 | module.exports = class Widget extends Base {
|
9 |
|
10 | constructor (config) {
|
11 | super({
|
12 |
|
13 |
|
14 |
|
15 | cacheComponentId: 'cache',
|
16 | ...config
|
17 | });
|
18 | this.module = this.view.module;
|
19 | this.controller = this.view.controller;
|
20 | if (this.caching && !this.cache) {
|
21 | this.cache = this.module.get(this.cacheComponentId);
|
22 | }
|
23 | }
|
24 |
|
25 | getCacheKey () {
|
26 | return `widget-${this.module.NAME}-${this.id}`;
|
27 | }
|
28 |
|
29 | async run () {
|
30 | return 'Place widget content here';
|
31 | }
|
32 |
|
33 | async execute (renderParams) {
|
34 | this.content = '';
|
35 | this.renderParams = renderParams;
|
36 | if (this.disabled) {
|
37 | this.log('trace', `Widget disabled: ${this.id}`);
|
38 | return this.content;
|
39 | }
|
40 | this.log('trace', `Execute widget: ${this.id}`);
|
41 | this.content = this.cache
|
42 | ? await this.cache.use(this.getCacheKey(), this.run.bind(this), this.cacheDuration)
|
43 | : await this.run();
|
44 | return this.content;
|
45 | }
|
46 |
|
47 | renderTemplate (template, params) {
|
48 | return this.view.renderTemplate(this.view.get(template), {
|
49 | _widget: this,
|
50 | ...this.renderParams,
|
51 | ...params
|
52 | });
|
53 | }
|
54 |
|
55 | log () {
|
56 | CommonHelper.log(this.view, this.constructor.name, ...arguments);
|
57 | }
|
58 | };
|
59 |
|
60 | const CommonHelper = require('../helper/CommonHelper'); |
\ | No newline at end of file |