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 */ 15import Logger from './Logger' 16import prompt from '@ohos.prompt'; 17 18export class WebObject { 19 controller: WebController; 20 isRegistered: boolean; 21 constructor(controller: WebController, isRegistered: boolean) { 22 this.controller = controller 23 this.isRegistered = isRegistered 24 } 25} 26 27@Observed 28class WebKey { 29 key: number; 30 timestamp: number; 31 32 constructor(key: number, timestamp: number) { 33 this.key = key 34 this.timestamp = timestamp 35 } 36} 37 38export enum LoadingStatus { 39 LOADING, 40 END 41} 42 43const TAG: string = '[browser]' 44 45export class Browser { 46 inputValue: string = "" 47 tabArrayIndex: number = 0 48 progress: number = 0 49 hideProgress: boolean = true 50 loadingStatus: LoadingStatus = LoadingStatus.END 51 52 webArray: Array<WebKey> = [new WebKey(0, new Date().getTime())] 53 tabsController: TabsController = new TabsController() 54 webControllerArray: Array<WebObject> = [new WebObject(new WebController(), false)] 55 56 deleteTab(index: number) { 57 Logger.info(TAG, `delete before tab index= ${index} controller length ${this.webControllerArray.length} tabArrayIndex= ${this.tabArrayIndex}`) 58 this.webArray.splice(index, 1) 59 this.webControllerArray.splice(index, 1) 60 if (this.tabArrayIndex > index || this.tabArrayIndex == this.webArray.length) { 61 this.tabArrayIndex -= 1 62 } 63 for (let i = index;i < this.webArray.length; ++i) { 64 this.webArray[i].key -= 1 65 } 66 for (let i = 0;i < this.webArray.length; ++i) { 67 Logger.info(TAG, `key ${this.webArray[i].key}, time=${this.webArray[i].timestamp}`) 68 } 69 Logger.info(`delete after tab index=${index}, controller length=${this.webControllerArray.length}, tabArrayIndex=${this.tabArrayIndex}`) 70 this.tabsController.changeIndex(this.tabArrayIndex) 71 } 72 73 getWebArray() { 74 return this.webArray 75 } 76 77 addTab() { 78 if (this.webArray.length > 10) { 79 prompt.showToast({ 80 message: '页签数量已满' 81 }) 82 return; 83 } 84 let webController: WebController = new WebController(); 85 let object = new WebObject(webController, false) 86 this.webControllerArray.push(object) 87 this.webArray.push(new WebKey(this.webArray.length, new Date().getTime())) 88 this.tabArrayIndex = this.webArray.length - 1 89 Logger.info(TAG, `add tab index= ${this.tabArrayIndex}`) 90 this.tabsController.changeIndex(this.tabArrayIndex) 91 } 92 93 setTabArrayIndex(tabArrayIndex: number) { 94 this.tabArrayIndex = tabArrayIndex 95 } 96 97 getTabArrayIndex() { 98 return this.tabArrayIndex 99 } 100 101 setInputVal(inputValue: string) { 102 this.inputValue = inputValue 103 } 104 105 getInputVal() { 106 return this.inputValue 107 } 108 109 loadUrl(addr: string) { 110 addr = "https://" + addr; 111 this.webControllerArray[this.tabArrayIndex].controller.loadUrl({ url: addr }) 112 } 113 114 Back() { 115 if (this.webControllerArray[this.tabArrayIndex].controller.accessBackward()) { 116 this.webControllerArray[this.tabArrayIndex].controller.backward() 117 } 118 } 119 120 Forward() { 121 if (this.webControllerArray[this.tabArrayIndex].controller.accessForward()) { 122 this.webControllerArray[this.tabArrayIndex].controller.forward() 123 } 124 } 125 126 Refresh() { 127 this.webControllerArray[this.tabArrayIndex].controller.refresh() 128 } 129} 130