Merge pull request #11489 from assuntad23/feature/11249/suggest-names-for-pairs

adding nameSuggestion to PairCollectionCreator
This commit is contained in:
John Chilton
2021-03-25 10:36:51 -04:00
committed by GitHub
5 changed files with 83 additions and 48 deletions
@@ -236,20 +236,6 @@ export default {
duplicateNames: [],
};
},
props: {
/** distance from list edge to begin autoscrolling list */
autoscrollDist: {
type: Number,
required: false,
default: 24,
},
/** Color passed to hoverhighlight */
highlightClr: {
type: String,
required: false,
default: "rgba( 64, 255, 255, 1.0 )",
},
},
computed: {
atLeastOneDatasetIsSelected() {
return this.selectedDatasetElems.length > 0;
@@ -82,6 +82,8 @@
@onUpdateHideSourceItems="onUpdateHideSourceItems"
@clicked-create="clickedCreate"
@remove-extensions-toggle="removeExtensionsToggle"
:renderExtensionsToggle="true"
:suggestedName="initialSuggestedName"
>
<template v-slot:help-content>
<p>
@@ -162,6 +164,11 @@ export default {
mixins: [mixin],
created() {
this._elementsSetUp();
this.initialSuggestedName = this._guessNameForPair(
this.workingElements[0],
this.workingElements[1],
this.removeExtensions
);
},
data: function () {
return {
@@ -177,6 +184,8 @@ export default {
minElements: 2,
workingElements: [],
invalidElements: [],
removeExtensions: true,
initialSuggestedName: "",
};
},
computed: {
@@ -263,6 +272,41 @@ export default {
toString: function () {
return "PairCollectionCreator";
},
removeExtensionsToggle: function () {
this.removeExtensions = !this.removeExtensions;
this.initialSuggestedName = this._guessNameForPair(
this.workingElements[0],
this.workingElements[1],
this.removeExtensions
);
},
_guessNameForPair: function (fwd, rev, removeExtensions) {
removeExtensions = removeExtensions ? removeExtensions : this.removeExtensions;
var fwdName = fwd.name;
var revName = rev.name;
var lcs = this._naiveStartingAndEndingLCS(fwdName, revName);
/** remove url prefix if files were uploaded by url */
var lastSlashIndex = lcs.lastIndexOf("/");
if (lastSlashIndex > 0) {
var urlprefix = lcs.slice(0, lastSlashIndex + 1);
lcs = lcs.replace(urlprefix, "");
fwdName = fwdName.replace(extension, "");
revName = revName.replace(extension, "");
}
if (removeExtensions) {
var lastDotIndex = lcs.lastIndexOf(".");
if (lastDotIndex > 0) {
var extension = lcs.slice(lastDotIndex, lcs.length);
lcs = lcs.replace(extension, "");
fwdName = fwdName.replace(extension, "");
revName = revName.replace(extension, "");
}
}
return lcs || `${fwdName} & ${revName}`;
},
},
};
</script>
@@ -719,38 +719,6 @@ export default {
}
return lcs || `${fwdName} & ${revName}`;
},
/** return the concat'd longest common prefix and suffix from two strings */
_naiveStartingAndEndingLCS: function (s1, s2) {
var fwdLCS = "";
var revLCS = "";
var i = 0;
var j = 0;
while (i < s1.length && i < s2.length) {
if (s1[i] !== s2[i]) {
break;
}
fwdLCS += s1[i];
i += 1;
}
if (i === s1.length) {
return s1;
}
if (i === s2.length) {
return s2;
}
i = s1.length - 1;
j = s2.length - 1;
while (i >= 0 && j >= 0) {
if (s1[i] !== s2[j]) {
break;
}
revLCS = [s1[i], revLCS].join("");
i -= 1;
j -= 1;
}
return fwdLCS + revLCS;
},
clickAutopair: function () {
// Unselect any selected elements
this.selectedForwardElement = null;
@@ -73,7 +73,7 @@
@click="$emit('clicked-create', collectionName)"
:disabled="!validInput"
>
{{ l("Create list") }}
{{ l("Create collection") }}
</button>
</div>
</div>
@@ -97,6 +97,11 @@ export default {
type: Boolean,
required: true,
},
suggestedName: {
type: String,
required: false,
default: "",
},
},
computed: {
validInput: function () {
@@ -111,7 +116,7 @@ export default {
placeholderEnterName: _l("Enter a name for your new collection"),
dropdownText: _l("Create a <i>single</> pair"),
isExpanded: false,
collectionName: "",
collectionName: this.suggestedName,
removeFileExtensionsText: "Remove file extensions?",
localHideSourceItems: this.hideSourceItems,
};
@@ -37,5 +37,37 @@ export default {
onUpdateHideSourceItems(hideSourceItems) {
this.hideSourceItems = hideSourceItems;
},
/** return the concat'd longest common prefix and suffix from two strings */
_naiveStartingAndEndingLCS: function (s1, s2) {
var fwdLCS = "";
var revLCS = "";
var i = 0;
var j = 0;
while (i < s1.length && i < s2.length) {
if (s1[i] !== s2[i]) {
break;
}
fwdLCS += s1[i];
i += 1;
}
if (i === s1.length) {
return s1;
}
if (i === s2.length) {
return s2;
}
i = s1.length - 1;
j = s2.length - 1;
while (i >= 0 && j >= 0) {
if (s1[i] !== s2[j]) {
break;
}
revLCS = [s1[i], revLCS].join("");
i -= 1;
j -= 1;
}
return fwdLCS + revLCS;
},
},
};