Skip to content

Commit e8ca075

Browse files
author
Anton Slipchenko
committed
add checking of atom pair existence
1 parent 784f93a commit e8ca075

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/mixins/measurement.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,46 @@ export const MeasurementMixin = (superclass) =>
337337
}
338338
if (this.shouldCalculateDistance()) {
339339
const lastChosenAtoms = this.chosenAtoms.slice(-2);
340-
this.drawLineBetweenAtoms(lastChosenAtoms);
341-
const distance = this.calculateDistanceBetweenAtoms(lastChosenAtoms);
342-
this.drawDistanceText(distance);
343-
updateState({ distance });
340+
const isPairAlreadyExist = this.checkAtomPairExistence(
341+
this.chosenAtoms.slice(0, -2),
342+
lastChosenAtoms,
343+
);
344+
if (!isPairAlreadyExist) {
345+
this.drawLineBetweenAtoms(lastChosenAtoms);
346+
const distance = this.calculateDistanceBetweenAtoms(lastChosenAtoms);
347+
this.drawDistanceText(distance);
348+
updateState({ distance });
349+
}
344350
}
345351
break;
346352
}
347353
}
348354
}
349355

356+
/**
357+
*
358+
* @param {Array} atomArray - array to be checked
359+
* @param {Array} param1 - atom pair to checked
360+
* @returns boolean value - true if atom pair is already exist in array.
361+
*/
362+
checkAtomPairExistence(atomArray, [atomA, atomB]) {
363+
for (let i = 0; i < atomArray.length; i += 2) {
364+
const currentAtom = atomArray[i];
365+
const nextAtom = atomArray[i + 1];
366+
367+
if (
368+
(currentAtom.uuid === atomA.uuid && atomB.uuid === nextAtom.uuid) ||
369+
(currentAtom.uuid === atomB.uuid && nextAtom.uuid === atomA.uuid)
370+
) {
371+
this.chosenAtoms.pop();
372+
this.chosenAtoms.pop();
373+
return true;
374+
}
375+
}
376+
377+
return false;
378+
}
379+
350380
shouldCalculateDistance() {
351381
return (
352382
this.chosenAtoms.length &&

0 commit comments

Comments
 (0)