Skip to content

Commit

Permalink
Fix: ItemValue Property Editor, Fast Entry works slow for several hun…
Browse files Browse the repository at this point in the history
…derds items #903
  • Loading branch information
andrewtelnov committed Jul 16, 2020
1 parent 929b81b commit a1fd2e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/surveyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,23 @@ export class SurveyHelper {
dest.splice(0, dest.length);
return;
}
var destHash = SurveyHelper.applyItemValueArrayCreateHash(dest);
var srcHash = SurveyHelper.applyItemValueArrayCreateHash(src);
for (var i = 0; i < dest.length; i++) {
var ind = srcHash[dest[i].value];
dest[i].srcIndex = ind === undefined ? -1 : ind;
if (dest.length > src.length) {
dest.splice(src.length, dest.length - src.length);
}
var insertIndex = 0;
for (var i = 0; i < src.length; i++) {
var ind = destHash[src[i].value];
if (ind === undefined) {
dest.splice(insertIndex, 0, src[i]);
insertIndex++;
} else {
insertIndex = ind + 1;
if (dest.length < src.length) {
var insertedArray = [];
for (var i = dest.length; i < src.length; i++) {
insertedArray.push(src[i]);
}
dest.splice.apply(dest, [dest.length, 0].concat(insertedArray));
}
for (var i = dest.length - 1; i >= 0; i--) {
if (dest[i].srcIndex === -1) {
dest.splice(i, 1);
} else {
if (dest[i].srcIndex > -1 && src[dest[i].srcIndex].hasText) {
dest[i].text = src[dest[i].srcIndex].text;
}
for (var i = 0; i < dest.length; i++) {
if (dest[i].value != src[i].value) {
dest[i].value = src[i].value;
}
dest[i].text = src[i].hasText ? src[i].text : "";
}
}
private static applyItemValueArrayCreateHash(arr: Array<Survey.ItemValue>) {
var res = {};
for (var i = 0; i < arr.length; i++) {
res[arr[i].value] = i;
}
return res;
}
public static disableSelectingObj(obj: Survey.Base) {
obj["disableSelecting"] = true;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/propertyEditors/propertyEditorsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2491,4 +2491,13 @@ QUnit.test("SurveyHelper.applyItemValueArray", function (assert) {
[1, { value: 2, text: "item 2" }, { value: 4, text: "item 4" }],
7
);
testSetFunc([1, 2, 2, 3, 4, 5, 6], [1, 2, 2, 3], 8);
testSetFunc([1, 2, 3, 4, 2, 5, 6], [1, 2, 3, 2], 9);
console.log(q2.toJSON());
testSetFunc(
[1, 2, 13, 14, 5, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
10
);
console.log(q2.toJSON());
});

0 comments on commit a1fd2e0

Please sign in to comment.