Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jexl): add cross-form jexl resolution #154

Merged
merged 3 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions addon/lib/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export default EmberObject.extend({
this.set(this._valueKey, value);
}

next(this, () =>
this.document.trigger("valueChanged", this.question.slug, value)
);
next(this, () => this.field.trigger("valueChanged", value));

return value;
}
Expand Down
50 changes: 32 additions & 18 deletions addon/lib/document.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import EmberObject, { computed } from "@ember/object";
import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
import Evented, { on } from "@ember/object/evented";
import Field from "ember-caluma/lib/field";
import jexl from "jexl";
import { atob } from "ember-caluma/helpers/atob";
Expand All @@ -15,7 +14,7 @@ const STATE_PRECEDENCE = ["invalid", "unfinished", "untouched", "valid"];
*
* @class Document
*/
export default EmberObject.extend(Evented, {
export default EmberObject.extend({
documentStore: service(),

async init() {
Expand Down Expand Up @@ -70,20 +69,44 @@ export default EmberObject.extend(Evented, {
questionJexl: computed(function() {
const questionJexl = new jexl.Jexl();

questionJexl.addTransform("answer", slug => this.findAnswer(slug));
questionJexl.addTransform("answer", slugWithPath =>
this.findAnswer(slugWithPath)
);

return questionJexl;
}),

findAnswer(slug) {
const result = this.findField(slug);
findAnswer(slugWithPath) {
const result = this.findField(slugWithPath);
return result && result.answer.value;
},

findField(slug) {
// Here it would be possible to extend the search range
// over the entire tree by calling findFieldInTree()
return this.fields.find(field => field.question.slug === slug);
findField(slugWithPath) {
const segments = slugWithPath.split(".");
const slug = segments.pop();
const doc = this.resolveDocument(segments);
return doc && doc.fields.find(field => field.question.slug === slug);
},

resolveDocument(segments) {
if (!segments) {
return this;
}
let _document = this;
for (let segment of segments) {
if (segment === "parent") {
_document = _document.parentDocument;
} else {
const formField = _document.fields.find(
field => field.question.slug === segment
);
if (!formField) {
return null;
}
_document = formField.childDocument;
}
}
return _document;
},

fields: computed(() => []).readOnly(),
Expand Down Expand Up @@ -130,14 +153,5 @@ export default EmberObject.extend(Evented, {
return STATE_PRECEDENCE.find(state =>
[this.get("childState"), this.get("ownState")].includes(state)
);
}),

updateHidden: on("valueChanged", "hiddenChanged", function(slug) {
const dependentFields = this.fields.filter(field =>
field.question.dependsOn.includes(slug)
);

// update hidden state of those fields
dependentFields.forEach(field => field.question.hiddenTask.perform());
})
});
14 changes: 13 additions & 1 deletion addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { camelize } from "@ember/string";
import { task } from "ember-concurrency";
import { all } from "rsvp";
import { validate } from "ember-validators";
import Evented, { on } from "@ember/object/evented";

import Answer from "ember-caluma/lib/answer";
import Question from "ember-caluma/lib/question";
Expand All @@ -33,7 +34,7 @@ const TYPE_MAP = {
*
* @class Field
*/
export default EmberObject.extend({
export default EmberObject.extend(Evented, {
saveDocumentFloatAnswerMutation,
saveDocumentIntegerAnswerMutation,
saveDocumentStringAnswerMutation,
Expand Down Expand Up @@ -101,6 +102,7 @@ export default EmberObject.extend({

this.setProperties({
_errors: [],
dependentFields: [],
question,
answer
});
Expand All @@ -118,6 +120,16 @@ export default EmberObject.extend({
return `Document:${this.document.id}:Question:${this.question.slug}`;
}).readOnly(),

updateHidden: on("valueChanged", "hiddenChanged", function() {
this.dependentFields.forEach(field => field.question.hiddenTask.perform());
}),

registerDependentField(field) {
if (!this.dependentFields.find(f => f.id === field.id)) {
this.dependentFields.push(field);
}
},

/**
* Whether the field is valid.
*
Expand Down
43 changes: 24 additions & 19 deletions addon/lib/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { next } from "@ember/runloop";
import { lastValue } from "ember-caluma/utils/concurrency";
import { getAST, getTransforms } from "ember-caluma/utils/jexl";
import { task } from "ember-concurrency";
import { assert } from "@ember/debug";
// import { findFieldInTree } from "ember-caluma/utils/tree";

/**
* Object which represents a question in context of a field
Expand Down Expand Up @@ -43,13 +41,24 @@ export default EmberObject.extend({
.map(transform => transform.subject.value)
)
];
dependents.forEach(slug => {
assert(
`Field "${slug}" is not present in this document`,
this.document.findField(slug)
);

return dependents.map(slugWithPath => {
const field = this.document.findField(slugWithPath);
if (!field) {
if (slugWithPath.includes(".")) {
const path = slugWithPath.split(".");
const slug = path.pop();
throw new Error(
`Field "${slug}" is not present in document "${path}"`
);
}
throw new Error(
`Field "${slugWithPath}" is not present in this document`
);
}
field.registerDependentField(this.field);
return field;
});
return dependents;
}).readOnly(),

/**
Expand All @@ -63,22 +72,18 @@ export default EmberObject.extend({
* @return {Boolean}
*/
hiddenTask: task(function*() {
let hidden = this.document.fields
.filter(field => this.dependsOn.includes(field.question.slug))
.some(
field =>
field.question.hidden ||
field.answer.value === null ||
field.answer.value === undefined
);
let hidden = this.dependsOn.some(
field =>
field.question.hidden ||
field.answer.value === null ||
field.answer.value === undefined
);

hidden =
hidden || (yield this.field.document.questionJexl.eval(this.isHidden));

if (this.get("hiddenTask.lastSuccessful.value") !== hidden) {
next(this, () =>
this.document.trigger("hiddenChanged", this.slug, hidden)
);
next(this, () => this.field.trigger("hiddenChanged"));
}

return hidden;
Expand Down
44 changes: 0 additions & 44 deletions addon/utils/tree.js

This file was deleted.

Loading