options.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //@ts-check
  2. // This script will be run within the webview itself
  3. // It cannot access the main VS Code APIs directly.
  4. (function () {
  5. const vscode = acquireVsCodeApi();
  6. const oldState = vscode.getState() || { options: [] };
  7. let options = oldState.options || [];
  8. refresh(options);
  9. // Handle messages sent from the extension to the webview
  10. window.addEventListener('message', event => {
  11. const message = event.data; // The json data that the extension sent
  12. switch (message.type) {
  13. case 'options':
  14. {
  15. refresh(message.data);
  16. break;
  17. }
  18. }
  19. });
  20. // Recreate the options UI from the incoming options definition
  21. function refresh(new_options){
  22. // sort new options
  23. new_options.sort((a, b) => {
  24. if(a.name < b.name) return -1;
  25. else if(a.name > b.name) return 1;
  26. else return 0;
  27. })
  28. // store option so that web page can be re-loaded and refreshed
  29. options = new_options;
  30. vscode.setState({ options: new_options });
  31. // clear existing option inputs
  32. const optionsItem = document.getElementById('options');
  33. optionsItem.innerHTML = "";
  34. for(const option of options){
  35. // create the label
  36. const labelItem = document.createElement("label");
  37. labelItem.className = "options-label";
  38. labelItem.textContent = option.name;
  39. optionsItem.append(labelItem);
  40. var optionValues = [];
  41. if (option.value != null && option.value != undefined) {
  42. optionValues = findOptionValues(option.value.toString(), option.values);
  43. }
  44. // create the value input
  45. // if there is a list of values then a drop down combo is used
  46. // otherwise a text input is used
  47. if(optionValues.length > 0)
  48. {
  49. // selection with combo
  50. const selectItem = document.createElement("select");
  51. selectItem.id = option.name + "Input";
  52. selectItem.oninput = onOptionsChange;
  53. for(let value of optionValues){
  54. const optionElement = document.createElement("option");
  55. optionElement.value = value;
  56. optionElement.textContent = value;
  57. if(option.value == value)
  58. optionElement.selected = true;
  59. selectItem.append(optionElement);
  60. }
  61. optionsItem.append(selectItem);
  62. }
  63. else
  64. {
  65. // no known values, use a text input
  66. const inputItem = document.createElement("input");
  67. inputItem.id = option.name + "Input";
  68. inputItem.type = "text";
  69. inputItem.value = option.value;
  70. inputItem.oninput = onOptionsChange;
  71. optionsItem.append(inputItem);
  72. }
  73. }
  74. }
  75. // options change event handler.
  76. // this is called whenever value of any option changes
  77. // read all the options and send them to the extension
  78. function onOptionsChange() {
  79. const optionValues = new Array();
  80. for(let option of options){
  81. const optionInput = document.getElementById(option.name + "Input");
  82. const optionValue = optionInput["value"];
  83. optionValues.push({name: option.name, value: optionValue});
  84. option.default = optionValue;
  85. }
  86. vscode.postMessage({type: "options", data: optionValues});
  87. vscode.setState({ options: options });
  88. }
  89. // Determine the list of option values depending on the user specified values and the default value
  90. // if the user has specified a list of values then use that list
  91. // If the default value is a known value (y,n,yes,no,true,false) then infer the possible values
  92. // Otherwise a list cannot be formed, a text input needs to be shown instead of a select.
  93. function findOptionValues(defaultValue, values){
  94. if(values != undefined && values.length > 0)
  95. return values;
  96. else if(defaultValue == 'y' || defaultValue == 'n')
  97. return ['y', 'n'];
  98. else if(defaultValue == 'yes' || defaultValue == 'no')
  99. return ['yes', 'no'];
  100. else if (defaultValue == 'true' || defaultValue == 'false')
  101. return ['true', 'false'];
  102. else
  103. return [];
  104. }
  105. }());