This is an automated email from the ASF dual-hosted git repository. dgnatyshyn pushed a commit to branch DLAB-1551 in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git
commit 2a6e5bc4bfca0390a730f41ccaa70f2014b87eae Author: Dmytro Gnatyshyn <[email protected]> AuthorDate: Mon Apr 6 18:10:04 2020 +0300 [DLAB-1551]: Added folder tree --- .../app/core/services/bucket-browser.service.ts | 92 +++++++++++ .../bucket-browser/bucket-browser.component.html | 62 ++++++++ .../bucket-browser/bucket-browser.component.scss | 123 +++++++++++++++ .../bucket-browser/bucket-browser.component.ts | 110 +++++++++++++ .../folder-tree/folder-tree.component.html | 32 ++++ .../folder-tree/folder-tree.component.scss | 7 + .../folder-tree/folder-tree.component.ts | 171 +++++++++++++++++++++ .../webapp/src/app/resources/resources.module.ts | 10 +- 8 files changed, 604 insertions(+), 3 deletions(-) diff --git a/services/self-service/src/main/resources/webapp/src/app/core/services/bucket-browser.service.ts b/services/self-service/src/main/resources/webapp/src/app/core/services/bucket-browser.service.ts new file mode 100644 index 0000000..0c334a1 --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/core/services/bucket-browser.service.ts @@ -0,0 +1,92 @@ +import { Injectable } from '@angular/core'; +import {BehaviorSubject} from 'rxjs'; + +export class TodoItemNode { + children: TodoItemNode[]; + item: string; +} + +/** Flat to-do item node with expandable and level information */ +export class TodoItemFlatNode { + item: string; + level: number; + expandable: boolean; +} + +/** + * The Json object for to-do list data. + */ +const TREE_DATA = { + FirsrFolder: { + folder: { + folder: ['file1', 'file2'], + file1: null, + file2: null + }, + 'file': null, + 'file2': null, + 'file3': null, + }, + SecondFolder: [ + 'file1', + 'file2', + 'file3' + ] +}; + +@Injectable({ + providedIn: 'root' +}) +export class BucketBrowserService { + dataChange = new BehaviorSubject<TodoItemNode[]>([]); + + get data(): TodoItemNode[] { return this.dataChange.value; } + + constructor() { + this.initialize(); + } + + initialize() { + // Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested + // file node as children. + const data = this.buildFileTree(TREE_DATA, 0); + + // Notify the change. + this.dataChange.next(data); + } + + /** + * Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object. + * The return value is the list of `TodoItemNode`. + */ + buildFileTree(obj: {[key: string]: any}, level: number): TodoItemNode[] { + return Object.keys(obj).reduce<TodoItemNode[]>((accumulator, key) => { + const value = obj[key]; + const node = new TodoItemNode(); + node.item = key; + + if (value != null) { + if (typeof value === 'object') { + node.children = this.buildFileTree(value, level + 1); + } else { + node.item = value; + } + } + + return accumulator.concat(node); + }, []); + } + + /** Add an item to to-do list */ + insertItem(parent: TodoItemNode, name: string) { + if (parent.children) { + parent.children.push({item: name} as TodoItemNode); + this.dataChange.next(this.data); + } + } + + updateItem(node: TodoItemNode, name: string) { + node.item = name; + this.dataChange.next(this.data); + } +} diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.html b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.html new file mode 100644 index 0000000..307f21b --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.html @@ -0,0 +1,62 @@ +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> + +<div class="bucket-browser" id="dialog-box"> + <header class="dialog-header"> + <h4 class="modal-title">Bucket browser</h4> + <button type="button" class="close" (click)="dialogRef.close()">×</button> + </header> + <div class="dialog-content tabs"> + <div class="submit m-bott-10 m-top-10"> + <button mat-raised-button type="button" class="butt action"><input type="file" (change)="handleFileInput($event.target.files)">Add file</button> +<!-- <button mat-raised-button type="button" class="butt action"><input type="file" (change)="filesPicked($event.target.files)" webkitdirectory directory multiple/>Upload folder</button>--> +<!-- <button mat-raised-button type="button" class="butt action">Download files</button>--> + <button mat-raised-button type="button" class="butt action">Create folder</button> + </div> + <div class="backet-wrapper" id="scrolling"> + <div class="navigation"> + <dlab-folder-tree></dlab-folder-tree> +<!-- <ul class="folder-tree">--> +<!-- <li class="folder-item">--> +<!-- <i class="material-icons">folder</i>--> +<!-- <span class="folder">bucket</span>--> +<!-- </li>--> +<!-- </ul>--> + </div> + <div class="directory"> +<!-- <dlab-folder-tree></dlab-folder-tree>--> + <ul class="folder-tree"> + <li *ngFor="let file of uploadPaths" class="folder-item"> + <div class="name">{{file}}</div> + <div class="size">2.4kb</div> + <div class="progres"></div> + </li> + <li class="folder-item"> + + </li> + </ul> + </div> + </div> + <div class="text-center m-top-30 m-bott-30"> + <button type="button" class="butt" mat-raised-button (click)="this.dialogRef.close()">Close</button> + <button type="button" class="butt butt-success" mat-raised-button>Download</button> + <button type="button" class="butt butt-success" mat-raised-button>Upload</button> + </div> + </div> +</div> diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.scss b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.scss new file mode 100644 index 0000000..9d839bb --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.scss @@ -0,0 +1,123 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +.bucket-browser { + + bottom: 0; + .dialog-content{ + padding: 0 35px; + } + .content-box { + height: 500px; + + } + + .submit{ + display: flex; + .butt{ + position: relative; + overflow: hidden; + margin: 10px; + input[type="file"] { + position: absolute; + left: 0; + right: 0; + top: 0; + bottom: 0; + opacity: 0; + } + } + } + + .text-center{ + position: absolute; + bottom: 50px; + left: 0; + right: 0; + } + +} + +.backet-wrapper{ + min-height: 450px; + border: 2px solid rgba(0,0,0,.12); + border-radius: 5px; + display: flex; + + .navigation{ + flex: 1; + border-right: 2px solid rgba(0,0,0,.12); + max-height: 500px; + overflow: auto; + .folder-tree{ + + .folder{ + line-height: 30px; + } + } + + } + + .directory{ + flex: 2; + .folder-tree{ + .name{ + flex:2; + } + .size{ + flex:1; + } + .progres{ + flex:1; + border: 1px solid rgba(0,0,0,.12); + height: 15px; + } + .action { + display: flex; + justify-content: space-around; + width: 100%; + .add-file { + overflow: hidden; + min-width: 100px ; + height: 30px; + position: relative; + input{ + position: absolute; + left: 0; + right: 0; + top: 0; + bottom: 0; + opacity: 0; + } + } + } + + } + } + + .folder-tree { + padding: 20px 30px; + } + + .folder-item{ + display: flex; + align-items: center; + + } +} diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.ts b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.ts new file mode 100644 index 0000000..447501e --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/bucket-browser.component.ts @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Component, OnInit, ViewChild, Inject } from '@angular/core'; +import { FormGroup, FormBuilder, Validators } from '@angular/forms'; +import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; +import { ToastrService } from 'ngx-toastr'; + +import { AccountCredentials, MangeUngitModel } from './bucket-browser.model'; +import { ManageUngitService } from '../../core/services'; +import {logger} from 'codelyzer/util/logger'; + +@Component({ + selector: 'dlab-bucket-browser', + templateUrl: './bucket-browser.component.html', + styleUrls: ['./bucket-browser.component.scss'] +}) +export class BucketBrowserComponent implements OnInit { + filenames: Array<any> = []; + uploadPaths = []; + @ViewChild('tabGroupGit', { static: false }) tabGroupGit; + + constructor( + public toastr: ToastrService, + public dialog: MatDialog, + public dialogRef: MatDialogRef<BucketBrowserComponent>, + private manageUngitService: ManageUngitService, + private _fb: FormBuilder, + ) { + + } + + ngOnInit() { + + } + + handleFileInput(files) { + // for (let i = 0; i < files.length; i++) { + // const file = files[i]; + // const path = file.webkitRelativePath.split('/'); + // } + // } + console.log(files) + this.filenames = Object['values'](files).map(v => v.name) + this.uploadPaths = [...this.uploadPaths, ...this.filenames]; + } + + filesPicked(files) { + console.log(files); + // this.uploadPaths = []; + Array.prototype.forEach.call(files, file => { + this.uploadPaths.push(file.webkitRelativePath); + }); + console.log(this.uploadPaths); + } + + private upload(tree, path) { + tree.files.forEach(file => { + this.uploadPaths.push(path + file.name); + }); + tree.directories.forEach(directory => { + const newPath = path + directory.name + '/'; + this.uploadPaths.push(newPath); + this.upload(directory, newPath); + }); + } +} + +// @Component({ +// selector: 'dialog-result-example-dialog', +// template: ` +// <div class="dialog-header"> +// <h4 class="modal-title"><i class="material-icons">priority_high</i>Warning</h4> +// <button type="button" class="close" (click)="dialogRef.close()">×</button> +// </div> +// <div mat-dialog-content class="content"> +// <p>Account <span class="strong">{{ data.hostname }}</span> will be decommissioned.</p> +// <p class="m-top-20"><span class="strong">Do you want to proceed?</span></p> +// </div> +// <div class="text-center"> +// <button type="button" class="butt" mat-raised-button (click)="dialogRef.close()">No</button> +// <button type="button" class="butt butt-success" mat-raised-button (click)="dialogRef.close(true)">Yes</button> +// </div> +// `, +// styles: [` +// .content { color: #718ba6; padding: 20px 50px; font-size: 14px; font-weight: 400 } +// `] +// }) +// export class ConfirmDeleteAccountDialog { +// constructor( +// public dialogRef: MatDialogRef<ConfirmDeleteAccountDialog>, +// @Inject(MAT_DIALOG_DATA) public data: any +// ) { } +// } diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.html b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.html new file mode 100644 index 0000000..2ba6ed8 --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.html @@ -0,0 +1,32 @@ +<mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> + <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding> + <button mat-icon-button disabled></button> +<!-- <mat-checkbox class="checklist-leaf-node"--> +<!-- [checked]="checklistSelection.isSelected(node)"--> +<!-- (change)="todoLeafItemSelectionToggle(node)"></mat-checkbox>--> + {{node.item}} + </mat-tree-node> + + <mat-tree-node *matTreeNodeDef="let node; when: hasNoContent" matTreeNodePadding> + <button mat-icon-button disabled></button> + <mat-form-field> + <mat-label>New item...</mat-label> + <input matInput #itemValue placeholder="Ex. Lettuce"> + </mat-form-field> + <button mat-button (click)="saveNode(node, itemValue.value)">Save</button> + </mat-tree-node> + + <mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding> + <button mat-icon-button matTreeNodeToggle + [attr.aria-label]="'toggle ' + node.filename"> + <mat-icon class="mat-icon-rtl-mirror"> + {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}} + </mat-icon> + </button> +<!-- <mat-checkbox [checked]="descendantsAllSelected(node)"--> +<!-- [indeterminate]="descendantsPartiallySelected(node)"--> +<!-- (change)="todoItemSelectionToggle(node)">{{node.item}}</mat-checkbox>--> + <i class="material-icons folder-icon">folder</i> <span class="folder">{{node.item}}</span> +<!-- <button mat-icon-button (click)="addNewItem(node)"><mat-icon>add</mat-icon></button>--> + </mat-tree-node> +</mat-tree> diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.scss b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.scss new file mode 100644 index 0000000..a842b13 --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.scss @@ -0,0 +1,7 @@ +.folder{ + padding-left: 5px; +} + +.folder-icon{ + color: rgb(232, 232, 232); +} diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.ts b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.ts new file mode 100644 index 0000000..bba1a45 --- /dev/null +++ b/services/self-service/src/main/resources/webapp/src/app/resources/bucket-browser/folder-tree/folder-tree.component.ts @@ -0,0 +1,171 @@ +import { Component, OnInit, Injectable } from '@angular/core'; +import {SelectionModel} from '@angular/cdk/collections'; +import {FlatTreeControl} from '@angular/cdk/tree'; +import {MatTreeFlatDataSource, MatTreeFlattener} from '@angular/material/tree'; +import {BucketBrowserService, TodoItemFlatNode, TodoItemNode} from '../../../core/services/bucket-browser.service'; + + +class ChecklistDatabase { +} + +/** + * @title Tree with checkboxes + */ + +@Component({ + selector: 'dlab-folder-tree', + templateUrl: './folder-tree.component.html', + styleUrls: ['./folder-tree.component.scss'] +}) +export class FolderTreeComponent { + /** Map from flat node to nested node. This helps us finding the nested node to be modified */ + flatNodeMap = new Map<TodoItemFlatNode, TodoItemNode>(); + + /** Map from nested node to flattened node. This helps us to keep the same object for selection */ + nestedNodeMap = new Map<TodoItemNode, TodoItemFlatNode>(); + + /** A selected parent node to be inserted */ + selectedParent: TodoItemFlatNode | null = null; + + /** The new item's name */ + newItemName = ''; + + treeControl: FlatTreeControl<TodoItemFlatNode>; + + treeFlattener: MatTreeFlattener<TodoItemNode, TodoItemFlatNode>; + + dataSource: MatTreeFlatDataSource<TodoItemNode, TodoItemFlatNode>; + + /** The selection for checklist */ + checklistSelection = new SelectionModel<TodoItemFlatNode>(true /* multiple */); + + constructor(private bucketBrowserService: BucketBrowserService) { + this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel, + this.isExpandable, this.getChildren); + this.treeControl = new FlatTreeControl<TodoItemFlatNode>(this.getLevel, this.isExpandable); + this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); + + bucketBrowserService.dataChange.subscribe(data => { + this.dataSource.data = data; + }); + } + + getLevel = (node: TodoItemFlatNode) => node.level; + + isExpandable = (node: TodoItemFlatNode) => node.expandable; + + getChildren = (node: TodoItemNode): TodoItemNode[] => node.children; + + hasChild = (_: number, _nodeData: TodoItemFlatNode) => _nodeData.expandable; + + hasNoContent = (_: number, _nodeData: TodoItemFlatNode) => _nodeData.item === ''; + + /** + * Transformer to convert nested node to flat node. Record the nodes in maps for later use. + */ + transformer = (node: TodoItemNode, level: number) => { + const existingNode = this.nestedNodeMap.get(node); + const flatNode = existingNode && existingNode.item === node.item + ? existingNode + : new TodoItemFlatNode(); + flatNode.item = node.item; + flatNode.level = level; + flatNode.expandable = !!node.children; + this.flatNodeMap.set(flatNode, node); + this.nestedNodeMap.set(node, flatNode); + return flatNode; + } + + /** Whether all the descendants of the node are selected. */ + descendantsAllSelected(node: TodoItemFlatNode): boolean { + const descendants = this.treeControl.getDescendants(node); + const descAllSelected = descendants.every(child => + this.checklistSelection.isSelected(child) + ); + return descAllSelected; + } + + /** Whether part of the descendants are selected */ + descendantsPartiallySelected(node: TodoItemFlatNode): boolean { + const descendants = this.treeControl.getDescendants(node); + const result = descendants.some(child => this.checklistSelection.isSelected(child)); + return result && !this.descendantsAllSelected(node); + } + + /** Toggle the to-do item selection. Select/deselect all the descendants node */ + todoItemSelectionToggle(node: TodoItemFlatNode): void { + this.checklistSelection.toggle(node); + const descendants = this.treeControl.getDescendants(node); + this.checklistSelection.isSelected(node) + ? this.checklistSelection.select(...descendants) + : this.checklistSelection.deselect(...descendants); + + // Force update for the parent + descendants.every(child => + this.checklistSelection.isSelected(child) + ); + this.checkAllParentsSelection(node); + } + + /** Toggle a leaf to-do item selection. Check all the parents to see if they changed */ + todoLeafItemSelectionToggle(node: TodoItemFlatNode): void { + this.checklistSelection.toggle(node); + this.checkAllParentsSelection(node); + } + + /* Checks all the parents when a leaf node is selected/unselected */ + checkAllParentsSelection(node: TodoItemFlatNode): void { + let parent: TodoItemFlatNode | null = this.getParentNode(node); + while (parent) { + this.checkRootNodeSelection(parent); + parent = this.getParentNode(parent); + } + } + + /** Check root node checked state and change it accordingly */ + checkRootNodeSelection(node: TodoItemFlatNode): void { + const nodeSelected = this.checklistSelection.isSelected(node); + const descendants = this.treeControl.getDescendants(node); + const descAllSelected = descendants.every(child => + this.checklistSelection.isSelected(child) + ); + if (nodeSelected && !descAllSelected) { + this.checklistSelection.deselect(node); + } else if (!nodeSelected && descAllSelected) { + this.checklistSelection.select(node); + } + } + + /* Get the parent node of a node */ + getParentNode(node: TodoItemFlatNode): TodoItemFlatNode | null { + const currentLevel = this.getLevel(node); + + if (currentLevel < 1) { + return null; + } + + const startIndex = this.treeControl.dataNodes.indexOf(node) - 1; + + for (let i = startIndex; i >= 0; i--) { + const currentNode = this.treeControl.dataNodes[i]; + + if (this.getLevel(currentNode) < currentLevel) { + return currentNode; + } + } + return null; + } + + /** Select the category so we can insert the new item. */ + addNewItem(node: TodoItemFlatNode) { + const parentNode = this.flatNodeMap.get(node); + this.bucketBrowserService.insertItem(parentNode!, ''); + this.treeControl.expand(node); + } + + /** Save the node to database */ + saveNode(node: TodoItemFlatNode, itemValue: string) { + const nestedNode = this.flatNodeMap.get(node); + this.bucketBrowserService.updateItem(nestedNode!, itemValue); + } +} diff --git a/services/self-service/src/main/resources/webapp/src/app/resources/resources.module.ts b/services/self-service/src/main/resources/webapp/src/app/resources/resources.module.ts index bbf6e09..d2d5f02 100644 --- a/services/self-service/src/main/resources/webapp/src/app/resources/resources.module.ts +++ b/services/self-service/src/main/resources/webapp/src/app/resources/resources.module.ts @@ -28,6 +28,8 @@ import { ExploratoryEnvironmentCreateModule } from './exploratory/create-environ import { ManageUngitComponent } from './manage-ungit/manage-ungit.component'; import { ConfirmDeleteAccountDialog } from './manage-ungit/manage-ungit.component'; import {BucketBrowserComponent} from './bucket-browser/bucket-browser.component'; +import {FolderTreeComponent} from './bucket-browser/folder-tree/folder-tree.component'; +import {MatTreeModule} from '@angular/material/tree'; @NgModule({ imports: [ @@ -36,15 +38,17 @@ import {BucketBrowserComponent} from './bucket-browser/bucket-browser.component' ReactiveFormsModule, ResourcesGridModule, ExploratoryEnvironmentCreateModule, - MaterialModule + MaterialModule, + MatTreeModule ], declarations: [ ResourcesComponent, ManageUngitComponent, ConfirmDeleteAccountDialog, - BucketBrowserComponent + BucketBrowserComponent, + FolderTreeComponent ], - entryComponents: [ManageUngitComponent, ConfirmDeleteAccountDialog, BucketBrowserComponent], + entryComponents: [ManageUngitComponent, ConfirmDeleteAccountDialog, BucketBrowserComponent, FolderTreeComponent], exports: [ResourcesComponent] }) export class ResourcesModule { } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
