blob: 633d70bb91f821d51d48a8b530aa45e8c346fc90 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* 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 components and component definitions.
10 */
11
12var TKR_leafNameXmlHttp;
13
14var TKR_leafNameRE = /^[a-zA-Z]([-_]?[a-zA-Z0-9])+$/;
15var TKR_oldName = '';
16
17/**
18 * Function to validate the component leaf name..
19 * @param {string} projectName Current project name.
20 * @param {string} parentPath Path to this component's parent.
21 * @param {string} originalName Original leaf name, keeping that is always OK.
22 * @param {string} token security token.
23 */
24function TKR_checkLeafName(projectName, parentPath, originalName, token) {
25 var name = $('leaf_name').value;
26 var feedback = $('leafnamefeedback');
27 if (name == originalName) {
28 $('submit_btn').disabled = '';
29 feedback.textContent = '';
30 } else if (name != TKR_oldName) {
31 $('submit_btn').disabled = 'disabled';
32 if (name == '') {
33 feedback.textContent = 'Please choose a name';
34 } else if (!TKR_leafNameRE.test(name)) {
35 feedback.textContent = 'Invalid component name';
36 } else if (name.length > 30) {
37 feedback.textContent = 'Name is too long';
38 } else {
39 TKR_checkLeafNameOnServer(projectName, parentPath, name, token);
40 }
41 }
42 TKR_oldName = name;
43}
44
45
46
47/**
48 * Function that communicates with the server.
49 * @param {string} projectName Current project name.
50 * @param {string} leafName The proposed leaf name.
51 * @param {string} token security token.
52 */
53async function TKR_checkLeafNameOnServer(projectName, parentPath, leafName) {
54 const message = {
55 project_name: projectName,
56 parent_path: parentPath,
57 component_name: leafName
58 };
59 const response = await window.prpcClient.call(
60 'monorail.Projects', 'CheckComponentName', message);
61
62 $('leafnamefeedback').textContent = response.error || '';
63 $('submit_btn').disabled = response.error ? 'disabled' : '';
64}