|
| 1 | +import { deepClone } from "../../utils/clone"; |
| 2 | + |
| 3 | +export const ContextAndRenderFieldsMixin = (superclass) => { |
| 4 | + return class extends superclass { |
| 5 | + constructor(config) { |
| 6 | + super(config); |
| 7 | + this._context = config.context || {}; |
| 8 | + } |
| 9 | + |
| 10 | + // in-memory, or "volatile" context that is reset when the `parent` object is destroyed |
| 11 | + get context() { |
| 12 | + return this._context; |
| 13 | + } |
| 14 | + |
| 15 | + set context(newContext) { |
| 16 | + this._context = newContext; |
| 17 | + } |
| 18 | + |
| 19 | + updateContext(ctx = {}, executeRender = false) { |
| 20 | + this._context = Object.assign({}, this.context, ctx); |
| 21 | + executeRender && this.render(); |
| 22 | + } |
| 23 | + |
| 24 | + // to get "persistent" context, that is stored in database and further should be provided to constructor |
| 25 | + // when the `parent` object is re-created |
| 26 | + getPersistentContext() { |
| 27 | + return this.prop("context"); |
| 28 | + } |
| 29 | + |
| 30 | + // to make context persistent in `_json` |
| 31 | + updatePersistentContext(ctx = {}) { |
| 32 | + this.setProp("context", Object.assign({}, ctx)); |
| 33 | + } |
| 34 | + |
| 35 | + // to get persistent and volatile context combined |
| 36 | + getCombinedContext() { |
| 37 | + return Object.assign({}, this.getPersistentContext(), this.context); |
| 38 | + } |
| 39 | + |
| 40 | + // override in subclasses |
| 41 | + render(context = this.context) { |
| 42 | + throw new Error("RenderInitMixin: render not implemented in derived class"); |
| 43 | + } |
| 44 | + }; |
| 45 | +}; |
| 46 | + |
| 47 | +/* |
| 48 | + * @summary Handles logic for domain-specific context, eg. "important settings". |
| 49 | + * Important settings are stored inside "important" property and have context providers associated with it. |
| 50 | + */ |
| 51 | +export const DomainContextProviderMixin = (superclass) => { |
| 52 | + return class extends superclass { |
| 53 | + constructor(config) { |
| 54 | + super(config); |
| 55 | + this._contextProviders = []; |
| 56 | + } |
| 57 | + |
| 58 | + get contextProviders() { |
| 59 | + // override in children |
| 60 | + return this._contextProviders; |
| 61 | + } |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export const ImportantSettingsProviderMixin = (superclass) => { |
| 66 | + return class extends DomainContextProviderMixin(superclass) { |
| 67 | + get important() { |
| 68 | + return deepClone(this._json.important || {}); |
| 69 | + } |
| 70 | + |
| 71 | + setImportant(key, value) { |
| 72 | + this.setProp("important", {[key]: value}) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return {JSONSchemaFormDataProvider[]} |
| 77 | + */ |
| 78 | + get importantSettingsProviders() { |
| 79 | + return this.contextProviders.filter(p => p.domain === "important"); |
| 80 | + } |
| 81 | + |
| 82 | + get isImportantEdited() { |
| 83 | + return this.prop("important.isEdited"); |
| 84 | + } |
| 85 | + |
| 86 | + set isImportantEdited(bool) { |
| 87 | + this.setProp("important", Object.assign(this.important, {isEdited: bool})); |
| 88 | + } |
| 89 | + }; |
| 90 | +}; |
0 commit comments