From 68df6138c44a7086398325db06285153fcd56f9b Mon Sep 17 00:00:00 2001 From: Francesco Corti Date: Mon, 16 Apr 2018 13:23:46 +0200 Subject: [PATCH] Updated the existing tutorials and added another about Using components. (#3191) --- docs/tutorials/README.md | 1 + docs/tutorials/new-component.md | 12 +-- docs/tutorials/new-view.md | 76 +++++------------ docs/tutorials/preparing-environment.md | 21 ++--- docs/tutorials/using-conponents.md | 106 ++++++++++++++++++++++++ docs/user-guide/summary.json | 3 +- 6 files changed, 142 insertions(+), 77 deletions(-) create mode 100644 docs/tutorials/using-conponents.md diff --git a/docs/tutorials/README.md b/docs/tutorials/README.md index 4cde6fccc9..08451e154b 100644 --- a/docs/tutorials/README.md +++ b/docs/tutorials/README.md @@ -17,3 +17,4 @@ The tutorials are graded as follows: | [**Preparing the development environment**](preparing-environment.md) | Beginner | In this content is shared all the prerequisites valid for all the tutorials and descriptions of the entire documentation. This content contains the development environment description, along with the details of the suggested versions for each tools, library or module. | | [**Adding a new component**](new-component.md) | Beginner | By definition a _component_ controls a patch of screen called a view. As an example, individual components define and control menus, tabs, forms, buttons and every simple or complex portion of layout of an application. In this tutorial you will learn how to create a component using [Angular CLI](https://cli.angular.io/). After the creation you will learn how to add it to an existing application. | | [**Adding a new view**](new-view.md) | Beginner | Every application developed in Angular is a single page application where the concepts of _view_ and _routing_ play a key role in the user experience. Being a single page application, the navigation between the different layouts (called _views_) is enabled through the _routing_. In this tutorial you will learn how to create a new view into your application and how to have access to it using a defined endpoint. | +| [**Using components**](using-components.md) | Beginner | There are three different ways to use, extend and configure an ADF component: configuration properties, event listeners, content projection / HTML extensions. In this tutorial you are going to see a practical example for each approach. As an example, the Login component will be used. | diff --git a/docs/tutorials/new-component.md b/docs/tutorials/new-component.md index bd3730b088..7cad360bfa 100644 --- a/docs/tutorials/new-component.md +++ b/docs/tutorials/new-component.md @@ -1,13 +1,8 @@ ---- -Level: Beginner ---- - # Adding a new component By definition a *component* controls a patch of screen called a view. As an example, individual components define and control menus, tabs, forms, buttons and every simple or complex portion of layout of an application. In this tutorial you will learn how to create a component using [Angular CLI](https://cli.angular.io/). After the creation you will learn how to add it to an existing application. ## Creating a component - Starting from the root of your project, run the following command into a terminal. ng generate component my-first-component @@ -15,13 +10,11 @@ Starting from the root of your project, run the following command into a termina If you are adding the component to an application with more than one module, you might want to specify it using the `--module` parameter. For example use `--module app` to add the new component to the root app of your application. ## Using the component - Once done, wherever you will use `` into the HTML file of another component, you will see the content of the new component rendered exactly in that place. As an example, add `` on top of the `app.component.html` file stored into the `src` folder, and run the application again. Directly in the browser you will see `my-first-component works!`, that shows exactly the place where the component is rendered in the layout. ## Anatomy of the component - By default the new component is created into the `src/app` path and everything is stored in a folder named like the component itself. In this example a folder named with `my-first-component` is added to `src/app`, with inside the following content: - `my-first-component.component.scss` containing the CSS used by the component. This file is created as empty. @@ -31,15 +24,12 @@ By default the new component is created into the `src/app` path and everything i To make the component usable, one or more modules should declare it (or import it). In this example the `app.module.ts` file stored into the `src/app` folder contains the following code. -```ts import { MyFirstComponentComponent } from './my-first-component/my-first-component.component'; @NgModule({ declarations: [ - ... + ... MyFirstComponentComponent ], -``` These are the very basic information you should be know about your brand new component. All you have read here is standard Angular, not customised or valid for ADF applications only. - diff --git a/docs/tutorials/new-view.md b/docs/tutorials/new-view.md index 9be80ded5d..9aab81e59c 100644 --- a/docs/tutorials/new-view.md +++ b/docs/tutorials/new-view.md @@ -1,68 +1,38 @@ --- Level: Beginner --- +# Adding a new component -# Adding a new view +By definition a *component* controls a patch of screen called a view. As an example, individual components define and control menus, tabs, forms, buttons and every simple or complex portion of layout of an application. In this tutorial you will learn how to create a component using [Angular CLI](https://cli.angular.io/). After the creation you will learn how to add it to an existing application. -Every application developed in Angular is a single page application where the concepts of *view* and *routing* play a key role in the user experience. Being a single page application, the navigation between the different layouts (called *views*) is enabled through the *routing*. In this tutorial you will learn how to create a new view into your application and how to have access to it using a defined endpoint. +## Creating a component +Starting from the root of your project, run the following command into a terminal. -## Creating a view + ng generate component my-first-component -Into an Angular application, a view is implemented by a regular component. A view can use other views (so other components), but a view can be used to implement the full layout of your application. This is the reason why creating a view is the task than creating a component. +If you are adding the component to an application with more than one module, you might want to specify it using the `--module` parameter. For example use `--module app` to add the new component to the root app of your application. -To create a view, run the following command into a terminal, starting from the root of your project. +## Using the component +Once done, wherever you will use `` into the HTML file of another component, you will see the content of the new component rendered exactly in that place. - ng generate component my-first-view +As an example, add `` on top of the `app.component.html` file stored into the `src` folder, and run the application again. Directly in the browser you will see `my-first-component works!`, that shows exactly the place where the component is rendered in the layout. -See the [Adding a new component](new-component.md) tutorial for further details. +## Anatomy of the component +By default the new component is created into the `src/app` path and everything is stored in a folder named like the component itself. In this example a folder named with `my-first-component` is added to `src/app`, with inside the following content: -## Routing the view + - `my-first-component.component.scss` containing the CSS used by the component. This file is created as empty. + - `my-first-component.component.html` containing the HTML used to render the component. This file is created with a very basic message rendering the name of the component included in a `p` tag. + - `my-first-component.component.spec.ts` containing the unit tests for the component. + - `my-first-component.component.ts` containing the `MyFirstComponentComponent` class implementing the business logic in typescript. -An Angular application has one singleton instance of the `Router` service that is used to match the browser's URL with the corresponding component to display. The `Router` service must be configured in a typescript file (usually in the `imports` , in with a syntax similar to the -following source code. +To make the component usable, one or more modules should declare it (or import it). In this example the `app.module.ts` file stored into the `src/app` folder contains the following code. + + import { MyFirstComponentComponent } from './my-first-component/my-first-component.component'; -```ts - const appRoutes: Routes = [ - { path: 'path-in-the-app', component: ExistingComponent }, - { path: '**', component: PageNotFoundComponent } - ]; - @NgModule({ - imports: [ - RouterModule.forRoot( - appRoutes, - { enableTracing: true } // <-- debugging purposes only. - ) - // other imports here - ], - ... - }) -``` + declarations: [ + ... + MyFirstComponentComponent + ], -To add the new view to the routing, change the `appRoutes` constant as follows: - -```ts - const appRoutes: Routes = [ - { path: 'path-in-the-app', component: ExistingComponent }, - { path: 'my-first-view', component: MyFirstViewComponent }, // <-- Add this! - { path: '**', component: PageNotFoundComponent } - ]; -``` - -And remember to import the component in the same file with the following syntax: - -```ts - import { MyFirstViewComponent } from './my-first-view/my-first-view.component'; -``` - -Be aware that the `Router` service can be declared in a file that can be stored in different places in the application's structure. Usually the place where the `Router` service is declared is closed to the file containing the root module. - -## Testing the view - -To render the new view through the application and check the user experience, restart the application and open a browser to the following URL. - - http://:/my-first-view - -The result should be a very simple page with the following content. - - my-first-view works! +These are the very basic information you should be know about your brand new component. All you have read here is standard Angular, not customised or valid for ADF applications only. diff --git a/docs/tutorials/preparing-environment.md b/docs/tutorials/preparing-environment.md index c22c867e1d..16f9fb4028 100644 --- a/docs/tutorials/preparing-environment.md +++ b/docs/tutorials/preparing-environment.md @@ -1,9 +1,7 @@ --- Level: Beginner --- - -# Preparing the development environment - +## Preparing the development environment In this content is shared all the prerequisites valid for all the tutorials and descriptions of the entire documentation. This content contains the development environment description, along with the details of the suggested versions for each tools, library or module. ## Node.js @@ -13,7 +11,7 @@ In this content is shared all the prerequisites valid for all the tutorials and You need the latest `node.js` of either the `8.x` or `9.x` branch. To check the version, run the following command in a terminal. - node -v + node -v ## Angular CLI @@ -23,11 +21,11 @@ Earlier and later versions have issues around `@angular/devkit-core`. 1.6.6 seem If you already have `Angular CLI` installed check the version by running: - ng --version + ng --version To globally install `Angular CLI` version globally 1.6.6 run: - sudo npm install -g @angular/cli@1.6.6 + sudo npm install -g @angular/cli@1.6.6 ## Code Editor @@ -37,17 +35,16 @@ We recommend [Visual Studio Code](http://code.visualstudio.com) - it's a free, l You might want to ensure that you have `Yeoman` installed by running `yo --version`. If this is not in your system make sure you run: - sudo npm install -g yo + sudo npm install -g yo If you have installed it previously, you might want to make sure you uninstall them before. In ADF 2.0 we renamed the generator packages and the update is highly suggested. Uninstall previous versions with: - sudo npm uninstall generator-alfresco-adf-app - sudo npm uninstall generator-ng2-alfresco-app - + sudo npm uninstall generator-alfresco-adf-app + sudo npm uninstall generator-ng2-alfresco-app + Install the latest version of the `generator-alfresco-adf-app` using the following command. - sudo npm install -g generator-alfresco-adf-app - + sudo npm install -g generator-alfresco-adf-app diff --git a/docs/tutorials/using-conponents.md b/docs/tutorials/using-conponents.md new file mode 100644 index 0000000000..a523d80a18 --- /dev/null +++ b/docs/tutorials/using-conponents.md @@ -0,0 +1,106 @@ +--- +Level: Beginner +--- +# Using components +The best option you should consider when you plan to use an ADF component and want to learn the details of its usage, is always to check the documentation for the component you are looking to use. More in general, there are three different ways to use, extend and configure an ADF component: +1. Configuration properties. +2. Event listeners. +3. Content projection / HTML extensions. + +In this tutorial you are going to see a practical example for each approach. As an example, the [Login component](https://alfresco.github.io/adf-component-catalog/components/LoginComponent.html) will be used. + +## Configuration properties +Angular components can easily be configured via properties in the HTML template. In this example we will act on the "Remember me" check and "Need Help?" + "Register" links in the footer of the Login component. + +To prepare the task, be sure you have and ADF application up and running by executing `npm start` in a terminal, from the root folder of the project. Access to the login page using your browser and edit the `login.component.html` file stored into the `src/app/.../login` folder. The content of the `login.component.html` file should look like the following source code. + + + + +When reviewing the documentation you can see that the `` component has a lot of different properties. As an example we will toggle `showRememberMe` and `showLoginActions` (all set to `true` by default). If not already specified, add both the properties both with the false value, suing the syntax described below in the example. If the properties are defined in the HTML template, toggle the value according to what you see in the source code (set them to `true` if they have the `false` value and viceversa). + + + + +Once saved the HTML template you will see the login page updated with a different layout accordingly with the property values. + +**Notice:** The two new properties are specified with `[]` around them. There are three ways to configure a component. + +1. `[property]=""` This will be an expression or property from the typescript controller. Use this for boolean expressions or variables. +2. `property=""` This will be passed in as raw text. +3. `[(property)]` This is called *banana in a box* and is used for two way binding. + +## Event listeners + +Now that you've successfully configured properties on the `` component, it's time to look at the events. + +As we did for the previous task, looking at the [Login component documentation](https://alfresco.github.io/adf-component-catalog/components/LoginComponent.html) we can see that it emits three events `success`, `error` and `executeSubmit`. + +We can subscribe to these events and have our custom code executed once these events are emitted. Let's hook into the `executeSubmit` and do a simple `alert()` once the form is submitted. + +Continue to edit the `login.component.html` file and add `(success)="mySuccessMethod($event)"` to the `` component (the position is not relevant). + + + + +Next you need to implement `myExecuteSubmitMethod` in the typescript class implementing the component. Edit the `login.component.ts` file stored in the same `src/app/.../login` folder and add the implementation of `myExecuteSubmitMethod` as follows. + + @Component({ + ... + }) + export class LoginComponent { + + ... + + // Add this! + myExecuteSubmitMethod(event: any) { + alert('Form was submitted!'); + console.log(event); + } + } + +Save both the files and the login component will be refreshed in your browser. Enter random values for username and password and you should see the alert after pressing the submit button. Looking in the console of the browser, you'll see the `event` data containing all the details of the form. + +**Bonus objective:** Add a custom logo and background to the login view. + +## Content projection / HTML extensions +The last way a component can be configured or extended is through an approach called *Content projection*. This allows components to put placeholders in their template, allowing developers to "project" their own code or components into pre-defined locations within the component. + +In regular HTML, elements can be nested, for example: + +
+

+ Here we have some bold text +

+
+ +We can use the same approach with ADF components to inject custom code or entire components into the ADF component. Going to the documentation you can find more details about which targets are in place. + +The `` component supports two targets: `login-header` and `login-footer`. Let's add a simple "Hello World" message in the footer. Edit the template `login.component.html` and add a new tag *inside* the `` tag. + + + + + Hello World! + + + + +Watch carefully that you place the `` tag *inside* the `` tag. Inside the `` or `` tags you can put anything you want, as long as you wrap it inside an `` tag. You can also source in custom or 3rd party components. + +Once done, save the template and you should see a "Hello World!" message in the footer of your login page through your browser. diff --git a/docs/user-guide/summary.json b/docs/user-guide/summary.json index d23a03b753..e65812d69c 100644 --- a/docs/user-guide/summary.json +++ b/docs/user-guide/summary.json @@ -9,7 +9,8 @@ "children": [ { "title": "Preparing the development environment", "file": "preparing-environment.md"}, { "title": "Adding a new component", "file": "new-component.md"}, - { "title": "Adding a new view", "file": "new-view.md"} + { "title": "Adding a new view", "file": "new-view.md"}, + { "title": "Using components", "file": "using-components.md"} ] } ]