1/* 2 * Copyright (c) 2021-2023 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 { FilesData } from '../../../databases/model/FileData' 17import FileAccessExec from '../../../base/utils/FileAccessExec' 18import { toast, isValidFileName } from '../../../base/utils/Common' 19import { getResourceString } from '../../../base/utils/Tools' 20import Logger from '../../../base/log/Logger' 21import { DialogTitle, DialogButton, DialogButtonDivider } from '../common/DialogComponent' 22 23const TAG = 'FileMkdirDialog' 24 25@Component 26@CustomDialog 27export struct FileMkdirDialog { 28 controller: CustomDialogController 29 cancel: Function 30 confirm: Function 31 @State folderName: string = '' 32 @State errorText: Resource = null 33 fileItems: Array<FilesData> 34 getCurrentDir: string = '' 35 36 aboutToAppear() { 37 this.folderName = this.getNewFolderName() 38 } 39 40 getNewFolderName(): string { 41 const newFolderText = getResourceString($r('app.string.addFolder')); 42 const regExp = new RegExp(`^${newFolderText}([ ]{1}[0-9]+)*$`); 43 const tempFolderList = this.fileItems.filter(item => item.isFolder && regExp.test(item.fileName)); 44 let tempFolderName = newFolderText; 45 let index = 0; 46 while (tempFolderList.some(item => item.fileName === tempFolderName)) { 47 index += 1; 48 tempFolderName = newFolderText + ' ' + index.toString(); 49 }; 50 return tempFolderName; 51 } 52 53 isSameName() { 54 let nameArr = [] 55 this.fileItems.forEach(item => { 56 nameArr.push(item.fileName) 57 }) 58 return nameArr.includes(this.folderName) 59 } 60 61 build() { 62 Column() { 63 DialogTitle({ 64 title: $r('app.string.addFolder') 65 }) 66 TextInput({ text: this.folderName }) 67 .margin({ 68 left: $r('app.float.text_input_margin_minus10'), 69 right: $r('app.float.text_input_margin_minus20') 70 }) 71 .fontSize($r('app.float.common_font_size16')) 72 .backgroundColor($r('app.color.text_input_bg_color')) 73 .onSubmit(() => { 74 }) 75 .onChange((value: string) => { 76 this.folderName = value 77 this.errorText = null 78 }) 79 Divider().vertical(false).strokeWidth(1).color(Color.Gray) 80 .margin({ 81 bottom: $r('app.float.common_margin10') 82 }) 83 Text(this.errorText) 84 .width('100%') 85 .padding({ 86 top: $r('app.float.common_padding5'), 87 bottom: $r('app.float.common_padding10') 88 }) 89 .fontSize($r('app.float.common_font_size14')) 90 .fontColor($r('app.color.error_message_color')) 91 Row() { 92 DialogButton({ 93 text: $r('app.string.cancel'), 94 isDisabled: false, 95 click: () => { 96 this.controller.close() 97 } 98 }) 99 DialogButtonDivider() 100 DialogButton({ 101 text: $r('app.string.confirm'), 102 isDisabled: !this.folderName.trim(), 103 click: () => { 104 if (!isValidFileName(this.folderName)) { 105 this.errorText = $r('app.string.illegal') 106 } else if (this.isSameName()) { 107 this.errorText = $r('app.string.sameName') 108 } else { 109 FileAccessExec.createFolder(this.getCurrentDir, this.folderName).then(folderUri => { 110 this.confirm({ 111 mkdirName: this.folderName, 112 path: folderUri 113 }) 114 }).catch(err => { 115 Logger.e(TAG, 'create Folder err: ' + JSON.stringify(err)) 116 toast($r('app.string.addFolder_fail')) 117 }) 118 this.controller.close() 119 } 120 } 121 }) 122 }.width('100%') 123 .margin({ bottom: $r('app.float.common_margin10') }) 124 }.padding({ 125 left: $r('app.float.common_margin24'), 126 right: $r('app.float.common_margin24') 127 }) 128 } 129} 130 131