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