Your code would be a bit easier to read if you had an initial
var INITIAL_COLUMNS = {
"RegExColExample - Number": {
"header": "Number",
"inputItemProperty": "name",
"regexp": "^(..) Number.*$",
"type": "number"
},
"RegExColExample - Double": {
"header": "Double",
"inputItemProperty": "name",
"regexp": "^(....) Double.*$",
"type": "double",
"justify": "right"
},
"RegExColExample - Percent": {
"header": "Percent",
"inputItemProperty": "name",
"regexp": "^(..) Percent.*$",
"type": "percent"
},
"RegExColExample - Graph": {
"header": "Graph",
"inputItemProperty": "name",
"regexp": "^(..) Graph.*$",
"type": "graph"
},
"RegExColExample - Rating": {
"header": "Rating",
"inputItemProperty": "name",
"regexp": "^(..) Rating.*$",
"type": "star",
"maxStars": "10",
"maxStarValue": "100"
},
"RegExColExample - Size": {
"header": "Size",
"inputItemProperty": "name",
"regexp": "^([0-9]*) Size.*$",
"type": "size"
},
"RegExColExample - Date": {
"header": "Date",
"inputItemProperty": "name",
"regexp": "^([0-9]{4}-[0-9]{2}-[0-9]{2}) Date.*$",
"type": "date",
"datetimeformat": "yyyy-mm-dd"
}
};
function SaveColumns(config, data) {
return config.RegExColumns = JSON.stringify(data, null, 2);
}
function LoadColumns(config) {
try {
return JSON.parse(config.RegExColumns);
}
catch (err) {
Logger (LOG_ERR, "Error parsing RegExColums!");
Logger (LOG_ERR, " " + err);
dialogRegExColumnsError(err);
return {};
}
}
block, it would mean that the initial configuration would get stored as a nicely-formatted JSON object that way - it doesn't have to stay that way, but it's easier to read IMO. And even if a user had saved it incorrectly, the rest of the code would work. With this, your OnInit() would just have
...
// RegExColumns Examples
SaveColumns(initData.config, INITIAL_COLUMNS);
ConfigDescriptions("RegExColumns") = 'RegEx Columns Definition in JSON Format.';
// Add Configuration Descriptions
initData.config_desc = ConfigDescriptions;
return false;
}
to save the initial config, and the try catch blocks in other methods would be shrunk to e.g.
function OnScriptConfigChange(data) {
LogLevel = Script.config.LogLevel;
Logger (LOG_DEBUG, "Config Changed.");
Logger (LOG_DEBUG, Script.config.RegExColumns);
Script.InitColumns();
}
function OnAddColumns(data) {
LogLevel = Script.config.LogLevel;
Logger(LOG_DEBUG, "Add/Update Columns");
var RegExColumns = LoadColumns(Script.config);
for(var ColumnName in RegExColumns)
{
var
columnData = RegExColumns[ColumnName],
column = data.AddColumn();
column.method = "OnColumn";
column.autorefresh = false;
column.namerefresh = true;
column.name = column.label = ColumnName;
if (columnData.header) column.header = columnData.header;
if (columnData.type) column.type = columnData.type;
if (columnData.nogroup) column.nogroup = columnData.nogroup;
if (columnData.nosort) column.nosort = columnData.nosort;
if (columnData.justify) column.justify = columnData.justify;
if (columnData.type === "star" && columnData.maxStars) column.maxstars = columnData.maxStars;
}
}