- if($scope.temp.policy.ruleData != null){
- var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
- propNames.forEach(function(name) {
- data.push(name);
- });
- for(a = 0; a < data.length; a++){
- var splitPlainAttributeKey1 = data[a].split(".");
- if(splitPlainAttributeKey1[0].indexOf("@") != -1){
- var key = splitPlainAttributeKey1[0];
- checkData.push(key);
- }
- }
- var unique = checkData.filter(onlyUnique);
- for(i =0; i < unique.length; i++){
- if(unique[i].indexOf("@0") == -1){
- var finalkey = unique[i].split("@")[0];
- $scope.addNewChoice(finalkey);
- }
- }
- }
+ // If ruleData contains extra elements created by clicked add button
+ if($scope.temp.policy.ruleData != null){
+ var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
+ propNames.forEach(function(name) {
+ data.push(name);
+ });
+
+ var extraElements = data;
+
+ if(plainAttributeKeys != null){
+ for(var b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
+ var newValue = plainAttributeKeys[b].split("*");
+ for(var a = 0; a < data.length; a++){
+ if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
+ extraElements.splice(a, 1);
+ }
+ }
+ }
+
+ //--- 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);
+
+ }
+ }
+ }
+ }
+ }
+ }
+