by PJFromOverHere » Thu Apr 07, 2011 3:20 pm
theChoices, as stated, would be either a Javascript associative array, or a simple numeric-indexed array. Programmer's choice depending on the situation. Associative arrays are quite valid in JS so why restrict to simple arrays?
As to the request text parameter, good idea, so that the rationale for the dialog can be explained a little further.
returning undefined on cancel, I think, is still valid (i.e. to quote RUSH from "Freewill": "If you choose not to decide you still have made a choice") and therefore always pre-selecting the first item would reduce functionality. Having a programmer-defined default choice allows the user to simply hit OK (or press Enter) to make the default choice which is good. Also, it may not always be desirable to consider the first item the default (i.e. I work for the Canadian government. If, working on a public or internal app, I were to display a dialog asking "What's your preferred language?" and I defaulted it to "English" I'd get skewered.)
I was thinking the dialog would have option buttons but a listbox would be fine. An extra parameter could be AllowMultiple boolean which would display a multi-select listbox or checkboxes instead of radio buttons and the return would be an array [of numeric indexes or string key values depending on the Choices array supplied] instead of a single value.
And, if the programmer decides there is to be no cancel then allow that as a parameter, too. But always have an "OK" or "Continue" button so that the closing of the dialog isn't immediate upon making a choice (which is necessary if allowing multiple).
The reason I figured for allowing an associative array is that the letters {restrict the array keys to single-letter} can be used as the selectors, like with a console menu.
( ) A: ChoiceA
( ) B: ChoiceB
( ) X: Exit
or, if Multi-select
[ ] M: Male
[ ] T: Tall
[ ] X: Executive
{substituting actual radio or checkbox controls for the ( ) and [ ] and including the infoText above that}
so, extending:
Syntax could now be:
var selection = optionsdlg(windowTitle, infoText, defaultChoice, theChoices, allowMultiple, allowCancel);
ex1:
var windowTitle = 'Make your choice';
var infoText= "text above listbox,\nfor example:\nPlease choose line termination type:";
var theChoices = {a:'ChoiceA', b:'ChoiceB', X: 'Exit'};
var defaultChoice = 'X'; // undefined = no default choice
var mychoice = optionsdlg(windowTitle , infoText, defaultChoice ,theChoices , false, true); //mychoice is a string
var mychoice = optionsdlg(windowTitle , infoText, defaultChoice ,theChoices , true, true); //mychoice is an array of strings
ex2:
var theChoices = {"DOS","UNIX","MAC"};
var defaultChoice = -1; // -1 or undefined = no default choice
var mychoice = optionsdlg(windowTitle , infoText, defaultChoice ,theChoices , false, true); //mychoice is an integer
var mychoice = optionsdlg(windowTitle , infoText, defaultChoice ,theChoices , true, true); //mychoice is an array of integers
I think this would give script writers an extra level of interaction with their users. It's somewhat like being given the power of creating a userform from within Javascript (albeit with limited functionality). I think this complements alert(), confirm(), and prompt() nicely.