Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | /* Copyright 2016 The Chromium Authors. All Rights Reserved. |
| 2 | * |
| 3 | * Use of this source code is governed by a BSD-style |
| 4 | * license that can be found in the LICENSE file or at |
| 5 | * https://developers.google.com/open-source/licenses/bsd |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * This file contains JS code for editing fields and field definitions. |
| 10 | */ |
| 11 | |
| 12 | var TKR_fieldNameXmlHttp; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Function that communicates with the server. |
| 17 | * @param {string} projectName Current project name. |
| 18 | * @param {string} fieldName The proposed field name. |
| 19 | */ |
| 20 | async function TKR_checkFieldNameOnServer(projectName, fieldName) { |
| 21 | fieldName = fieldName.toLowerCase(); |
| 22 | |
| 23 | const fieldNameMessage = { |
| 24 | project_name: projectName, |
| 25 | field_name: fieldName, |
| 26 | }; |
| 27 | const labelOptionsMessage = { |
| 28 | project_name: projectName, |
| 29 | }; |
| 30 | const responses = await Promise.all([ |
| 31 | window.prpcClient.call( |
| 32 | 'monorail.Projects', 'CheckFieldName', fieldNameMessage), |
| 33 | window.prpcClient.call( |
| 34 | 'monorail.Projects', 'GetLabelOptions', labelOptionsMessage), |
| 35 | ]); |
| 36 | |
| 37 | const fieldNameResponse = responses[0]; |
| 38 | const labelsResponse = responses[1]; |
| 39 | |
| 40 | $('fieldnamefeedback').textContent = fieldNameResponse.error || ''; |
| 41 | $('submit_btn').disabled = fieldNameResponse.error ? 'disabled' : ''; |
| 42 | |
| 43 | const maskedLabels = (labelsResponse.labelOptions || []).filter( |
| 44 | label_def => label_def.label.toLowerCase().startsWith(fieldName + '-')); |
| 45 | |
| 46 | if (maskedLabels.length === 0) { |
| 47 | enableOtherTypeOptions(false); |
| 48 | } else { |
| 49 | const prefixLength = fieldName.length + 1; |
| 50 | const padLength = Math.max.apply(null, maskedLabels.map( |
| 51 | label_def => label_def.label.length - prefixLength)); |
| 52 | const choicesLines = maskedLabels.map(label_def => { |
| 53 | // Strip the field name from the label. |
| 54 | const choice = label_def.label.substr(prefixLength); |
| 55 | return choice.padEnd(padLength) + ' = ' + label_def.docstring; |
| 56 | }); |
| 57 | $('choices').textContent = choicesLines.join('\n'); |
| 58 | $('field_type').value = 'enum_type'; |
| 59 | $('choices_row').style.display = ''; |
| 60 | enableOtherTypeOptions(true); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | function enableOtherTypeOptions(disabled) { |
| 66 | let type_option_el = $('field_type').firstChild; |
| 67 | while (type_option_el) { |
| 68 | if (type_option_el.tagName == 'OPTION') { |
| 69 | if (type_option_el.value != 'enum_type') { |
| 70 | type_option_el.disabled = disabled ? 'disabled' : ''; |
| 71 | } |
| 72 | } |
| 73 | type_option_el = type_option_el.nextSibling; |
| 74 | } |
| 75 | } |