@tanglemedia/directus-core-settings 0.0.3 → 0.0.5

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.
@@ -0,0 +1,139 @@
1
+ export function getBasicCollectionFields(collectionName) {
2
+ return [
3
+ {
4
+ collection: collectionName,
5
+ field: "id",
6
+ hidden: true,
7
+ interface: "input",
8
+ readonly: true,
9
+ required: false,
10
+ sort: 1,
11
+ },
12
+ {
13
+ collection: collectionName,
14
+ field: "sort",
15
+ interface: "input",
16
+ readonly: false,
17
+ hidden: true,
18
+ sort: 2,
19
+ width: "full",
20
+ required: false,
21
+ },
22
+ {
23
+ collection: collectionName,
24
+ display: "labels",
25
+ display_options: JSON.stringify({
26
+ choices: [
27
+ {
28
+ text: "$t:published",
29
+ color: "var(--theme--primary)",
30
+ value: "published",
31
+ background: "var(--theme--primary-background)",
32
+ foreground: "var(--theme--primary)",
33
+ },
34
+ {
35
+ text: "$t:draft",
36
+ color: "var(--theme--foreground)",
37
+ value: "draft",
38
+ background: "var(--theme--background-normal)",
39
+ foreground: "var(--theme--foreground)",
40
+ },
41
+ {
42
+ text: "$t:archived",
43
+ color: "var(--theme--warning)",
44
+ value: "archived",
45
+ background: "var(--theme--warning-background)",
46
+ foreground: "var(--theme--warning)",
47
+ },
48
+ ],
49
+ showAsDot: true,
50
+ }),
51
+ field: "status",
52
+ hidden: false,
53
+ interface: "select-dropdown",
54
+ options: JSON.stringify({
55
+ choices: [
56
+ {
57
+ text: "$t:published",
58
+ color: "var(--theme--primary)",
59
+ value: "published",
60
+ },
61
+ {
62
+ text: "$t:draft",
63
+ color: "var(--theme--foreground)",
64
+ value: "draft",
65
+ },
66
+ {
67
+ text: "$t:archived",
68
+ color: "var(--theme--warning)",
69
+ value: "archived",
70
+ },
71
+ ],
72
+ }),
73
+ readonly: false,
74
+ required: false,
75
+ sort: 3,
76
+ width: "full",
77
+ },
78
+ {
79
+ collection: collectionName,
80
+ display: "user",
81
+ field: "user_created",
82
+ hidden: true,
83
+ interface: "select-dropdown-m2o",
84
+ options: JSON.stringify({
85
+ template: "{{avatar.$thumbnail}} {{first_name}} {{last_name}}",
86
+ }),
87
+ readonly: true,
88
+ required: false,
89
+ sort: 4,
90
+ special: "user-created",
91
+ width: "half",
92
+ },
93
+ {
94
+ collection: collectionName,
95
+ display: "user",
96
+ field: "user_updated",
97
+ hidden: true,
98
+ interface: "select-dropdown-m2o",
99
+ options: JSON.stringify({
100
+ template: "{{avatar.$thumbnail}} {{first_name}} {{last_name}}",
101
+ }),
102
+ readonly: true,
103
+ required: false,
104
+ sort: 5,
105
+ special: "user-updated",
106
+ width: "half",
107
+ },
108
+ {
109
+ collection: collectionName,
110
+ display: "datetime",
111
+ display_options: JSON.stringify({
112
+ relative: true,
113
+ }),
114
+ field: "date_created",
115
+ hidden: true,
116
+ interface: "datetime",
117
+ readonly: true,
118
+ required: false,
119
+ sort: 6,
120
+ special: "date-created",
121
+ width: "half",
122
+ },
123
+ {
124
+ collection: collectionName,
125
+ display: "datetime",
126
+ display_options: JSON.stringify({
127
+ relative: true,
128
+ }),
129
+ field: "date_updated",
130
+ hidden: true,
131
+ interface: "datetime",
132
+ readonly: true,
133
+ required: false,
134
+ sort: 7,
135
+ special: "date-updated",
136
+ width: "half",
137
+ },
138
+ ];
139
+ }
@@ -0,0 +1,37 @@
1
+ import { updateOneAllowedCollections } from "./relations.mjs";
2
+
3
+ export async function addCollectionToMenu(knex, collectionName) {
4
+ const menuItemCollectionExists = await knex("directus_relations")
5
+ .where({
6
+ many_collection: "tngl_menu_item_collection",
7
+ many_field: "item",
8
+ })
9
+ .first();
10
+
11
+ if (menuItemCollectionExists?.id) {
12
+ await updateOneAllowedCollections(
13
+ knex,
14
+ menuItemCollectionExists.id,
15
+ collectionName,
16
+ "add",
17
+ );
18
+ }
19
+ }
20
+
21
+ export async function removeCollectionFromMenu(knex, collectionName) {
22
+ const menuItemCollectionExists = await knex("directus_relations")
23
+ .where({
24
+ many_collection: "tngl_menu_item_collection",
25
+ many_field: "item",
26
+ })
27
+ .first();
28
+
29
+ if (menuItemCollectionExists?.id) {
30
+ await updateOneAllowedCollections(
31
+ knex,
32
+ menuItemCollectionExists.id,
33
+ collectionName,
34
+ "remove",
35
+ );
36
+ }
37
+ }
@@ -0,0 +1,46 @@
1
+ export async function updateOneAllowedCollections(
2
+ knex,
3
+ directusRelationID,
4
+ collectionName,
5
+ action,
6
+ ) {
7
+ const relation = await knex("directus_relations")
8
+ .where({ id: directusRelationID })
9
+ .first();
10
+ const relatedCollections = relation?.one_allowed_collections;
11
+ let finalRelatedCollection = relatedCollections.split(",");
12
+
13
+ switch (action) {
14
+ case "add":
15
+ {
16
+ if (finalRelatedCollection.length > 0) {
17
+ finalRelatedCollection.push(collectionName);
18
+ } else {
19
+ finalRelatedCollection = [collectionName];
20
+ }
21
+ }
22
+ break;
23
+
24
+ case "remove":
25
+ {
26
+ if (finalRelatedCollection.length > 0) {
27
+ finalRelatedCollection = finalRelatedCollection.filter(
28
+ (item) => item !== collectionName,
29
+ );
30
+ } else {
31
+ finalRelatedCollection = [];
32
+ }
33
+ }
34
+ break;
35
+ }
36
+
37
+ finalRelatedCollection = [...new Set(finalRelatedCollection)].filter((item) =>
38
+ item.trim(),
39
+ );
40
+
41
+ await knex("directus_relations")
42
+ .where({ id: directusRelationID })
43
+ .update({
44
+ one_allowed_collections: finalRelatedCollection.join(","),
45
+ });
46
+ }
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "license": "MIT",
8
8
  "icon": "extension",
9
- "version": "0.0.3",
9
+ "version": "0.0.5",
10
10
  "keywords": [
11
11
  "directus",
12
12
  "directus-extension",