structured-fw 1.0.5 → 1.0.7

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/README.md CHANGED
@@ -644,7 +644,7 @@ Methods:
644
644
  - `find(componentName: string, recursive: boolean = true): ClientComponent | null` - find a child component
645
645
  - `findParent(componentName: string): ClientComponent | null` - find the first parent with given name
646
646
  - `query(componentName: string, recursive: boolean = true): Array<ClientComponent>` - return all components with given name found within this component, if `recursive = false`, only direct children are considered
647
- - `bind<T extends LooseObject | undefined = undefined>(element: HTMLElement, eventName: string | Array<string>, callback: (e: Event, data: T) => void): void` - adds event listener(s) to given element. This is preferred over addEventListener because when the component is redrawn/removed, the event listeners added using bind method are automatically restored/removed. Callback receives event as the first argument. Any "data-" prefixed attributes found on `element` are parsed into an object and provided as second argument to callback (you can specify data using attr helper if you want to pass in something other than a string). The method is generic, allowing you to specify expected data type
647
+ - `bind<T extends LooseObject | undefined = undefined>(element: HTMLElement | Window | Array<HTMLElement | Window>, eventName: string | Array<string>, callback: (e: Event, data: T) => void): void` - adds event listener(s) to given element(s). This is preferred over addEventListener because when the component is redrawn/removed, the event listeners added using bind method are automatically restored/removed. Callback receives event as the first argument. Any "data-" prefixed attributes found on `element` are parsed into an object and provided as second argument to callback (you can specify data using attr helper if you want to pass in something other than a string). Third argument provided to callback is the `element`. The method is generic, allowing you to specify expected data type received as the second argument.
648
648
  - `ref<T>(refName: string): T` - get a HTMLElement or ClientComponent that has attribute `ref="[refName]"`
649
649
  - `arrayRef<T>(refName: string): Array<T>` - get an array of HTMLElement or ClientComponent that have attribute `array:ref="[refName]"`
650
650
  - `add(appendTo: HTMLElement, componentName: string, data?: LooseObject): Promise<ClientComponent | null>` - add `componentName` component to `appendTo` element, optionally passing `data` to the component when it's being rendered. Returns a promise that resolves with added ClientComponent or null if something went wrong
@@ -159,9 +159,9 @@ export type InitializerFunctionContext = {
159
159
  isRedraw: boolean;
160
160
  };
161
161
  export type StoreChangeCallback = (key: string, value: any, oldValue: any, componentId: string) => void;
162
- export type ClientComponentEventCallback<T> = T extends undefined ? (e: Event) => void : (e: Event, data: T) => void;
162
+ export type ClientComponentEventCallback<T> = (e: Event, data: T, element: HTMLElement | Window) => void;
163
163
  export type ClientComponentBoundEvent<T extends LooseObject | undefined = undefined> = {
164
- element: HTMLElement;
164
+ element: HTMLElement | Window;
165
165
  event: keyof HTMLElementEventMap;
166
166
  callback: (e: Event) => void;
167
167
  callbackOriginal: ClientComponentEventCallback<T>;
@@ -56,7 +56,7 @@ export declare class ClientComponent extends EventEmitter {
56
56
  private transitionAttributes;
57
57
  private transitionAxis;
58
58
  private destroy;
59
- bind<T extends LooseObject | undefined = undefined>(element: HTMLElement, event: keyof HTMLElementEventMap | Array<keyof HTMLElementEventMap>, callback: ClientComponentEventCallback<T>): void;
59
+ bind<T extends LooseObject | undefined = undefined>(element: HTMLElement | Window | Array<HTMLElement | Window>, event: keyof HTMLElementEventMap | Array<keyof HTMLElementEventMap>, callback: ClientComponentEventCallback<T>): void;
60
60
  unbind<T extends LooseObject | undefined = undefined>(element: HTMLElement, event: keyof HTMLElementEventMap | Array<keyof HTMLElementEventMap>, callback: ClientComponentEventCallback<T>): void;
61
61
  private unbindAll;
62
62
  }
@@ -721,15 +721,22 @@ export class ClientComponent extends EventEmitter {
721
721
  this.emit('afterDestroy');
722
722
  }
723
723
  bind(element, event, callback) {
724
+ if (Array.isArray(element)) {
725
+ element.forEach((el) => {
726
+ this.bind(el, event, callback);
727
+ });
728
+ return;
729
+ }
724
730
  if (Array.isArray(event)) {
725
731
  event.forEach((eventName) => {
726
732
  this.bind(element, eventName, callback);
727
733
  });
728
734
  return;
729
735
  }
730
- if (element instanceof HTMLElement) {
736
+ const isWindow = element instanceof Window;
737
+ if (element instanceof HTMLElement || isWindow) {
731
738
  const callbackWrapper = (e) => {
732
- callback.apply(this, [e, this.attributeData(element)]);
739
+ callback.apply(this, [e, isWindow ? undefined : this.attributeData(element), element]);
733
740
  };
734
741
  this.bound.push({
735
742
  element,
@@ -89,8 +89,14 @@ export class Component extends EventEmitter {
89
89
  return;
90
90
  }
91
91
  const importedParentData = this.parent ? this.importedParentData(this.parent.data) : {};
92
- const dataServerSidePart = (this.entry && this.entry.module ?
93
- await this.entry.module.getData(Object.assign(importedParentData, this.attributes, data || {}), this.document.ctx, this.document.application, this) : {}) || {};
92
+ let dataServerSidePart = {};
93
+ try {
94
+ dataServerSidePart = (this.entry && this.entry.module ?
95
+ await this.entry.module.getData(Object.assign(importedParentData, this.attributes, data || {}), this.document.ctx, this.document.application, this) : {}) || {};
96
+ }
97
+ catch (e) {
98
+ throw new Error(`Error executing getData in component ${this.name}: ${e.message}`);
99
+ }
94
100
  if (data === undefined) {
95
101
  if (this.entry && this.entry.module) {
96
102
  this.data = Object.assign(this.data, dataServerSidePart);
@@ -250,6 +256,11 @@ export class Component extends EventEmitter {
250
256
  return;
251
257
  }
252
258
  const html = this.entry ? this.entry.html : this.dom.innerHTML;
253
- this.dom.innerHTML = this.document.application.handlebars.compile(html, data);
259
+ try {
260
+ this.dom.innerHTML = this.document.application.handlebars.compile(html, data);
261
+ }
262
+ catch (e) {
263
+ throw new Error(`Error compiling Handlebars template in component ${this.name}, error: ${e.message}`);
264
+ }
254
265
  }
255
266
  }
@@ -25,6 +25,10 @@ export class Layout {
25
25
  }
26
26
  const component = new Component(componentName, layoutComponent[0], doc, false);
27
27
  await component.init(`<${componentName}></${componentName}>`, data);
28
+ const conditionals = component.dom.queryByHasAttribute('data-if');
29
+ for (let i = 0; i < conditionals.length; i++) {
30
+ conditionals[i].style.display = 'none';
31
+ }
28
32
  return doc;
29
33
  }
30
34
  }
package/package.json CHANGED
@@ -19,14 +19,13 @@
19
19
  "license": "MIT",
20
20
  "type": "module",
21
21
  "main": "build/index",
22
- "version": "1.0.5",
22
+ "version": "1.0.7",
23
23
  "scripts": {
24
24
  "develop": "tsc --watch",
25
25
  "startDev": "cd build && nodemon --watch '../app/**/*' --watch '../build/**/*' -e js,html,css index.js",
26
26
  "start": "cd build && node index.js",
27
- "prepublish": "tsc",
28
- "pack": "tsc && npm pack",
29
- "publish": "tsc && npm publish"
27
+ "compileAndPack": "tsc && npm pack",
28
+ "compileAndPublish": "tsc && npm publish"
30
29
  },
31
30
  "bin": {
32
31
  "structured": "./build/system/bin/structured.js"
@@ -57,6 +56,7 @@
57
56
  "./Component": "./build/system/server/Component.js",
58
57
  "./Layout": "./build/system/server/Layout.js",
59
58
  "./FormValidation": "./build/system/server/FormValidation.js",
60
- "./ClientComponent": "./build/system/client/ClientComponent.js"
59
+ "./ClientComponent": "./build/system/client/ClientComponent.js",
60
+ "./Net": "./build/system/client/Net.js"
61
61
  }
62
62
  }