- }
- }
-
- //--- populate these extral elements generated from clicking add button
- for(a = 0; a < extraElements.length; a++){
- if(extraElements[a].includes("@")){
- var n = extraElements[a].lastIndexOf("@");
- var key = extraElements[a].substring(0, n+2); //include @x in key also by n+2 since x can be 1, or 2, or 3
- checkData.push(key);
- }
- }
- var unique = checkData.filter(onlyUnique);
- for(i =0; i < unique.length; i++){
- //remove @x and let addNewChoice add @1 or @2...
- var newKey = unique[i].substring(0, unique[i].length-2);
- $scope.addNewChoice(newKey);
- }
+ }
+
+ //--- Populate these extra elements created by clicked add button
+ for(var a = 0; a < extraElements.length; a++){
+ if(extraElements[a].includes("@")){
+ var index = extraElements[a].lastIndexOf("@");
+ if(index > 0){
+ // Get the number after @
+ var n = getNumOfDigits(extraElements[a], index+1);
+
+ var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
+ console.log("key: " + key);
+ checkData.push(key);
+ }
+ }
+ }
+ var unique = checkData.filter(onlyUnique);
+ var parentLevelElements = [];
+ if(unique){
+ //--- get all root level exta elments first (only contains one "@")
+ for(var i =0; i < unique.length; i++){
+ var firstIndex = unique[i].indexOf("@");
+ var lastIndex = unique[i].lastIndexOf("@");
+ if(firstIndex == lastIndex){
+ var newKey = unique[i].substring(0, firstIndex);
+ console.log("root element: " + newKey);
+ parentLevelElements.push(newKey);
+ unique[i] = "*processed*";
+ }
+ }
+ }
+ //if no layout order info, keep the process as before
+ if(!dataOrderInfo){
+ for(var i =0; i < unique.length; i++){
+ //remove @x and let addNewChoice add @1 or @2...
+ //var newKey = unique[i].substring(0, unique[i].length-2);
+ var index = unique[i].lastIndexOf("@");
+ var newKey = unique[i].substring(0, index);
+ console.log("newKey: " + newKey);
+ addNewChoice(newKey);
+ }
+ }else{
+
+ for (var i = 0; i < $scope.labelManyKeys.length; i++) {
+ //console.log("dataOrderInfo["+i+"]"+ dataOrderInfo[i]);
+ var label = $scope.labelManyKeys[i];
+
+ if(parentLevelElements){
+ for (var k = 0; k < parentLevelElements.length; k++){
+ if(label == parentLevelElements[k]){
+ addNewChoice(label);
+ }
+ }
+
+ }
+
+ }
+
+ //---reset to default
+ dataOrderInfo = [];
+ $scope.labelManyKeys = [];
+
+ //---process none labels
+ for (var j = 0; j < unique.length; j++){
+ if(unique[j] != "*processed*"){
+ // if not created yet
+ if(!document.getElementById(unique[j])){
+ var index = unique[j].lastIndexOf("@");
+ var newKey = unique[j].substring(0, index);
+
+ var newElement = document.getElementById("div."+unique[j]);
+
+ //check weather it has been created already
+ if(newElement != null){
+ continue;
+ }else{
+ newElement = document.getElementById(unique[j]);
+ if(newElement != null){
+ continue;
+ }
+ }
+ //if not created yet, then create it.
+ addNewChoice(newKey);
+
+ }
+ }
+ }
+ }
+ }