UNPKG

5.77 kBJavaScriptView Raw
1const K = window.K;
2
3// -----------------------------------------------------------------------------
4// Prototypes
5// -----------------------------------------------------------------------------
6
7K.NateAbstractDomProto = Object.assign({}, K.BSC.NateAbstractProto,
8{
9 init(parentOrElem, namespace)
10 {
11 K.BSC.NateAbstractProto.init.call(this, parentOrElem, namespace);
12
13 this.nateParent = null;
14 this.elem = null;
15 this.isDeleted = false;
16
17 // If there is parentOrElem, check the type of it.
18 if (K.Object.isNotNull(parentOrElem))
19 {
20 if (parentOrElem.addNateChild)
21 {
22 // Possible improvement: Better way to check is it nate object?
23 parentOrElem.addNateChild(this)
24 }
25 else if (K.Elem.isElem(parentOrElem) || K.Elem.isText(parentOrElem))
26 {
27 if (K.Object.isNotNull(parentOrElem.nate))
28 {
29 throw 'Cannot create 2nd Nate object for same elem! The element you provided as elem for Nate is already a elem for different nate object';
30 }
31 else
32 {
33 // First, attach this Nate object to element.
34 this.attachNateToElem(parentOrElem);
35
36 // Then, check if DOM parent is Nate object - if yes, tell it about me.
37 if (K.Object.isNotNull(parentOrElem.parentNode) &&
38 K.Object.isNotNull(parentOrElem.parentNode.nate))
39 {
40 parentOrElem.parentNode.nate.addNateChild(this);
41 }
42 }
43 }
44 else
45 {
46 throw new K.Error({
47 errorCode: 'invalidNateParent',
48 ext: {
49 parent: parentOrElem,
50 hint: 'parentOrElem should be Nate object or nate-element'
51 }
52 })
53 }
54 }
55 else
56 {
57 throw new K.Error({
58 errorCode: 'invalidNateParent',
59 ext: {
60 parent: parentOrElem,
61 hint: 'parentOrElem should be Nate object or nate-element - cannot be null'
62 }
63 })
64 }
65 },
66
67 _confirmIsNotDeleted()
68 {
69 if (this.isDeleted)
70 {
71 console.log('PANIC! Nate item', this.elem, 'already deleted');
72
73 throw new K.Error({
74 errorCode: 'nateItemAlreadyDeleted',
75 ext: {elem: this.elem}});
76 }
77 },
78
79 deleteAllChildren()
80 {
81 this._confirmIsNotDeleted();
82 // Possible improvement: optimize this
83 while (this.children.length > 0)
84 {
85 this.children[0].delete();
86 }
87 this.isNateChildAllowed = true;
88 },
89
90 delete()
91 {
92 this._confirmIsNotDeleted();
93
94 K.BSC.NateAbstractProto.delete.call(this);
95
96 // Remove nate elem from tree.
97 if (K.Object.isNotNull(this.nateParent))
98 {
99 this.isDeleted = true;
100 this.nateParent.removeNateChild(this);
101 }
102 else
103 {
104 try
105 {
106 this.elem.parentNode.removeChild(this.elem);
107 }
108 catch(err)
109 {
110 console.log('Error during removeChild parent is:', this.elem.parentNode, 'child is:', this.elem);
111 }
112 }
113 },
114
115 addNateChild(nateChild)
116 {
117 this._confirmIsNateChildAllowed();
118
119 if (K.Object.isNotNull(nateChild.nateParent))
120 {
121 throw 'Moving nate-child between parents not yet supported.';
122 }
123
124 nateChild.nateParent = this;
125
126 this.children.push(nateChild);
127
128 if (K.Object.isNotNull(this.elem) &&
129 K.Object.isNotNull(nateChild.elem) &&
130 (nateChild.elem.parentNode != this.elem))
131 {
132 this.elem.appendChild(nateChild.elem);
133 }
134 },
135
136 removeNateChild(nateChild)
137 {
138 if (nateChild.nateParent != this)
139 {
140 throw 'Can\'t remove nateChild from me, as the child claims his parent is another object.';
141 }
142
143 K.Array.removeAll(this.children, nateChild);
144
145 if (K.Object.isNotNull(this.elem) &&
146 K.Object.isNotNull(nateChild.elem))
147 {
148 try
149 {
150 this.elem.removeChild(nateChild.elem);
151 }
152 catch(err)
153 {
154 console.log('Error during removeChild parent is:', this.elem, 'child is:', nateChild.elem);
155 }
156 }
157
158 nateChild.nateParent = null;
159 },
160
161 attachNateToElem(elem)
162 {
163 this._confirmIsNotDeleted();
164
165 // Avoid registering >1 Nate objects to one elem.
166 if (K.Object.isNotNull(elem.nate))
167 {
168 throw 'Provided elem already has Nate object';
169 }
170
171 // Remove from current element is not supported.
172 if (K.Object.isNotNull(this.elem))
173 {
174 throw 'This nate object is already attached to element.';
175 }
176
177 // Attach this object to elem.
178 this.elem = elem;
179 this.elem.nate = this;
180
181 for (let child in this.children)
182 {
183 throw 'This needs to be updated...';
184
185 this.elem.appendChild(child);
186 }
187 },
188
189 attachElemToNate(elem)
190 {
191 // First, attach this Nate object to element.
192 this.attachNateToElem(elem)
193
194 if (K.Object.isNotNull(newNate.nateParent.elem) &&
195 K.Object.isNotNull(newNate.elem) &&
196 (newNate.elem.parentNode != newNate.nateParent))
197 {
198 newNate.nateParent.elem.appendChild(newNate.elem);
199 }
200 },
201
202 getElem()
203 {
204 this._confirmIsNotDeleted();
205
206 return this.elem;
207 },
208
209 commonInnerContentSetter(nNode, key, value) {
210 nNode._confirmThereAreNoNateChildren()
211
212 if (K.Object.isUndefinedOrNull(value) || (value === '')) {
213 // Clear inner content, enable newXxx() calls to create nate-childs.
214 nNode.elem[key] = ''
215 nNode.isNateChildAllowed = true
216
217 } else {
218 // Set new inner content - disable newXxx() calls to forbid nate-childs.
219 nNode.elem[key] = value
220 nNode.isNateChildAllowed = false
221 }
222 }
223})