-
Notifications
You must be signed in to change notification settings - Fork 2
/
rubric-utils.ts
189 lines (166 loc) · 6.04 KB
/
rubric-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {PieContent, PieModel} from "./interface";
import {
modelsForPackage,
elementForPackage,
pieShortIdGenerator,
elementsHasPackage,
getPackageWithoutVersion
} from "./utils/utils";
import cloneDeep from "lodash/cloneDeep";
export const COMPLEX_RUBRIC = 'complex-rubric';
/**
* Allows you to modify the markup for a package that is present in elements/model but
* missing from markup.
* @param content the pie content
* @param npmPackage the npm package
* @param template a callback function for modifing the markup
*/
export const addMarkupForPackage = (
content: PieContent,
npmPackage: string,
template: (id, tag: string, markup: string) => string
): PieContent => {
const out = cloneDeep(content);
const elem = out && elementForPackage(out, npmPackage);
if (elem && modelsForPackage(out, npmPackage).length > 0) {
const match = out.markup.match(new RegExp(elem));
if (out.markup !== null && !match) {
const id = out.models && out.models.find(m => m.element === elem).id;
if (id) {
out.markup = template(id, elem, out.markup);
}
}
}
return out;
};
/**
* This function takes the config and configSettings and determines if there should/shouldn't be a complex-rubric in the config
* depending on configSettings (withRubric.forceEnabled) and config (model.rubricEnabled)
* @param content
* @param configSettings
*/
export const complexRubricChecks = (content: PieContent, configSettings = {}) => {
const elements = content.elements || {};
const elementsKeys = Object.keys(elements || {});
// tODO make sure not to use versions (eg: @1.1.0, @latest)
const elementsValues = Object.values(elements || {}).map(elementValue => getPackageWithoutVersion(elementValue));
// complexRubricElements: those elements in config where item type is @pie-element/complex-rubric
const complexRubricElements = elementsKeys.filter(key => elements[key] && elements[key].indexOf(COMPLEX_RUBRIC) >= 0);
// complexRubricItemsLength: how many complex-rubric elements are declared
const complexRubricItemsLength = complexRubricElements.length;
if (complexRubricItemsLength === elementsKeys.length) {
// if item config ONLY has complex-rubrics, then all the steps below are not necessary
// this is added to treat the special case of testing complex-rubric (as single item type) in pie-website
return {};
}
// if at least one model has rubricEnabled = true, then we should have complex-rubric in the config
let shouldHaveComplexRubric = !!(content.models || []).filter(model => model.rubricEnabled).length;
// if there's at least one configuration where withRubric has forceEnabled: true
const shouldForceEnableComplexRubric = configuration => configuration && configuration.withRubric && configuration.withRubric.forceEnabled;
let shouldHaveForcedComplexRubric = !!(elementsValues.filter(elementsKey => shouldForceEnableComplexRubric(configSettings[elementsKey])).length);
shouldHaveComplexRubric = shouldHaveComplexRubric || shouldHaveForcedComplexRubric;
return {
shouldAddComplexRubric: shouldHaveComplexRubric && !complexRubricItemsLength,
shouldRemoveComplexRubric: !shouldHaveComplexRubric && complexRubricItemsLength,
complexRubricElements
}
}
/**
* Removes complex-rubric html from markup.
*/
export const removeComplexRubricFromMarkup = (content: PieContent, complexRubricElements: string[], doc): { markupWithoutComplexRubric: string, deletedComplexRubricItemIds: string[] } => {
const tempDiv = doc.createElement("div");
tempDiv.innerHTML = content.markup;
const elsWithId = tempDiv.querySelectorAll("[id]");
const deletedComplexRubricItemIds = [];
elsWithId.forEach(el => {
const pieElName = el.tagName.toLowerCase().split("-config")[0];
// we have to remove the complex-rubric item from the markup
if (complexRubricElements.includes(pieElName)) {
try {
deletedComplexRubricItemIds.push(el.id);
tempDiv.querySelector(`#${el.id}`).remove();
} catch (e) {
console.log(e.toString());
}
}
});
const newMarkup = tempDiv.innerHTML;
tempDiv.remove();
return {
markupWithoutComplexRubric: newMarkup,
deletedComplexRubricItemIds
};
}
/**
* Adds complex-rubric html to markup.
* @param content
*/
export const addComplexRubric = (content: PieContent): PieContent => {
return addMarkupForPackage(
cloneDeep(content),
`@pie-element/${COMPLEX_RUBRIC}`,
(id, tag, markup) => {
return `${markup}<${tag} id="${id}"></${tag}>`;
}
);
};
/**
* Adds rubric html to markup.
* @param content
*/
export const addRubric = (content: PieContent): PieContent => {
return addMarkupForPackage(
cloneDeep(content),
"@pie-element/rubric",
(id, tag, markup) => {
return `
${markup}
<div style="width: 75%">
<${tag} id="${id}"></${tag}>
</div>
`;
}
);
};
/**
* Adds multi-trait-rubric html to markup.
* @param content
*/
export const addMultiTraitRubric = (content: PieContent): PieContent => {
return addMarkupForPackage(
cloneDeep(content),
"@pie-element/multi-trait-rubric",
(id, tag, markup) => {
return `
${markup}
<div style="margin-top: 20px;">
<${tag} id="${id}"></${tag}>
</div>
`;
}
);
};
/**
* Adds the provided package to the provided PieContent Object's `elements` and `models` properties.
*
* @param content the PieContent for rendering
* @param packageToAdd the NPM Package to add to the content config
* @param model optional the PieModel to add, `id` and `element` properties will be replaced by this function if present
*/
export const addPackageToContent = (
content: PieContent,
packageToAdd,
model?: PieModel
) => {
if (packageToAdd && !elementsHasPackage(content.elements, packageToAdd)) {
model = model ? model : ({} as any);
// add package
model.id = pieShortIdGenerator();
const elementName = pieShortIdGenerator();
model.element = elementName;
content.models && content.models.push(model);
content.elements && (content.elements[elementName] = packageToAdd);
return content;
}
};