-
Notifications
You must be signed in to change notification settings - Fork 1
ActionIconService
This @Injectable()
makes it easier to add menu items to a toolbar, similar to like Android. (For those of you who don't know, those icons on that toolbar are known as menu item just shown as action buttons and the toolbar's also known as an ActionBar
.) Anyways, enough of Android and Material Design and whatever.
In the future, this will be revisited and may be integrated with SharedInjectable
.
Ensure that ActionIconsModule
is imported to your main app's module under the imports
array:
import { ActionIconsModule } from 'path/to/actionitem.service';
@NgModule({
imports: [ActionIconsModule]
})
That's it! There's nothing else there is! On to the documentation then...
(Examples will be at the bottom this time...)
addActionIcon(actionItem: ActionIcon): void;
Adds a menu item.
Param | Type | Description | Notes |
---|---|---|---|
actionItem |
ActionIcon |
The menu item to add | - |
getActionIcons(): ActionIcon[];
Gets the menu items.
ActionIcon[]
removeActionIcon(id: number): void;
Removes the specified menu item at the index.
Param | Type | Description | Notes |
---|---|---|---|
id |
number |
The index of the menu item | - |
removeActionIcons(): void;
Removes all menu items.
getActionIconById(id: number): ActionIcon;
Gets a menu item by its index.
ActionIcon
Param | Type | Description | Notes |
---|---|---|---|
id |
number |
The index of the menu item to get | - |
addActionIconOnClickListener(id: number, callback: (ev?: Event) => void): void;
Adds an onclick listener to the specified menu item.
ActionIcon
Param | Type | Description | Notes |
---|---|---|---|
id |
number |
The index of the menu item to get | - |
callback |
(ev?: Event)=> void |
The callback of the onclick listener | - |
my-app.component.html
:
<mat-toolbar>
<app-actionicons></app-actionicons>
</mat-toolbar>
See the source code for app.component.html
.
Snippet of code:
/**
* Gets the menu items (getter)
* @returns {ActionIcon[]} The menu items
*/
get actionItems: ActionIcon[] {
return this.actionItemService.getActionIcons();
}
<div id="more-btns" *ngFor="let actionItem of actionItems">
<button mat-icon-button *ngIf="actionItem.showAsAction && actionItem.href == null" (click)="actionItem.onClickListener($event)" [matTooltip]="actionItem.title">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
</button>
<a mat-icon-button *ngIf="actionItem.showAsAction && actionItem.href" [href]="actionItem.href" [matTooltip]="actionItem.title">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
</a>
<a mat-icon-button *ngIf="actionItem.showAsAction && actionItem.routerLink" [routerLink]="[actionItem.routerLink]" [matTooltip]="actionItem.title">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
</a>
</div>
<button mat-icon-button *ngIf="showMoreMenu" [matMenuTriggerFor]="moreMenu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #moreMenu="matMenu">
<div *ngFor="let actionItem of actionItems">
<button mat-menu-item *ngIf="!actionItem.showAsAction && actionItem.href == null">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
{{actionItem.title}}
</button>
<a mat-menu-item *ngIf="!actionItem.showAsAction && actionItem.href" [href]="actionItem.href">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
{{actionItem.title}}
</a>
<a mat-menu-item *ngIf="!actionItem.showAsAction && actionItem.routerLink" [routerLink]="[actionItem.routerLink]">
<mat-icon *ngIf="actionItem.icon">{{actionItem.icon}}</mat-icon>
{{actionItem.title}}
</a>
</div>
</mat-menu>
export class MyComponent implements OnInit {
constructor(private actionItemService: ActionIconService){}
ngOnInit() {
this.actionItemService.addActionIcon({title: "Reload", icon: "refresh", onClickListener: (ev: Event)=> {
console.log("Reload was clicked");
window.location.reload(true);
}})
}
}
export class MyComponent implements OnInit {
constructor(private actionItemService: ActionIconService){}
ngOnInit() {
this.actionItemService.addActionIcon({title: "Open sidenav", icon: "menu"});
this.actionItemService.addActionIconOnClickListener(0, (ev: Event)=> {
console.log("Clicked");
})
}
}
Licensed with MIT
:
MIT License
Copyright (c) 2017-18 Edric Chan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the Software), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- Code originally from becompany/angular2-rss-reader-tutorial, more functionality added to fork of original repo.