I've added a refresh button in case you are changing the vars from somewhere else (eg testing a script). Its simply removing all items and calling onDialogPopulateColumnsList()
I'm using the script as script only version, if you have the osp version (downloaded from above), rename its extension from osp => zip and update the script inside, dont forget to restore original extension.
/*
Adds A dialog for managing dopus global variables.
Sample button to open dialog.
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
<label>vars</label>
<tip>Manage Dopus Variables</tip>
<icon1>#ManageGlobalVariables:VarGreen</icon1>
<function type="normal">
<instruction>ManageDopusVariables</instruction>
</function>
</button>
*/
// -------------------- Options
var gbUseLogging=false;
var doVars = DOpus.vars;
// Called by Directory Opus to initialize the script.
function OnInit(initData){
var uid = "306f852a-e136-4829-913b-5256fa2233a4";
var url = "http://resource.dopus.com/";
initData.url = url;
initData.name = "Manage Dopus variables";
initData.desc = "Dialog for viewing and managing Dopus variables.";
initData.copyright = "wowbagger";
initData.version = "0.2";
initData.default_enable = true;
initData.config_desc = DOpus.NewMap();
//Set CustomColumns default to be defaultCustomColumns pretty printed.
//initData.config.CustomColumns = JSON.stringify(defaultCustomColumns, undefined, 2).replace(/(\n)+/g,"\r\n");
//initData.config_desc("CustomColumns") = "Custom Columns";
initData.config.exclusionFilter = DOpus.NewVector();
initData.config.displayForAll = false;
initData.config.facilitateSubjectGrouping = true;
initData.min_version = "12.0";
}
function OnAddCommands(addCmdData)
{
var cmd = addCmdData.AddCommand();
cmd.name = "ManageDopusVariables";
cmd.method = "OnManageDopusVariables";
cmd.desc = "Manage Dopus variables";
cmd.label = "ManageDopusVariables";
cmd.template = "";
}
//Custom commands
// Implement the ExternalCompare command
function OnManageDopusVariables(scriptCmdData)
{
var dlg = DOpus.Dlg;
dlg.window = scriptCmdData.func.sourcetab;
dlg.template = "ManageDopusVariables";
dlg.detach = true;
dlg.Show();
onDialogPopulateColumnsList(dlg);
var closeDialog;
while (!closeDialog) {
var msg = dlg.GetMsg();
if (!msg.result) break;
LogMessage("Msg: " + msg.control + " value: " + msg.value + " event:" + msg.event);
switch (msg.control) {
case "varsList" :
onDialogVarSelected(dlg, msg);
break;
case "buttonAdd" :
onDialogAddColumn(dlg, msg);
break;
case "buttonDelete" :
onDialogDeleteColumn(dlg, msg);
break;
case "buttonApply" :
onDialogApplyChanges(dlg, msg);
break;
case "buttonClipboard" :
CopyToClipboard(dlg);
break;
case "buttonClearSettings" :
ClearCustomColumns();
closeDialog = true;
break;
case "buttonUpdate" :
Update(dlg);
break;
case "varTypeCombo" :
if(msg.event == "selchange")
ChangeVarType(dlg);
break;
case "buttonOK" :
//params = DialogClosed(dlg, msg);
closeDialog = true;
break;
}
}
//LogMessage("Dialog Return code = " + dlg.result);
}
function forEach(enumerable, delegate)
{
for (var enumerator = new Enumerator(enumerable); !enumerator.atEnd(); enumerator.moveNext())
{
delegate(enumerator.item());
}
}
function Update(dlg)
{
dlg.Control("varsList").RemoveItem(-1);
onDialogPopulateColumnsList(dlg);
}
function onDialogPopulateColumnsList(dlg)
{
var columnsList = dlg.Control("varsList");
var selectedColumn;
forEach(DOpus.Vars,
function(variable)
{
LogMessage("item: " + variable);
if(variable)
{
var location = columnsList.AddItem(variable.name);
var item = columnsList.GetItemAt(location);
if(!selectedColumn)
{
item.selected = true;
selectedColumn = variable;
//onDialogVarSelected(dlg);
}
LogMessage("item: " + item.index + " location: " + location + " variable:" + variable.name);
}
}
);
}
function ChangeVarType(dlg)
{
var selectedVarListItem = GetSelectedItem(dlg ,"varTypeCombo");
LogMessage("ChangeVarType: " + ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : 'null'));
switch ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : "") {
case "string" :
dlg.Control("valueNumber").visible = false;
dlg.Control("varValueStatic").visible = true;
dlg.Control("valueCheckBox").visible = false;
dlg.Control("varValue").visible = true;
dlg.Control("varValue").enabled = true;
dlg.Control("buttonApply").enabled = true;
dlg.Control("varName").enabled = true;
break;
case "boolean" :
dlg.Control("valueNumber").visible = false;
dlg.Control("varValueStatic").visible = false;
dlg.Control("valueCheckBox").visible = true;
dlg.Control("varValue").visible = false;
dlg.Control("buttonApply").enabled = true;
dlg.Control("varName").enabled = true;
break;
case "number" :
dlg.Control("valueNumber").visible = true;
dlg.Control("varValueStatic").visible = true;
dlg.Control("valueCheckBox").visible = false;
dlg.Control("varValue").visible = false;
dlg.Control("buttonApply").enabled = true;
dlg.Control("varName").enabled = true;
break;
default :
dlg.Control("valueNumber").visible = false;
dlg.Control("varValueStatic").visible = true;
dlg.Control("valueCheckBox").visible = false;
dlg.Control("varValue").visible = true;
dlg.Control("varValue").enabled = false;
dlg.Control("buttonApply").enabled = true;
dlg.Control("varName").enabled = false;
break;
}
}
// Dialog functions
function onDialogVarSelected(dlg)
{
var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
//LogMessage("onDialogVarSelected: " + ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : 'null'));
if(!selectedVarListItem) return;
var selectedVar = (doVars.Exists(selectedVarListItem.name) ? doVars(selectedVarListItem.name) : null);
//todo: check type
var type = typeof(selectedVar.value);
switch (type) {
case "string" :
dlg.Control("valueCheckBox").value = false;
dlg.Control("valueNumber").value = 0;
dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
SetSelectedItem(dlg,"varTypeCombo",type);
break;
case "boolean" :
dlg.Control("valueCheckBox").value = selectedVar ? selectedVar.value : false;
dlg.Control("valueNumber").value = 0;
dlg.Control("varValue").value = "";
SetSelectedItem(dlg,"varTypeCombo",type);
break;
case "number" :
dlg.Control("valueCheckBox").value = false;
dlg.Control("valueNumber").value = selectedVar ? selectedVar.value : 0;
dlg.Control("varValue").value = "";
SetSelectedItem(dlg,"varTypeCombo",type);
break;
default :
dlg.Control("valueCheckBox").value = false;
dlg.Control("valueNumber").value = 0;
dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
SetSelectedItem(dlg,"varTypeCombo","other");
break;
}
ChangeVarType(dlg);
dlg.Control("varName").value = selectedVar ? selectedVar.name : "";
dlg.Control("varType").value = (selectedVar) ? type : "null";
dlg.Control("checkPersist").value = selectedVar ? selectedVar.persist : false;
dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
}
function onDialogApplyChanges(dlg, msg)
{
var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
var oldName = "";
if (selectedVarListItem)
oldName = selectedVarListItem.name;
var newName = dlg.Control("varName").value;
var selectedVarListItem = GetSelectedItem(dlg ,"varTypeCombo");
switch (selectedVarListItem.name) {
case "string" :
doVars(newName) = dlg.Control("varValue").value;
break;
case "boolean" :
doVars(newName) = dlg.Control("valueCheckBox").value;
break;
case "number" :
doVars(newName) = parseInt(dlg.Control("valueNumber").value);
break;
case "object" :
Default :
break;
}
if(doVars.Exists(newName))
{
doVars(newName).persist = dlg.Control("checkPersist").value;
}
if(oldName != newName)
{
if(doVars.Exists(oldName))
{
//doVars.Delete(oldName);
}
AddToVarsList(dlg, newName, true);
}
}
function onDialogAddColumn(dlg, name)
{
//LogMessage("onDialogAddColumn");
var newColumn = "newvar";
var customColumnsConfig = LoadCustomColumns();
customColumnsConfig.columns.push(newColumn);
SaveCustomColumns(customColumnsConfig);
//customColumnsConfig.columns[customColumnsConfig.columns.length - 1]
AddToVarsList(dlg, newColumn.name, true);
onDialogVarSelected(dlg);
}
function onDialogDeleteColumn(dlg, msg)
{
var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
if(!selectedVarListItem) return;
if(doVars.Exists(selectedVarListItem.name))
{
doVars.Delete(selectedVarListItem.name);
}
RemoveFromVarList(dlg, selectedVarListItem);
onDialogVarSelected(dlg);
}
function RemoveFromVarList(dlg, varItem)
{
var varList = dlg.Control("varsList");
var index = varItem.index;
varList.RemoveItem(varItem);
if(varList.count > 0)
{
var selectedVarListItem = varList.GetItemAt((index > 0) ? index -1 : 0);
selectedVarListItem.selected = true;
}
}
function AddToVarsList(dlg, name, setSelected)
{
var varsList = dlg.Control("varsList");
var location = varsList.AddItem(name);
if(setSelected)
{
var item = varsList.GetItemAt(location);
item.selected = true;
}
}
function GetSelectedItem(dlg, controlName)
{
var listItems = dlg.Control(controlName);
for(var i = 0; i < listItems.count; ++i) {
var column = listItems.GetItemAt(i);
if(column && column.selected)
{
return column;
}
}
}
function SetSelectedItem(dlg, controlName, value)
{
var listItems = dlg.Control(controlName);
if(typeof(value) == "undefined")
{
listItems.DeselectItem(-1);
return;
}
var found = false;
//listItems.GetItemByName(value);
for(var i = 0; i < listItems.count; ++i) {
var item = listItems.GetItemAt(i);
if(item && item.name == value)
{
LogMessage("SetSelectedItem: " + value + " " + found);
found = true;
item.selected = true;
break;
}
}
if(!found)
{ LogMessage("SetSelectedItem: " + found);
listItems.DeselectItem(-1);
}
}
function CopyToClipboard(dlg){
var doCmd = DOpus.NewCommand;
LogMessage("CopyToClipboard: " + dlg.Control("varValue").value);
var cmd = "Clipboard SET " + (dlg.Control("varValue").value);
doCmd.RunCommand(cmd);
}
var doLogCmd = DOpus.NewCommand;
function IsDebugEnabled()
{
return gbUseLogging || Script.config.DEBUG || doLogCmd.IsSet(Script.config.DebugEnableVar);
}
function LogMessage(message, force)
{
if (force || IsDebugEnabled()) { DOpus.OutputString(message)};
}
// *********************
// * Date/Time Parsing *
// *********************
function parseDate(input, format) {
if(!input) return;
var parsedDate = new Date(Date.parse(input));
if(!parsedDate || parsedDate == "NaN")
{
format = format || 'yyyy-mm-dd'; // default format
var parts = input.match(/(\d+)/g),
i = 0, fmt = {};
if(parts)
{
// extract date-part indexes from the format
format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });
parsedDate = new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}
}
//LogMessage("parseDate for " +input + "-" + parsedDate);
return (!parsedDate || parsedDate == "NaN") ? null : parsedDate;
}
function InArray(col,arr){
for (var i=0;i<arr.length;i++){
if (col==arr[i].name) return true;
}
}
==SCRIPT RESOURCES
<resources>
<resource name="ManageDopusVariables" type="dialog">
<dialog fontsize="8" height="198" lang="english" resize="yes" title="Dopus variable management" width="366">
<control close="0" height="14" name="buttonClose" resize="xy" title="Close" type="button" width="50" x="300" y="174" />
<control height="186" name="varsGroup" resize="h" title="Variables" type="group" width="150" x="6" y="6" />
<control height="150" name="varsList" resize="h" type="listbox" width="138" x="12" y="18" />
<control halign="left" height="8" name="varNameSatic" title="Name" type="static" valign="top" width="66" x="162" y="18" />
<control halign="left" height="12" name="varName" resize="w" tip="Not set" type="edit" width="132" x="228" y="18" />
<control halign="left" height="12" name="varType" readonly="yes" resize="w" type="edit" width="132" x="228" y="36" />
<control halign="left" height="8" name="varValueStatic" title="Value" type="static" valign="top" width="60" x="162" y="90" />
<control halign="left" height="36" multiline="yes" name="varValue" resize="wh" tip="Not set I.E. ./" type="edit" width="198" x="162" y="102" />
<control halign="left" height="8" name="varTypeStatic" title="Current Type" type="static" valign="top" width="66" x="162" y="36" />
<control header="yes" height="64" name="group1" resize="yw" type="group" width="204" x="156" y="162" />
<control halign="left" height="8" name="varTypeComboStatic" title="Saving Type" type="static" valign="top" width="60" x="162" y="54" />
<control height="40" name="varTypeCombo" type="combo" width="102" x="228" y="54">
<contents>
<item text="other" />
<item text="boolean" />
<item text="number" />
<item text="string" />
</contents>
</control>
<control default="yes" height="14" name="buttonApply" resize="xy" title="Save Changes" type="button" width="74" x="288" y="144" />
<control height="14" name="buttonDelete" resize="y" title="Delete Variable" type="button" width="60" x="90" y="174" />
<control height="14" name="buttonClipboard" resize="y" title="Copy Variable to clipboard" type="button" width="102" x="162" y="144" />
<control height="10" name="checkPersist" title="Is persistent " type="check" width="64" x="228" y="72" />
<control height="10" name="valueCheckBox" title="Value" type="check" width="64" x="228" y="90" />
<control halign="left" height="12" name="valueNumber" number="yes" type="edit" visible="no" width="132" x="228" y="90" />
<control height="14" name="buttonUpdate" resize="xy" title="Refresh" type="button" width="50" x="187" y="174" />
</dialog>
</resource>
</resources>