drag to reorder conditions

This commit is contained in:
Joel McKinnon 2020-01-24 13:23:02 -08:00
parent 7e0f475c63
commit 96746f4042
3 changed files with 48 additions and 24 deletions

View File

@ -29,16 +29,19 @@
</button> </button>
</div> </div>
<div class="c-c condition-collection"> <div class="c-c condition-collection">
<div id="js-c-drag-ghost"
class="c-c__drag-ghost"
></div>
<div class="c-c__container-holder"> <div class="c-c__container-holder">
<div v-for="(conditionIdentifier, index) in conditionCollection" <div v-for="(conditionIdentifier, index) in conditionCollection"
:key="conditionIdentifier.key" :key="conditionIdentifier.key"
> >
<div v-if="isEditing"> <div v-if="isEditing">
<div :id="'drop' + index"
class="c-c__drag-ghost"
@drop.prevent="dropCondition"
@dragover.prevent
></div>
<ConditionEdit :condition-identifier="conditionIdentifier" <ConditionEdit :condition-identifier="conditionIdentifier"
:current-condition-identifier="currentConditionIdentifier" :current-condition-identifier="currentConditionIdentifier"
:condition-index="index"
@update-current-condition="updateCurrentCondition" @update-current-condition="updateCurrentCondition"
@remove-condition="removeCondition" @remove-condition="removeCondition"
@condition-result-updated="handleConditionResult" @condition-result-updated="handleConditionResult"
@ -98,6 +101,27 @@ export default {
} }
}, },
methods: { methods: {
dropCondition(e) {
const newCondIndex = Number(e.dataTransfer.getData('conditionIndex'));
const oldCondIndex = Number(e.target.id.slice(4));
let reorderPlan = [];
this.conditionCollection.forEach((condition, index) => {
const reorderObj = {};
reorderObj.oldIndex = index;
if (index === oldCondIndex) {
reorderObj.newIndex = newCondIndex;
} else if (index === newCondIndex) {
reorderObj.newIndex = oldCondIndex;
} else {
reorderObj.newIndex = index;
}
reorderPlan.push(reorderObj);
});
this.reorder(reorderPlan);
},
handleConditionResult(args) { handleConditionResult(args) {
let idAsString = this.openmct.objects.makeKeyString(args.id); let idAsString = this.openmct.objects.makeKeyString(args.id);
this.conditionResults[idAsString] = args.result; this.conditionResults[idAsString] = args.result;
@ -173,6 +197,7 @@ export default {
reorderPlan.forEach((reorderEvent) => { reorderPlan.forEach((reorderEvent) => {
this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]); this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]);
}); });
this.persist();
}, },
persist() { persist() {
this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.conditionCollection); this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.conditionCollection);

View File

@ -1,9 +1,11 @@
<template> <template>
<div v-if="condition" <div v-if="condition"
:data-condition-index="conditionIndex"
class="c-c-editui__conditions c-c-container__container c-c__drag-wrapper" class="c-c-editui__conditions c-c-container__container c-c__drag-wrapper"
:class="['widget-condition', { 'widget-condition--current': currentConditionIdentifier && (currentConditionIdentifier.key === conditionIdentifier.key) }]" :class="['widget-condition', { 'widget-condition--current': currentConditionIdentifier && (currentConditionIdentifier.key === conditionIdentifier.key) }]"
draggable="true" :draggable="!condition.isDefault"
@dragstart="initDrag" @dragstart="dragStart"
@dragover.stop
> >
<div class="title-bar"> <div class="title-bar">
<span <span
@ -153,6 +155,10 @@ export default {
type: Object, type: Object,
required: true required: true
}, },
conditionIndex: {
type: Number,
required: true
}
}, },
data() { data() {
return { return {
@ -209,24 +215,8 @@ export default {
this.persist(); this.persist();
}, },
methods: { methods: {
initDrag() { dragStart: e => {
// let type = this.openmct.types.get(this.domainObject.type), e.dataTransfer.setData('conditionIndex', e.target.getAttribute('data-condition-index'));
// iconClass = type.definition ? type.definition.cssClass : 'icon-object-unknown';
// if (this.dragGhost) {
// let originalClassName = this.dragGhost.classList[0];
// this.dragGhost.className = '';
// this.dragGhost.classList.add(originalClassName, iconClass);
// this.dragGhost.innerHTML = `<span>${this.domainObject.name}</span>`;
// event.dataTransfer.setDragImage(this.dragGhost, 0, 0);
// }
console.log('conditionId', this.condition.id);
console.log('containerIndex', this.containerIndex);
event.dataTransfer.setData('conditionId', this.condition.id);
event.dataTransfer.setData('containerIndex', this.containerIndex);
}, },
handleConditionResult(args) { handleConditionResult(args) {
// console.log('ConditionEdit::Result', args); // console.log('ConditionEdit::Result', args);

View File

@ -1,6 +1,6 @@
.widget-condition { .widget-condition {
background-color: #eee; background-color: #eee;
margin: 0 0 0.6em; margin: 0 0 5px;
border-radius: 3px; border-radius: 3px;
&--current { &--current {
@ -9,6 +9,10 @@
} }
.c-c-editui__conditions.widget-condition {
margin: 0;
}
.title-bar { .title-bar {
display: flex; display: flex;
align-items: center; align-items: center;
@ -150,3 +154,8 @@
content: $glyph-icon-trash; content: $glyph-icon-trash;
} }
} }
.c-c__drag-ghost {
width: 100%;
min-height: 10px;
}