|
1 |
| -import { EntryModel } from "."; |
| 1 | +import { EntryModel } from "." |
2 | 2 |
|
3 |
| -export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us') : void { |
4 |
| - if (entry) |
5 |
| - entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale) |
6 |
| -} |
7 |
| - |
8 |
| -function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string): object { |
9 |
| - const tags: any = {} |
10 |
| - Object.entries(content).forEach(([key, value]) => { |
11 |
| - if (key === '$') return |
| 3 | + export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us') : void { |
| 4 | + if (entry) { |
| 5 | + const appliedVariants = entry._applied_variants || null; |
| 6 | + entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale, {_applied_variants: appliedVariants, shouldApplyVariant: !!appliedVariants, metaKey: ''}) } |
| 7 | + } |
| 8 | + |
| 9 | + function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string, appliedVariants: {_applied_variants:{[key: string]: any}, shouldApplyVariant: boolean, metaKey: string}): object { |
| 10 | + const tags: any = {} |
| 11 | + const { metaKey, shouldApplyVariant, _applied_variants } = appliedVariants |
| 12 | + Object.entries(content).forEach(([key, value]) => { |
| 13 | + if (key === '$') return |
| 14 | + let metaUID = value && typeof value === 'object' && value._metadata && value._metadata.uid ? value._metadata.uid : ''; |
| 15 | + let updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '' }${key}` : ''; |
| 16 | + if(metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID; |
| 17 | + switch (typeof value) { |
| 18 | + case "object": |
| 19 | + if (Array.isArray(value)) { |
| 20 | + value.forEach((obj, index) => { |
| 21 | + const childKey = `${key}__${index}` |
| 22 | + const parentKey = `${key}__parent` |
| 23 | + metaUID = value && typeof value === 'object' && obj._metadata && obj._metadata.uid ? obj._metadata.uid : ''; |
| 24 | + updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '' }${key}` : ''; |
| 25 | + if(metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID; |
| 26 | + /** |
| 27 | + * Indexes of array are handled here |
| 28 | + * { |
| 29 | + * "array": ["hello", "world"], |
| 30 | + * "$": { |
| 31 | + * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"} |
| 32 | + * "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"} |
| 33 | + * "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"} |
| 34 | + * } |
| 35 | + * } |
| 36 | + */ |
| 37 | + tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey }) |
| 38 | + tags[parentKey] = getParentTagsValue(`${prefix}.${key}`, tagsAsObject) |
| 39 | + if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) { |
| 40 | + /** |
| 41 | + * References are handled here |
| 42 | + * { |
| 43 | + * "reference": [{ |
| 44 | + * "title": "title", |
| 45 | + * "uid": "ref_uid", |
| 46 | + * "_content_type_uid": "ref_content_type_uid", |
| 47 | + * "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}} |
| 48 | + * }] |
| 49 | + * } |
| 50 | + */ |
| 51 | + const newAppliedVariants = obj._applied_variants || _applied_variants; |
| 52 | + const newShouldApplyVariant = !!newAppliedVariants |
| 53 | + value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale, {_applied_variants:newAppliedVariants, shouldApplyVariant:newShouldApplyVariant, metaKey: ""}) |
| 54 | + }else if (typeof obj === "object") { |
| 55 | + /** |
| 56 | + * Objects inside Array like modular blocks are handled here |
| 57 | + * { |
| 58 | + * "array": [{ |
| 59 | + * "title": "title", |
| 60 | + * "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}} |
| 61 | + * }] |
| 62 | + * } |
| 63 | + */ |
| 64 | + obj.$ = getTag(obj,`${prefix}.${key}.${index}`, tagsAsObject, locale, {_applied_variants, shouldApplyVariant, metaKey: updatedMetakey}) |
| 65 | + } |
| 66 | + }) |
| 67 | + }else { |
| 68 | + if (value) { |
| 69 | + /** |
| 70 | + * Objects are handled here |
| 71 | + * { |
| 72 | + * "object": { |
| 73 | + * "title": "title", |
| 74 | + * "$": { |
| 75 | + * "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"} |
| 76 | + * } |
| 77 | + * }, |
| 78 | + * } |
| 79 | + */ |
| 80 | + value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale, {_applied_variants, shouldApplyVariant, metaKey: updatedMetakey}) |
| 81 | + } |
| 82 | + } |
| 83 | + /** |
| 84 | + * { |
| 85 | + * "object": { |
| 86 | + * "title": "title", |
| 87 | + * }, |
| 88 | + * "array": ["hello", "world"] |
| 89 | + * "$": { |
| 90 | + * "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"}, |
| 91 | + * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"} |
| 92 | + * } |
| 93 | + * } |
| 94 | + */ |
12 | 95 |
|
13 |
| - switch (typeof value) { |
14 |
| - case "object": |
15 |
| - if (Array.isArray(value)) { |
16 |
| - value.forEach((obj, index) => { |
17 |
| - const childKey = `${key}__${index}` |
18 |
| - const parentKey = `${key}__parent` |
19 |
| - /** |
20 |
| - * Indexes of array are handled here |
21 |
| - * { |
22 |
| - * "array": ["hello", "world"], |
23 |
| - * "$": { |
24 |
| - * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"} |
25 |
| - * "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"} |
26 |
| - * "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"} |
27 |
| - * } |
28 |
| - * } |
29 |
| - */ |
30 |
| - tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject) |
31 |
| - tags[parentKey] = getParentTagsValue(`${prefix}.${key}`, tagsAsObject) |
32 |
| - if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) { |
33 |
| - /** |
34 |
| - * References are handled here |
35 |
| - * { |
36 |
| - * "reference": [{ |
37 |
| - * "title": "title", |
38 |
| - * "uid": "ref_uid", |
39 |
| - * "_content_type_uid": "ref_content_type_uid", |
40 |
| - * "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}} |
41 |
| - * }] |
42 |
| - * } |
43 |
| - */ |
44 |
| - value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale) |
45 |
| - }else if (typeof obj === "object") { |
46 |
| - /** |
47 |
| - * Objects inside Array like modular blocks are handled here |
48 |
| - * { |
49 |
| - * "array": [{ |
50 |
| - * "title": "title", |
51 |
| - * "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}} |
52 |
| - * }] |
53 |
| - * } |
54 |
| - */ |
55 |
| - obj.$ = getTag(obj,`${prefix}.${key}.${index}`, tagsAsObject, locale) |
56 |
| - } |
57 |
| - }) |
58 |
| - }else { |
59 |
| - if (value) { |
60 |
| - /** |
61 |
| - * Objects are handled here |
62 |
| - * { |
63 |
| - * "object": { |
64 |
| - * "title": "title", |
65 |
| - * "$": { |
66 |
| - * "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"} |
67 |
| - * } |
68 |
| - * }, |
69 |
| - * } |
70 |
| - */ |
71 |
| - value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale) |
72 |
| - } |
73 |
| - } |
74 |
| - /** |
75 |
| - * { |
76 |
| - * "object": { |
77 |
| - * "title": "title", |
78 |
| - * }, |
79 |
| - * "array": ["hello", "world"] |
80 |
| - * "$": { |
81 |
| - * "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"}, |
82 |
| - * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"} |
83 |
| - * } |
84 |
| - * } |
85 |
| - */ |
86 |
| - tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject) |
87 |
| - break; |
88 |
| - default: |
89 |
| - /** |
90 |
| - * All primitive values are handled here |
91 |
| - * { |
92 |
| - * "title": "title", |
93 |
| - * "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}} |
94 |
| - * } |
95 |
| - */ |
96 |
| - tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject) |
97 |
| - } |
98 |
| - }) |
99 |
| - return tags |
100 |
| -} |
101 |
| - |
102 |
| -function getTagsValue (dataValue:string, tagsAsObject: boolean): any { |
| 96 | + tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey }) |
| 97 | + break; |
| 98 | + default: |
| 99 | + /** |
| 100 | + * All primitive values are handled here |
| 101 | + * { |
| 102 | + * "title": "title", |
| 103 | + * "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}} |
| 104 | + * } |
| 105 | + */ |
| 106 | + tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey }) |
| 107 | + } |
| 108 | + }) |
| 109 | + return tags |
| 110 | + } |
| 111 | + |
| 112 | +function getTagsValue (dataValue:string, tagsAsObject: boolean, appliedVariants: {_applied_variants: {[key: string]: any}, shouldApplyVariant: boolean, metaKey: string}): any { |
| 113 | + if(appliedVariants.shouldApplyVariant && appliedVariants._applied_variants && appliedVariants._applied_variants[appliedVariants.metaKey]) { |
| 114 | + const variant = appliedVariants._applied_variants[appliedVariants.metaKey] |
| 115 | + // Adding v2 prefix to the cslp tag. New cslp tags are in v2 format. ex: v2:content_type_uid.entry_uid.locale.title |
| 116 | + const newDataValueArray = ('v2:' + dataValue).split('.'); |
| 117 | + newDataValueArray[1] = newDataValueArray[1] + '_' + variant; |
| 118 | + dataValue = newDataValueArray.join('.'); |
| 119 | + } |
103 | 120 | if (tagsAsObject) {
|
104 | 121 | return { "data-cslp": dataValue };
|
105 | 122 | } else {
|
|
0 commit comments