Skip to content

Commit caa8adc

Browse files
authored
Fixed CE multiple forms issues (#193)
* removed confirm dialog on form creation; fixed error set on tab * fixed next/previous tab selection --------- Co-authored-by: Stefano Ricci <SteRiccio@users.noreply.github.com>
1 parent 0eea7a9 commit caa8adc

2 files changed

Lines changed: 68 additions & 50 deletions

File tree

  • collect-server/src/main/resources/org/openforis/collect/designer/templates/collectearth/earthFiles/js
  • collect-webapp/src/main/webapp/earthFiles/js

collect-server/src/main/resources/org/openforis/collect/designer/templates/collectearth/earthFiles/js/earth_new.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,7 @@ var addStepHeadingAddButtons = function() {
605605
var entityLabel = $templateSectionHeading.text();
606606
var button = $('<button class="form-add-btn" data-node-def-name="' + entityName + '" title="Add"><span>+</span></button>');
607607
button.on("click", function () {
608-
showConfirm('Add a new ' + entityLabel + "?", function () {
609-
addStepByNodeDefName(entityName);
610-
});
608+
addStepByNodeDefName(entityName);
611609
});
612610
var templateSectionHeadingId = $templateSectionHeading.attr('id');
613611
var tabAnchorId = getSourceTabAnchorIdBySectionHeadingId(templateSectionHeadingId);
@@ -841,10 +839,19 @@ var addEntityAndCloneStepTemplate = function (options) {
841839
// new step created when response data is processed
842840
$stepsContainer.steps("setCurrentIndex", indexNext);
843841
}, function() {
844-
console.log("ERROR")
842+
logError("Error creating new entity");
845843
})
846844
}
847845

846+
var isStepHeadingFormTemplate = function(index) {
847+
var stepHeadings = getStepHeadings();
848+
var stepHeading = $(stepHeadings[index]);
849+
var headingId = stepHeading.find('a').attr('id');
850+
var sourceHeadingId = getStepSourceHeadingIdByTabHeadingId(headingId);
851+
var sourceHeading = findById(sourceHeadingId);
852+
return sourceHeading.hasClass("form-template")
853+
}
854+
848855
var initSteps = function() {
849856
$steps = $stepsContainer.steps({
850857
headerTag : "h3",
@@ -859,29 +866,29 @@ var initSteps = function() {
859866
previous: PREVIOUS_LABEL
860867
},
861868
onStepChanging: function (_event, currentIndex, nextIndex) {
862-
var stepHeadings = getStepHeadings();
863-
var nextStepHeading = $(stepHeadings[nextIndex]);
864-
var nextHeadingId = nextStepHeading.find('a').attr('id');
865-
var nextSourceHeadingId = getStepSourceHeadingIdByTabHeadingId(nextHeadingId);
866-
var nextSourceHeading = findById(nextSourceHeadingId);
867-
if (nextSourceHeading.hasClass("form-template")) {
868-
var finalIndex = nextIndex + (nextIndex > currentIndex ? 1: -1);
869-
if (finalIndex >= 0 && finalIndex <= stepHeadings.length - 1 && !$(stepHeadings[finalIndex]).hasClass('notrelevant')) {
870-
$stepsContainer.steps('setCurrentIndex', finalIndex);
871-
}
869+
if (isStepHeadingFormTemplate(nextIndex)) {
870+
var firstRelevantHeadingIdx = findFirstRelevantStepHeadingIndex(currentIndex, currentIndex > nextIndex);
871+
if (firstRelevantHeadingIdx >= 0) {
872+
$stepsContainer.steps('setCurrentIndex', firstRelevantHeadingIdx);
873+
}
874+
// workaround: when returning "false", the class "error" will be added to the tab; remove it with a timeout
875+
var stepHeadings = getStepHeadings();
876+
var sourceStepHeading = $(stepHeadings[currentIndex]);
877+
if (!sourceStepHeading.hasClass('error')) {
878+
setTimeout(function () {
879+
sourceStepHeading.removeClass("error");
880+
}, 10);
881+
}
872882
return false;
873883
}
874884
return true;
875885
},
876886
onStepChanged : function(_event, currentIndex, priorIndex) {
877887
var stepHeadings = getStepHeadings();
878888
var stepHeading = $(stepHeadings[currentIndex]);
879-
var headingId = stepHeading.find('a')[0].id
880-
var sourceHeadingId = getStepSourceHeadingIdByTabHeadingId(headingId)
881-
var sourceHeading = $("#" + sourceHeadingId)
882889
if (stepHeading.hasClass("notrelevant")) {
883890
var nextStepIndex;
884-
var firstRelevantHeadingIdx = findFirstRelevantElementIndex(stepHeadings, currentIndex, currentIndex < priorIndex);
891+
var firstRelevantHeadingIdx = findFirstRelevantStepHeadingIndex(currentIndex, currentIndex < priorIndex);
885892
if (firstRelevantHeadingIdx >= 0) {
886893
nextStepIndex = firstRelevantHeadingIdx;
887894
} else {
@@ -890,7 +897,7 @@ var initSteps = function() {
890897
}
891898
$stepsContainer.steps('setCurrentIndex', nextStepIndex);
892899
currentStepIndex = nextStepIndex;
893-
} else if (sourceHeading.hasClass("form-template")) {
900+
} else if (isStepHeadingFormTemplate(currentIndex)) {
894901
// it should not happen, prevented by onStepChanging
895902
} else {
896903
currentStepIndex = currentIndex;
@@ -906,14 +913,16 @@ var initSteps = function() {
906913
addStepHeadingAddButtons()
907914
};
908915

909-
var findFirstRelevantElementIndex = function(group, startFromIndex, reverseOrder) {
910-
var idx = reverseOrder ? startFromIndex - 1 : startFromIndex + 1;
911-
while (reverseOrder ? idx >= 0 : idx < group.length) {
912-
var el = $(group[idx]);
913-
if (! el.hasClass("notrelevant")) {
916+
var findFirstRelevantStepHeadingIndex = function(startFromIndex, reverseOrder) {
917+
var stepHeadings = getStepHeadings();
918+
var offset = reverseOrder ? -1 : 1;
919+
var idx = startFromIndex + offset;
920+
while (reverseOrder ? idx >= 0 : idx < stepHeadings.length) {
921+
var el = $(stepHeadings[idx]);
922+
if (!el.hasClass("notrelevant") && !isStepHeadingFormTemplate(idx)) {
914923
return idx;
915924
}
916-
idx = reverseOrder ? idx - 1 : idx + 1;
925+
idx = idx + offset;
917926
}
918927
return -1;
919928
};

collect-webapp/src/main/webapp/earthFiles/js/earth_new.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,7 @@ var addStepHeadingAddButtons = function() {
605605
var entityLabel = $templateSectionHeading.text();
606606
var button = $('<button class="form-add-btn" data-node-def-name="' + entityName + '" title="Add"><span>+</span></button>');
607607
button.on("click", function () {
608-
showConfirm('Add a new ' + entityLabel + "?", function () {
609-
addStepByNodeDefName(entityName);
610-
});
608+
addStepByNodeDefName(entityName);
611609
});
612610
var templateSectionHeadingId = $templateSectionHeading.attr('id');
613611
var tabAnchorId = getSourceTabAnchorIdBySectionHeadingId(templateSectionHeadingId);
@@ -841,10 +839,19 @@ var addEntityAndCloneStepTemplate = function (options) {
841839
// new step created when response data is processed
842840
$stepsContainer.steps("setCurrentIndex", indexNext);
843841
}, function() {
844-
console.log("ERROR")
842+
logError("Error creating new entity");
845843
})
846844
}
847845

846+
var isStepHeadingFormTemplate = function(index) {
847+
var stepHeadings = getStepHeadings();
848+
var stepHeading = $(stepHeadings[index]);
849+
var headingId = stepHeading.find('a').attr('id');
850+
var sourceHeadingId = getStepSourceHeadingIdByTabHeadingId(headingId);
851+
var sourceHeading = findById(sourceHeadingId);
852+
return sourceHeading.hasClass("form-template")
853+
}
854+
848855
var initSteps = function() {
849856
$steps = $stepsContainer.steps({
850857
headerTag : "h3",
@@ -859,29 +866,29 @@ var initSteps = function() {
859866
previous: PREVIOUS_LABEL
860867
},
861868
onStepChanging: function (_event, currentIndex, nextIndex) {
862-
var stepHeadings = getStepHeadings();
863-
var nextStepHeading = $(stepHeadings[nextIndex]);
864-
var nextHeadingId = nextStepHeading.find('a').attr('id');
865-
var nextSourceHeadingId = getStepSourceHeadingIdByTabHeadingId(nextHeadingId);
866-
var nextSourceHeading = findById(nextSourceHeadingId);
867-
if (nextSourceHeading.hasClass("form-template")) {
868-
var finalIndex = nextIndex + (nextIndex > currentIndex ? 1: -1);
869-
if (finalIndex >= 0 && finalIndex <= stepHeadings.length - 1 && !$(stepHeadings[finalIndex]).hasClass('notrelevant')) {
870-
$stepsContainer.steps('setCurrentIndex', finalIndex);
871-
}
869+
if (isStepHeadingFormTemplate(nextIndex)) {
870+
var firstRelevantHeadingIdx = findFirstRelevantStepHeadingIndex(currentIndex, currentIndex > nextIndex);
871+
if (firstRelevantHeadingIdx >= 0) {
872+
$stepsContainer.steps('setCurrentIndex', firstRelevantHeadingIdx);
873+
}
874+
// workaround: when returning "false", the class "error" will be added to the tab; remove it with a timeout
875+
var stepHeadings = getStepHeadings();
876+
var sourceStepHeading = $(stepHeadings[currentIndex]);
877+
if (!sourceStepHeading.hasClass('error')) {
878+
setTimeout(function () {
879+
sourceStepHeading.removeClass("error");
880+
}, 10);
881+
}
872882
return false;
873883
}
874884
return true;
875885
},
876886
onStepChanged : function(_event, currentIndex, priorIndex) {
877887
var stepHeadings = getStepHeadings();
878888
var stepHeading = $(stepHeadings[currentIndex]);
879-
var headingId = stepHeading.find('a')[0].id
880-
var sourceHeadingId = getStepSourceHeadingIdByTabHeadingId(headingId)
881-
var sourceHeading = $("#" + sourceHeadingId)
882889
if (stepHeading.hasClass("notrelevant")) {
883890
var nextStepIndex;
884-
var firstRelevantHeadingIdx = findFirstRelevantElementIndex(stepHeadings, currentIndex, currentIndex < priorIndex);
891+
var firstRelevantHeadingIdx = findFirstRelevantStepHeadingIndex(currentIndex, currentIndex < priorIndex);
885892
if (firstRelevantHeadingIdx >= 0) {
886893
nextStepIndex = firstRelevantHeadingIdx;
887894
} else {
@@ -890,7 +897,7 @@ var initSteps = function() {
890897
}
891898
$stepsContainer.steps('setCurrentIndex', nextStepIndex);
892899
currentStepIndex = nextStepIndex;
893-
} else if (sourceHeading.hasClass("form-template")) {
900+
} else if (isStepHeadingFormTemplate(currentIndex)) {
894901
// it should not happen, prevented by onStepChanging
895902
} else {
896903
currentStepIndex = currentIndex;
@@ -906,14 +913,16 @@ var initSteps = function() {
906913
addStepHeadingAddButtons()
907914
};
908915

909-
var findFirstRelevantElementIndex = function(group, startFromIndex, reverseOrder) {
910-
var idx = reverseOrder ? startFromIndex - 1 : startFromIndex + 1;
911-
while (reverseOrder ? idx >= 0 : idx < group.length) {
912-
var el = $(group[idx]);
913-
if (! el.hasClass("notrelevant")) {
916+
var findFirstRelevantStepHeadingIndex = function(startFromIndex, reverseOrder) {
917+
var stepHeadings = getStepHeadings();
918+
var offset = reverseOrder ? -1 : 1;
919+
var idx = startFromIndex + offset;
920+
while (reverseOrder ? idx >= 0 : idx < stepHeadings.length) {
921+
var el = $(stepHeadings[idx]);
922+
if (!el.hasClass("notrelevant") && !isStepHeadingFormTemplate(idx)) {
914923
return idx;
915924
}
916-
idx = reverseOrder ? idx - 1 : idx + 1;
925+
idx = idx + offset;
917926
}
918927
return -1;
919928
};

0 commit comments

Comments
 (0)