UNPKG

3.51 kBJavaScriptView Raw
1let K = null
2
3// is it server?
4if (typeof exports !== 'undefined' && exports !== null)
5{
6 K = require('kcore')
7 require('./KBSCNateNamespace')
8}
9else
10{
11 K = window.K
12}
13
14// -----------------------------------------------------------------------------
15// Prototypes
16// -----------------------------------------------------------------------------
17
18K.BSC.NateAbstractProto =
19{
20 USE_DEFAULT_NAMESPACE: Symbol('use_default_namespace'),
21
22 init(parentOrElem, namespace)
23 {
24 this.isDeleted = false;
25 this.children = [];
26 this.isNateChildAllowed = true;
27
28 if (namespace == K.BSC.NateAbstractProto.USE_DEFAULT_NAMESPACE) {
29 // Special case - ignore parent's namespace and use default one.
30 this.namespace = this._getDefaultNamespace()
31 } else if (namespace) {
32 // Namespace is given explicitly - just use it.
33 this.namespace = namespace
34 } else if (parentOrElem && parentOrElem.getNamespace) {
35 // Get namespace from parent if existent.
36 this.namespace = parentOrElem.getNamespace()
37 } else {
38 // Get default namespace otherwise.
39 this.namespace = this._getDefaultNamespace()
40 }
41 },
42
43 _getDefaultNamespace() {
44 // Overload if needed.
45 return K.BSC.nateNullNamespace
46 },
47
48 getNamespace()
49 {
50 return this.namespace
51 },
52
53 assignNamespace(namespace)
54 {
55 this.namespace = namespace
56 },
57
58 _validateDataKey(key)
59 {
60 const IS_OK_AS_DATAKEY_REGEXP = new RegExp('^([a-zA-Z0-9\.\:_-]+)$')
61
62 if ((typeof(key) !== 'string') || !IS_OK_AS_DATAKEY_REGEXP.test(key))
63 {
64 throw new K.Error({errorCode: 'invalidKey', ext: {key: key}})
65 }
66 },
67
68 _confirmIsNotDeleted()
69 {
70 // Overload if needed.
71 },
72
73 _confirmThereAreNoNateChildren()
74 {
75 if (this.children.length > 0) {
76 throw new K.Error({
77 errorCode: 'nateChildrenAlreadyExists',
78 ext: {
79 namespace: this.getNamespace().name,
80 nateObj: this
81 }
82 });
83 }
84 },
85
86 _confirmIsNateChildAllowed()
87 {
88 if (!this.isNateChildAllowed) {
89 throw new K.Error({
90 errorCode: 'nateChildNotAllowed',
91 ext: {
92 namespace: this.getNamespace().name,
93 nateObj: this
94 }
95 });
96 }
97 },
98
99 delete()
100 {
101 // Call custom delete function if any.
102 const deleteFunction = this.getNamespace().getDeleteFunction(this.typeName);
103
104 if (deleteFunction) {
105 deleteFunction.call(this);
106 }
107 },
108
109 set(key, value)
110 {
111 this._confirmIsNotDeleted()
112
113 const fct = this.namespace.getSetter(key)
114
115 if (fct) {
116 fct(this, key, value)
117 } else {
118 throw new K.Error({errorCode: 'unknownNateParam', ext: {param: key}})
119 }
120
121 return this;
122 },
123
124 setFromMap(map) {
125 for (let key in map) {
126 this.set(key, map[key])
127 }
128 return this
129 },
130
131 newNate(nateType, params) {
132 let rv = null;
133
134 const createFunction = this.getNamespace().getCreateFunction(nateType);
135
136 if (createFunction) {
137 // Create function found - go on.
138 rv = createFunction.call(this, nateType, params);
139
140 } else {
141 // Error - there is no create function in related namespace.
142 throw new K.Error({
143 errorCode: 'unknownNateType',
144 ext: {
145 nateType: nateType,
146 namespace: this.getNamespace().name
147 }
148 });
149 }
150
151 return rv;
152 }
153}