<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.1/build/pure-min.css" integrity="sha384-CCTZv2q9I9m3UOxRLaJneXrrqKwUNOzZ6NGEUMwHtShDJ+nCoiXJCAgi05KfkLGY" crossorigin="anonymous">
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* it has to be block for Pure (flex by default) */
dorf-field-wrapper {
display: block !important;
}
### DORF - readonly demo
Simple example which points out how to disable a reactive form created with DORF.
https://github.com/mat3e/dorf
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'dorf': 'npm:dorf@3.2.0',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
dorf: {
main: "./index.js",
defaultExtension: "js"
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {DorfModule, DorfField} from 'dorf'
import {Person} from './person/model'
import {PersonDetailComponent} from './person/person-detail.component'
@Component({
selector: "my-app",
template: `
<person-details [domainObject]="activePerson"></person-details>
`
})
export class App {
activePerson = new Person({
name: 'Test',
surname: 'Tester',
age: 15,
gender: 'm',
cardCode: 1111,
favColor: '#000',
smart: 'yes'
});
}
@NgModule({
imports: [ BrowserModule, DorfModule.forRoot({
css: {
form: 'pure-form pure-form-aligned',
wrapper: 'pure-control-group',
error: 'pure-form-message-inline',
buttons: {
save: 'hidden',
reset: 'hidden'
}
},
dorfFields: [{
tag: DorfField.CHECKBOX,
css: {
innerLabel: 'pure-checkbox'
}
}]
})],
declarations: [ App, PersonDetailComponent ],
bootstrap: [ App ]
})
export class AppModule {}
import { Validators } from '@angular/forms';
import { DorfObject, DorfInput, DorfRadio, DorfSelect, DorfCheckbox } from 'dorf';
/**
* Reactive Form will return set of properties, not a class with methods.
* It's recommended to store those properties in the interface and use them in class.
*/
export interface IPerson {
name: string;
surname: string;
gender: string;
age: number;
cardCode: number;
favColor: string;
smart: string;
}
/**
* Domain Object.
* Big part of the class is a standard definition - properties, getters, constructor (based on the interface).
* Real additional job is when providing FieldDefinitions.
*/
@DorfObject()
export class Person implements IPerson {
/*
Example properties.
*/
@DorfInput<string>({
label: 'Name', type: 'text',
validator: Validators.required, errorMessage: 'Name is required',
updateModelOnChange: true
})
name: string;
@DorfInput<string>({
label: 'Surname', type: 'text',
validator: Validators.required, errorMessage: 'Surname is required',
debounce: 1000,
updateModelOnChange: true
})
surname: string;
@DorfRadio<string>({
label: 'Gender',
optionsToSelect: [{ key: 'm', value: 'male' }, { key: 'f', value: 'female' }],
validator: Validators.required, errorMessage: 'Gender is required'
})
gender: string;
@DorfInput<number>({ label: 'Age', type: 'number' })
age: number;
@DorfInput<number>({
label: 'Credit card PIN', type: 'password',
debounce: 1000,
validator: Validators.pattern('[0-9]{4}'), errorMessage: 'PIN should contain just 4 digits'
})
cardCode: number;
@DorfSelect({
label: 'Favourite color',
optionsToSelect: [
{ key: '#fff', value: 'white' },
{ key: '#000', value: 'black' },
{ key: '#ff0000', value: 'red' },
{ key: '#00ff00', value: 'green' },
{ key: '#0000ff', value: 'blue' }
]
})
favColor: string;
@DorfCheckbox<string>({
innerLabel: 'Is smart?',
mapping: { trueValue: 'yes', falseValue: 'no' }
})
smart: string;
/**
* Shortcut for getting name and surname pair.
*/
get fullname() {
return this.name + ' ' + this.surname;
}
/**
* Creation from the interface should be supported.
*/
constructor(base: IPerson = null) {
if (base) {
this.name = base.name;
this.surname = base.surname;
this.gender = base.gender;
this.age = base.age;
this.cardCode = base.cardCode;
this.favColor = base.favColor;
this.smart = base.smart;
}
}
}
import { Component, Output, EventEmitter } from '@angular/core';
import { DorfConfigService, IDorfForm, DorfObjectInput, DorfForm } from 'dorf';
import { IPerson, Person } from './model';
/**
* Lightweight class which creates a form.
* Template provided from the library, but it is possible to provide a custom one (which uses Dorf components).
*
* It's important to pass config in the constructor and define onDorfSubmit method.
*/
@DorfForm()
@Component({
selector: 'person-details'
})
export class PersonDetailComponent implements IDorfForm {
@DorfObjectInput() domainObject: Person;
@Output() createUpdate = new EventEmitter<IPerson>();
constructor(public config: DorfConfigService) {
config.isDisabled = true;
}
}