UNPKG

2.53 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./Base');
7
8module.exports = class Component extends Base {
9
10 static getExtendedClassProperties () {
11 return ['BEHAVIORS'];
12 }
13
14 static getConstants () {
15 return {
16 // BEHAVIORS: { // auto-attached behaviors
17 // behavior1: require('./UserBehavior1'),
18 // behavior2: {Class: require('./UserBehavior2'), property1: ..., property2: ...}
19 // }
20 };
21 }
22
23 constructor (config) {
24 super({
25 // depends: ['#start', '#end', 'componentId'] // order of component init
26 // parent: [component from parent module]
27 ...config
28 });
29 this.events = ClassHelper.spawn(this.eventManager || EventManager, {
30 owner: this
31 });
32 this.behaviors = ClassHelper.spawn(this.behaviorManager || BehaviorManager, {
33 owner: this,
34 autoAttachedMap: this.BEHAVIORS
35 });
36 }
37
38 once () {
39 this.ensureBehaviors();
40 this.events.once(...arguments);
41 }
42
43 on () {
44 this.ensureBehaviors();
45 this.events.on(...arguments);
46 }
47
48 off () {
49 this.ensureBehaviors();
50 this.events.off(...arguments);
51 }
52
53 trigger () {
54 this.ensureBehaviors();
55 return this.events.trigger(...arguments);
56 }
57
58 getBehavior (name) {
59 return this.behaviors.get(name);
60 }
61
62 attachBehavior () {
63 return this.behaviors.attach(...arguments);
64 }
65
66 attachBehaviorOnce () {
67 return this.behaviors.attachOnce(...arguments);
68 }
69
70 detachBehavior (name) {
71 return this.behaviors.detach(name);
72 }
73
74 getAllBehaviors () {
75 return this.behaviors.getAll();
76 }
77
78 attachAllBehaviors (data) {
79 this.behaviors.attachAll(data);
80 }
81
82 detachAllBehaviors () {
83 this.behaviors.detachAll();
84 }
85
86 ensureBehaviors () {
87 this.behaviors.ensure();
88 }
89
90 translate () {
91 return this.module.translate(...arguments);
92 }
93
94 log () {
95 CommonHelper.log(this.module, this.constructor.name, ...arguments);
96 }
97};
98
99const ClassHelper = require('../helper/ClassHelper');
100const CommonHelper = require('../helper/CommonHelper');
101const EventManager = require('./EventManager');
102const BehaviorManager = require('./BehaviorManager');
\No newline at end of file