Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * This file contains JS code for editing components and component definitions. |
| 7 | */ |
| 8 | |
| 9 | var TKR_leafNameXmlHttp; |
| 10 | |
| 11 | var TKR_leafNameRE = /^[a-zA-Z]([-_]?[a-zA-Z0-9])+$/; |
| 12 | var 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 | */ |
| 21 | function 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 | */ |
| 50 | async 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 | } |