====== Examples of developing for the Web client ======
===== Angular =====
==== Minimum Working Example: Basic screen ====
=== Create a new component ===
Components are the building blocks of the Angular staff client. Each one contains some TypeScript and some view code (called a template, which may or may not be broken up into a separate file).
To start with, we will create a super simple component that just displays the words "Evergreen is good at math". To do this, create a new folder called Open-ILS/src/eg2/src/app/staff/math.
Inside your folder, create a file called eg-math.component.ts with the following content:
Evergreen is good at math Sum: {{sum}}import { Component } from '@angular/core';
@Component({
template: '
=== Add your component to a module ===
Next, components have to be grouped into modules -- basically collections of similar components. Let's create a math module inside the same folder. Create a file called math.module.ts with the following content:
import {NgModule} from '@angular/core';
import {EgMathComponent} from './eg-math.component';
import {RouterModule, Routes} from '@angular/router';
const routes: Routes = [{
path: 'eg',
component: EgMathComponent
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations: [EgMathComponent]
})
export class MathModule { }
=== Add your module to the Staff router ===
Finally, it's time to let the rest of Evergreen's Angular client know where to find your code. Add this to Open-ILS/src/eg2/src/app/staff/routing.module.ts, within the routes constant:
{
path: 'math',
loadChildren: '@eg/staff/math/math.module#MathModule'
},
Re-compile, the client, and you should see your brand new screen at https://[yourdomain.edu]/eg2/en-US/staff/math/eg
=== Add another component with an OpenSRF call ===
Create a new .ts file in Open-ILS/src/eg2/src/app/staff/math, called adder.component.ts:
import {Component, OnInit} from '@angular/core';
import {NetService} from '@eg/core/net.service';
@Component({
selector: 'eg-adder',
templateUrl: './adder.component.html',
})
export class AdderComponent implements OnInit {
sum: number = 0;
addTwoNumbers: (first: number, second: number) => void;
constructor(
private net: NetService
){}
ngOnInit() {
this.addTwoNumbers = (first: number = 0, second: number = 0) => {
this.net.request(
'opensrf.math',
'add',
first, second)
.subscribe(response => this.sum = response);
}
}
}
This has some differences from our first one. First of all, it has a selector, which means that we can include it in other components by saying
Let's update our module to let it know about the new component:
import {NgModule} from '@angular/core';
import {EgMathComponent} from './eg-math.component';
import {AdderComponent} from './adder.component';
import {RouterModule, Routes} from '@angular/router';
const routes: Routes = [{
path: 'eg',
component: EgMathComponent
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations: [EgMathComponent, AdderComponent]
})
export class MathModule { }
Finally, let's update our first component's template to include a reference to our new component. To do this, just change the line ''template: '
Evergreen is good at math
'' to ''template: 'Evergreen is good at math
$routeProvider.when('/cat/catalog/math', {
templateUrl: './cat/catalog/t_add',
controller: 'MathematicsCtrl',
resolve : resolver
});
== Create a controller for your screen ==
This can be in the same js file as your route was. Here's one to add to ''web/js/ui/default/staff/cat/catalog/app.js'':
.controller('MathematicsCtrl', ['$scope', 'egCore', function($scope, egCore) {
$scope.firstNumber = 1;
$scope.secondNumber = 6;
$scope.add_things = function() {
egCore.net.request('opensrf.math', 'add', $scope.firstNumber, $scope.secondNumber
).then(
function(response) {
$scope.sum = response;
});
}
}])
==Create a tt2 template for your screen==
Look for an index.tt2 file that includes the JS file you were working on. Create a new file that begins with ''t_'' in the same directory. Starting the template with ''t_'' will add the Evergreen Web client header and other goodies from the index.tt2 file in the same directory. This includes the controller that you created during the last step.
The name and path to your new tt2 file should match the **templateUrl** you entered in your route. For example, if your **templateUrl** was ''./cat/catalog/t_add'', create a tt2 file at ''Open-ILS/src/templates/staff/cat/catalog/t_add.tt2'' in the git repo or ''/openils/var/templates/staff/cat/catalog/t_add.tt2'' in an installed system.
==Test your screen==
It will be available at https://[DOMAIN_NAME]/eg/staff/cat/catalog/math
====Add a popup modal to your screen====
This example builds on the previous example of an addition screen by adding a button, which, when pressed, opens up a modal asking the user to confirm.
===Edit the controller===
First of all, edit the controller to make sure it can access the egAlertDialog factory:
controller('MathematicsCtrl', ['$scope', 'egCore', 'egAlertDialog', function($scope, egCore, egAlertDialog) {
Then add a function to the controller that makes use of the factory:
$scope.provide_feedback = function() {
egAlertDialog.open(egCore.strings.SHARE_YOUR_OPINION);
}
===Add your string to egCore.strings===
We want to be able to translate the egAlertDialog text to the language that the user prefers. To do this, we will define it in the parent .tt2 page (in this case, ''templates/staff/cat/catalog/index.tt2'').
To do this, look for the area in between '''', where other egCore.strings are defined. Then add:
s.SHARE_YOUR_OPINION = "[% l('I think so too!') %]";
===Edit the tt2 template===
Here, we'll just add a simple button outside of the