Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 100 additions & 117 deletions src/entry-editable.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,105 @@
import { EntryModel } from "."
import { EntryModel } from ".";

export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us') : void {
if (entry) {
const appliedVariants = entry._applied_variants || null;
entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale, {_applied_variants: appliedVariants, shouldApplyVariant: !!appliedVariants, metaKey: ''}) }
}

function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string, appliedVariants: {_applied_variants:{[key: string]: any}, shouldApplyVariant: boolean, metaKey: string}): object {
const tags: any = {}
const { metaKey, shouldApplyVariant, _applied_variants } = appliedVariants
Object.entries(content).forEach(([key, value]) => {
if (key === '$') return
let metaUID = value && typeof value === 'object' && value._metadata && value._metadata.uid ? value._metadata.uid : '';
let updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '' }${key}` : '';
if(metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID;
switch (typeof value) {
case "object":
if (Array.isArray(value)) {
value.forEach((obj, index) => {
const childKey = `${key}__${index}`
const parentKey = `${key}__parent`
metaUID = value && typeof value === 'object' && obj._metadata && obj._metadata.uid ? obj._metadata.uid : '';
updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '' }${key}` : '';
if(metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID;
/**
* Indexes of array are handled here
* {
* "array": ["hello", "world"],
* "$": {
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"}
* "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"}
* }
* }
*/
tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
tags[parentKey] = getParentTagsValue(`${prefix}.${key}`, tagsAsObject)
if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) {
/**
* References are handled here
* {
* "reference": [{
* "title": "title",
* "uid": "ref_uid",
* "_content_type_uid": "ref_content_type_uid",
* "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}}
* }]
* }
*/
const newAppliedVariants = obj._applied_variants || _applied_variants;
const newShouldApplyVariant = !!newAppliedVariants
value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale, {_applied_variants:newAppliedVariants, shouldApplyVariant:newShouldApplyVariant, metaKey: ""})
}else if (typeof obj === "object") {
/**
* Objects inside Array like modular blocks are handled here
* {
* "array": [{
* "title": "title",
* "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}}
* }]
* }
*/
obj.$ = getTag(obj,`${prefix}.${key}.${index}`, tagsAsObject, locale, {_applied_variants, shouldApplyVariant, metaKey: updatedMetakey})
}
})
}else {
if (value) {
/**
* Objects are handled here
* {
* "object": {
* "title": "title",
* "$": {
* "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"}
* }
* },
* }
*/
value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale, {_applied_variants, shouldApplyVariant, metaKey: updatedMetakey})
}
}
/**
* {
* "object": {
* "title": "title",
* },
* "array": ["hello", "world"]
* "$": {
* "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"},
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* }
* }
*/
export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us') : void {
if (entry)
entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale)
}

tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
break;
default:
/**
* All primitive values are handled here
* {
* "title": "title",
* "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}}
* }
*/
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
}
})
return tags
}

function getTagsValue (dataValue:string, tagsAsObject: boolean, appliedVariants: {_applied_variants: {[key: string]: any}, shouldApplyVariant: boolean, metaKey: string}): any {
if(appliedVariants.shouldApplyVariant && appliedVariants._applied_variants && appliedVariants._applied_variants[appliedVariants.metaKey]) {
const variant = appliedVariants._applied_variants[appliedVariants.metaKey]
// Adding v2 prefix to the cslp tag. New cslp tags are in v2 format. ex: v2:content_type_uid.entry_uid.locale.title
const newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
dataValue = newDataValueArray.join('.');
}
function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string): object {
const tags: any = {}
Object.entries(content).forEach(([key, value]) => {
if (key === '$') return

switch (typeof value) {
case "object":
if (Array.isArray(value)) {
value.forEach((obj, index) => {
const childKey = `${key}__${index}`
const parentKey = `${key}__parent`
/**
* Indexes of array are handled here
* {
* "array": ["hello", "world"],
* "$": {
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"}
* "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"}
* }
* }
*/
tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject)
tags[parentKey] = getParentTagsValue(`${prefix}.${key}`, tagsAsObject)
if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) {
/**
* References are handled here
* {
* "reference": [{
* "title": "title",
* "uid": "ref_uid",
* "_content_type_uid": "ref_content_type_uid",
* "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}}
* }]
* }
*/
value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale)
}else if (typeof obj === "object") {
/**
* Objects inside Array like modular blocks are handled here
* {
* "array": [{
* "title": "title",
* "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}}
* }]
* }
*/
obj.$ = getTag(obj,`${prefix}.${key}.${index}`, tagsAsObject, locale)
}
})
}else {
if (value) {
/**
* Objects are handled here
* {
* "object": {
* "title": "title",
* "$": {
* "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"}
* }
* },
* }
*/
value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale)
}
}
/**
* {
* "object": {
* "title": "title",
* },
* "array": ["hello", "world"]
* "$": {
* "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"},
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* }
* }
*/
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject)
break;
default:
/**
* All primitive values are handled here
* {
* "title": "title",
* "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}}
* }
*/
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject)
}
})
return tags
}

function getTagsValue (dataValue:string, tagsAsObject: boolean): any {
if (tagsAsObject) {
return { "data-cslp": dataValue };
} else {
Expand Down
Loading