|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2021, Oracle and/or its affiliates. |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | +let promptId = null; |
| 7 | +let promptOptions = null; |
| 8 | + |
| 9 | +function promptError(error) { |
| 10 | + if (error instanceof Error) { |
| 11 | + error = error.message; |
| 12 | + } |
| 13 | + |
| 14 | + window.api.ipc.sendSync('prompt-error:' + promptId, error); |
| 15 | +} |
| 16 | + |
| 17 | +function promptCancel() { |
| 18 | + window.api.ipc.sendSync('prompt-post-data:' + promptId, null); |
| 19 | +} |
| 20 | + |
| 21 | +function promptSubmit() { |
| 22 | + let data = null; |
| 23 | + |
| 24 | + if(promptOptions.fields) { |
| 25 | + data = {}; |
| 26 | + promptOptions.fields.forEach(field => { |
| 27 | + const inputElement = document.getElementById(field.id); |
| 28 | + data[field.id] = inputElement.value; |
| 29 | + }); |
| 30 | + |
| 31 | + } else { |
| 32 | + const dataElement = document.querySelector('#data'); |
| 33 | + |
| 34 | + if (promptOptions.type === 'input') { |
| 35 | + data = dataElement.value; |
| 36 | + } else if (promptOptions.type === 'select') { |
| 37 | + if (promptOptions.selectMultiple) { |
| 38 | + data = dataElement.querySelectorAll('option[selected]').map(o => o.getAttribute('value')); |
| 39 | + } else { |
| 40 | + data = dataElement.value; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + window.api.ipc.sendSync('prompt-post-data:' + promptId, data); |
| 46 | +} |
| 47 | + |
| 48 | +function promptCreateInput(promptOptions) { |
| 49 | + const dataElement = document.createElement('input'); |
| 50 | + dataElement.setAttribute('type', 'text'); |
| 51 | + |
| 52 | + if (promptOptions.value) { |
| 53 | + dataElement.value = promptOptions.value; |
| 54 | + } else { |
| 55 | + dataElement.value = ''; |
| 56 | + } |
| 57 | + |
| 58 | + if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') { |
| 59 | + for (const k in promptOptions.inputAttrs) { |
| 60 | + if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + dataElement.setAttribute(k, promptOptions.inputAttrs[k]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + dataElement.addEventListener('keyup', event => { |
| 69 | + if (event.key === 'Escape') { |
| 70 | + promptCancel(); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + dataElement.addEventListener('keypress', event => { |
| 75 | + if (event.key === 'Enter') { |
| 76 | + event.preventDefault(); |
| 77 | + document.querySelector('#ok').click(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + return dataElement; |
| 82 | +} |
| 83 | + |
| 84 | +function promptCreateSelect() { |
| 85 | + const dataElement = document.createElement('select'); |
| 86 | + let optionElement; |
| 87 | + |
| 88 | + for (const k in promptOptions.selectOptions) { |
| 89 | + if (!Object.prototype.hasOwnProperty.call(promptOptions.selectOptions, k)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + optionElement = document.createElement('option'); |
| 94 | + optionElement.setAttribute('value', k); |
| 95 | + optionElement.textContent = promptOptions.selectOptions[k]; |
| 96 | + if (k === promptOptions.value) { |
| 97 | + optionElement.setAttribute('selected', 'selected'); |
| 98 | + } |
| 99 | + |
| 100 | + dataElement.append(optionElement); |
| 101 | + } |
| 102 | + |
| 103 | + return dataElement; |
| 104 | +} |
| 105 | + |
| 106 | +function promptRegister() { |
| 107 | + promptId = document.location.hash.replace('#', ''); |
| 108 | + |
| 109 | + try { |
| 110 | + promptOptions = JSON.parse(window.api.ipc.sendSync('prompt-get-options:' + promptId)); |
| 111 | + } catch (error) { |
| 112 | + return promptError(error); |
| 113 | + } |
| 114 | + |
| 115 | + const labelContainer = document.querySelector('#label'); |
| 116 | + if (labelContainer) { |
| 117 | + if (promptOptions.useHtmlLabel) { |
| 118 | + labelContainer.innerHTML = promptOptions.label; |
| 119 | + } else { |
| 120 | + labelContainer.textContent = promptOptions.label; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (promptOptions.buttonLabels && promptOptions.buttonLabels.ok) { |
| 125 | + document.querySelector('#ok').textContent = promptOptions.buttonLabels.ok; |
| 126 | + } |
| 127 | + |
| 128 | + if (promptOptions.buttonLabels && promptOptions.buttonLabels.cancel) { |
| 129 | + document.querySelector('#cancel').textContent = promptOptions.buttonLabels.cancel; |
| 130 | + } |
| 131 | + |
| 132 | + document.querySelector('#form').addEventListener('submit', promptSubmit); |
| 133 | + document.querySelector('#cancel').addEventListener('click', promptCancel); |
| 134 | + |
| 135 | + if (promptOptions.fields) { |
| 136 | + const formElement = document.querySelector('#form'); |
| 137 | + const buttonContainer = document.querySelector('#buttons'); |
| 138 | + |
| 139 | + promptOptions.fields.forEach(field => { |
| 140 | + if(field.label) { |
| 141 | + const labelContainer = document.createElement('div'); |
| 142 | + labelContainer.setAttribute('class', 'label'); |
| 143 | + labelContainer.innerHTML = field.label; |
| 144 | + formElement.insertBefore(labelContainer, buttonContainer); |
| 145 | + } |
| 146 | + |
| 147 | + const dataElement = createDataElement(field); |
| 148 | + if(dataElement) { |
| 149 | + dataElement.setAttribute('id', field.id); |
| 150 | + dataElement.setAttribute('class', 'data'); |
| 151 | + const dataContainer = document.createElement('div'); |
| 152 | + dataContainer.setAttribute('class', 'data-container'); |
| 153 | + dataContainer.append(dataElement); |
| 154 | + formElement.insertBefore(dataContainer, buttonContainer); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + } else { |
| 159 | + const dataContainerElement = document.querySelector('#data-container'); |
| 160 | + |
| 161 | + const dataElement = createDataElement(promptOptions); |
| 162 | + if(!dataElement) { |
| 163 | + return promptError(`Unhandled input type '${promptOptions.type}'`); |
| 164 | + } |
| 165 | + |
| 166 | + dataContainerElement.append(dataElement); |
| 167 | + dataElement.setAttribute('id', 'data'); |
| 168 | + |
| 169 | + dataElement.focus(); |
| 170 | + if (promptOptions.type === 'input') { |
| 171 | + dataElement.select(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + const height = document.querySelector('body').offsetHeight; |
| 176 | + window.api.ipc.sendSync('prompt-size:' + promptId, height); |
| 177 | +} |
| 178 | + |
| 179 | +function createDataElement(promptOptions) { |
| 180 | + let dataElement; |
| 181 | + if (promptOptions.type === 'input') { |
| 182 | + dataElement = promptCreateInput(promptOptions); |
| 183 | + } else if (promptOptions.type === 'select') { |
| 184 | + dataElement = promptCreateSelect(); |
| 185 | + } else { |
| 186 | + dataElement = null; |
| 187 | + } |
| 188 | + return dataElement; |
| 189 | +} |
| 190 | + |
| 191 | +window.addEventListener('error', error => { |
| 192 | + if (promptId) { |
| 193 | + promptError('An error has occurred on the prompt window: \n' + error); |
| 194 | + } |
| 195 | +}); |
| 196 | + |
| 197 | +document.addEventListener('DOMContentLoaded', promptRegister); |
0 commit comments