# Alfresco Angular 2 Components core
Core library for other ng2-alfresco components.
This should be added as a dependency for any project using the components.
## Prerequisites
Before you start using this development framework, make sure you have installed all required software and done all the
necessary configuration, see this [page](https://github.com/Alfresco/alfresco-ng2-components/blob/master/PREREQUISITES.md).
## Install
```sh
npm install --save ng2-alfresco-core
```
## Main components and services
### Components
- Context Menu directive
- Material Design directives
- [mdl]
- [alfresco-mdl-button]
- [alfresco-mdl-menu]
- [alfresco-mdl-tabs]
### Services
- **AlfrescoApiService**, provides access to Alfresco JS API instance
- **AlfrescoAuthenticationService**, main authentication APIs
- **AlfrescoTranslationService**, various i18n-related APIs
- **ContextMenuService**, global context menu APIs
#### Alfresco Api Service
Provides access to initialized **AlfrescoJSApi** instance.
```ts
import { OnInit } from '@angular/core';
import { AlfrescoApiService } from 'ng2-alfresco-core';
export class MyComponent implements OnInit {
constructor(private apiService: AlfrescoApiService) {
}
ngOnInit() {
let nodeId = 'some-node-id';
let params = {};
this.getAlfrescoApi().nodes
.getNodeChildren(nodeId, params)
.then(result => console.log(result));
}
}
```
**Note for developers**: _the TypeScript declaration files for Alfresco JS API
are still under development and some Alfresco APIs may not be accessed
via your favourite IDE's intellisense or TypeScript compiler.
In case of any TypeScript type check errors you can still call any supported
Alfresco JS api by casting the instance to `any` type like the following:_
```ts
let apiService: any = this.authService.getAlfrescoApi();
apiService.nodes.addNode('-root-', body, {});
```
#### Context Menu directive
_See **Demo Shell** or **DocumentList** implementation for more details and use cases._
```html
```
```ts
@Component({
selector: 'my-component
})
export class MyComponent implements OnInit {
menuItems: any[];
constructor() {
this.menuItems = [
{ title: 'Item 1', subject: new Subject() },
{ title: 'Item 2', subject: new Subject() },
{ title: 'Item 3', subject: new Subject() }
];
}
ngOnInit() {
this.menuItems.forEach(l => l.subject.subscribe(item => this.commandCallback(item)));
}
commandCallback(item) {
alert(`Executing ${item.title} command.`);
}
}
```
#### Authentication Service
The authentication service is used inside the [login component](../ng2-alfresco-login) and is possible to find there an example of how to use it.
```ts
import { Component } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS } from '@angular/http';
import {
ALFRESCO_CORE_PROVIDERS,
AlfrescoSettingsService,
AlfrescoAuthenticationService
} from 'ng2-alfresco-core';
@Component({
selector: 'my-app',
template: `
Authentication failed to ip {{ ecmHost }} with user: admin, admin
Authentication successfull to ip {{ ecmHost }} with user: admin, admin, your token is {{ token }}
`
})
class MyDemoApp {
authenticated: boolean = false;
ecmHost: string = 'http://127.0.0.1:8080';
token: string;
constructor(public alfrescoAuthenticationService: AlfrescoAuthenticationService,
private alfrescoSettingsService: AlfrescoSettingsService) {
alfrescoSettingsService.ecmHost = this.ecmHost;
alfrescoSettingsService.setProviders('ECM');
}
ngOnInit() {
this.login();
}
login() {
this.alfrescoAuthenticationService.login('admin', 'admin').subscribe(
token => {
this.token = token.ticket;
this.authenticated = true;
},
error => {
console.log(error);
this.authenticated = false;
});
}
}
bootstrap(MyDemoApp, [
HTTP_PROVIDERS,
ALFRESCO_CORE_PROVIDERS
]);
```
#### Renditions Service
* getRenditionsListByNodeId(nodeId: string)
* createRendition(nodeId: string, encoding: string)
* getRendition(nodeId: string, encoding: string)
* isRenditionAvailable(nodeId: string, encoding: string)
## Build from sources
Alternatively you can build component from sources with the following commands:
```sh
npm install
npm run build
```
### Additional scripts
- `npm run build:w` builds the files and keep watching for changes
- `npm test` runs unit tests
- `npm run test-browser` runs unit tests in browser. This task rebuilds all the code, runs tslint, license checks and other quality check tools
before performing unit testing.
- `npm run coverage` runs code coverage report