-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(schematics): tree schematic #11739
Changes from 1 commit
85d8f0e
5e1dfe7
9168692
b6e51aa
5994219
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.type-icon { | ||
color: #999; | ||
margin-right: 5px; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> | ||
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding> | ||
<button mat-icon-button disabled></button> | ||
<mat-icon class="type-icon" [attr.aria-label]="node.type + 'icon'"> | ||
{{ node.type ==='file' ? 'description' : 'folder' }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you always omit braces here, no (here and elsewhere)? Also, there's a missing whitespace before |
||
</mat-icon> | ||
{{node.name}} | ||
</mat-tree-node> | ||
|
||
<mat-tree-node *matTreeNodeDef="let node;when: hasChild" matTreeNodePadding> | ||
<button mat-icon-button matTreeNodeToggle | ||
[attr.aria-label]="'toggle ' + node.name"> | ||
<mat-icon class="mat-icon-rtl-mirror"> | ||
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}} | ||
</mat-icon> | ||
</button> | ||
<mat-icon class="type-icon" [attr.aria-label]="node.type + 'icon'"> | ||
{{ node.type ==='file' ? 'description' : 'folder' }} | ||
</mat-icon> | ||
{{node.name}} | ||
</mat-tree-node> | ||
</mat-tree> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component'; | ||
|
||
describe('<%= classify(name) %>Component', () => { | ||
let component: <%= classify(name) %>Component; | ||
let fixture: ComponentFixture<<%= classify(name) %>Component>; | ||
|
||
beforeEach(fakeAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ <%= classify(name) %>Component ] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(<%= classify(name) %>Component); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
})); | ||
|
||
it('should compile', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its used in every component schematic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I commented on this in the other schematics too; I don't see a the need for it, and "because it was already like that" isn't a great reason for keeping it |
||
import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree'; | ||
import { of as observableOf } from 'rxjs'; | ||
import { FlatTreeControl } from '@angular/cdk/tree'; | ||
import { files } from './example-data'; | ||
|
||
/** File node data with nested structure. */ | ||
export interface FileNode { | ||
name: string; | ||
type: string; | ||
children?: FileNode[]; | ||
} | ||
|
||
/** Flat node with expandable and level information */ | ||
export interface TreeNode { | ||
name: string; | ||
type: string; | ||
level: number; | ||
expandable: boolean; | ||
} | ||
|
||
@Component({ | ||
selector: '<%= selector %>',<% if(inlineTemplate) { %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing whitespace before |
||
template: ` | ||
<mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> | ||
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding> | ||
<button mat-icon-button disabled></button> | ||
<mat-icon class="type-icon" [attr.aria-label]="node.type + 'icon'"> | ||
{{ node.type ==='file' ? 'description' : 'folder' }} | ||
</mat-icon> | ||
{{node.name}} | ||
</mat-tree-node> | ||
|
||
<mat-tree-node *matTreeNodeDef="let node;when: hasChild" matTreeNodePadding> | ||
<button mat-icon-button matTreeNodeToggle | ||
[attr.aria-label]="'toggle ' + node.name"> | ||
<mat-icon class="mat-icon-rtl-mirror"> | ||
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}} | ||
</mat-icon> | ||
</button> | ||
<mat-icon class="type-icon" [attr.aria-label]="node.type + 'icon'"> | ||
{{ node.type ==='file' ? 'description' : 'folder' }} | ||
</mat-icon> | ||
{{node.name}} | ||
</mat-tree-node> | ||
</mat-tree> | ||
`,<% } else { %> | ||
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %> | ||
styles: [ | ||
` | ||
.type-icon { | ||
color: #999; | ||
margin-right: 5px; | ||
} | ||
` | ||
]<% } else { %> | ||
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>']<% } %><% if(!!viewEncapsulation) { %>, | ||
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>, | ||
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %> | ||
}) | ||
export class <%= classify(name) %>Component { | ||
|
||
/** The TreeControl controls the expand/collapse state of tree nodes. */ | ||
treeControl: FlatTreeControl<TreeNode>; | ||
|
||
/** The TreeFlattener is used to generate the flat list of items from hierarchical data. */ | ||
treeFlattener: MatTreeFlattener<FileNode, TreeNode>; | ||
|
||
/** The MatTreeFlatDataSource connects the control and flattener to provide data. */ | ||
dataSource: MatTreeFlatDataSource<FileNode, TreeNode>; | ||
|
||
constructor() { | ||
this.treeFlattener = new MatTreeFlattener( | ||
this.transformer, | ||
this.getLevel, | ||
this.isExpandable, | ||
this.getChildren); | ||
|
||
this.treeControl = new FlatTreeControl<TreeNode>(this.getLevel, this.isExpandable); | ||
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); | ||
this.dataSource.data = files; | ||
} | ||
|
||
/** Transform the data to something the tree can read. */ | ||
transformer(node: FileNode, level: number) { | ||
return { | ||
name: node.name, | ||
type: node.type, | ||
level: level, | ||
expandable: !!node.children | ||
}; | ||
} | ||
|
||
/** Get the level of the node */ | ||
getLevel(node: TreeNode) { | ||
return node.level; | ||
} | ||
|
||
/** Return whether the node is expanded or not. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
isExpandable(node: TreeNode) { | ||
return node.expandable; | ||
}; | ||
|
||
/** Get the children for the node. */ | ||
getChildren(node: FileNode) { | ||
return observableOf(node.children); | ||
} | ||
|
||
/** Get whether the node has children or not. */ | ||
hasChild(index: number, node: TreeNode){ | ||
return node.expandable; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** Example file/folder data. */ | ||
export const files = [ | ||
{ | ||
name: 'material2', | ||
type: 'folder', | ||
children: [ | ||
{ | ||
name: 'src', | ||
type: 'folder', | ||
children: [ | ||
{ | ||
name: 'cdk', | ||
children: [ | ||
{ name: 'package.json', type: 'file' }, | ||
{ name: 'BUILD.bazel', type: 'file' }, | ||
] | ||
}, | ||
{ name: 'lib', type: 'folder' } | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'angular', | ||
type: 'folder', | ||
children: [ | ||
{ | ||
name: 'packages', | ||
type: 'folder', | ||
children: [ | ||
{ name: '.travis.yml', type: 'file' }, | ||
{ name: 'firebase.json', type: 'file' } | ||
] | ||
}, | ||
{ name: 'package.json', type: 'file' } | ||
] | ||
}, | ||
{ | ||
name: 'angularjs', | ||
type: 'folder', | ||
children: [ | ||
{ name: 'gulpfile.js', type: 'file' }, | ||
{ name: 'README.md', type: 'file' } | ||
] | ||
} | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {chain, Rule, noop, Tree, SchematicContext} from '@angular-devkit/schematics'; | ||
import {Schema} from './schema'; | ||
import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast'; | ||
import {buildComponent} from '../utils/devkit-utils/component'; | ||
|
||
/** | ||
* Scaffolds a new navigation component. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "navigation" -> "tree" ? |
||
* Internally it bootstraps the base component schematic | ||
*/ | ||
export default function(options: Schema): Rule { | ||
return chain([ | ||
buildComponent({ ...options }), | ||
options.skipImport ? noop() : addTreeModulesToModule(options) | ||
]); | ||
} | ||
|
||
/** | ||
* Adds the required modules to the relative module. | ||
*/ | ||
function addTreeModulesToModule(options: Schema) { | ||
return (host: Tree) => { | ||
const modulePath = findModuleFromOptions(host, options); | ||
addModuleImportToModule(host, modulePath, 'MatTreeModule', '@angular/material'); | ||
addModuleImportToModule(host, modulePath, 'MatIconModule', '@angular/material'); | ||
addModuleImportToModule(host, modulePath, 'MatButtonModule', '@angular/material'); | ||
return host; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsMaterialTree", | ||
"title": "Material Tree Options Schema", | ||
"type": "object", | ||
"properties": { | ||
"path": { | ||
"type": "string", | ||
"format": "path", | ||
"description": "The path to create the component.", | ||
"visible": false | ||
}, | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"visible": false | ||
}, | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the component.", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
} | ||
}, | ||
"inlineStyle": { | ||
"description": "Specifies if the style will be in the ts file.", | ||
"type": "boolean", | ||
"default": false, | ||
"alias": "s" | ||
}, | ||
"inlineTemplate": { | ||
"description": "Specifies if the template will be in the ts file.", | ||
"type": "boolean", | ||
"default": false, | ||
"alias": "t" | ||
}, | ||
"viewEncapsulation": { | ||
"description": "Specifies the view encapsulation strategy.", | ||
"enum": ["Emulated", "Native", "None"], | ||
"type": "string", | ||
"alias": "v" | ||
}, | ||
"changeDetection": { | ||
"description": "Specifies the change detection strategy.", | ||
"enum": ["Default", "OnPush"], | ||
"type": "string", | ||
"default": "Default", | ||
"alias": "c" | ||
}, | ||
"prefix": { | ||
"type": "string", | ||
"format": "html-selector", | ||
"description": "The prefix to apply to generated selectors.", | ||
"alias": "p" | ||
}, | ||
"styleext": { | ||
"description": "The file extension to be used for style files.", | ||
"type": "string", | ||
"default": "css" | ||
}, | ||
"spec": { | ||
"type": "boolean", | ||
"description": "Specifies if a spec file is generated.", | ||
"default": true | ||
}, | ||
"flat": { | ||
"type": "boolean", | ||
"description": "Flag to indicate if a dir is created.", | ||
"default": false | ||
}, | ||
"skipImport": { | ||
"type": "boolean", | ||
"description": "Flag to skip the module import.", | ||
"default": false | ||
}, | ||
"selector": { | ||
"type": "string", | ||
"format": "html-selector", | ||
"description": "The selector to use for the component." | ||
}, | ||
"module": { | ||
"type": "string", | ||
"description": "Allows specification of the declaring module.", | ||
"alias": "m" | ||
}, | ||
"export": { | ||
"type": "boolean", | ||
"default": false, | ||
"description": "Specifies if declaring module exports the component." | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {Schema as ComponentSchema} from '@schematics/angular/component/schema'; | ||
|
||
export interface Schema extends ComponentSchema {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we omit this (empty/nonfunctional) button completely and just capture the spacing with a different element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just copied what the docs examples did. Open to other ideas though. Just let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @tinayuangao there's no reason this needs to be a button, right? It's just to take up space?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should remove the button and use style to take up space.