vsn 0.1.92 → 0.1.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vsn",
3
- "version": "0.1.92",
3
+ "version": "0.1.95",
4
4
  "description": "SEO Friendly Javascript/Typescript Framework",
5
5
  "keywords": [
6
6
  "framework",
@@ -226,6 +226,11 @@ export class ClassNode extends Node implements TreeNode {
226
226
  tag = await dom.getTagForElement(element, true);
227
227
  }
228
228
 
229
+ if (!tag) {
230
+ console.error('no tag found for element', element);
231
+ return;
232
+ }
233
+
229
234
  const classNode: ClassNode = Registry.instance.classes.getSynchronous(selector);
230
235
  if (classNode) {
231
236
  await classNode.constructTag(tag, dom);
package/src/DOM.ts CHANGED
@@ -189,16 +189,8 @@ export class DOM extends EventDispatcher {
189
189
  }
190
190
  }
191
191
 
192
- async buildFrom(ele: any, isRoot: boolean = false, forComponent: boolean = false) {
193
- if (isRoot) {
194
- document.body.setAttribute('vsn-root', '');
195
- document.ondragover = (e) => e.cancelable && e.preventDefault(); // Allow dragging over document
196
- }
197
-
198
- // Create tags for each html element with a v-attribute
199
- const newTags: Tag[] = [];
200
- const toBuild: HTMLElement[] = [];
201
-
192
+ async discover(ele: HTMLElement, forComponent: boolean = false): Promise<HTMLElement[]> {
193
+ const discovered: HTMLElement[] = [];
202
194
  const checkElement = (e: HTMLElement): boolean => {
203
195
  if (ElementHelper.hasVisionAttribute(e)) {
204
196
  if (
@@ -206,7 +198,7 @@ export class DOM extends EventDispatcher {
206
198
  ) return false;
207
199
  if (this.queued.indexOf(e) > -1) return false;
208
200
  this.queued.push(e);
209
- toBuild.push(e);
201
+ discovered.push(e);
210
202
  }
211
203
 
212
204
  return true;
@@ -221,38 +213,39 @@ export class DOM extends EventDispatcher {
221
213
  checkElement(ele);
222
214
  scanChildren(ele);
223
215
 
224
- for (const element of toBuild) {
225
- if (element[Tag.TaggedVariable]) continue;
226
- const tag: Tag = new Tag(element, this);
227
- this.tags.push(tag);
228
- newTags.push(tag);
229
- }
216
+ return discovered;
217
+ }
230
218
 
231
- if (isRoot)
232
- this._root = await this.getTagForElement(document.body);
219
+ async buildTag(element: HTMLElement, returnExisting: boolean = false): Promise<Tag> {
220
+ if (element[Tag.TaggedVariable]) return returnExisting ? element[Tag.TaggedVariable] : null;
221
+ const tag: Tag = new Tag(element, this);
222
+ this.tags.push(tag);
223
+ return tag;
224
+ }
233
225
 
234
- // Configure, setup & execute attributes
235
- for (const tag of newTags)
226
+ async setupTags(tags: Tag[]) {
227
+ // Configure, setup & execute attributes
228
+ for (const tag of tags)
236
229
  await tag.buildAttributes();
237
230
 
238
- for (const tag of newTags)
231
+ for (const tag of tags)
239
232
  await tag.compileAttributes();
240
233
 
241
- for (const tag of newTags)
234
+ for (const tag of tags)
242
235
  await tag.setupAttributes();
243
236
 
244
- for (const tag of newTags)
237
+ for (const tag of tags)
245
238
  await tag.extractAttributes();
246
239
 
247
- for (const tag of newTags)
240
+ for (const tag of tags)
248
241
  await tag.connectAttributes();
249
242
 
250
- for (const tag of newTags) {
243
+ for (const tag of tags) {
251
244
  await tag.finalize();
252
245
  this.queued.splice(this.queued.indexOf(tag.element), 1);
253
246
  }
254
247
 
255
- for (const tag of newTags) {
248
+ for (const tag of tags) {
256
249
  this.observer.observe(tag.element, {
257
250
  attributes: true,
258
251
  characterData: true,
@@ -260,6 +253,28 @@ export class DOM extends EventDispatcher {
260
253
  subtree: true
261
254
  });
262
255
  }
256
+ }
257
+
258
+ async buildFrom(ele: any, isRoot: boolean = false, forComponent: boolean = false) {
259
+ if (isRoot) {
260
+ document.body.setAttribute('vsn-root', '');
261
+ document.ondragover = (e) => e.cancelable && e.preventDefault(); // Allow dragging over document
262
+ }
263
+
264
+ // Create tags for each html element with a vsn-attribute
265
+ const newTags: Tag[] = [];
266
+ const toBuild: HTMLElement[] = await this.discover(ele, forComponent);
267
+
268
+ for (const element of toBuild) {
269
+ const tag = await this.buildTag(element);
270
+ if (tag)
271
+ newTags.push(tag);
272
+ }
273
+
274
+ if (isRoot)
275
+ this._root = await this.getTagForElement(document.body);
276
+
277
+ await this.setupTags(newTags);
263
278
 
264
279
  if (isRoot) {
265
280
  this._built = true;
@@ -82,7 +82,8 @@ export class List extends Attribute {
82
82
  }
83
83
  }
84
84
 
85
- this.tags = [];
85
+ this.tags = this.tags || [];
86
+ this.tags.length = 0;
86
87
 
87
88
  if (defaultList)
88
89
  for (const existingItem of defaultList) {
@@ -112,11 +113,11 @@ export class List extends Attribute {
112
113
  }
113
114
 
114
115
  public get listItemName(): string {
115
- return this.tag.getRawAttributeValue('vsn-list-item-name', 'item');
116
+ return this.tag.getRawAttributeValue('list-item-name', 'item');
116
117
  }
117
118
 
118
119
  public get listItemModel(): string {
119
- return this.tag.getRawAttributeValue('vsn-list-item-model');
120
+ return this.tag.getRawAttributeValue('list-item-model');
120
121
  }
121
122
 
122
123
  public remove(item: any) {
@@ -134,6 +135,7 @@ export class List extends Attribute {
134
135
  }
135
136
 
136
137
  protected async add(obj) {
138
+ // Setup new HTML element
137
139
  const clone = this.template.cloneNode(true);
138
140
  let element: HTMLElement;
139
141
  if (clone instanceof DocumentFragment) {
@@ -143,20 +145,29 @@ export class List extends Attribute {
143
145
  }
144
146
  delete element[Tag.TaggedVariable];
145
147
 
146
- this.tag.element.appendChild(element);
148
+ // Setup new tag
149
+ const tag = await this.tag.dom.buildTag(element, true);
150
+ tag.createScope(true);
147
151
 
148
- await this.tag.dom.buildFrom(this.tag.element);
149
- const tag: Tag = await this.tag.dom.getTagForElement(element);
150
- this.tags.push(tag);
152
+ // Setup new scope & class, if defined
153
+ const modelName: string = this.listItemModel;
154
+ let cls;
155
+ if (modelName)
156
+ cls = await Registry.instance.models.get(modelName);
151
157
 
152
- if (obj) {
153
- if (tag.scope.wrapped) {
154
- tag.scope.data.setData(obj);
155
- } else {
156
- tag.wrap(obj);
158
+ if (cls) {
159
+ if (!obj || !(obj instanceof cls)) {
160
+ obj = new cls(obj);
157
161
  }
158
162
  }
159
163
 
164
+ tag.scope.set(this.listItemName, tag.scope);
165
+ tag.wrap(obj);
166
+
167
+ // Add to DOM & build
168
+ this.tag.element.appendChild(element);
169
+ await this.tag.dom.buildFrom(this.tag.element);
170
+ this.tags.push(tag);
160
171
  this.tag.dispatch('add', obj);
161
172
  }
162
173
  }
@@ -18,24 +18,13 @@ export class ListItem extends Attribute {
18
18
  this._list = this.tag.findAncestorByAttribute('vsn-list');
19
19
  if (!this._list)
20
20
  throw Error(ListItem.ERROR_NO_PARENT);
21
- const modelName: string = (await this.getList()).listItemModel;
22
- if (modelName) {
23
- const cls = await Registry.instance.models.get(modelName);
24
- this.instantiateModel(cls);
25
- }
26
- this.tag.scope.set(this.listItemName, this.tag.scope);
27
- await super.setup();
28
- }
29
21
 
30
- public get listItemName(): string {
31
- return this.getAttributeBinding('item');
32
- }
22
+ this.tag.scope.set((await this.getListAttribute()).listItemName, this.tag.scope);
33
23
 
34
- public async getList(): Promise<List> {
35
- return await this._list.getAttribute<List>('vsn-list');
24
+ await super.setup();
36
25
  }
37
26
 
38
- private instantiateModel(model: any) {
39
- this.tag.wrap(model, false, true);
27
+ public async getListAttribute(): Promise<List> {
28
+ return await this._list.getAttribute<List>('vsn-list');
40
29
  }
41
30
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.1.92';
1
+ export const VERSION = '0.1.95';
2
2
 
@@ -41,7 +41,7 @@ describe('ListItem', () => {
41
41
 
42
42
  it("vsn-list-item should find it's parent list", (done) => {
43
43
  document.body.innerHTML = `
44
- <ul vsn-list:list vsn-list-item-model="ListItemSpecTestItem" id="test"><li vsn-list-item:item id="test-item"></li></ul>
44
+ <ul vsn-list:list list-item-model="ListItemSpecTestItem" id="test"><li vsn-list-item id="test-item"></li></ul>
45
45
  `;
46
46
 
47
47
  const dom = new DOM(document);
@@ -58,7 +58,7 @@ describe('ListItem', () => {
58
58
  it("should properly wrap list item class", (done) => {
59
59
  document.body.innerHTML = `
60
60
  <div vsn-controller:controller="ListItemController">
61
- <ul vsn-list:controller.items vsn-list-item-model="ListItemSpecTestItem" id="test">
61
+ <ul vsn-list:controller.items list-item-model="ListItemSpecTestItem" id="test">
62
62
  <li vsn-template vsn-list-item:item></li>
63
63
  </ul>
64
64
  </div>
@@ -89,7 +89,7 @@ describe('ListItem', () => {
89
89
 
90
90
  it("vsn-list-item should work with vsn-set", (done) => {
91
91
  document.body.innerHTML = `
92
- <ul vsn-list:list vsn-list-item-model="ListItemSpecTestItem" id="test"><li vsn-list-item:item id="test-item" vsn-set:item.testing|integer="1"></li></ul>
92
+ <ul vsn-list:list list-item-model="ListItemSpecTestItem" id="test"><li vsn-list-item:item id="test-item" vsn-set:item.testing|integer="1"></li></ul>
93
93
  `;
94
94
 
95
95
  const dom = new DOM(document);
@@ -105,14 +105,15 @@ describe('ListItem', () => {
105
105
 
106
106
  it("vsn-list-item should work with vsn-exec", (done) => {
107
107
  document.body.innerHTML = `
108
- <ul vsn-list:list vsn-list-item-model="ListItemSpecTestItem" id="test"><li vsn-list-item:item id="test-item" vsn-exec="item.test = 1"></li></ul>
108
+ <ul vsn-list:list list-item-model="ListItemSpecTestItem" id="test">
109
+ <li vsn-list-item id="test-item" vsn-exec="item.test = 1"></li>
110
+ </ul>
109
111
  `;
110
112
 
113
+ console.log('################# building dom');
111
114
  const dom = new DOM(document);
112
115
  dom.once('built', async () => {
113
- const list = await dom.getTagForElement(document.getElementById('test'));
114
116
  const listItem = await dom.getTagForElement(document.getElementById('test-item'));
115
- const listItemAttr: ListItem = await listItem.getAttribute('vsn-list-item') as ListItem;
116
117
  console.log('test keys', listItem.scope.keys);
117
118
  expect(listItem.scope.get('test')).toBe(1);
118
119
  done();