|
| 1 | +import CryptoJS from "crypto-js"; |
| 2 | + |
| 3 | +import { compareEntitiesInOrderedSetForSorting } from "../entity/set/ordered/utils"; |
| 4 | + |
| 5 | +export const ApplicationContextMixinBuilder = (applicationCls) => (superclass) => class extends superclass { |
| 6 | + constructor(config) { |
| 7 | + super(config); |
| 8 | + if (!applicationCls) throw Error("ApplicationContextMixinBuilder: applicationCls is undefined"); |
| 9 | + this._application = config.context && config.context.application || applicationCls.createDefault(); |
| 10 | + } |
| 11 | + |
| 12 | + get application() { |
| 13 | + return this._application; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +export const MaterialContextMixinBuilder = (materialCls) => (superclass) => class extends superclass { |
| 18 | + |
| 19 | + constructor(config) { |
| 20 | + super(config); |
| 21 | + if (!materialCls) throw Error("MaterialContextMixinBuilder: materialCls is undefined"); |
| 22 | + this._material = config.context && config.context.material; |
| 23 | + if (!this._material) this._material = materialCls.createDefault(); |
| 24 | + this.updateMaterialHash(); |
| 25 | + } |
| 26 | + |
| 27 | + get isEditedIsSetToFalseOnMaterialUpdate() { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + updateMaterialHash() { |
| 32 | + if (this.isEditedIsSetToFalseOnMaterialUpdate) this.isEdited = false; |
| 33 | + this.extraData = {materialHash: this.material.hash}; |
| 34 | + } |
| 35 | + |
| 36 | + // Workaround: Material.createDefault() used to initiate workflow reducer and hence here too |
| 37 | + // does not have an id. Here we catch when such material is used and avoid resetting isEdited |
| 38 | + get isMaterialCreatedDefault() { |
| 39 | + return !Boolean(this.material.id); |
| 40 | + } |
| 41 | + |
| 42 | + get isMaterialUpdated() { |
| 43 | + return Boolean(this.extraData && (this.extraData.materialHash !== this.material.hash)); |
| 44 | + } |
| 45 | + |
| 46 | + get material() { |
| 47 | + return this._material; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +export const MaterialsSetContextMixin = (superclass) => class extends superclass { |
| 52 | + |
| 53 | + constructor(config) { |
| 54 | + super(config); |
| 55 | + this._materialsSet = this.config.context && this.config.context.materialsSet; |
| 56 | + } |
| 57 | + |
| 58 | + get materialsSet() {return this._materialsSet} |
| 59 | + |
| 60 | + sortMaterialsByIndexInSet(materials = []) { |
| 61 | + // DO NOT SORT IN PLACE AS IT CHANGES THE ORDER IN `this.materials` AND HAS SIDE EFFECTS (MaterialViewer). |
| 62 | + return materials.concat().sort((a, b) => compareEntitiesInOrderedSetForSorting(a, b, this.materialsSet._id, false)); |
| 63 | + } |
| 64 | + |
| 65 | +}; |
| 66 | + |
| 67 | +export const MaterialsContextMixinBuilder = (materialCls) => (superclass) => class extends superclass { |
| 68 | + |
| 69 | + constructor(config) { |
| 70 | + super(config); |
| 71 | + const materials = this.config.context && this.config.context.materials; |
| 72 | + if (!materialCls) throw Error("MaterialsContextMixinBuilder: materialCls is undefined"); |
| 73 | + this._materials = (materials && materials.length) ? materials : [materialCls.createDefault()]; |
| 74 | + } |
| 75 | + |
| 76 | + get materials() {return this._materials} |
| 77 | +}; |
| 78 | + |
| 79 | +export const MethodDataContextMixin = (superclass) => class extends superclass { |
| 80 | + |
| 81 | + constructor(config) { |
| 82 | + super(config); |
| 83 | + this._methodData = config.context && config.context.methodData || {}; |
| 84 | + this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) |
| 85 | + } |
| 86 | + |
| 87 | + /* @summary Replace the logic in constructor with this in order to enable passing `methodDataHash` between |
| 88 | + * subsequent initializations of the derived class. Not used at present and kept for the record. |
| 89 | + */ |
| 90 | + _initMethodDataHash() { |
| 91 | + this.methodDataHash = CryptoJS.MD5(JSON.stringify(this.methodData)).toString(); |
| 92 | + this.extraData = {methodDataHash: this.methodDataHash}; |
| 93 | + if (!this._methodData) { |
| 94 | + this._methodData = {}; |
| 95 | + this.isEdited = false; |
| 96 | + // Commented out to reduce effect on performance. Uncomment for debugging purposes. |
| 97 | + // TODO: remove on next refactoring or convert to log |
| 98 | + // console.warn("MethodDataContextMixin: methodData is undefined or null"); |
| 99 | + } else { |
| 100 | + if (this.isMethodDataUpdated) { |
| 101 | + this.isEdited = false; |
| 102 | + } else { |
| 103 | + this.isEdited = config.isEdited; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + get methodData() {return this._methodData} |
| 109 | + |
| 110 | + get isMethodDataUpdated() { |
| 111 | + return Boolean(this.extraData && (this.extraData.methodDataHash !== this.methodDataHash)); |
| 112 | + } |
| 113 | + |
| 114 | +}; |
| 115 | + |
| 116 | +export const WorkflowContextMixin = (superclass) => class extends superclass { |
| 117 | + |
| 118 | + constructor(config) { |
| 119 | + super(config); |
| 120 | + this._workflow = config.context && config.context.workflow || {}; |
| 121 | + this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) |
| 122 | + } |
| 123 | + |
| 124 | + get workflow() {return this._workflow} |
| 125 | + |
| 126 | +}; |
| 127 | + |
| 128 | +export const JobContextMixin = (superclass) => class extends superclass { |
| 129 | + |
| 130 | + constructor(config) { |
| 131 | + super(config); |
| 132 | + this._job = config.context && config.context.job || {}; |
| 133 | + this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) |
| 134 | + } |
| 135 | + |
| 136 | + get job() {return this._job} |
| 137 | + |
| 138 | +}; |
0 commit comments