saasufy-components 2.3.0 → 2.4.1

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.
@@ -223,8 +223,8 @@ class CollectionBrowser extends SocketConsumer {
223
223
  );
224
224
  let viewParams = Object.fromEntries(
225
225
  collectionViewParams.split(',')
226
- .map(pair => pair.split('='))
227
- .filter(([field]) => field)
226
+ .map(pair => pair.split('='))
227
+ .filter(([field]) => field)
228
228
  );
229
229
  if (this.collection) this.collection.destroy();
230
230
  this.isStale = true;
@@ -282,6 +282,8 @@ class CollectionBrowser extends SocketConsumer {
282
282
 
283
283
  (async () => {
284
284
  for await (let event of this.collection.listener('change')) {
285
+ // Ignore change events which originate from this collection instance.
286
+ if (!event.isRemote) continue;
285
287
  this.renderList();
286
288
  this.updatePageButtons();
287
289
  }
package/model-input.js CHANGED
@@ -23,6 +23,7 @@ class ModelInput extends SocketConsumer {
23
23
  'input-id',
24
24
  'list',
25
25
  'model-instance-property',
26
+ 'bind-to-collection',
26
27
  'type',
27
28
  'placeholder',
28
29
  'consumers',
@@ -65,6 +66,7 @@ class ModelInput extends SocketConsumer {
65
66
  render() {
66
67
  this.innerHTML = '';
67
68
  let modelInstanceProperty = this.getAttribute('model-instance-property');
69
+ let bindToCollection = this.hasAttribute('bind-to-collection');
68
70
  let showErrorMessage = this.hasAttribute('show-error-message');
69
71
  let inputId = this.getAttribute('input-id');
70
72
  let inputList = this.getAttribute('list');
@@ -80,41 +82,64 @@ class ModelInput extends SocketConsumer {
80
82
  let currentNode = this.parentNode;
81
83
  let model;
82
84
  let isModelLocal = false;
83
- if (modelInstanceProperty) {
85
+ if (bindToCollection) {
86
+ let collection;
84
87
  while (currentNode) {
85
- model = currentNode[modelInstanceProperty];
86
- if (model && modelType && (model.type !== modelType || !(model.fields || []).includes(modelField))) {
87
- model = null;
88
+ collection = currentNode.collection;
89
+ if (collection && (collection.type !== modelType || !(collection.fields || []).includes(modelField))) {
90
+ collection = null;
88
91
  }
89
- if (model) break;
92
+ if (collection && (!collection.agModels || !collection.agModels[modelId] || !collection.agModels[modelId].agFields[modelField])) {
93
+ collection = null;
94
+ }
95
+ if (collection) break;
90
96
  currentNode = currentNode.getRootNode().host || currentNode.parentNode;
91
97
  }
92
- if (!model) {
93
- throw new Error(
94
- `The ${
95
- this.nodeName.toLowerCase()
96
- } element failed to obtain a model via the specified model-instance-property - Ensure that the element is nested inside a parent element which exposes a model instance of the same type which has the relevant field`
97
- );
98
- };
99
- } else {
100
- this.socket = this.getSocket();
101
- model = new AGModel({
102
- socket: this.socket,
103
- type: modelType,
104
- id: modelId,
105
- fields: [ modelField ],
106
- enableRebound
107
- });
108
- if (!hideErrorLogs) {
109
- (async () => {
110
- for await (let { error } of model.listener('error')) {
111
- console.error(
112
- `Model input encountered an error: ${error.message}`
113
- );
98
+ if (collection) {
99
+ model = collection.agModels[modelId];
100
+ }
101
+ }
102
+ if (!model) {
103
+ currentNode = this.parentNode;
104
+ if (modelInstanceProperty) {
105
+ while (currentNode) {
106
+ model = currentNode[modelInstanceProperty];
107
+ if (model && modelType && (model.type !== modelType || !(model.fields || []).includes(modelField))) {
108
+ model = null;
114
109
  }
115
- })();
110
+ if (model && !model.agFields) {
111
+ model = null;
112
+ }
113
+ if (model) break;
114
+ currentNode = currentNode.getRootNode().host || currentNode.parentNode;
115
+ }
116
+ if (!model) {
117
+ throw new Error(
118
+ `The ${
119
+ this.nodeName.toLowerCase()
120
+ } element failed to obtain a model via the specified model-instance-property - Ensure that the element is nested inside a parent element which exposes a model instance of the same type which has the relevant field`
121
+ );
122
+ };
123
+ } else {
124
+ this.socket = this.getSocket();
125
+ model = new AGModel({
126
+ socket: this.socket,
127
+ type: modelType,
128
+ id: modelId,
129
+ fields: [ modelField ],
130
+ enableRebound
131
+ });
132
+ if (!hideErrorLogs) {
133
+ (async () => {
134
+ for await (let { error } of model.listener('error')) {
135
+ console.error(
136
+ `Model input encountered an error: ${error.message}`
137
+ );
138
+ }
139
+ })();
140
+ }
141
+ isModelLocal = true;
116
142
  }
117
- isModelLocal = true;
118
143
  }
119
144
  if (model.isLoaded) {
120
145
  this.setAttribute('is-loaded', '');
package/model-text.js CHANGED
@@ -21,6 +21,7 @@ class ModelText extends SocketConsumer {
21
21
  static get observedAttributes() {
22
22
  return [
23
23
  'model-instance-property',
24
+ 'bind-to-collection',
24
25
  'model-type',
25
26
  'model-id',
26
27
  'model-field',
@@ -37,6 +38,7 @@ class ModelText extends SocketConsumer {
37
38
  render() {
38
39
  this.innerHTML = '';
39
40
  let modelInstanceProperty = this.getAttribute('model-instance-property');
41
+ let bindToCollection = this.hasAttribute('bind-to-collection');
40
42
  let modelType = this.getAttribute('model-type');
41
43
  let modelId = this.getAttribute('model-id');
42
44
  let modelField = this.getAttribute('model-field');
@@ -44,40 +46,63 @@ class ModelText extends SocketConsumer {
44
46
  let currentNode = this.parentNode;
45
47
  let model;
46
48
  let isModelLocal = false;
47
- if (modelInstanceProperty) {
49
+ if (bindToCollection) {
50
+ let collection;
48
51
  while (currentNode) {
49
- model = currentNode[modelInstanceProperty];
50
- if (model && modelType && (model.type !== modelType || !(model.fields || []).includes(modelField))) {
51
- model = null;
52
+ collection = currentNode.collection;
53
+ if (collection && (collection.type !== modelType || !(collection.fields || []).includes(modelField))) {
54
+ collection = null;
52
55
  }
53
- if (model) break;
56
+ if (collection && (!collection.agModels || !collection.agModels[modelId] || !collection.agModels[modelId].agFields[modelField])) {
57
+ collection = null;
58
+ }
59
+ if (collection) break;
54
60
  currentNode = currentNode.getRootNode().host || currentNode.parentNode;
55
61
  }
56
- if (!model) {
57
- throw new Error(
58
- `The ${
59
- this.nodeName.toLowerCase()
60
- } element failed to obtain a model via the specified model-instance-property - Ensure that the element is nested inside a parent element which exposes a model instance of the same type which has the relevant field`
61
- );
62
- };
63
- } else {
64
- this.socket = this.getSocket();
65
- model = new AGModel({
66
- socket: this.socket,
67
- type: modelType,
68
- id: modelId,
69
- fields: [ modelField ]
70
- });
71
- if (!hideErrorLogs) {
72
- (async () => {
73
- for await (let { error } of model.listener('error')) {
74
- console.error(
75
- `Model input encountered an error: ${error.message}`
76
- );
62
+ if (collection) {
63
+ model = collection.agModels[modelId];
64
+ }
65
+ }
66
+ if (!model) {
67
+ currentNode = this.parentNode;
68
+ if (modelInstanceProperty) {
69
+ while (currentNode) {
70
+ model = currentNode[modelInstanceProperty];
71
+ if (model && modelType && (model.type !== modelType || !(model.fields || []).includes(modelField))) {
72
+ model = null;
77
73
  }
78
- })();
74
+ if (model && !model.agFields) {
75
+ model = null;
76
+ }
77
+ if (model) break;
78
+ currentNode = currentNode.getRootNode().host || currentNode.parentNode;
79
+ }
80
+ if (!model) {
81
+ throw new Error(
82
+ `The ${
83
+ this.nodeName.toLowerCase()
84
+ } element failed to obtain a model via the specified model-instance-property - Ensure that the element is nested inside a parent element which exposes a model instance of the same type which has the relevant field`
85
+ );
86
+ };
87
+ } else {
88
+ this.socket = this.getSocket();
89
+ model = new AGModel({
90
+ socket: this.socket,
91
+ type: modelType,
92
+ id: modelId,
93
+ fields: [ modelField ]
94
+ });
95
+ if (!hideErrorLogs) {
96
+ (async () => {
97
+ for await (let { error } of model.listener('error')) {
98
+ console.error(
99
+ `Model input encountered an error: ${error.message}`
100
+ );
101
+ }
102
+ })();
103
+ }
104
+ isModelLocal = true;
79
105
  }
80
- isModelLocal = true;
81
106
  }
82
107
  if (model.isLoaded) {
83
108
  this.setAttribute('is-loaded', '');
package/model-viewer.js CHANGED
@@ -104,6 +104,9 @@ class ModelViewer extends SocketConsumer {
104
104
  if (model && modelType && (model.type !== modelType || !(model.fields || []).includes(modelField))) {
105
105
  model = null;
106
106
  }
107
+ if (model && !model.agFields) {
108
+ model = null;
109
+ }
107
110
  if (model) break;
108
111
  currentNode = currentNode.getRootNode().host || currentNode.parentNode;
109
112
  }
package/oauth-handler.js CHANGED
@@ -4,19 +4,6 @@ import { generateRandomHexString } from './utils.js';
4
4
  const DEFAULT_AUTH_TIMEOUT = 10000;
5
5
 
6
6
  class OAuthHandler extends SocketConsumer {
7
- static get observedAttributes() {
8
- return [
9
- 'provider',
10
- 'code-param-name',
11
- 'state-param-name',
12
- 'state-session-storage-key',
13
- 'success-url',
14
- 'success-location-hash',
15
- 'navigate-event-path',
16
- 'auth-timeout'
17
- ];
18
- }
19
-
20
7
  successRedirect() {
21
8
  let successURL = this.getAttribute('success-url');
22
9
  let successLocationHash = this.getAttribute('success-location-hash');
@@ -46,9 +33,19 @@ class OAuthHandler extends SocketConsumer {
46
33
  throw new Error('The provider attribute of oauth-handler was not specified');
47
34
  }
48
35
 
49
- let sessionStorageKey = this.getAttribute('state-session-storage-key') || `oauth.${provider}.state`;
36
+ let sessionStorageKey = this.getAttribute('state-session-storage-key') || 'oauth.state';
50
37
  let expectedOAuthState = sessionStorage.getItem(sessionStorageKey);
51
- if (!expectedOAuthState) return;
38
+ if (!expectedOAuthState) {
39
+ this.innerHTML = `
40
+ <div class="error">OAuth authentication failed because the state query parameter was missing.</div>
41
+ `;
42
+ return;
43
+ };
44
+
45
+ let stateParts = expectedOAuthState.split('-');
46
+ // Do not process. The state may have been set for a different provider
47
+ // which is handled on the same page.
48
+ if (stateParts[0] !== provider) return;
52
49
 
53
50
  let codeParamName = this.getAttribute('code-param-name') || 'code';
54
51
  let stateParamName = this.getAttribute('state-param-name') || 'state';
package/oauth-link.js CHANGED
@@ -27,6 +27,10 @@ class OAuthLink extends HTMLElement {
27
27
 
28
28
  connectedCallback() {
29
29
  this.isReady = true;
30
+
31
+ let urlSearchParams = new URLSearchParams(location.search);
32
+ this.code = urlSearchParams.get('code');
33
+
30
34
  this.render();
31
35
  this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
32
36
  }
@@ -36,27 +40,21 @@ class OAuthLink extends HTMLElement {
36
40
  }
37
41
 
38
42
  renderItem() {
39
- let provider = this.getAttribute('provider');
40
- if (!provider) {
41
- throw new Error('The provider attribute of oauth-link was not specified');
42
- }
43
+ if (this.code || !this.state) return;
44
+
43
45
  let viewportNode = this.shadowRoot.querySelector('slot[name="viewport"]').assignedNodes()[0];
44
46
  if (!viewportNode) return;
45
47
  let itemTemplate = this.shadowRoot.querySelector('slot[name="item"]').assignedNodes()[0];
46
48
  if (!itemTemplate) return;
47
49
 
48
50
  let clientId = this.getAttribute('client-id');
49
- let stateSize = Number(this.getAttribute('state-size') || 20);
50
- let sessionStorageKey = this.getAttribute('state-session-storage-key') || `oauth.${provider}.state`;
51
- let state = generateRandomHexString(stateSize);
52
- sessionStorage.setItem(sessionStorageKey, state);
53
51
 
54
52
  let itemString = renderTemplate(
55
53
  itemTemplate.innerHTML,
56
54
  {
57
55
  oauth: {
58
56
  clientId,
59
- state
57
+ state: this.state
60
58
  }
61
59
  }
62
60
  );
@@ -64,14 +62,20 @@ class OAuthLink extends HTMLElement {
64
62
  }
65
63
 
66
64
  render() {
67
- let urlSearchParams = new URLSearchParams(location.search);
68
- let code = urlSearchParams.get('code');
69
-
70
- if (code) {
65
+ if (this.code) {
71
66
  this.shadowRoot.innerHTML = '';
72
67
  return;
73
68
  }
74
69
 
70
+ let provider = this.getAttribute('provider');
71
+ if (!provider) {
72
+ throw new Error('The provider attribute of oauth-link was not specified');
73
+ }
74
+ let stateSize = Number(this.getAttribute('state-size') || 20);
75
+ let sessionStorageKey = this.getAttribute('state-session-storage-key') || 'oauth.state';
76
+ this.state = `${provider}-${generateRandomHexString(stateSize)}`;
77
+ sessionStorage.setItem(sessionStorageKey, this.state);
78
+
75
79
  this.shadowRoot.innerHTML = `
76
80
  <slot name="item"></slot>
77
81
  <slot name="viewport"></slot>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "2.3.0",
3
+ "version": "2.4.1",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {