1/* 2 * Copyright (C) 2022 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 to 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 16import { BaseElement, element } from '../../../../base-ui/BaseElement'; 17import { LitTable } from '../../../../base-ui/table/lit-table'; 18import { MarkStruct } from '../../../bean/MarkStruct'; 19import { SpSystemTrace } from '../../SpSystemTrace'; 20import { SlicesTime, StType } from '../timer-shaft/SportRuler'; 21import { getTimeString } from './TabPaneCurrentSelection'; 22import { TabPaneCurrentHtml } from './TabPaneCurrent.html'; 23 24@element('tabpane-current') 25export class TabPaneCurrent extends BaseElement { 26 private slicesTimeList: Array<SlicesTime> = []; 27 private slicesTime: SlicesTime | null = null; 28 private systemTrace: SpSystemTrace | undefined | null; 29 private tableDataSource: Array<MarkStruct | any> = []; 30 private panelTable: LitTable | undefined | null; 31 32 initElements(): void { 33 this.systemTrace = document 34 .querySelector('body > sp-application') 35 ?.shadowRoot!.querySelector<SpSystemTrace>('#sp-system-trace'); 36 this.shadowRoot?.querySelector('#text')?.addEventListener('keyup', (event: any) => { 37 event.stopPropagation(); 38 if (event.keyCode == '13') { 39 if (this.slicesTime) { 40 window.publish(window.SmartEvent.UI.KeyboardEnable, { 41 enable: true, 42 }); 43 this.slicesTime.text = event?.target.value; 44 document.dispatchEvent( 45 new CustomEvent('slices-change', { 46 detail: this.slicesTime, 47 }) 48 ); 49 } 50 } 51 }); 52 this.shadowRoot?.querySelector('#text')?.addEventListener('blur', (event: any) => { 53 (window as any).flagInputFocus = false; 54 window.publish(window.SmartEvent.UI.KeyboardEnable, { 55 enable: true, 56 }); 57 }); 58 this.shadowRoot?.querySelector('#text')?.addEventListener('focus', (event: any) => { 59 (window as any).flagInputFocus = true; 60 window.publish(window.SmartEvent.UI.KeyboardEnable, { 61 enable: false, 62 }); 63 }); 64 this.panelTable = this.shadowRoot!.querySelector<LitTable>('.notes-editor-panel'); 65 this.rowClickListener(); 66 this.mouseOutListener(); 67 } 68 69 private rowClickListener(): void { 70 this.panelTable!.addEventListener('row-click', (evt: any) => { 71 if (evt.detail.data.startTime === undefined) { 72 return; 73 } 74 // 点击表格某一行后,背景变色 75 // @ts-ignore 76 let data = evt.detail.data; 77 this.systemTrace!.slicesList = this.slicesTimeList || []; 78 // 页面上对应的slice变为实心 79 this.slicesTimeList.forEach((slicesTime, index) => { 80 if (data.startTime === slicesTime.startTime && data.endTime === slicesTime.endTime) { 81 slicesTime.selected = true; 82 this.setTableSelection(index + 1); 83 } else { 84 slicesTime.selected = false; 85 } 86 this.systemTrace?.timerShaftEL!.sportRuler!.draw(); 87 }); 88 }); 89 } 90 91 private mouseOutListener(): void { 92 // 当鼠标移出panel时重新加载备注信息 93 this.systemTrace?.shadowRoot?.querySelector('trace-sheet')?.addEventListener( 94 'mouseout', 95 (event: any) => { 96 if (this.slicesTimeList.length === 0) { 97 return; 98 } 99 let tr = this.panelTable!.shadowRoot!.querySelectorAll('.tr') as NodeListOf<HTMLDivElement>; 100 // 第一个tr是移除全部,所以跳过,从第二个tr开始,和this.slicesTimeList数组的第一个对应……,所以i从1开始,在this.slicesTimeList数组中取值时用i-1 101 for (let i = 1; i < tr.length; i++) { 102 tr[i].querySelector<HTMLInputElement>('#text-input')!.value = this.slicesTimeList[i - 1].text; 103 } 104 event.stopPropagation(); 105 }, 106 {capture: true} 107 ); 108 } 109 110 public setCurrentSlicesTime(slicesTime: SlicesTime): void { 111 this.slicesTimeList = this.systemTrace?.timerShaftEL!.sportRuler?.slicesTimeList || []; 112 this.slicesTime = slicesTime; 113 // 判断当前传入的卡尺是否已经存在 114 let findSlicesTime = this.slicesTimeList.find( 115 (it) => it.startTime === slicesTime.startTime && it.endTime === slicesTime.endTime 116 ); 117 // m键生成的临时卡尺只能同时出现最后一个,所以将永久卡尺过滤出来,并加上最后一个临时卡尺 118 if (this.slicesTime.type === StType.TEMP) { 119 this.slicesTimeList = this.slicesTimeList.filter( 120 (item: SlicesTime) => 121 item.type === StType.PERM || 122 (item.type === StType.TEMP && 123 item.startTime === this.slicesTime!.startTime && 124 item.endTime === this.slicesTime!.endTime) 125 ); 126 } 127 // 如果this.slicesTimeList为空,或者没有在同一位置绘制过,就将当前的框选的范围画上线 128 if (!findSlicesTime || this.slicesTimeList.length === 0) { 129 this.slicesTimeList!.push(this.slicesTime); 130 } 131 this.setTableData(); 132 } 133 134 /** 135 * 根据this.slicesTimeList设置表格数据 136 */ 137 private setTableData(): void { 138 this.tableDataSource = []; 139 // 按照开始时间进行排序,保证泳道图上的卡尺(shift+m键)和表格的顺序一致 140 this.slicesTimeList.sort(function (a, b) { 141 return a.startTime - b.startTime; 142 }); 143 for (let slice of this.slicesTimeList) { 144 let btn = document.createElement('button'); 145 btn.className = 'remove'; 146 let color = document.createElement('input'); 147 color.type = 'color'; 148 let text = document.createElement('input'); 149 text.type = 'text'; 150 let sliceData = new MarkStruct( 151 btn, 152 color, 153 text, 154 getTimeString(slice.startTime), 155 slice.startTime, 156 getTimeString(slice.endTime), 157 slice.endTime 158 ); 159 color!.value = slice.color; 160 text.value = slice.text; 161 slice.selected === true ? (sliceData.isSelected = true) : (sliceData.isSelected = false); 162 this.tableDataSource.push(sliceData); 163 } 164 // 表格第一行只添加一个RemoveAll按钮 165 let removeAll = document.createElement('button'); 166 removeAll.className = 'removeAll'; 167 removeAll.innerHTML = 'RemoveAll'; 168 let sliceData = new MarkStruct(removeAll); 169 this.tableDataSource.unshift(sliceData); 170 171 // 当前点击了哪个卡尺,就将对应的表格中的那行的背景变色 172 this.tableDataSource.forEach((data, index) => { 173 if (data.startTime === this.slicesTime?.startTime && data.endTime === this.slicesTime?.endTime) { 174 this.setTableSelection(index); 175 } 176 }); 177 this.panelTable!.recycleDataSource = this.tableDataSource; 178 this.eventHandler(); 179 this.systemTrace!.slicesList = this.slicesTimeList || []; 180 } 181 182 /** 183 * 修改卡尺颜色事件和移除卡尺的事件处理 184 */ 185 private eventHandler(): void { 186 let tr = this.panelTable!.shadowRoot!.querySelectorAll('.tr') as NodeListOf<HTMLDivElement>; 187 tr[0].querySelector<HTMLInputElement>('#text-input')!.disabled = true; 188 this.trClickEvent(tr); 189 this.panelTableClick(tr); 190 191 // 第一个tr是移除全部,所以跳过,从第二个tr开始,和this.slicesTimeList数组的第一个对应……,所以i从1开始,在this.slicesTimeList数组中取值时用i-1 192 for (let i = 1; i < tr.length; i++) { 193 // 修改颜色 194 tr[i].querySelector<HTMLInputElement>('#color-input')!.value = this.slicesTimeList[i - 1].color; 195 // 点击色块修改颜色 196 this.trChangeEvent(tr, i); 197 198 // 修改备注 199 tr[i].querySelector<HTMLInputElement>('#text-input')!.value = this.slicesTimeList[i - 1].text; 200 // // 点击色块修改颜色 201 tr[i].querySelector('#text-input')?.addEventListener('keyup', (event: any) => { 202 if ( 203 this.tableDataSource[i].startTime === this.slicesTimeList[i - 1].startTime && 204 this.tableDataSource[i].endTime === this.slicesTimeList[i - 1].endTime && 205 event.keyCode == '13' 206 ) { 207 this.systemTrace!.slicesList = this.slicesTimeList || []; 208 this.slicesTimeList[i - 1].text = event?.target.value; 209 window.publish(window.SmartEvent.UI.KeyboardEnable, { 210 enable: true, 211 }); 212 document.dispatchEvent(new CustomEvent('slices-change', { detail: this.slicesTimeList[i - 1] })); 213 214 this.systemTrace?.refreshCanvas(true); 215 } 216 event.stopPropagation(); 217 }); 218 219 tr[i].querySelector('#text-input')?.addEventListener('blur', (event: any) => { 220 (window as any).flagInputFocus = false; 221 window.publish(window.SmartEvent.UI.KeyboardEnable, { 222 enable: true, 223 }); 224 if ( 225 this.tableDataSource[i].startTime === this.slicesTimeList[i - 1].startTime && 226 this.tableDataSource[i].endTime === this.slicesTimeList[i - 1].endTime 227 ) { 228 this.slicesTimeList[i - 1].text = event?.target.value; 229 document.dispatchEvent(new CustomEvent('slices-change', { detail: this.slicesTimeList[i - 1] })); 230 231 this.systemTrace?.refreshCanvas(true); 232 } 233 event.stopPropagation(); 234 }); 235 this.trFocusEvent(tr, i); 236 this.removeButtonClickEvent(tr, i); 237 } 238 } 239 240 private trChangeEvent(tr: NodeListOf<HTMLDivElement>, i: number): void { 241 tr[i].querySelector<HTMLInputElement>('#color-input')?.addEventListener('change', (event: any) => { 242 if ( 243 this.tableDataSource[i].startTime === this.slicesTimeList[i - 1].startTime && 244 this.tableDataSource[i].endTime === this.slicesTimeList[i - 1].endTime 245 ) { 246 this.systemTrace!.slicesList = this.slicesTimeList || []; 247 this.slicesTimeList[i - 1].color = event?.target.value; 248 document.dispatchEvent(new CustomEvent('slices-change', {detail: this.slicesTimeList[i - 1]})); 249 // 卡尺颜色改变时,重绘泳道图 250 this.systemTrace?.refreshCanvas(true); 251 } 252 event.stopPropagation(); 253 }); 254 } 255 256 private trFocusEvent(tr: NodeListOf<HTMLDivElement>, i: number): void { 257 tr[i].querySelector('#text-input')?.addEventListener('focus', (event: any) => { 258 (window as any).flagInputFocus = true; 259 window.publish(window.SmartEvent.UI.KeyboardEnable, { 260 enable: false, 261 }); 262 let tr = this.panelTable!.shadowRoot!.querySelectorAll('.tr') as NodeListOf<HTMLDivElement>; 263 // 第一个tr是移除全部,所以跳过,从第二个tr开始,和this.flagList数组的第一个对应……,所以i从1开始,在this.flagList数组中取值时用i-1 264 for (let i = 1; i < tr.length; i++) { 265 tr[i].querySelector<HTMLInputElement>('#text-input')!.value = this.slicesTimeList[i - 1].text; 266 } 267 }); 268 } 269 270 private trClickEvent(tr: NodeListOf<HTMLDivElement>): void { 271 tr[0].querySelector('.removeAll')!.addEventListener('click', (evt: any) => { 272 this.systemTrace!.slicesList = []; 273 let slicesTimeList = [...this.slicesTimeList]; 274 for (let i = 0; i < slicesTimeList.length; i++) { 275 slicesTimeList[i].hidden = true; 276 document.dispatchEvent(new CustomEvent('slices-change', {detail: slicesTimeList[i]})); 277 } 278 this.slicesTimeList = []; 279 return; 280 }); 281 } 282 283 private removeButtonClickEvent(tr: NodeListOf<HTMLDivElement>, i: number): void { 284 // 点击remove按钮移除 285 tr[i]!.querySelector('.remove')?.addEventListener('click', (event: any) => { 286 if ( 287 this.tableDataSource[i].startTime === this.slicesTimeList[i - 1].startTime && 288 this.tableDataSource[i].endTime === this.slicesTimeList[i - 1].endTime 289 ) { 290 this.slicesTimeList[i - 1].hidden = true; 291 this.systemTrace!.slicesList = this.slicesTimeList || []; 292 document.dispatchEvent(new CustomEvent('slices-change', {detail: this.slicesTimeList[i - 1]})); 293 // 移除时更新表格内容 294 this.setTableData(); 295 } 296 event.stopPropagation(); 297 }); 298 } 299 300 private panelTableClick(tr: NodeListOf<HTMLDivElement>): void { 301 // 更新备注信息 302 this.panelTable!.addEventListener('click', (event: any) => { 303 if (this.slicesTimeList.length === 0) { 304 return; 305 } 306 for (let i = 1; i < tr.length; i++) { 307 let inputValue = tr[i].querySelector<HTMLInputElement>('#text-input')!.value; 308 if ( 309 this.tableDataSource[i].startTime === this.slicesTimeList[i - 1].startTime && 310 this.tableDataSource[i].endTime === this.slicesTimeList[i - 1].endTime 311 ) { 312 this.slicesTimeList[i - 1].text = inputValue; 313 document.dispatchEvent(new CustomEvent('slices-change', {detail: this.slicesTimeList[i - 1]})); 314 // 旗子颜色改变时,重绘泳道图 315 this.systemTrace?.refreshCanvas(true); 316 } 317 } 318 }); 319 } 320 321 /** 322 * 修改表格指定行数的背景颜色 323 * @param line 要改变的表格行数 324 */ 325 public setTableSelection(line: any): void { 326 this.tableDataSource[line].isSelected = true; 327 this.panelTable?.clearAllSelection(this.tableDataSource[line]); 328 this.panelTable?.setCurrentSelection(this.tableDataSource[line]); 329 } 330 331 initHtml(): string { 332 return TabPaneCurrentHtml; 333 } 334} 335