//Dynamic child controls inc. fields /* ScriptDescription Allows you to create relationships between fields and variables to hide and show in the editor based on selections */ var existingRules = new Array(); var elementsArr = new Array(); var rules_string = ''; var globalArray = Document.getProperty("ChildControlRules"); if (globalArray){ if (globalArray.getValue() === ''){ globalArray.setValue("[]"); } } else { globalArray = Document.createProperty("ChildControlRules",TEXT_PROPERTY,false,false,false); globalArray.setValue("[]"); } var jsonArray = eval('(' + globalArray.getValue() + ')'); //And show the dialogue var res; var nNumRules; do { getExistingRules(); nNumRules = jsonArray.length; res = ShowDialog(); } while(nNumRules != jsonArray.length); function getExistingRules(){ if (jsonArray.length > 0) { var count = 0; existingRules = []; for (i in jsonArray) { var parent_id = (jsonArray[i].id); var value = (jsonArray[i].value); var childList = (jsonArray[i].child); var limitSelect = (jsonArray[i].selectionValues); count +=1; rules_string = count + ": if "; checkFieldType(parent_id); rules_string += " has a selected value of " + value + ", then show "; for (h in childList){ var child = childList[h]; checkFieldType(child); if (limitSelect !== undefined){ var nKey = 0; for (var keyVal in limitSelect) { nKey += 1; } if (nKey > 0){ rules_string += " with values restricted to "; for (j in limitSelect){ if (j == child){ rules_string += limitSelect[child]; } } } } } existingRules.push(rules_string); } } else { alert("you currently have no rules setup"); } } function checkFieldType(str){ if (str.indexOf('g_') > -1){ str = str.replace('g_', ''); rules_string += " - (global variable) " + str; } else { str = str.replace('f_', ''); var field = Document.getFieldByID(parseInt(str)); if (field){ str = field.getName(); } rules_string += " - (field) " + str; } } function getAllElements(parent) { elementsArr = []; //Globals var nNumGlobals = Document.getNumberOfGlobalTextVariables(); for(var m = 0; m < nNumGlobals; ++m) { var global = Document.getGlobalTextVariable(m); if (parent){ if (global.getProperty("VarType").getValue() == 'TextList'){ elementsArr.push("Global: " + global.getName()); } } else { elementsArr.push("Global: " + global.getName()); } } //Fields var nNumFields = Document.getNumberOfFields(); for(var i = 0; i < nNumFields; ++i) { var field = Document.getField(i); if(field.hasProperty("FieldType")) { property = field.getProperty("FieldType"); if (parent){ if (property.getValue() == 'TextList'){ elementsArr.push("Field: " + field.getName()); } } else { elementsArr.push("Field: " + field.getName()); } } } } function RemoveLogic(prop){ var selection = prop[0][2]; var index = existingRules.indexOf(selection); if (index > -1){ jsonArray.splice(index, 1); writeToDocumentProp(jsonArray); return PROPERTYDLG_CLOSE_OK; } else { alert("couldn't delete rule"); } return PROPERTYDLG_CLOSE_CANCEL; } function AddLogic(properties, pos){ getAllElements(true); var index; var parentElem = select(elementsArr,"Select the field/global variable which will control the fields", "Dynamic Child: New Rule"); if (parentElem) { var parentElemMod, field, parentPrefix; if (parentElem.indexOf("Global") > -1){ parentElemMod = parentElem.replace("Global: ", ""); field = Document.getGlobalTextVariable(parentElemMod); parentPrefix = parentElem.replace("Global: ", "g_"); } else if (parentElem.indexOf("Field") > -1){ //parentElem = Field: Test parentElemMod = parentElem.replace("Field: ", ""); //parentElemMod = Test field = Document.getField(parentElemMod); //field = Test field object fieldId = field.getFieldID(); //fieldId = Test field Id parentPrefix = "f_" + fieldId; //parentPrefix = f_Test } var parentVal = select(field.getProperty("TextListValues").getValue(), "Select the value of " + parentElem + " to set rules for", "Dynamic Child: Parent Value select"); if (parentVal){ getAllElements(false); var availOptions, childField; //remove already selected parent element from the selection var index = elementsArr.indexOf(parentElem); if (index > - 1) { elementsArr.splice(index, 1); } var childrenSelect = multiselect(elementsArr, "Now select which elements to show", "Dynamic Child: Child selector"); if (childrenSelect){ var selections = {}; for (i in childrenSelect){ var childElem = childrenSelect[i]; if (childElem.indexOf("Global") > -1){ //returning it back to its global variable name childField = childElem.replace("Global: ", ""); //retrieve the global variable object childField = Document.getGlobalTextVariable(childField); //replace friendly text with array text var childMod = childElem.replace("Global: ", "g_"); } else if (childElem.indexOf("Field") > -1){ childField = childElem.replace("Field: ", ""); childField = Document.getField(childField); childFieldId = childField.getFieldID(); var childMod = "f_" + childFieldId; } //replacing friendly version with array version var exists = childrenSelect.indexOf(childElem); if (exists !== -1){ childrenSelect[exists] = childMod; } availOptions = multiselect(childField.getProperty("TextListValues").getValue(), "Select what options you would like to be made available from " + childElem, "Dynamic Child: Available Options Select"); if (availOptions != '') { var k = childMod; selections[k] = availOptions; } } jsonArray.push ({"id" : parentPrefix, "value" : parentVal, "child" : childrenSelect, "selectionValues" : selections}); writeToDocumentProp(jsonArray); return PROPERTYDLG_CLOSE_OK; } else { PROPERTYDLG_CLOSE_CANCEL; } } else { PROPERTYDLG_CLOSE_CANCEL; } } else { PROPERTYDLG_CLOSE_CANCEL; } } function writeToDocumentProp(array) { var str = "["; for (var j = 0; j < array.length; ++j){ str += '{ "id" : "' + array[j].id + '", '; str += '"value" : "' + array[j].value + '", '; str += '"child" : [ '; if (array[j].child !== ''){ for(var i = 0; i < array[j].child.length; ++i){ var childValue = array[j].child[i]; str += '"' + array[j].child[i] + '"'; if (i + 1 != array[j].child.length){ str += ','; } } } var nKey = 0; for (var keyVal in array[j].selectionValues) { nKey += 1; } str += ']'; if (nKey > 0) { str += ', "selectionValues" : {'; var limitCount = 0; for (var key in array[j].selectionValues) { if (array[j].selectionValues.hasOwnProperty(key)){ limitCount += 1; var obj = array[j].selectionValues[key]; str += '"' + key + '" : ['; var nCount = 0; for (var prop in obj) { if(obj.hasOwnProperty(prop)){ nCount += 1; str += '"' + obj[prop] + '"'; if (nCount != obj.length){ str += ','; } } } str += ']'; if (limitCount != nKey){ str += ','; } } } str += '}'; } if (j+1 != array.length){ str += '},'; } else { str += '}'; } } str += "]"; try { globalArray.setValue(str); alert("Rules have been updated"); } catch (err){ alert(err); } } function ShowDialog() { var properties; if(existingRules.length > 0) { var rulesProp = new Array("Existing rules:", existingRules,existingRules[0]); var removeProp = new Array("",RemoveLogic,"Remove"); var addProp = new Array("",AddLogic,"Add"); properties = new Array(rulesProp,removeProp,addProp); } else { var addProp = new Array("No rules currently set.",AddLogic,"Add"); properties = new Array(addProp); } properties = propertyDialog(properties,"Dynamic child setup"); return properties; }