Skip to content

Commit 4993a20

Browse files
author
Etienne Laurent
committed
fix conditional fields in image editor
1 parent 6b4ee72 commit 4993a20

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

modules/@apostrophecms/image/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,19 @@ module.exports = {
104104
fileGroup: 'images',
105105
required: true
106106
},
107+
showAlt: {
108+
type: 'boolean',
109+
label: 'Show Alt?',
110+
def: false
111+
},
107112
alt: {
108113
type: 'string',
109114
label: 'apostrophe:altText',
110-
help: 'apostrophe:altTextHelp'
115+
help: 'apostrophe:altTextHelp',
116+
if: {
117+
'getOrdersCount()': 10,
118+
showAlt: true
119+
}
111120
},
112121
credit: {
113122
type: 'string',
@@ -131,6 +140,7 @@ module.exports = {
131140
fields: [
132141
'attachment',
133142
'title',
143+
'showAlt',
134144
'alt',
135145
'_tags',
136146
'credit',
@@ -399,6 +409,12 @@ module.exports = {
399409
},
400410
methods(self) {
401411
return {
412+
async getOrdersCount(req, { docId }) {
413+
// simulates API response takes 500ms
414+
await new Promise(resolve => setTimeout(resolve, 2500));
415+
return 9;
416+
},
417+
402418
// This method is available as a template helper: apos.image.first
403419
//
404420
// Find the first image attachment referenced within an object that may

modules/@apostrophecms/image/ui/apos/components/AposMediaManagerEditor.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
:trigger-validation="triggerValidation"
8888
:doc-id="docFields.data._id"
8989
:following-values="followingValues()"
90+
:conditional-fields="conditionalFields"
9091
:server-errors="serverErrors"
9192
@validate="triggerValidate"
9293
@reset="$emit('modified', false)"
@@ -231,6 +232,7 @@ export default {
231232
'docFields.data': {
232233
deep: true,
233234
handler(newData, oldData) {
235+
this.evaluateConditions();
234236
this.$nextTick(() => {
235237
// If either old or new state are an empty object, it's not
236238
// "modified."
@@ -252,13 +254,14 @@ export default {
252254
}
253255
}
254256
},
255-
media(newVal) {
256-
this.updateActiveDoc(newVal);
257+
async media(newVal) {
258+
await this.updateActiveDoc(newVal);
257259
}
258260
},
259-
mounted() {
261+
async mounted() {
260262
this.generateLipKey();
261263
this.$emit('modified', false);
264+
262265
},
263266
methods: {
264267
moreMenuHandler(item) {
@@ -271,6 +274,8 @@ export default {
271274
this.restoreOnly = !!this.activeMedia.archived;
272275
this.original = klona(newMedia);
273276
this.docFields.data = klona(newMedia);
277+
this.evaluateConditions();
278+
await this.evaluateExternalConditions();
274279
this.generateLipKey();
275280
await this.unlock();
276281
// Distinguish between an actual doc and an empty placeholder
@@ -402,7 +407,7 @@ export default {
402407
}
403408
404409
await this.cancel();
405-
this.updateActiveDoc(this.activeMedia);
410+
await this.updateActiveDoc(this.activeMedia);
406411
}
407412
apos.bus.$emit('admin-menu-click', {
408413
itemName: '@apostrophecms/i18n:localize',

modules/@apostrophecms/modal/ui/apos/mixins/AposEditorMixin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default {
7878
async evaluateExternalConditions() {
7979
this.externalConditionsResults = await evaluateExternalConditions(
8080
this.schema,
81-
this.docId || this.docFields?.data?._docId,
81+
this.docId || this.docFields?.data?._docId || this.docFields?.data?._id,
8282
this.$t
8383
);
8484
},
@@ -162,7 +162,10 @@ export default {
162162
},
163163

164164
evaluateConditions() {
165+
console.log('evaluateConditions', this.schema, this.docFields.data, this.externalConditionsResults);
166+
console.trace();
165167
this.conditionalFields = this.getConditionalFields();
168+
console.log('conditionalFields', this.conditionalFields);
166169
},
167170

168171
// Overridden by components that split the fields into several AposSchemas

modules/@apostrophecms/schema/ui/apos/lib/conditionalFields.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ export const getConditionTypesObject = () => Object
1414
// `docId` - the current docId (from prop or context)
1515
// `$t` - the i18n function (usually `this.$t`)
1616
export async function evaluateExternalConditions(schema, docId, $t) {
17+
console.log('evaluateExternalConditions', schema, docId);
1718
const externalConditionsResults = getConditionTypesObject();
1819

1920
for (const field of schema) {
2021
for (const conditionType of conditionTypes) {
22+
console.log(field[conditionType]);
2123
if (field[conditionType]) {
2224
const externalConditionKeys = Object
2325
.entries(field[conditionType])
2426
.flatMap((entry) => getExternalConditionKeys(entry, conditionType))
2527
.filter(Boolean);
2628

2729
const uniqExternalConditionKeys = [ ...new Set(externalConditionKeys) ];
30+
console.log('uniqExternalConditionKeys', uniqExternalConditionKeys);
2831

2932
try {
3033
const promises = uniqExternalConditionKeys
@@ -51,6 +54,7 @@ export async function evaluateExternalConditions(schema, docId, $t) {
5154
}
5255
}
5356
}
57+
console.log('externalConditionsResults', externalConditionsResults);
5458
return externalConditionsResults;
5559

5660
function getExternalConditionKeys([ key, val ], conditionType) {
@@ -66,6 +70,7 @@ export async function evaluateExternalConditions(schema, docId, $t) {
6670
}
6771

6872
export async function evaluateExternalCondition(conditionKey, fieldId, docId) {
73+
console.log('XHR');
6974
const { result } = await apos.http.get(
7075
`${apos.schema.action}/evaluate-external-condition`,
7176
{
@@ -115,6 +120,7 @@ export function getConditionalFields(
115120
values,
116121
externalConditionsResults
117122
) {
123+
console.log('getConditionalFields', schema, values, externalConditionsResults);
118124
const result = getConditionTypesObject();
119125

120126
for (const field of schema) {
@@ -130,6 +136,7 @@ export function getConditionalFields(
130136
}
131137
}
132138

139+
console.log('result', result);
133140
return result;
134141

135142
// Handle external conditions as a voter function.

0 commit comments

Comments
 (0)