mirror of
https://github.com/nasa/openmct.git
synced 2025-03-10 14:34:08 +00:00
Refactoring conditionals
Move logic for conditions into conditionManager out of conditionCollection.vue
This commit is contained in:
parent
50b331c451
commit
6ab84c0bc3
207
src/plugins/condition/ConditionManager.js
Normal file
207
src/plugins/condition/ConditionManager.js
Normal file
@ -0,0 +1,207 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2020, United States Government
|
||||
* as represented by the Administrator of the National Aeronautics and Space
|
||||
* Administration. All rights reserved.
|
||||
*
|
||||
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
* Open MCT includes source code licensed under additional open source
|
||||
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||
* this source code distribution or the Licensing information page available
|
||||
* at runtime from the About dialog for additional information.
|
||||
*****************************************************************************/
|
||||
|
||||
import Condition from "./Condition";
|
||||
import uuid from "uuid";
|
||||
import * as EventEmitter from 'eventemitter3';
|
||||
|
||||
export default class ConditionManager extends EventEmitter {
|
||||
constructor(domainObject, openmct) {
|
||||
super();
|
||||
this.domainObject = domainObject;
|
||||
this.openmct = openmct;
|
||||
this.instantiate = this.openmct.$injector.get('instantiate');
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
initialize() {
|
||||
this.openmct.objects.get(this.domainObject.identifier).then((obj) => {
|
||||
this.observeForChanges(obj);
|
||||
this.conditionCollection = [];
|
||||
if (this.domainObject.configuration.conditionCollection.length) {
|
||||
this.domainObject.configuration.conditionCollection.forEach((conditionConfigurationId, index) => {
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.initCondition(conditionConfiguration, index)
|
||||
});
|
||||
});
|
||||
} else {
|
||||
this.addCondition(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
observeForChanges(domainObject) {
|
||||
//TODO: Observe only the conditionCollection property instead of the whole domainObject
|
||||
this.stopObservingForChanges = this.openmct.objects.observe(domainObject, '*', this.handleConditionCollectionUpdated.bind(this));
|
||||
}
|
||||
|
||||
handleConditionCollectionUpdated(newDomainObject) {
|
||||
let oldConditionIdentifiers = this.domainObject.configuration.conditionCollection.map((conditionConfigurationId) => {
|
||||
return this.openmct.objects.makeKeyString(conditionConfigurationId);
|
||||
});
|
||||
let newConditionIdentifiers = newDomainObject.configuration.conditionCollection.map((conditionConfigurationId) => {
|
||||
return this.openmct.objects.makeKeyString(conditionConfigurationId);
|
||||
});
|
||||
|
||||
this.domainObject = newDomainObject;
|
||||
|
||||
//check for removed conditions
|
||||
oldConditionIdentifiers.forEach((identifier, index) => {
|
||||
if (newConditionIdentifiers.indexOf(identifier) < 0) {
|
||||
let condition = this.conditionCollection[index];
|
||||
if (condition) {
|
||||
this.removeCondition(condition);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let newConditionCount = this.domainObject.configuration.conditionCollection.length - this.conditionCollection.length;
|
||||
|
||||
for (let i = 0; i < newConditionCount; i++) {
|
||||
let conditionConfigurationId = this.domainObject.configuration.conditionCollection[i];
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.initCondition(conditionConfiguration, i);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
initCondition(conditionConfiguration, index) {
|
||||
let condition = new Condition(conditionConfiguration, this.openmct);
|
||||
condition.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
if (index !== undefined) {
|
||||
this.conditionCollection.splice(index + 1, 0, condition);
|
||||
} else {
|
||||
this.conditionCollection.unshift(condition);
|
||||
}
|
||||
}
|
||||
|
||||
createConditionDomainObject(isDefault, conditionConfiguration) {
|
||||
let conditionObj;
|
||||
if (conditionConfiguration) {
|
||||
conditionObj = {
|
||||
...conditionConfiguration,
|
||||
name: `Copy of ${conditionConfiguration.name}`,
|
||||
identifier: {
|
||||
...this.domainObject.identifier,
|
||||
key: uuid()
|
||||
}
|
||||
};
|
||||
} else {
|
||||
conditionObj = {
|
||||
isDefault: isDefault,
|
||||
type: 'condition',
|
||||
name: isDefault ? 'Default' : 'Unnamed Condition',
|
||||
identifier: {
|
||||
...this.domainObject.identifier,
|
||||
key: uuid()
|
||||
},
|
||||
configuration: {
|
||||
name: isDefault ? 'Default' : 'Unnamed Condition',
|
||||
output: 'false',
|
||||
trigger: 'any',
|
||||
criteria: isDefault ? [] : [{
|
||||
telemetry: '',
|
||||
operation: '',
|
||||
input: '',
|
||||
metadata: ''
|
||||
}]
|
||||
},
|
||||
summary: ''
|
||||
};
|
||||
}
|
||||
let conditionDomainObjectKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier);
|
||||
let newDomainObject = this.instantiate(conditionObj, conditionDomainObjectKeyString);
|
||||
|
||||
return newDomainObject.useCapability('adapter');
|
||||
}
|
||||
|
||||
addCondition(isDefault, index) {
|
||||
this.createAndSaveConditionDomainObject(!!isDefault, index);
|
||||
}
|
||||
|
||||
cloneCondition(conditionConfigurationId, index) {
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.createAndSaveConditionDomainObject(false, index, conditionConfiguration);
|
||||
});
|
||||
}
|
||||
|
||||
createAndSaveConditionDomainObject(isDefault, index, conditionConfiguration) {
|
||||
let newConditionDomainObject = this.createConditionDomainObject(isDefault, conditionConfiguration);
|
||||
//persist the condition domain object so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet
|
||||
this.openmct.objects.mutate(newConditionDomainObject, 'created', new Date());
|
||||
if (index !== undefined) {
|
||||
this.domainObject.configuration.conditionCollection.splice(index + 1, 0, newConditionDomainObject.identifier);
|
||||
} else {
|
||||
this.domainObject.configuration.conditionCollection.unshift(newConditionDomainObject.identifier);
|
||||
}
|
||||
this.persist();
|
||||
}
|
||||
|
||||
removeCondition(condition, index) {
|
||||
if (index !== undefined && index > -1) {
|
||||
condition = this.conditionCollection[index];
|
||||
}
|
||||
if (condition) {
|
||||
condition.destroyCriteria();
|
||||
condition.off('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
this.conditionCollection.splice(index, 1);
|
||||
this.domainObject.configuration.conditionCollection.splice(index, 1);
|
||||
this.persist();
|
||||
}
|
||||
}
|
||||
|
||||
//this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]);
|
||||
reorderConditions(reorderPlan) {
|
||||
let oldConditions = Array.from(this.domainObject.configuration.conditionCollection);
|
||||
let newCollection = [];
|
||||
console.log(this.domainObject.configuration.conditionCollection);
|
||||
reorderPlan.forEach((reorderEvent) => {
|
||||
let item = oldConditions[reorderEvent.oldIndex];
|
||||
newCollection.push(item);
|
||||
this.domainObject.configuration.conditionCollection = newCollection;
|
||||
});
|
||||
console.log(this.domainObject.configuration.conditionCollection);
|
||||
this.persist();
|
||||
}
|
||||
|
||||
handleConditionResult(args) {
|
||||
this.emit('conditionResultUpdated', {
|
||||
id: args.id,
|
||||
result: args.data.result
|
||||
})
|
||||
}
|
||||
|
||||
persist() {
|
||||
this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.domainObject.configuration.conditionCollection);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (typeof this.stopObservingForChanges === 'function') {
|
||||
this.stopObservingForChanges();
|
||||
}
|
||||
this.conditionCollection.forEach((condition) => {
|
||||
condition.off('conditionResultUpdated', this.handleConditionResult);
|
||||
condition.destroy();
|
||||
})
|
||||
}
|
||||
}
|
79
src/plugins/condition/ConditionManagerSpec.js
Normal file
79
src/plugins/condition/ConditionManagerSpec.js
Normal file
@ -0,0 +1,79 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2020, United States Government
|
||||
* as represented by the Administrator of the National Aeronautics and Space
|
||||
* Administration. All rights reserved.
|
||||
*
|
||||
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
* Open MCT includes source code licensed under additional open source
|
||||
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||
* this source code distribution or the Licensing information page available
|
||||
* at runtime from the About dialog for additional information.
|
||||
*****************************************************************************/
|
||||
|
||||
import ConditionManager from './ConditionManager';
|
||||
|
||||
describe('ConditionManager', () => {
|
||||
|
||||
let conditionMgr;
|
||||
let testTelemetryObject;
|
||||
let openmct = {};
|
||||
let parentDomainObject;
|
||||
let composition;
|
||||
|
||||
beforeAll(function () {
|
||||
testTelemetryObject = {
|
||||
identifier:{ namespace: "", key: "test-object"},
|
||||
type: "test-object",
|
||||
name: "Test Object",
|
||||
telemetry: {
|
||||
values: [{
|
||||
key: "some-key",
|
||||
name: "Some attribute",
|
||||
hints: {
|
||||
domain: 1
|
||||
}
|
||||
}, {
|
||||
key: "some-other-key",
|
||||
name: "Another attribute",
|
||||
hints: {
|
||||
range: 1
|
||||
}
|
||||
}]
|
||||
}
|
||||
};
|
||||
openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString']);
|
||||
openmct.objects.get.and.returnValue(testTelemetryObject);
|
||||
openmct.objects.makeKeyString.and.returnValue(testTelemetryObject.identifier.key);
|
||||
openmct.telemetry = jasmine.createSpyObj('telemetry', ['isTelemetryObject']);
|
||||
conditionMgr = new ConditionManager(openmct);
|
||||
parentDomainObject = {};
|
||||
composition = {};
|
||||
});
|
||||
|
||||
it('creates a conditionCollection with a default condition', function () {
|
||||
|
||||
});
|
||||
|
||||
it('adds a condition to the collection with one criterion', function () {
|
||||
|
||||
});
|
||||
|
||||
it('updates a condition\'s criteria to something valid', function () {
|
||||
|
||||
});
|
||||
|
||||
it('removes a condition from the conditionCollection', function () {
|
||||
|
||||
});
|
||||
|
||||
});
|
@ -140,7 +140,6 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import ConditionClass from "@/plugins/condition/Condition";
|
||||
import Criterion from '../../condition/components/Criterion.vue';
|
||||
|
||||
export default {
|
||||
@ -199,10 +198,6 @@ export default {
|
||||
methods: {
|
||||
initialize() {
|
||||
this.setOutput();
|
||||
if (!this.domainObject.isDefault) {
|
||||
this.conditionClass = new ConditionClass(this.domainObject, this.openmct);
|
||||
this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
}
|
||||
},
|
||||
addCriteria() {
|
||||
const criteriaObject = {
|
||||
@ -217,19 +212,6 @@ export default {
|
||||
this.$emit('setMoveIndex', Number(e.target.getAttribute('data-condition-index')));
|
||||
},
|
||||
destroy() {
|
||||
if (this.conditionClass) {
|
||||
this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
if (typeof this.conditionClass.destroy === 'function') {
|
||||
this.conditionClass.destroy();
|
||||
}
|
||||
delete this.conditionClass;
|
||||
}
|
||||
},
|
||||
handleConditionResult(args) {
|
||||
this.$emit('conditionResultUpdated', {
|
||||
id: this.conditionIdentifier,
|
||||
result: args.data.result
|
||||
})
|
||||
},
|
||||
removeCondition(ev) {
|
||||
this.$emit('removeCondition', this.conditionIdentifier);
|
||||
@ -260,9 +242,6 @@ export default {
|
||||
this.domainObject.configuration.output = this.selectedOutputKey;
|
||||
}
|
||||
},
|
||||
updateCurrentCondition() {
|
||||
this.$emit('updateCurrentCondition', this.currentConditionIdentifier);
|
||||
},
|
||||
hasTelemetry(identifier) {
|
||||
// TODO: check parent domainObject.composition.hasTelemetry
|
||||
return this.currentCriteria && identifier;
|
||||
|
@ -70,7 +70,6 @@
|
||||
@updateCurrentCondition="updateCurrentCondition"
|
||||
@removeCondition="removeCondition"
|
||||
@cloneCondition="cloneCondition"
|
||||
@conditionResultUpdated="handleConditionResult"
|
||||
@setMoveIndex="setMoveIndex"
|
||||
/>
|
||||
</li>
|
||||
@ -82,6 +81,7 @@
|
||||
|
||||
<script>
|
||||
import Condition from '../../condition/components/Condition.vue';
|
||||
import ConditionManager from '../../condition/ConditionManager';
|
||||
import uuid from 'uuid';
|
||||
|
||||
|
||||
@ -109,21 +109,29 @@ export default {
|
||||
destroyed() {
|
||||
this.composition.off('add', this.addTelemetryObject);
|
||||
this.composition.off('remove', this.removeTelemetryObject);
|
||||
if(this.conditionManager) {
|
||||
this.conditionManager.off('conditionResultUpdated', this.handleConditionResult);
|
||||
}
|
||||
if (typeof this.stopObservingForChanges === 'function') {
|
||||
this.stopObservingForChanges();
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.instantiate = this.openmct.$injector.get('instantiate');
|
||||
this.composition = this.openmct.composition.get(this.domainObject);
|
||||
this.composition.on('add', this.addTelemetryObject);
|
||||
this.composition.on('remove', this.removeTelemetryObject);
|
||||
this.composition.load();
|
||||
this.conditionCollection = this.domainObject.configuration ? this.domainObject.configuration.conditionCollection : [];
|
||||
if (!this.conditionCollection.length) {
|
||||
this.addCondition(null, true);
|
||||
} else {
|
||||
this.updateCurrentCondition(this.conditionCollection[0]);
|
||||
}
|
||||
this.conditionCollection = this.domainObject.configuration.conditionCollection;
|
||||
this.conditionManager = new ConditionManager(this.domainObject, this.openmct);
|
||||
this.conditionManager.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
this.observeForChanges();
|
||||
},
|
||||
methods: {
|
||||
observeForChanges() {
|
||||
this.stopObservingForChanges = this.openmct.objects.observe(this.domainObject, '*', (newDomainObject) => {
|
||||
this.conditionCollection = newDomainObject.configuration.conditionCollection;
|
||||
});
|
||||
},
|
||||
setMoveIndex(index) {
|
||||
this.moveIndex = index;
|
||||
this.isDragging = true;
|
||||
@ -200,99 +208,25 @@ export default {
|
||||
this.telemetryObjs.splice(index, 1);
|
||||
}
|
||||
},
|
||||
removeTelemetry(telemetryDomainObjectIdentifier) {
|
||||
let index = _.findIndex(this.telemetryObjs, (obj) => {
|
||||
let objId = this.openmct.objects.makeKeyString(obj.identifier);
|
||||
let id = this.openmct.objects.makeKeyString(telemetryDomainObjectIdentifier);
|
||||
return objId === id;
|
||||
});
|
||||
if (index > -1) {
|
||||
this.telemetryObjs.splice(index, 1);
|
||||
}
|
||||
},
|
||||
/*
|
||||
Adds a condition to list via programatic creation of default for initial list, manual
|
||||
creation via Add Condition button, or duplication via button in title bar of condition.
|
||||
Params:
|
||||
event: always null,
|
||||
isDefault (boolean): true if conditionList is empty
|
||||
isClone (boolean): true if duplicating a condition
|
||||
definition (string): definition property of condition being duplicated with new name
|
||||
index (number): index of condition being duplicated
|
||||
*/
|
||||
addCondition(event, isDefault, isClone, configuration, index) {
|
||||
let conditionDomainObject = this.createConditionDomainObject(!!isDefault, isClone, configuration);
|
||||
//persist the condition domain object so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet
|
||||
this.openmct.objects.mutate(conditionDomainObject, 'created', new Date());
|
||||
if (!isClone) {
|
||||
this.conditionCollection.unshift(conditionDomainObject.identifier);
|
||||
} else {
|
||||
this.conditionCollection.splice(index + 1, 0, conditionDomainObject.identifier);
|
||||
}
|
||||
this.persist();
|
||||
addCondition(event, isDefault, index) {
|
||||
this.conditionManager.addCondition(!!isDefault, index);
|
||||
},
|
||||
updateCurrentCondition(identifier) {
|
||||
this.currentConditionIdentifier = identifier;
|
||||
},
|
||||
createConditionDomainObject(isDefault, isClone, configuration) {
|
||||
const configurationTemplate = {
|
||||
name: isDefault ? 'Default' : (isClone ? 'Copy of ' : '') + 'Unnamed Condition',
|
||||
output: 'false',
|
||||
trigger: 'any',
|
||||
criteria: isDefault ? [] : [{
|
||||
telemetry: '',
|
||||
operation: '',
|
||||
input: '',
|
||||
metadata: '',
|
||||
key: ''
|
||||
}]
|
||||
};
|
||||
let conditionObj = {
|
||||
isDefault: isDefault,
|
||||
type: 'condition',
|
||||
name: isDefault ? 'Default' : (isClone ? 'Copy of ' : '') + 'Unnamed Condition',
|
||||
identifier: {
|
||||
namespace: this.domainObject.identifier.namespace,
|
||||
key: uuid()
|
||||
},
|
||||
configuration: isClone ? configuration: configurationTemplate,
|
||||
summary: 'summary description',
|
||||
created: new Date()
|
||||
};
|
||||
let conditionDomainObjectKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier);
|
||||
let newDomainObject = this.instantiate(conditionObj, conditionDomainObjectKeyString);
|
||||
|
||||
return newDomainObject.useCapability('adapter');
|
||||
},
|
||||
updateCondition(updatedCondition) {
|
||||
let index = _.findIndex(this.conditions, (condition) => condition.id === updatedCondition.id);
|
||||
this.conditions[index] = updatedCondition;
|
||||
},
|
||||
removeCondition(identifier) {
|
||||
let index = _.findIndex(this.conditionCollection, (condition) => {
|
||||
let conditionId = this.openmct.objects.makeKeyString(condition);
|
||||
let id = this.openmct.objects.makeKeyString(identifier);
|
||||
return conditionId === id;
|
||||
});
|
||||
this.conditionCollection.splice(index, 1);
|
||||
this.persist();
|
||||
this.updateCurrentConditionId();
|
||||
this.conditionManager.removeCondition(null, index);
|
||||
},
|
||||
reorder(reorderPlan) {
|
||||
let oldConditions = Array.from(this.conditionCollection);
|
||||
reorderPlan.forEach((reorderEvent) => {
|
||||
this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]);
|
||||
});
|
||||
this.persist();
|
||||
this.conditionManager.reorderConditions(reorderPlan);
|
||||
},
|
||||
cloneCondition(condition) {
|
||||
this.openmct.objects.get(condition.identifier).then((obj) => {
|
||||
obj.configuration.name = 'Copy of ' + obj.configuration.name;
|
||||
this.addCondition(null, false, true, obj.configuration, condition.index);
|
||||
});
|
||||
},
|
||||
persist() {
|
||||
this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.conditionCollection);
|
||||
this.conditionManager.cloneCondition(condition.identifier, condition.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,389 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2020, United States Government
|
||||
* as represented by the Administrator of the National Aeronautics and Space
|
||||
* Administration. All rights reserved.
|
||||
*
|
||||
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
* Open MCT includes source code licensed under additional open source
|
||||
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||
* this source code distribution or the Licensing information page available
|
||||
* at runtime from the About dialog for additional information.
|
||||
*****************************************************************************/
|
||||
|
||||
<template>
|
||||
<!-- TODO: current condition class should be set using openmct.objects.makeKeyString(<identifier>) -->
|
||||
<div v-if="condition"
|
||||
class="c-c-editui__conditions c-c-container__container c-c__drag-wrapper"
|
||||
:class="['widget-condition', { 'widget-condition--current': currentConditionIdentifier && (currentConditionIdentifier.key === conditionIdentifier.key) }]"
|
||||
>
|
||||
<div class="title-bar">
|
||||
<span class="c-c__menu-hamburger"
|
||||
:class="{ 'is-enabled': !condition.isDefault }"
|
||||
:draggable="!condition.isDefault"
|
||||
@dragstart="dragStart"
|
||||
@dragover.stop
|
||||
></span>
|
||||
<span
|
||||
class="is-enabled flex-elem"
|
||||
:class="['c-c__disclosure-triangle', { 'c-c__disclosure-triangle--expanded': expanded }]"
|
||||
@click="expanded = !expanded"
|
||||
></span>
|
||||
<div class="condition-summary">
|
||||
<span class="condition-name">{{ condition.definition.name }}</span>
|
||||
<!-- TODO: description should be derived from criteria -->
|
||||
<span class="condition-description">{{ condition.definition.name }}</span>
|
||||
</div>
|
||||
<span v-if="!condition.isDefault"
|
||||
class="is-enabled c-c__duplicate"
|
||||
@click="cloneCondition"
|
||||
></span>
|
||||
<span v-if="!condition.isDefault"
|
||||
class="is-enabled c-c__trash"
|
||||
@click="removeCondition"
|
||||
></span>
|
||||
</div>
|
||||
<div v-if="expanded"
|
||||
class="condition-config-edit widget-condition-content c-sw-editui__conditions-wrapper holder widget-conditions-wrapper flex-elem expanded"
|
||||
>
|
||||
<div id="conditionArea"
|
||||
class="c-c-editui__condition widget-conditions"
|
||||
>
|
||||
<div class="c-c-condition">
|
||||
<div class="c-c-condition__ui l-compact-form l-widget-condition has-local-controls">
|
||||
<div>
|
||||
<ul class="t-widget-condition-config">
|
||||
<li>
|
||||
<label>Condition Name</label>
|
||||
<span class="controls">
|
||||
<input v-model="condition.definition.name"
|
||||
class="t-condition-name-input"
|
||||
type="text"
|
||||
>
|
||||
</span>
|
||||
</li>
|
||||
<li>
|
||||
<label>Output</label>
|
||||
<span class="controls">
|
||||
<select v-model="selectedOutputKey"
|
||||
@change="checkInputValue"
|
||||
>
|
||||
<option value="">- Select Output -</option>
|
||||
<option v-for="option in outputOptions"
|
||||
:key="option.key"
|
||||
:value="option.key"
|
||||
>
|
||||
{{ option.text }}
|
||||
</option>
|
||||
</select>
|
||||
<input v-if="selectedOutputKey === outputOptions[2].key"
|
||||
v-model="condition.definition.output"
|
||||
class="t-condition-name-input"
|
||||
type="text"
|
||||
>
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
<div v-if="!condition.isDefault"
|
||||
class="widget-condition-content expanded"
|
||||
>
|
||||
<ul class="t-widget-condition-config">
|
||||
<li class="has-local-controls t-condition">
|
||||
<label>Match when</label>
|
||||
<span class="controls">
|
||||
<select>
|
||||
<option value="all">all criteria are met</option>
|
||||
<option value="any">any criteria are met</option>
|
||||
</select>
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="t-widget-condition-config">
|
||||
<li v-if="telemetry.length"
|
||||
class="has-local-controls t-condition"
|
||||
>
|
||||
<label>when</label>
|
||||
<span class="t-configuration">
|
||||
<span class="controls">
|
||||
<select v-model="selectedTelemetryKey"
|
||||
@change="updateTelemetryMetaData"
|
||||
>
|
||||
<option value="">- Select Telemetry -</option>
|
||||
<option v-for="telemetryOption in telemetry"
|
||||
:key="telemetryOption.identifier.key"
|
||||
:value="telemetryOption.identifier"
|
||||
>
|
||||
{{ telemetryOption.name }}
|
||||
</option>
|
||||
</select>
|
||||
</span>
|
||||
<span class="controls">
|
||||
<select v-model="selectedMetaDataKey">
|
||||
<option value="">- Select Field -</option>
|
||||
<option v-for="option in telemetryMetadata"
|
||||
:key="option.key"
|
||||
:value="option.key"
|
||||
>
|
||||
{{ option.name }}
|
||||
</option>
|
||||
</select>
|
||||
</span>
|
||||
<span class="controls">
|
||||
<select v-model="selectOperationName"
|
||||
@change="setInputValueVisibility"
|
||||
>
|
||||
<option value="">- Select Comparison -</option>
|
||||
<option v-for="option in operations"
|
||||
:key="option.name"
|
||||
:value="option.name"
|
||||
>
|
||||
{{ option.text }}
|
||||
</option>
|
||||
</select>
|
||||
<input v-if="comparisonValueField"
|
||||
class="t-condition-name-input"
|
||||
type="text"
|
||||
v-model="operationValue"
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { OPERATIONS } from '../utils/operations';
|
||||
import ConditionClass from "@/plugins/condition/Condition";
|
||||
|
||||
export default {
|
||||
inject: ['openmct', 'domainObject'],
|
||||
props: {
|
||||
conditionIdentifier: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
currentConditionIdentifier: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
telemetry: {
|
||||
type: Array,
|
||||
required: true,
|
||||
default: () => []
|
||||
},
|
||||
conditionIndex: {
|
||||
type: Number,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
condition: this.condition,
|
||||
expanded: true,
|
||||
telemetryObject: this.telemetryObject,
|
||||
telemetryMetadata: this.telemetryMetadata,
|
||||
operations: OPERATIONS,
|
||||
selectedMetaDataKey: '',
|
||||
selectedTelemetryKey: '',
|
||||
selectOperationName: '',
|
||||
selectedOutputKey: '',
|
||||
stringOutputField: false,
|
||||
comparisonValueField: false,
|
||||
operationValue: this.operationValue,
|
||||
outputOptions: [
|
||||
{
|
||||
key: 'false',
|
||||
text: 'False'
|
||||
},
|
||||
{
|
||||
key: 'true',
|
||||
text: 'True'
|
||||
},
|
||||
{
|
||||
key: 'string',
|
||||
text: 'String'
|
||||
}
|
||||
]
|
||||
|
||||
};
|
||||
},
|
||||
destroyed() {
|
||||
this.destroy();
|
||||
},
|
||||
mounted() {
|
||||
this.openmct.objects.get(this.conditionIdentifier).then((obj => {
|
||||
this.condition = obj;
|
||||
this.initialize();
|
||||
}));
|
||||
},
|
||||
updated() {
|
||||
//validate telemetry exists, update criteria as needed
|
||||
this.validate();
|
||||
this.persist();
|
||||
},
|
||||
methods: {
|
||||
dragStart(e) {
|
||||
e.dataTransfer.effectAllowed = "copyMove";
|
||||
e.dataTransfer.setDragImage(e.target.closest('.c-c-container__container'), 0, 0);
|
||||
this.$emit('setMoveIndex', this.conditionIndex);
|
||||
},
|
||||
initialize() {
|
||||
this.setOutput();
|
||||
this.setOperation();
|
||||
this.updateTelemetry();
|
||||
this.conditionClass = new ConditionClass(this.condition, this.openmct);
|
||||
this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
},
|
||||
destroy() {
|
||||
this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
if (this.conditionClass && typeof this.conditionClass.destroy === 'function') {
|
||||
this.conditionClass.destroy();
|
||||
delete this.conditionClass;
|
||||
}
|
||||
},
|
||||
reset() {
|
||||
this.selectedMetaDataKey = '';
|
||||
this.selectedTelemetryKey = '';
|
||||
this.selectOperationName = '';
|
||||
this.operationValue = '';
|
||||
},
|
||||
validate() {
|
||||
if (this.hasTelemetry() && !this.getTelemetryKey()) {
|
||||
this.reset();
|
||||
} else {
|
||||
if (!this.conditionClass) {
|
||||
this.initialize();
|
||||
}
|
||||
}
|
||||
},
|
||||
handleConditionResult(args) {
|
||||
this.$emit('conditionResultUpdated', {
|
||||
id: this.conditionIdentifier,
|
||||
result: args.data.result
|
||||
})
|
||||
},
|
||||
removeCondition(ev) {
|
||||
this.$emit('removeCondition', this.conditionIdentifier);
|
||||
},
|
||||
cloneCondition(ev) {
|
||||
this.$emit('cloneCondition', {
|
||||
identifier: this.conditionIdentifier,
|
||||
index: Number(ev.target.closest('.widget-condition').getAttribute('data-condition-index'))
|
||||
});
|
||||
},
|
||||
setOutput() {
|
||||
let conditionOutput = this.condition.definition.output;
|
||||
if (conditionOutput !== 'false' && conditionOutput !== 'true') {
|
||||
this.selectedOutputKey = this.outputOptions[2].key;
|
||||
} else {
|
||||
if (conditionOutput === 'true') {
|
||||
this.selectedOutputKey = this.outputOptions[1].key;
|
||||
} else {
|
||||
this.selectedOutputKey = this.outputOptions[0].key;
|
||||
}
|
||||
}
|
||||
},
|
||||
setOperation() {
|
||||
if (this.condition.definition.criteria.length && this.condition.definition.criteria[0].operation) {
|
||||
for (let i=0, ii=this.operations.length; i < ii; i++) {
|
||||
if (this.condition.definition.criteria[0].operation === this.operations[i].name) {
|
||||
this.selectOperationName = this.operations[i].name;
|
||||
this.comparisonValueField = this.operations[i].inputCount > 0;
|
||||
if (this.comparisonValueField) {
|
||||
this.operationValue = this.condition.definition.criteria[0].input[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
updateTelemetry() {
|
||||
if (this.hasTelemetry()) {
|
||||
this.openmct.objects.get(this.condition.definition.criteria[0].key).then((obj) => {
|
||||
this.telemetryObject = obj;
|
||||
this.telemetryMetadata = this.openmct.telemetry.getMetadata(this.telemetryObject).values();
|
||||
this.selectedMetaDataKey = this.getTelemetryMetadataKey();
|
||||
this.selectedTelemetryKey = this.getTelemetryKey();
|
||||
});
|
||||
} else {
|
||||
this.telemetryObject = null;
|
||||
}
|
||||
},
|
||||
getTelemetryMetadataKey() {
|
||||
let index = -1;
|
||||
if (this.condition.definition.criteria[0].metaDataKey) {
|
||||
index = _.findIndex(this.telemetryMetadata, (metadata) => {
|
||||
return metadata.key === this.condition.definition.criteria[0].metaDataKey;
|
||||
});
|
||||
}
|
||||
return this.telemetryMetadata.length && index > -1 ? this.telemetryMetadata[index].key : '';
|
||||
},
|
||||
getTelemetryKey() {
|
||||
let index = -1;
|
||||
if (this.condition.definition.criteria[0].key) {
|
||||
index = _.findIndex(this.telemetry, (obj) => {
|
||||
let key = this.openmct.objects.makeKeyString(obj.identifier);
|
||||
let conditionKey = this.openmct.objects.makeKeyString(this.condition.definition.criteria[0].key);
|
||||
return key === conditionKey;
|
||||
});
|
||||
}
|
||||
return this.telemetry.length && index > -1 ? this.telemetry[index].identifier : '';
|
||||
},
|
||||
hasTelemetry() {
|
||||
return this.condition.definition.criteria.length && this.condition.definition.criteria[0].key;
|
||||
},
|
||||
updateConditionCriteria() {
|
||||
if (this.condition.definition.criteria.length) {
|
||||
let criterion = this.condition.definition.criteria[0];
|
||||
criterion.key = this.selectedTelemetryKey;
|
||||
criterion.metaDataKey = this.selectedMetaDataKey;
|
||||
criterion.operation = this.selectOperationName;
|
||||
criterion.input = [this.operationValue];
|
||||
}
|
||||
},
|
||||
persist() {
|
||||
this.updateConditionCriteria();
|
||||
this.openmct.objects.mutate(this.condition, 'definition', this.condition.definition);
|
||||
},
|
||||
checkInputValue() {
|
||||
if (this.selectedOutputKey === this.outputOptions[2].key) {
|
||||
this.condition.definition.output = '';
|
||||
} else {
|
||||
this.condition.definition.output = this.selectedOutputKey;
|
||||
}
|
||||
},
|
||||
setInputValueVisibility(ev) {
|
||||
for (let i=0, ii=this.operations.length; i < ii; i++) {
|
||||
if (this.selectOperationName === this.operations[i].name) {
|
||||
this.comparisonValueField = this.operations[i].inputCount > 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//find the criterion being updated and set the operation property
|
||||
},
|
||||
updateTelemetryMetaData() {
|
||||
this.selectedMetaDataKey = '';
|
||||
this.updateConditionCriteria();
|
||||
this.updateTelemetry();
|
||||
},
|
||||
updateCurrentCondition() {
|
||||
this.$emit('updateCurrentCondition', this.conditionIdentifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
x
Reference in New Issue
Block a user