1/* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16function readCsv() { 17 return new Promise(async (resolve) => { 18 await fetch('/api/getCsv') 19 .then(response => response.json()) 20 .then(testCases => { 21 resolve(testCases); 22 }) 23 .catch((error) => { 24 throw error; 25 }); 26 }); 27} 28 29function createCheckboxCell(name, value, caseTree, id) { 30 let result; 31 const tdEle = document.createElement('td'); 32 const newElement = document.createElement('input'); 33 const buttonEle = document.createElement('button'); 34 const textEle = document.createTextNode(value); 35 newElement.type = 'checkbox'; 36 if ("Project Name" === name) { 37 newElement.id = id; 38 result = findPathByName(caseTree, id); 39 } else if ("Component Name" === name) { 40 newElement.id = id; 41 result = findPathByName(caseTree, id); 42 } else { 43 newElement.id = value; 44 result = findPathByName(caseTree, id); 45 } 46 newElement.value = JSON.stringify([value, name, result]); 47 48 newElement.addEventListener('change', changeHandler.bind(null, caseTree, newElement)); 49 buttonEle.innerHTML = '-'; 50 buttonEle.style.width = '18px'; 51 buttonEle.style.height = '18px'; 52 if (name === 'Project Name') { 53 buttonEle.className = 'checkBoxButton'; 54 } 55 if (name === 'Project Name') { 56 buttonEle.addEventListener('click', foldTr.bind(null, value, name)); 57 tdEle.appendChild(buttonEle); 58 } else if (name === 'Component Name') { 59 buttonEle.addEventListener('click', foldTr.bind(null, id, name)); 60 tdEle.appendChild(buttonEle); 61 } 62 tdEle.appendChild(newElement); 63 tdEle.appendChild(textEle); 64 return tdEle; 65} 66 67function run(list, pr) { 68 let data = JSON.stringify({list, pr}); 69 return new Promise(async (resolve, reject) => { 70 const options = { 71 method: 'POST', 72 headers: { 73 'Content-Type': 'application/json', 74 }, 75 body: data 76 }; 77 await fetch('/api/runCase', options) 78 .then(response => response.json()) 79 .then(mes => { 80 resolve(mes); 81 }) 82 .catch((error) => { 83 throw error; 84 }); 85 }); 86} 87 88function getIdByName(nameList) { 89 let idList = []; 90 nameList[2].forEach((val) => { 91 if (nameList[1] === "Project Name") { 92 idList.push(componentMap.get(`${val[0]}_${val[1]}`), val[2]); 93 } else if (nameList[1] === "Component Name") { 94 idList.push(val[2]); 95 } 96 }); 97 return uniqueArray(idList); 98} 99 100function findPathByName(data, targetName) { 101 let values = []; 102 for (let item of data) { 103 if (item.id === targetName) { 104 if (item.type === "Project Name") { 105 for (let subItem of item.value) { 106 values = values.concat(findPathByName(item.value, subItem.id)); 107 } 108 return values; 109 } else if (Array.isArray(item.value)) { 110 for (let subItem of item.value) { 111 values.push(subItem.path); 112 } 113 return values; 114 } else if (!('type' in item)) { 115 return [item.path]; 116 } 117 } else if (Array.isArray(item.value)) { 118 let result = findPathByName(item.value, targetName); 119 if (result !== undefined) { 120 return result; 121 } 122 } 123 } 124 return undefined; 125} 126 127function listToTree(data) { 128 const result = []; 129 const projectMap = {}; 130 data.forEach(item => { 131 const projectName = item["Project Name"]; 132 const componentName = item["Component Name"]; 133 const fileName = item["File Name"]; 134 const id = item["id"]; 135 if (!projectMap[projectName]) { 136 projectMap[projectName] = { 137 type: "Project Name", 138 name: projectName, 139 path: [projectName], 140 value: [], 141 id: `${projectName}${id}` 142 }; 143 result.push(projectMap[projectName]); 144 } 145 let componentEntry = projectMap[projectName].value.find(component => component.name === componentName); 146 if (!componentEntry) { 147 componentEntry = { 148 type: "Component Name", 149 name: componentName, 150 path: [projectName, componentName], 151 value: [], 152 id: `${projectName}_${componentName}_${id}` 153 }; 154 projectMap[projectName].value.push(componentEntry); 155 } 156 componentEntry.value.push({ 157 path: [projectName, componentName, fileName], 158 name: fileName, 159 id: fileName 160 }); 161 }); 162 return result; 163} 164 165function changeHandler(componentMap, checkbox, event) { 166 const value = checkbox.id; 167 let checkboxData = JSON.parse(checkbox.value); 168 if ("Component Name" === JSON.parse(checkbox.value)[1]) { 169 let array = JSON.parse(checkbox.value)[2]; 170 let arrayData = []; 171 array.forEach(e => { 172 if (e[1] === checkboxData[0]) { 173 arrayData.push(e); 174 } 175 }) 176 checkboxData[2] = arrayData; 177 } 178 const selectList = getIdByName(checkboxData); 179 let list = []; 180 selectList.forEach((val) => { 181 list.push(val); 182 }) 183 184 list.forEach((val) => { 185 const otherCheckbox = document.getElementById(val); 186 if (otherCheckbox) { 187 otherCheckbox.checked = event.target.checked; 188 } 189 }); 190 let data = document.getElementById(value + 2) ? document.getElementById(value + 2) : document.getElementById(value); 191 192 let valueList = JSON.parse(data.value)[2]; 193 if (event.target.checked) { 194 checkboxLst = checkboxLst.concat(valueList); 195 } else { 196 valueList.forEach((val) => { 197 let index = checkboxLst.findIndex(subArray => JSON.stringify(subArray) === JSON.stringify(val)); 198 checkboxLst.splice(index, 1); 199 }) 200 } 201} 202 203function foldTr(value, name, event) { 204 let elementList = document.querySelectorAll(`tr[${value}]`); 205 const componentButton = elementList[0].getElementsByTagName('td')[1].querySelector('button'); 206 let isFoldStatus = false; 207 for (let i = 1; i < elementList.length; i++) { 208 if (name === 'Project Name') { 209 if (event.target.textContent === '-') { 210 componentButton.disabled = true; 211 if (!elementList[i].classList.contains('hidden')) { 212 elementList[i].classList.add('hidden'); 213 } 214 isFoldStatus = true; 215 } else { 216 elementList[i].classList.remove('hidden'); 217 componentButton.disabled = false; 218 componentButton.textContent = '-'; 219 isFoldStatus = false; 220 } 221 } else { 222 if (elementList[i].classList.contains('hidden')) { 223 elementList[i].classList.remove('hidden'); 224 isFoldStatus = false; 225 } else { 226 elementList[i].classList.add('hidden'); 227 isFoldStatus = true; 228 } 229 } 230 } 231 isFoldStatus ? event.target.textContent = '+' : event.target.textContent = '-'; 232} 233 234function uniqueArray(arr) { 235 return [...new Set(arr.map(JSON.stringify))].map(JSON.parse); 236}