saasufy-components 1.0.1 → 1.0.3

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.
@@ -9,19 +9,78 @@ class CollectionBrowser extends HTMLElement {
9
9
  super();
10
10
  this.attachShadow({ mode: 'open' });
11
11
  this.socket = getSocket();
12
+
13
+ this.handleShowModalEvent = (event) => {
14
+ let modal = this.shadowRoot.querySelector('slot[name="modal"]').assignedNodes()[0];
15
+ modal.show(event.detail.message, event.detail.callback);
16
+ };
17
+
18
+ this.handleCRUDCreateEvent = (event) => {
19
+ if (this.collection) {
20
+ this.collection.create(event.detail);
21
+ }
22
+ };
23
+
24
+ this.handleCRUDDeleteEvent = (event) => {
25
+ if (this.collection) {
26
+ this.collection.delete(event.detail);
27
+ }
28
+ };
29
+
30
+ this.handleGoToPreviousPageEvent = () => {
31
+ this.goToPreviousPage();
32
+ };
33
+
34
+ this.handleGoToNextPageEvent = () => {
35
+ this.goToNextPage();
36
+ };
37
+
38
+ this.handleGoToPageEvent = (event) => {
39
+ this.goToPage((event.detail || {}).offset);
40
+ };
41
+
42
+ this.handleSlotChangeEvent = () => {
43
+ this.renderTable();
44
+ };
12
45
  }
13
46
 
14
47
  connectedCallback() {
15
48
  this.isReady = true;
16
- this.shadowRoot.addEventListener('slotchange', () => this.renderTable());
17
- this.addEventListener('showModal', (event) => this.handleShowModalEvent(event));
18
- this.addEventListener('crudCreate', (event) => this.handleCRUDCreateEvent(event));
19
- this.addEventListener('crudDelete', (event) => this.handleCRUDDeleteEvent(event));
49
+ this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
50
+ this.addEventListener('showModal', this.handleShowModalEvent);
51
+ this.addEventListener('crudCreate', this.handleCRUDCreateEvent);
52
+ this.addEventListener('crudDelete', this.handleCRUDDeleteEvent);
53
+ this.addEventListener('goToPreviousPage', this.handleGoToPreviousPageEvent);
54
+ this.addEventListener('goToNextPage', this.handleGoToNextPageEvent);
55
+ this.addEventListener('goToPage', this.handleGoToPageEvent);
20
56
  this.render();
21
57
  }
22
58
 
23
59
  disconnectedCallback() {
24
60
  if (this.collection) this.collection.destroy();
61
+ this.shadowRoot.removeEventListener('slotchange', this.handleSlotChangeEvent);
62
+ this.removeEventListener('showModal', this.handleShowModalEvent);
63
+ this.removeEventListener('crudCreate', this.handleCRUDCreateEvent);
64
+ this.removeEventListener('crudDelete', this.handleCRUDDeleteEvent);
65
+ this.removeEventListener('goToPreviousPage', this.handleGoToPreviousPageEvent);
66
+ this.removeEventListener('goToNextPage', this.handleGoToNextPageEvent);
67
+ this.removeEventListener('goToPage', this.handleGoToPageEvent);
68
+ }
69
+
70
+ goToPreviousPage() {
71
+ if (!this.collection) return;
72
+ this.collection.fetchPreviousPage();
73
+ this.setAttribute('collection-page-offset', this.collection.meta.pageOffset);
74
+ }
75
+
76
+ goToNextPage() {
77
+ if (!this.collection) return;
78
+ this.collection.fetchNextPage();
79
+ this.setAttribute('collection-page-offset', this.collection.meta.pageOffset);
80
+ }
81
+
82
+ goToPage(offset) {
83
+ this.setAttribute('collection-page-offset', offset);
25
84
  }
26
85
 
27
86
  static get observedAttributes() {
@@ -30,13 +89,61 @@ class CollectionBrowser extends HTMLElement {
30
89
  'collection-fields',
31
90
  'collection-view',
32
91
  'collection-view-params',
33
- 'collection-page-size'
92
+ 'collection-page-size',
93
+ 'collection-page-offset'
34
94
  ];
35
95
  }
36
96
 
37
97
  attributeChangedCallback(name, oldValue, newValue) {
38
98
  if (!this.isReady) return;
39
- this.render();
99
+ if (this.collection && name === 'collection-page-offset') {
100
+ let newOffset = Number(newValue);
101
+ if (newOffset !== this.collection.meta.pageOffset) {
102
+ this.collection.fetchPage(newOffset);
103
+ }
104
+ this.updatePageNumberElements();
105
+ this.updatePageButtons();
106
+ } else {
107
+ this.render();
108
+ }
109
+ }
110
+
111
+ updatePageNumberElements() {
112
+ if (!this.collection) return;
113
+ let currentPageElements = this.shadowRoot.querySelector('slot[name="page-number"]').assignedElements();
114
+ let pageNumber = Math.floor(this.collection.meta.pageOffset / this.collection.meta.pageSize + 1);
115
+ for (let element of currentPageElements) {
116
+ if (element.nodeName === 'INPUT') {
117
+ element.value = pageNumber;
118
+ } else {
119
+ element.innerHTML = pageNumber;
120
+ }
121
+ }
122
+ }
123
+
124
+ updatePageButtons() {
125
+ if (this.collection) {
126
+ let previousPageElements = this.shadowRoot.querySelector('slot[name="previous-page"]').assignedElements();
127
+ let nextPageElements = this.shadowRoot.querySelector('slot[name="next-page"]').assignedElements();
128
+ if (this.collection.meta.pageOffset <= 0) {
129
+ for (let element of previousPageElements) {
130
+ element.setAttribute('disabled', '');
131
+ }
132
+ } else {
133
+ for (let element of previousPageElements) {
134
+ element.removeAttribute('disabled');
135
+ }
136
+ }
137
+ if (this.collection.meta.isLastPage) {
138
+ for (let element of nextPageElements) {
139
+ element.setAttribute('disabled', '');
140
+ }
141
+ } else {
142
+ for (let element of nextPageElements) {
143
+ element.removeAttribute('disabled');
144
+ }
145
+ }
146
+ }
40
147
  }
41
148
 
42
149
  renderTable() {
@@ -64,29 +171,13 @@ class CollectionBrowser extends HTMLElement {
64
171
  }
65
172
  }
66
173
 
67
- handleShowModalEvent(event) {
68
- let modal = this.shadowRoot.querySelector('slot[name="modal"]').assignedNodes()[0];
69
- modal.show(event.detail.message, event.detail.callback);
70
- }
71
-
72
- handleCRUDCreateEvent(event) {
73
- if (this.collection) {
74
- this.collection.create(event.detail);
75
- }
76
- }
77
-
78
- handleCRUDDeleteEvent(event) {
79
- if (this.collection) {
80
- this.collection.delete(event.detail);
81
- }
82
- }
83
-
84
174
  render() {
85
175
  let collectionType = this.getAttribute('collection-type');
86
176
  let collectionFields = this.getAttribute('collection-fields');
87
177
  let collectionView = this.getAttribute('collection-view');
88
178
  let collectionPageSize = this.getAttribute('collection-page-size');
89
179
  let collectionViewParams = this.getAttribute('collection-view-params');
180
+ let collectionPageOffset = this.getAttribute('collection-page-offset');
90
181
  let collectionReloadDelay = Number(
91
182
  this.getAttribute('collection-reload-delay') || DEFAULT_RELOAD_DELAY
92
183
  );
@@ -100,10 +191,32 @@ class CollectionBrowser extends HTMLElement {
100
191
  view: collectionView,
101
192
  viewParams,
102
193
  pageSize: Number(collectionPageSize || 10),
194
+ pageOffset: Number(collectionPageOffset || 0),
103
195
  changeReloadDelay: collectionReloadDelay
104
196
  });
105
197
 
106
- this.shadowRoot.innerHTML = '<slot name="item"></slot><slot name="no-item"></slot><slot name="list"></slot><slot name="modal"></slot>';
198
+ this.shadowRoot.innerHTML = `
199
+ <slot name="item"></slot>
200
+ <slot name="no-item"></slot>
201
+ <slot name="list"></slot>
202
+ <slot name="previous-page"></slot>
203
+ <slot name="page-number"></slot>
204
+ <slot name="next-page"></slot>
205
+ <slot name="modal"></slot>
206
+ `;
207
+
208
+ let previousPageSlot = this.shadowRoot.querySelector('slot[name="previous-page"]');
209
+ previousPageSlot.addEventListener('click', () => {
210
+ this.goToPreviousPage();
211
+ });
212
+
213
+ let nextPageSlot = this.shadowRoot.querySelector('slot[name="next-page"]');
214
+ nextPageSlot.addEventListener('click', () => {
215
+ this.goToNextPage();
216
+ });
217
+
218
+ this.updatePageNumberElements();
219
+ this.updatePageButtons();
107
220
 
108
221
  (async () => {
109
222
  await this.collection.listener('load').once();
@@ -117,6 +230,7 @@ class CollectionBrowser extends HTMLElement {
117
230
  (async () => {
118
231
  for await (let event of this.collection.listener('change')) {
119
232
  this.renderTable();
233
+ this.updatePageButtons();
120
234
  }
121
235
  })();
122
236
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  "author": "Jonathan Gros-Dubois",
18
18
  "license": "MIT",
19
19
  "dependencies": {
20
- "ag-collection": "^3.1.0",
20
+ "ag-collection": "^3.2.0",
21
21
  "ag-model": "^1.3.5",
22
22
  "socketcluster-client": "^18.3.0"
23
23
  },
package/socket.js CHANGED
@@ -9,29 +9,12 @@ export function createSocket(options) {
9
9
  authTokenName: DEFAULT_AUTH_TOKEN_NAME,
10
10
  ...options
11
11
  });
12
-
13
- (async () => {
14
- for await (let { error } of socket.listener('error')) {
15
- console.error(error);
16
- }
17
- })();
18
-
19
- (async () => {
20
- for await (let event of socket.listener('connect')) {
21
- console.log('Socket is connected');
22
- }
23
- })();
24
-
25
12
  return socket;
26
13
  }
27
14
 
28
- window.createSocket = createSocket;
29
-
30
15
  export function getSocket(options) {
31
16
  if (socket) {
32
17
  return socket;
33
18
  }
34
19
  return createSocket(options);
35
20
  }
36
-
37
- window.getSocket = getSocket;