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