/////////////////////////////////////////////////////////////////////////////// function OnInit(data){ data.name = "Command.GUI: GoRefreshKeepSelection"; data.desc = "Refreshs tab content while keeping and scrolling back to selected items."; data.copyright = "tbone in o9/2o14"; data.version = "0.1"; data.default_enable = true; var cmd = data.AddCommand(); cmd.name = "GoRefreshKeepSelection"; cmd.method = "Command_GoRefreshKeepSelection"; cmd.desc = data.desc; cmd.label = "GoRefreshKeepSelection"; cmd.template = "NOSCROLLTOSELECTION/S,SCROLLTOCHECKED/S,REFRESHCOMMAND/R"; /////////////////////////////////////////////////////////////////////////// function ConfigHelper(data){ //v1.0 this.data=data; this.descriptions=null; this.last=null; this.add = function(name, val, description){ this.data.config[name]=val; this.last=[this.data.config[name],name]; if (description!=undefined) this.des(description); return this;} this.des = function(description){ if (!(description && DOpus.version.AtLeast("11.6.1"))) return this; if (!this.descriptions){ this.descriptions=DOpus.NewMap(); data.config._descriptions=this.descriptions; } this.descriptions(this.last[1])=description; return this;} this.val = function(val){ if (typeof this.last[0]=="object") this.last[0].push_back(val); else this.last[0]=val; return this;} }////////////////////////////////////////////////////////////////////////// var cfg = new ConfigHelper(data); cfg.add('DebugOutput', false). des('Enable debug output in the script console.'); cfg.add('NoScrollToSelection', false). des('Disable automatic scrolling to selected items after the refresh.'); cfg.add('ScrollToChecked', false). des('Priorizes checked items when scrolling to selected items.'); cfg.add('RefreshCommand', "Go REFRESH=source"). des('The refresh command to run (lookup the docs for "Go REFRESH*" switches and options).'); } /////////////////////////////////////////////////////////////////////////////// var COMMAND_FAILURE = true; var COMMAND_SUCCESS = false; /////////////////////////////////////////////////////////////////////////////// function Command_GoRefreshKeepSelection(data){ Debug("GoRefreshKeepSelection():"); var cmd = data.func.command; cmd.ClearFiles(); var wasCheckboxMode = data.func.sourcetab.stats.checkbox_mode==true; var isCheckboxMode = wasCheckboxMode; var numSelItems = data.func.sourcetab.stats.selitems; var numCheItems = data.func.sourcetab.stats.checkeditems; var scroll = GetParameter(data, "NOSCROLLTOSELECTION", "NoScrollToSelection", false)==false; var prioCB = GetParameter(data, "SCROLLTOCHECKED", "ScrollToChecked", false); var refCMD = GetParameter(data, "REFRESHCOMMAND", "RefreshCommand", false); Debug(" SelItems: " + numSelItems); Debug(" CheItems: " + numCheItems); Debug(" NOSCROLLTOSELECTION: " + (scroll==false)); Debug(" SCROLLTOCHECKED : " + prioCB); var itemsSelected = null; var itemsChecked = null; if (numCheItems || numSelItems){ if (isCheckboxMode){ if (numCheItems){ Debug(" Getting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsChecked = GetSelectedItems(data); } if (numSelItems){ isCheckboxMode = SwitchCheckboxMode( false, data.func); Debug(" Getting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsSelected = GetSelectedItems(data); } } else { if (numSelItems){ Debug(" Getting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsSelected = GetSelectedItems(data); } if (numCheItems){ isCheckboxMode = SwitchCheckboxMode( true, data.func); Debug(" Getting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsChecked = GetSelectedItems(data); } } } Debug(' Refreshing ('+refCMD+')..'); cmd.RunCommand(refCMD); isCheckboxMode = (data.func.sourcetab.selstats.checkbox_mode==true); if (isCheckboxMode){ if(numCheItems){ Debug(" Setting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsChecked, data, (scroll && (prioCB || numSelItems==0))); data.func.sourcetab.Update(); } if(numSelItems){ isCheckboxMode = SwitchCheckboxMode( false, data.func); Debug(" Setting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsSelected, data, (scroll && (!prioCB || numCheItems==0))); } } else { if(numSelItems){ Debug(" Setting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsSelected, data, (scroll && (!prioCB || numCheItems==0))); data.func.sourcetab.Update(); } if(numCheItems){ isCheckboxMode = SwitchCheckboxMode( true, data.func); Debug(" Setting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsChecked, data, (scroll && (prioCB || numSelItems==0))); } } if (wasCheckboxMode && !isCheckboxMode){ Debug(" Resetting to checkbox mode"); isCheckboxMode = SwitchCheckboxMode( true, data.func); } return COMMAND_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// function GetSelectedItems(data){ if (data.func.sourcetab.selected.count != 0) return data.func.sourcetab.selected; return null; } /////////////////////////////////////////////////////////////////////////////// function Debug(text){ if (Script.config.DebugOutput) DOpus.Output(text); } /////////////////////////////////////////////////////////////////////////////// function SelectItems(items, data, makevisible){ var cmd = DOpus.NewCommand(); cmd.SetSourceTab(data.func.sourcetab); cmd.ClearFiles(); cmd.AddFiles(items); var cmdLine = 'Select '+(makevisible?'MAKEVISIBLE ':'')+'EXACT FROMSCRIPT'; Debug(" CMD: " + cmdLine); cmd.AddLine(cmdLine); cmd.Run(); } /////////////////////////////////////////////////////////////////////////////// function SwitchCheckboxMode( truefalse, func){ var targetState = "off"; if (truefalse) targetState = "on"; Debug(' Set CheckboxMode='+targetState); func.command.RunCommand('Set CheckboxMode='+targetState); func.sourcetab.Update(); return func.sourcetab.stats.checkbox_mode; } /////////////////////////////////////////////////////////////////////////////// function GetParameter( data, templateName, configName, defaultValue){ if (templateName && data.func.args.got_arg[templateName]) return data.func.args[templateName] if (configName && Script.config[configName]!=undefined){ switch(typeof(Script.config[configName])){ case "string": if(Script.config[configName]!=="") return Script.config[configName]; break; case "boolean": return Script.config[configName]; } } return defaultValue; }