@cocreate/organizations 1.28.1 → 1.28.2

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/package.json +1 -1
  3. package/src/client.js +250 -224
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.28.2](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.1...v1.28.2) (2025-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * dispatch end event on action element rather than document ([dd5ea2a](https://github.com/CoCreate-app/CoCreate-organizations/commit/dd5ea2a43c00418448189369c39f72f83f0206b6))
7
+
1
8
  ## [1.28.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.0...v1.28.1) (2024-12-22)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.28.1",
3
+ "version": "1.28.2",
4
4
  "description": "A simple organizations component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "organizations",
package/src/client.js CHANGED
@@ -1,253 +1,279 @@
1
- import Crud from '@cocreate/crud-client';
2
- import Action from '@cocreate/actions';
3
- import Elements from '@cocreate/elements';
4
- import Config from '@cocreate/config';
5
- import Indexeddb from '@cocreate/indexeddb';
6
- import uuid from '@cocreate/uuid';
7
-
8
- async function generateDB(organization = { object: {} }, user = { object: {} }) {
9
- const organization_id = organization.object._id || Crud.ObjectId().toString();
10
- const apikey = organization.object.key || uuid.generate();
11
- const user_id = user.object._id || Crud.ObjectId().toString();
12
-
13
- try {
14
- // Create organization
15
- organization.method = 'object.create'
16
- organization.storage = 'indexeddb'
17
- organization.database = organization_id
18
- organization.array = 'organizations'
19
- organization.object._id = organization_id
20
- organization.object.name = organization.object.name || 'untitiled'
21
- organization.organization_id = organization_id
22
- Indexeddb.send(organization);
23
-
24
- // Create user
25
- user.method = 'object.create'
26
- user.storage = 'indexeddb'
27
- user.database = organization_id
28
- user.array = 'users'
29
- user.object._id = user_id
30
- user.object.firstname = user.object.firstname || 'untitiled'
31
- user.object.lastname = user.object.lastname || 'untitiled'
32
- user.organization_id = organization_id
33
- Indexeddb.send(user);
34
-
35
- // Create default key
36
- let key = {
37
- method: 'object.create',
38
- storage: 'indexeddb',
39
- database: organization_id,
40
- array: 'keys',
41
- object: {
42
- _id: Crud.ObjectId().toString(),
43
- type: "key",
44
- key: apikey,
45
- actions: {
46
- signIn: true,
47
- signUp: true
48
- },
49
- default: true
50
- },
51
- organization_id
52
- }
53
- Indexeddb.send(key);
54
-
55
- // Create role
56
- let role_id = Crud.ObjectId().toString();
57
- let role = {
58
- method: 'object.create',
59
- storage: 'indexeddb',
60
- database: organization_id,
61
- array: 'keys',
62
- object: {
63
- _id: role_id,
64
- type: "role",
65
- name: "admin",
66
- admin: "true"
67
- },
68
- organization_id
69
- };
70
- Indexeddb.send(role);
71
-
72
- // Create user key
73
- let userKey = {
74
- method: 'object.create',
75
- storage: 'indexeddb',
76
- database: organization_id,
77
- array: 'keys',
78
- object: {
79
- _id: Crud.ObjectId().toString(),
80
- type: "user",
81
- key: user_id,
82
- array: 'users', // could be any array
83
- roles: [role_id],
84
- email: user.object.email,
85
- password: user.object.password || btoa('0000')
86
- },
87
- organization_id
88
- };
89
- Indexeddb.send(userKey);
90
-
91
- return { organization: organization.object, apikey, user: user.object, role: role.object, userKey: userKey.object }
92
-
93
- } catch (error) {
94
- return false
95
- }
1
+ import Crud from "@cocreate/crud-client";
2
+ import Action from "@cocreate/actions";
3
+ import Elements from "@cocreate/elements";
4
+ import Config from "@cocreate/config";
5
+ import Indexeddb from "@cocreate/indexeddb";
6
+ import uuid from "@cocreate/uuid";
7
+
8
+ async function generateDB(
9
+ organization = { object: {} },
10
+ user = { object: {} }
11
+ ) {
12
+ const organization_id =
13
+ organization.object._id || Crud.ObjectId().toString();
14
+ const apikey = organization.object.key || uuid.generate();
15
+ const user_id = user.object._id || Crud.ObjectId().toString();
16
+
17
+ try {
18
+ // Create organization
19
+ organization.method = "object.create";
20
+ organization.storage = "indexeddb";
21
+ organization.database = organization_id;
22
+ organization.array = "organizations";
23
+ organization.object._id = organization_id;
24
+ organization.object.name = organization.object.name || "untitiled";
25
+ organization.organization_id = organization_id;
26
+ Indexeddb.send(organization);
27
+
28
+ // Create user
29
+ user.method = "object.create";
30
+ user.storage = "indexeddb";
31
+ user.database = organization_id;
32
+ user.array = "users";
33
+ user.object._id = user_id;
34
+ user.object.firstname = user.object.firstname || "untitiled";
35
+ user.object.lastname = user.object.lastname || "untitiled";
36
+ user.organization_id = organization_id;
37
+ Indexeddb.send(user);
38
+
39
+ // Create default key
40
+ let key = {
41
+ method: "object.create",
42
+ storage: "indexeddb",
43
+ database: organization_id,
44
+ array: "keys",
45
+ object: {
46
+ _id: Crud.ObjectId().toString(),
47
+ type: "key",
48
+ key: apikey,
49
+ actions: {
50
+ signIn: true,
51
+ signUp: true
52
+ },
53
+ default: true
54
+ },
55
+ organization_id
56
+ };
57
+ Indexeddb.send(key);
58
+
59
+ // Create role
60
+ let role_id = Crud.ObjectId().toString();
61
+ let role = {
62
+ method: "object.create",
63
+ storage: "indexeddb",
64
+ database: organization_id,
65
+ array: "keys",
66
+ object: {
67
+ _id: role_id,
68
+ type: "role",
69
+ name: "admin",
70
+ admin: "true"
71
+ },
72
+ organization_id
73
+ };
74
+ Indexeddb.send(role);
75
+
76
+ // Create user key
77
+ let userKey = {
78
+ method: "object.create",
79
+ storage: "indexeddb",
80
+ database: organization_id,
81
+ array: "keys",
82
+ object: {
83
+ _id: Crud.ObjectId().toString(),
84
+ type: "user",
85
+ key: user_id,
86
+ array: "users", // could be any array
87
+ roles: [role_id],
88
+ email: user.object.email,
89
+ password: user.object.password || btoa("0000")
90
+ },
91
+ organization_id
92
+ };
93
+ Indexeddb.send(userKey);
94
+
95
+ return {
96
+ organization: organization.object,
97
+ apikey,
98
+ user: user.object,
99
+ role: role.object,
100
+ userKey: userKey.object
101
+ };
102
+ } catch (error) {
103
+ return false;
104
+ }
96
105
  }
97
106
 
98
107
  async function get() {
99
- let organization_id = await getOrganizationFromServiceWorker()
100
- if (!organization_id) {
101
- let data = await Indexeddb.send({ method: 'database.read' })
102
- for (let database of data.database) {
103
- let name = database.name
104
- if (name.match(/^[0-9a-fA-F]{24}$/)) {
105
- organization_id = name
106
- }
107
- }
108
- }
109
-
110
- if (!organization_id) {
111
- let file = await fetch('/')
112
- organization_id = file.headers.get('organization');
113
- }
114
-
115
- if (!organization_id)
116
- organization_id = await createOrganization()
117
-
118
- if (organization_id)
119
- Config.set('organization_id', organization_id)
120
-
121
- return organization_id
122
-
108
+ let organization_id = await getOrganizationFromServiceWorker();
109
+ if (!organization_id) {
110
+ let data = await Indexeddb.send({ method: "database.read" });
111
+ for (let database of data.database) {
112
+ let name = database.name;
113
+ if (name.match(/^[0-9a-fA-F]{24}$/)) {
114
+ organization_id = name;
115
+ }
116
+ }
117
+ }
118
+
119
+ if (!organization_id) {
120
+ let file = await fetch("/");
121
+ organization_id = file.headers.get("organization");
122
+ }
123
+
124
+ if (!organization_id) organization_id = await createOrganization();
125
+
126
+ if (organization_id) Config.set("organization_id", organization_id);
127
+
128
+ return organization_id;
123
129
  }
124
130
 
125
131
  async function getOrganizationFromServiceWorker() {
126
- return new Promise((resolve, reject) => {
127
- if (!navigator.serviceWorker)
128
- return resolve()
129
-
130
- const handleMessage = (event) => {
131
- if (event.data.action === 'getOrganization') {
132
- navigator.serviceWorker.removeEventListener('message', handleMessage); // Remove the event listener
133
- resolve(event.data.organization_id);
134
- }
135
- };
136
-
137
- navigator.serviceWorker.addEventListener('message', handleMessage);
138
-
139
- // Send message to Service Worker
140
- const msg = new MessageChannel();
141
-
142
- navigator.serviceWorker.ready.then(registration => {
143
- if (navigator.serviceWorker.controller) {
144
- // If there's an active controller, send the message
145
- navigator.serviceWorker.controller.postMessage({ action: 'getOrganization' }, [msg.port1]);
146
- } else {
147
- // Listen for a new service worker to start controlling the page
148
- navigator.serviceWorker.addEventListener('controllerchange', () => {
149
- if (navigator.serviceWorker.controller) {
150
- navigator.serviceWorker.controller.postMessage({ action: 'getOrganization' }, [msg.port1]);
151
- }
152
- });
153
- }
154
- }).catch(error => {
155
- console.error(reject);
156
- });
157
- });
132
+ return new Promise((resolve, reject) => {
133
+ if (!navigator.serviceWorker) return resolve();
134
+
135
+ const handleMessage = (event) => {
136
+ if (event.data.action === "getOrganization") {
137
+ navigator.serviceWorker.removeEventListener(
138
+ "message",
139
+ handleMessage
140
+ ); // Remove the event listener
141
+ resolve(event.data.organization_id);
142
+ }
143
+ };
144
+
145
+ navigator.serviceWorker.addEventListener("message", handleMessage);
146
+
147
+ // Send message to Service Worker
148
+ const msg = new MessageChannel();
149
+
150
+ navigator.serviceWorker.ready
151
+ .then((registration) => {
152
+ if (navigator.serviceWorker.controller) {
153
+ // If there's an active controller, send the message
154
+ navigator.serviceWorker.controller.postMessage(
155
+ { action: "getOrganization" },
156
+ [msg.port1]
157
+ );
158
+ } else {
159
+ // Listen for a new service worker to start controlling the page
160
+ navigator.serviceWorker.addEventListener(
161
+ "controllerchange",
162
+ () => {
163
+ if (navigator.serviceWorker.controller) {
164
+ navigator.serviceWorker.controller.postMessage(
165
+ { action: "getOrganization" },
166
+ [msg.port1]
167
+ );
168
+ }
169
+ }
170
+ );
171
+ }
172
+ })
173
+ .catch((error) => {
174
+ console.error(reject);
175
+ });
176
+ });
158
177
  }
159
178
 
160
179
  let organizationPromise = null;
161
180
  async function createOrganizationPromise() {
162
- let createOrganization = document.querySelector('[actions*="createOrganization"]')
163
- if (createOrganization)
164
- return Crud.socket.organization = 'canceled'
165
-
166
- if (Crud.socket.organization == 'canceled' || Crud.socket.organization == 'pending') return
167
-
168
- const organization_id = prompt("An organization_id could not be found, if you already have an organization_id input it now.\n\nOr leave blank and click 'OK' to create a new organization");
169
-
170
- if (organization_id)
171
- return organization_id
172
- if (organization_id === null)
173
- return Crud.socket.organization = 'canceled'
174
-
175
- Crud.socket.organization = 'pending'
176
- if (Indexeddb) {
177
- try {
178
- let org = { object: {} }
179
- if (organization_id)
180
- org.object._id = organization_id
181
- let { organization, apikey, user } = await generateDB(org)
182
- if (organization && apikey && user) {
183
- Crud.socket.apikey = apikey
184
- Crud.socket.user_id = user._id
185
- Config.set('organization_id', organization._id)
186
- Config.set('apikey', apikey)
187
- Config.set('user_id', user._id)
188
- Crud.socket.organization = true
189
- return organization._id
190
- }
191
- } catch (error) {
192
- console.error('Failed to load the script:', error);
193
- }
194
- }
181
+ let createOrganization = document.querySelector(
182
+ '[actions*="createOrganization"]'
183
+ );
184
+ if (createOrganization) return (Crud.socket.organization = "canceled");
185
+
186
+ if (
187
+ Crud.socket.organization == "canceled" ||
188
+ Crud.socket.organization == "pending"
189
+ )
190
+ return;
191
+
192
+ const organization_id = prompt(
193
+ "An organization_id could not be found, if you already have an organization_id input it now.\n\nOr leave blank and click 'OK' to create a new organization"
194
+ );
195
+
196
+ if (organization_id) return organization_id;
197
+ if (organization_id === null)
198
+ return (Crud.socket.organization = "canceled");
199
+
200
+ Crud.socket.organization = "pending";
201
+ if (Indexeddb) {
202
+ try {
203
+ let org = { object: {} };
204
+ if (organization_id) org.object._id = organization_id;
205
+ let { organization, apikey, user } = await generateDB(org);
206
+ if (organization && apikey && user) {
207
+ Crud.socket.apikey = apikey;
208
+ Crud.socket.user_id = user._id;
209
+ Config.set("organization_id", organization._id);
210
+ Config.set("apikey", apikey);
211
+ Config.set("user_id", user._id);
212
+ Crud.socket.organization = true;
213
+ return organization._id;
214
+ }
215
+ } catch (error) {
216
+ console.error("Failed to load the script:", error);
217
+ }
218
+ }
195
219
  }
196
220
 
197
-
198
221
  async function createOrganization() {
199
- return organizationPromise || (organizationPromise = createOrganizationPromise());
222
+ return (
223
+ organizationPromise ||
224
+ (organizationPromise = createOrganizationPromise())
225
+ );
200
226
  }
201
227
 
202
- async function create(form) {
203
- if (!form) return;
228
+ async function create(action) {
229
+ let form = action.form;
230
+ if (!form) return;
204
231
 
205
- let organization = Elements.getData(form, 'organizations')
206
- let user = Elements.getData(form, 'users')
232
+ let organization = Elements.getData(form, "organizations");
233
+ let user = Elements.getData(form, "users");
207
234
 
208
- if (!organization || !organization.object)
209
- return
210
- if (!user || !user.object)
211
- return
235
+ if (!organization || !organization.object) return;
236
+ if (!user || !user.object) return;
212
237
 
213
- if (!organization.object._id && !user.object._id) {
214
- let objects = await generateDB(organization, user)
215
- if (!objects)
216
- return
217
- }
238
+ if (!organization.object._id && !user.object._id) {
239
+ let objects = await generateDB(organization, user);
240
+ if (!objects) return;
241
+ }
218
242
 
219
- Elements.setTypeValue(form, organization)
220
- Elements.setTypeValue(form, user)
243
+ Elements.setTypeValue(form, organization);
244
+ Elements.setTypeValue(form, user);
221
245
 
222
- organization = organization.object[0]
223
- user = user.object[0]
246
+ organization = organization.object[0];
247
+ user = user.object[0];
224
248
 
225
- let organization_id = organization._id
249
+ let organization_id = organization._id;
226
250
 
227
- if (Crud.socket.organization !== true) {
228
- Crud.socket.organization = true
229
- Crud.socket.create({ organization_id })
230
- }
251
+ if (Crud.socket.organization !== true) {
252
+ Crud.socket.organization = true;
253
+ Crud.socket.create({ organization_id });
254
+ }
231
255
 
232
- let response = await Crud.socket.send({
233
- method: 'createOrganization',
234
- organization,
235
- user,
236
- broadcastBrowser: false,
237
- organization_id
238
- });
256
+ let response = await Crud.socket.send({
257
+ method: "createOrganization",
258
+ organization,
259
+ user,
260
+ broadcastBrowser: false,
261
+ organization_id
262
+ });
239
263
 
240
- document.dispatchEvent(new CustomEvent('createdOrganization', {
241
- detail: response
242
- }));
264
+ action.element.dispatchEvent(
265
+ new CustomEvent("createdOrganization", {
266
+ detail: response
267
+ })
268
+ );
243
269
  }
244
270
 
245
271
  Action.init({
246
- name: "createOrganization",
247
- endEvent: "createdOrganization",
248
- callback: (action) => {
249
- create(action.form);
250
- }
272
+ name: "createOrganization",
273
+ endEvent: "createdOrganization",
274
+ callback: (action) => {
275
+ create(action);
276
+ }
251
277
  });
252
278
 
253
- export default { generateDB, create, get };
279
+ export default { generateDB, create, get };