/////////////////////////////////////////////////////////////////////////////// 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.2"; 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,GOREFRESHOPTIONS/K,GOPARAMS/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('GoRefreshOptions', ""). des('Refresh options for "Go REFRESH". I do not recommend adding options here, put them directly in the button with the GOREFRESHOPTIONS parameter, as you cannot override to default="" anymore.'); cfg.add('GoParams', ""). des('Additional params for the "Go" command used internally (lookup the docs for additional "Go" switches and options, "REFRESHTHUMBNAILS" maybe handy for example).'); } /////////////////////////////////////////////////////////////////////////////// var COMMAND_FAILURE = true; var COMMAND_SUCCESS = false; /////////////////////////////////////////////////////////////////////////////// function Command_GoRefreshKeepSelection(data){ Debug("GoRefreshKeepSelection():"); data.func.command.ClearFiles(); var settings = { scroll : GetParameter(data, "NOSCROLLTOSELECTION", "NoScrollToSelection", false)==false, prioCB : GetParameter(data, "SCROLLTOCHECKED", "ScrollToChecked", false) } var refOps = GetParameter(data, "GOREFRESHOPTIONS", "GoRefreshOptions", ""). toLowerCase(). replace(/\s*$/,'').replace(/^\s*/,''). //trim whitespaces replace('all',"both,tree"); var params = GetParameter(data, "GOPARAMS", "GoParams", ""); if (params!="") params = " "+params; var dual = data.func.sourcetab.lister.dual; var both = refOps.indexOf('both')!=-1; var DEFAULT = (refOps==''); var SOURCE = ((refOps.indexOf('source')!=-1) || both); var DEST = ((refOps.indexOf('dest')!=-1 || both) && dual); var LEFT = ((refOps.indexOf('left')!=-1) || both) && !(SOURCE || DEST); var RIGHT = ((refOps.indexOf('right')!=-1 || both) && dual) && !(SOURCE || DEST); Debug(" NOSCROLLTOSELECTION: " + (settings.scroll==false)); Debug(" SCROLLTOCHECKED : " + settings.prioCB); Debug(" GOREFRESHOPTIONS : " + refOps); Debug(" GOPARAMS : " + params); refOps = refOps.replace("source","").replace("dest","").replace("left",""). replace("right","").replace("both",""); refOps = refOps.replace(/\,\,/g,','); refOps = refOps.replace(/\,*$/,'').replace(/^\,*/,''); //trim "," if (refOps!="") refOps = ","+refOps; if (DEFAULT) RefreshTab(data.func.sourcetab, settings, "Go REFRESH"+(refOps!=""?"="+refOps:"")+params); if (SOURCE) RefreshTab(data.func.sourcetab, settings, "Go REFRESH=source"+refOps+params); if (DEST) RefreshTab(data.func.desttab, settings, "Go REFRESH=dest"+refOps+params); if (LEFT){ var leftTab = data.func.sourcetab.lister.activetab; if (leftTab.right) leftTab = data.func.sourcetab.lister.desttab; RefreshTab(leftTab, settings, "Go REFRESH=left"+refOps+params); } if (RIGHT){ var rightTab = data.func.sourcetab.lister.activetab; if (!rightTab.right) rightTab = data.func.sourcetab.lister.desttab; RefreshTab(rightTab, settings, "Go REFRESH=right"+refOps+params); } if (!DEFAULT && !SOURCE && !DEST && !LEFT && !RIGHT){ refOps = refOps.replace(/\,*$/,'').replace(/^\,*/,''); //trim "," var cmd = DOpus.NewCommand(); cmd.SetSourceTab(data.func.sourcetab); cmd.ClearFiles(); var cmdLine = "GO REFRESH="+refOps+params; Debug(" CMD: " + cmdLine); cmd.AddLine(cmdLine); cmd.Run(); } return COMMAND_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// function RefreshTab( tab, settings, goRefreshCmdLine){ Debug(" RefreshTab():"); var wasCheckboxMode = tab.stats.checkbox_mode==true; var isCheckboxMode = wasCheckboxMode; var numSelItems = tab.stats.selitems; var numCheItems = tab.stats.checkeditems; var s = settings; Debug(" Path : " + tab.path); Debug(" SelItems: " + numSelItems); Debug(" CheItems: " + numCheItems); var itemsSelected = null; var itemsChecked = null; if (numCheItems || numSelItems){ if (isCheckboxMode){ if (numCheItems){ Debug(" Getting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsChecked = GetSelectedItemsFromTab(tab); } if (numSelItems){ isCheckboxMode = SwitchCheckboxMode( false, tab); Debug(" Getting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsSelected = GetSelectedItemsFromTab(tab); } } else { if (numSelItems){ Debug(" Getting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsSelected = GetSelectedItemsFromTab(tab); } if (numCheItems){ isCheckboxMode = SwitchCheckboxMode( true, tab); Debug(" Getting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); itemsChecked = GetSelectedItemsFromTab(tab); } } } Debug(' Refreshing ('+goRefreshCmdLine+')..'); DOpus.NewCommand.RunCommand(goRefreshCmdLine); tab.Update(); isCheckboxMode = (tab.stats.checkbox_mode==true); if (isCheckboxMode){ if(numCheItems){ Debug(" Setting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsChecked, tab, (s.scroll && wasCheckboxMode && (s.prioCB || numSelItems==0))); } if(numSelItems){ isCheckboxMode = SwitchCheckboxMode( false, tab); Debug(" Setting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsSelected, tab, (s.scroll && (!s.prioCB || numCheItems==0))); } } else { if(numSelItems){ Debug(" Setting selected items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsSelected, tab, (s.scroll && (!s.prioCB || numCheItems==0))); } if(numCheItems){ isCheckboxMode = SwitchCheckboxMode( true, tab); Debug(" Setting checked items in "+(isCheckboxMode ? "CheckboxMode" : "RegularMode")); SelectItems(itemsChecked, tab, (s.scroll && wasCheckboxMode && (s.prioCB || numSelItems==0))); } } if (wasCheckboxMode && !isCheckboxMode){ Debug(" Resetting to checkbox mode"); isCheckboxMode = SwitchCheckboxMode( true, tab); } if (!wasCheckboxMode && isCheckboxMode){ Debug(" Resetting to non-checkbox mode"); isCheckboxMode = SwitchCheckboxMode( false, tab); } return COMMAND_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// function GetSelectedItemsFromTab(tab){ if (tab.selected.count != 0) return tab.selected; return null; } /////////////////////////////////////////////////////////////////////////////// function Debug(text){ if (Script.config.DebugOutput) DOpus.Output(text); } /////////////////////////////////////////////////////////////////////////////// function SelectItems(items, tab, makevisible){ var cmd = DOpus.NewCommand(); cmd.SetSourceTab(tab); cmd.ClearFiles(); cmd.AddFiles(items); var cmdLine = 'Select '+(makevisible==true?'MAKEVISIBLE ':'')+'EXACT FROMSCRIPT'; Debug(" CMD: " + cmdLine); cmd.AddLine(cmdLine); //Debug('# Selected :'+tab.selected.count); cmd.Run(); tab.Update(); //Debug('# Selecting:'+items.count); //Debug('# Selected :'+tab.selected.count); } /////////////////////////////////////////////////////////////////////////////// function SwitchCheckboxMode( truefalse, tab){ var targetState = "off"; if (truefalse) targetState = "on"; Debug(' Set CheckboxMode='+targetState); var cmd = DOpus.NewCommand(); cmd.SetSourceTab(tab); cmd.RunCommand('Set CheckboxMode='+targetState); tab.Update(); var abort = 0; while ((truefalse != tab.selstats.checkbox_mode) && (abort++ < 100)){ Debug('E Setting CheckboxMode failed to set:' + tab.selstats.checkbox_mode + " target:"+targetState); DOpus.Delay(10); tab.Update(); } if (abort){ Debug('E Setting CheckboxMode finally:' + tab.selstats.checkbox_mode); Debug('E TOOK: '+ abort*10 +' milliseconds!'); } return tab.selstats.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; }