|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | +import { LogCreationModel } from './LogCreationModel.js'; |
| 14 | +import { OnCallLogTemplate } from './OnCallLogTemplate.js'; |
| 15 | + |
| 16 | +/** |
| 17 | + * @typedef OnCallLogTemplateFormData |
| 18 | + * @property {string} shortDescription |
| 19 | + * @property {string} detectorOrSubsystem |
| 20 | + * @property {string} severity |
| 21 | + * @property {string} scope |
| 22 | + * @property {string} shifterName |
| 23 | + * @property {string} shifterPosition |
| 24 | + * @property {string} lhcBeamMode |
| 25 | + * @property {string} issueDescription |
| 26 | + * @property {string} reason |
| 27 | + * @property {string} alreadyTakenActions |
| 28 | + */ |
| 29 | + |
| 30 | +// Only one template for now |
| 31 | +/** |
| 32 | + * @typedef {OnCallLogTemplate} LogTemplate |
| 33 | + */ |
| 34 | + |
| 35 | +/** |
| 36 | + * @typedef {'on-call'} logTemplateKey |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * Return a new instance of log template for the given key |
| 41 | + * |
| 42 | + * @param {logTemplateKey} key the template key |
| 43 | + * @return {LogTemplate|null} the new log template |
| 44 | + */ |
| 45 | +const logTemplatesFactory = (key) => { |
| 46 | + const templateClass = { ['on-call']: OnCallLogTemplate }[key] ?? null; |
| 47 | + if (templateClass) { |
| 48 | + return new templateClass(); |
| 49 | + } |
| 50 | + return null; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Log creation model based on templates |
| 55 | + */ |
| 56 | +export class TemplatedLogCreationModel extends LogCreationModel { |
| 57 | + /** |
| 58 | + * Constructor |
| 59 | + * |
| 60 | + * @param {function} [onCreation] function called when log is created, with the id of the created log |
| 61 | + * @param {LogCreationRelations} relations the relations of the log |
| 62 | + */ |
| 63 | + constructor(onCreation, relations) { |
| 64 | + super(onCreation, relations); |
| 65 | + |
| 66 | + /** |
| 67 | + * @type {logTemplateKey|null} |
| 68 | + * @private |
| 69 | + */ |
| 70 | + this._templateKey = null; |
| 71 | + |
| 72 | + /** |
| 73 | + * @type {LogTemplate|null} |
| 74 | + * @private |
| 75 | + */ |
| 76 | + this._templateModel = null; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Defines the template to use, defined by its key |
| 81 | + * |
| 82 | + * @param {logTemplateKey|null} key the key of the template to use (there may be no model for the given key) |
| 83 | + * @return {void} |
| 84 | + */ |
| 85 | + useTemplate(key) { |
| 86 | + const templateModel = logTemplatesFactory(key); |
| 87 | + if (templateModel) { |
| 88 | + templateModel.bubbleTo(this); |
| 89 | + } |
| 90 | + this._templateModel = templateModel; |
| 91 | + this._templateKey = key; |
| 92 | + this.notify(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Set the current template key |
| 97 | + * |
| 98 | + * @return {logTemplateKey} the current key |
| 99 | + */ |
| 100 | + get templateKey() { |
| 101 | + return this._templateKey; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Return the current template model |
| 106 | + * |
| 107 | + * @return {LogTemplate|null} the template model |
| 108 | + */ |
| 109 | + get templateModel() { |
| 110 | + return this._templateModel; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * States if the log creation is valid |
| 115 | + * |
| 116 | + * @return {boolean} true if the form is valid |
| 117 | + */ |
| 118 | + get isValid() { |
| 119 | + return this._templateModel?.isValid ?? super.isValid; |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * Return log properties generated by the current template if it exists, else return the parent's property |
| 124 | + */ |
| 125 | + |
| 126 | + // eslint-disable-next-line valid-jsdoc |
| 127 | + /** |
| 128 | + * @inheritDoc |
| 129 | + */ |
| 130 | + get title() { |
| 131 | + return this._templateModel?.title ?? super.title; |
| 132 | + } |
| 133 | + |
| 134 | + // eslint-disable-next-line valid-jsdoc |
| 135 | + /** |
| 136 | + * @inheritDoc |
| 137 | + */ |
| 138 | + set title(title) { |
| 139 | + super.title = title; |
| 140 | + } |
| 141 | + |
| 142 | + // eslint-disable-next-line valid-jsdoc |
| 143 | + /** |
| 144 | + * @inheritDoc |
| 145 | + */ |
| 146 | + get text() { |
| 147 | + return this._templateModel?.text ?? super.text; |
| 148 | + } |
| 149 | + |
| 150 | + // eslint-disable-next-line valid-jsdoc |
| 151 | + /** |
| 152 | + * @inheritDoc |
| 153 | + */ |
| 154 | + set text(text) { |
| 155 | + super.text = text; |
| 156 | + } |
| 157 | +} |
0 commit comments