fixed reorder bugs

This commit is contained in:
Joel McKinnon 2020-01-27 09:34:50 -08:00
parent 5055a18ca1
commit bc9cadaa77
3 changed files with 53 additions and 23 deletions

View File

@ -34,22 +34,19 @@
:key="conditionIdentifier.key" :key="conditionIdentifier.key"
> >
<div v-if="isEditing"> <div v-if="isEditing">
<div v-if="index !== conditionCollection.length - 1" <div class="c-c__drag-ghost"
:id="'drop' + index"
class="c-c__drag-ghost"
@drop.prevent="dropCondition" @drop.prevent="dropCondition"
@dragenter="dragEnter"
@dragleave="dragLeave"
@dragover.prevent @dragover.prevent
></div> ></div>
<div v-else
class="c-c__drag-ghost"
>
</div>
<ConditionEdit :condition-identifier="conditionIdentifier" <ConditionEdit :condition-identifier="conditionIdentifier"
:current-condition-identifier="currentConditionIdentifier" :current-condition-identifier="currentConditionIdentifier"
:condition-index="index" :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"
@set-move-index="setMoveIndex"
/> />
</div> </div>
<div v-else> <div v-else>
@ -85,7 +82,8 @@ export default {
parentKeyString: this.openmct.objects.makeKeyString(this.domainObject.identifier), parentKeyString: this.openmct.objects.makeKeyString(this.domainObject.identifier),
conditionCollection: [], conditionCollection: [],
conditions: [], conditions: [],
currentConditionIdentifier: this.currentConditionIdentifier || {} currentConditionIdentifier: this.currentConditionIdentifier || {},
moveIndex: null
}; };
}, },
destroyed() { destroyed() {
@ -106,23 +104,49 @@ export default {
} }
}, },
methods: { methods: {
setMoveIndex(index) {
this.moveIndex = index;
},
dropCondition(e) { dropCondition(e) {
const moveIndex = Number(e.dataTransfer.getData('conditionIndex')); let targetIndex = Array.from(document.querySelectorAll('.c-c__drag-ghost')).indexOf(e.target);
const targetIndex = Number(e.target.id.slice(4)); if (targetIndex > this.moveIndex) { targetIndex-- }
const oldIndexArr = Object.keys(this.conditionCollection);
function moveArrIndex(arr, from, to) { const move = function (arr, old_index, new_index) {
arr.splice(to, 0, arr.splice(from, 1)[0]); while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr; return arr;
} }
const newIndexArr = move(oldIndexArr, this.moveIndex, targetIndex);
const oldIndexArr = Object.keys(this.conditionCollection);
const newIndexArr = moveArrIndex(Object.keys(this.conditionCollection), moveIndex, targetIndex);
const reorderPlan = []; const reorderPlan = [];
for (let i = 0; i < oldIndexArr.length; i++) { for (let i = 0; i < oldIndexArr.length; i++) {
reorderPlan.push({oldIndex: i, newIndex: newIndexArr[i]}); reorderPlan.push({oldIndex: Number(newIndexArr[i]), newIndex: i});
} }
this.reorder(reorderPlan); this.reorder(reorderPlan);
e.target.classList.remove("dragging");
},
dragEnter(e) {
let targetIndex = Array.from(document.querySelectorAll('.c-c__drag-ghost')).indexOf(e.target);
if (targetIndex > this.moveIndex) { targetIndex-- }
if (this.moveIndex === targetIndex) { return }
e.target.classList.add("dragging");
},
dragLeave(e) {
e.target.classList.remove("dragging");
}, },
handleConditionResult(args) { handleConditionResult(args) {
let idAsString = this.openmct.objects.makeKeyString(args.id); let idAsString = this.openmct.objects.makeKeyString(args.id);

View File

@ -11,12 +11,11 @@
<span <span
class="c-c__menu-hamburger" class="c-c__menu-hamburger"
:class="{ 'is-enabled': !condition.isDefault }" :class="{ 'is-enabled': !condition.isDefault }"
@click="expanded = !condition.expanded"
></span> ></span>
<span <span
class="is-enabled flex-elem" class="is-enabled flex-elem"
:class="['c-c__disclosure-triangle', { 'c-c__disclosure-triangle--expanded': expanded }]" :class="['c-c__disclosure-triangle', { 'c-c__disclosure-triangle--expanded': expanded }]"
@click="expanded = !condition.expanded" @click="expanded = !expanded"
></span> ></span>
<div class="condition-summary"> <div class="condition-summary">
<span class="condition-name">{{ condition.definition.name }}</span> <span class="condition-name">{{ condition.definition.name }}</span>
@ -215,8 +214,8 @@ export default {
this.persist(); this.persist();
}, },
methods: { methods: {
dragStart: e => { dragStart(e) {
e.dataTransfer.setData('conditionIndex', e.target.getAttribute('data-condition-index')); this.$emit('set-move-index', Number(e.target.getAttribute('data-condition-index')));
}, },
handleConditionResult(args) { handleConditionResult(args) {
// console.log('ConditionEdit::Result', args); // console.log('ConditionEdit::Result', args);

View File

@ -157,5 +157,12 @@
.c-c__drag-ghost { .c-c__drag-ghost {
width: 100%; width: 100%;
min-height: 10px; min-height: 5px;
&.dragging {
min-height: 20px;
border: solid 1px blue;
background-color: lightblue;
border-radius: 2px;
}
} }