Remote question and value service
This commit is contained in:
@@ -1 +1,3 @@
|
||||
<h1>{{formName}}</h1>
|
||||
<p>{{formDescription}}</p>
|
||||
<dyn-form [questions]="formQuestions" (onSubmit)="submit($event)" [value]="formValue" [type]="'insert'"></dyn-form>
|
||||
@@ -1,6 +1,6 @@
|
||||
import {Component} from '@angular/core';
|
||||
import {QuestionInterface} from './modules/dyn-form/types/question.interface';
|
||||
import {QuestionService} from './modules/dyn-form/services/question.service';
|
||||
import {FormService} from './modules/dyn-form/services/form.service';
|
||||
import {ValueService} from './modules/dyn-form/services/value.service';
|
||||
|
||||
@Component({
|
||||
@@ -11,15 +11,27 @@ import {ValueService} from './modules/dyn-form/services/value.service';
|
||||
export class AppComponent {
|
||||
public formQuestions: QuestionInterface[] = null;
|
||||
public formValue: Object = null;
|
||||
public formName: string = "";
|
||||
public formDescription ="";
|
||||
|
||||
constructor(private questionService: QuestionService, private valueService: ValueService) {
|
||||
this.questionService.getQuestions((res, err) => {
|
||||
if (!err)
|
||||
this.formQuestions = res;
|
||||
constructor(private questionService: FormService, private valueService: ValueService) {
|
||||
this.questionService.getForm('/crf/domains', (res, err) => {
|
||||
if (err)
|
||||
console.error(err);
|
||||
else {
|
||||
this.formQuestions = res.questions;
|
||||
this.formName = res.name;
|
||||
this.formDescription = res.description;
|
||||
console.log("questions", this.formQuestions);
|
||||
}
|
||||
});
|
||||
this.valueService.getValues((res, err) => {
|
||||
if (!err)
|
||||
this.valueService.getValue('/crf/domains', 3, (res, err) => {
|
||||
if (err)
|
||||
console.error(err);
|
||||
else {
|
||||
this.formValue = res;
|
||||
console.log("values", this.formValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import {FormControl, FormGroup} from '@angular/forms';
|
||||
[question]="question" [form]="form" [type]="type" (onChange)="valueChange.emit(form.value)"></dyn-question>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<button [disabled]="form.invalid" type="submit">Save</button>
|
||||
<button [disabled]="form.invalid && type!='delete'" type="submit">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
`
|
||||
@@ -19,7 +19,7 @@ import {FormControl, FormGroup} from '@angular/forms';
|
||||
export class DynFormComponent implements OnChanges {
|
||||
@Input() questions: QuestionInterface[];
|
||||
@Input() value: {[_:string]: any};
|
||||
@Input() type: "'insert'|'update'|'delete'|'view'";
|
||||
@Input() type: 'insert'|'update'|'delete'|'view';
|
||||
@Output() valueChange: EventEmitter<any> = new EventEmitter<any>();
|
||||
@Output() onSubmit: EventEmitter<any> = new EventEmitter<any>();
|
||||
|
||||
@@ -41,14 +41,14 @@ export class DynFormComponent implements OnChanges {
|
||||
}
|
||||
|
||||
private submit() {
|
||||
if (this.form.valid) {
|
||||
if (this.form.valid || this.type=='delete') {
|
||||
let vals = {};
|
||||
let type: string = this.form.value.type;
|
||||
for (let question of this.questions) {
|
||||
let specType = question.properties.specialization;
|
||||
let methods = question.properties.methods;
|
||||
let key = question.properties.key;
|
||||
if (!(methods && methods.indexOf(this.type)==-1 || specType && type!=specType)) {
|
||||
if (!(methods && methods.indexOf(this.type)==-1 || specType && type!=specType) && (this.type!='delete' || question.type=='hidden')) {
|
||||
vals[key] = this.form.value[key];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import {CounterInputComponent} from './inputs/counter-input.component';
|
||||
import {TagInputComponent} from './inputs/tag-input.component';
|
||||
import {HiddenInputComponent} from './inputs/hidden-input.component';
|
||||
import {DropdownInputComponent} from './inputs/dropdown-input.component';
|
||||
import {QuestionService} from './services/question.service';
|
||||
import {FormService} from './services/form.service';
|
||||
import {TextareaInputComponent} from './inputs/textarea-input.component';
|
||||
import {ValueService} from './services/value.service';
|
||||
import {KeysPipe} from './types/keys.pipe';
|
||||
@@ -36,7 +36,7 @@ import {CheckboxInputComponent} from './inputs/checkbox-input.component';
|
||||
KeysPipe
|
||||
],
|
||||
providers: [
|
||||
QuestionService,
|
||||
FormService,
|
||||
ValueService
|
||||
],
|
||||
})
|
||||
|
||||
@@ -1,14 +1,37 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {HttpCachedService} from '../../../services/http-cached.service';
|
||||
import {QuestionInterface} from '../types/question.interface';
|
||||
import {ResponseInterface} from '../../../services/response.interface';
|
||||
import { Response } from '@angular/http';
|
||||
import {FormInterface} from '../types/form.interface';
|
||||
|
||||
@Injectable()
|
||||
export class QuestionService {
|
||||
export class FormService {
|
||||
private readonly PREFIX = "/form";
|
||||
|
||||
constructor(private httpCachedService: HttpCachedService) {
|
||||
|
||||
}
|
||||
|
||||
public getForm(path: string, cb: (res: FormInterface, err: any) => void) {
|
||||
this.httpCachedService.getJSON(this.PREFIX+path, {}, (data: ResponseInterface, e: Response | any) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
let form: FormInterface = data.data;
|
||||
form.questions = FormService.orderItems(form.questions);
|
||||
form.filters = FormService.orderItems(form.filters);
|
||||
cb(data.data, e);
|
||||
});
|
||||
}
|
||||
|
||||
public getQuestions(cb: (res: QuestionInterface[], err: any) => void) {
|
||||
setTimeout(() => cb(QuestionService.orderItems([
|
||||
setTimeout(() => cb(FormService.orderItems([
|
||||
{
|
||||
type: 'flag',
|
||||
description: 'This is a help tooltip',
|
||||
@@ -1,12 +1,59 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {HttpCachedService} from '../../../services/http-cached.service';
|
||||
import {Response} from '@angular/http';
|
||||
import {ResponseInterface} from '../../../services/response.interface';
|
||||
|
||||
@Injectable()
|
||||
export class ValueService {
|
||||
private readonly PREFIX = "";
|
||||
|
||||
constructor(private httpCachedService: HttpCachedService) {
|
||||
|
||||
}
|
||||
public getValues(cb: (res: Object, err: any) => void) {
|
||||
setTimeout(() => cb({flags:{alpha:true}, ID: 50, textarea: "a long text", cooldrop: "u"}, null), 15);
|
||||
|
||||
public getValue(path: string, id: any, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.getJSON(this.PREFIX+path+"/"+id, {}, (data: ResponseInterface, e: Response | any) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(data.data[0], null);
|
||||
});
|
||||
}
|
||||
|
||||
public getValues(path: string, filters: any, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.getJSON(this.PREFIX+path, this.flatObject(filters), (data: ResponseInterface, e: Response | any) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(data.data, null);
|
||||
});
|
||||
}
|
||||
|
||||
private flatObject(obj: any, prefix?: string): {[_:string]: any} {
|
||||
let ret: {[_:string]: any} = {};
|
||||
if (!prefix)
|
||||
prefix = '';
|
||||
else
|
||||
prefix = prefix+'.';
|
||||
if (Object.prototype.toString.call(obj) == "[object Object]") {
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
ret = Object.assign(ret, this.flatObject(obj[key], prefix+key));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ret[prefix] = obj;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
8
src/app/modules/dyn-form/types/form.interface.ts
Normal file
8
src/app/modules/dyn-form/types/form.interface.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import {QuestionInterface} from './question.interface';
|
||||
export interface FormInterface {
|
||||
description: string,
|
||||
uri: string,
|
||||
name: string,
|
||||
questions: QuestionInterface[],
|
||||
filters: QuestionInterface[]
|
||||
}
|
||||
@@ -22,14 +22,14 @@ export class HttpCachedService extends HttpService {
|
||||
// pending request, add to event emitter
|
||||
this._stored[hash].push(dataFunc);
|
||||
} else {
|
||||
// request alredy stored, send result
|
||||
// request already stored, send result
|
||||
dataFunc(this._stored[hash].data, this._stored[hash].e);
|
||||
}
|
||||
} else {
|
||||
// fulfil request, no matching request stored
|
||||
this._stored[hash] = [];
|
||||
super.getJSON(uri, query, (data, e) => {
|
||||
// request recieved, emit to caller and event subscriber
|
||||
// request received, emit to caller and event subscriber
|
||||
dataFunc(data, e);
|
||||
for (let i = this._stored[hash].length - 1; i >= 0; i--) {
|
||||
this._stored[hash][i](data, e);
|
||||
|
||||
@@ -5,7 +5,7 @@ import { HttpBaseService } from './http-base.service';
|
||||
@Injectable()
|
||||
export class HttpService extends HttpBaseService {
|
||||
get httpBase() {
|
||||
return "http://ccrruby1-1:3085";
|
||||
return "http://localhost:5000";
|
||||
//return '/public/mocks';
|
||||
}
|
||||
get httpSuffix() {
|
||||
|
||||
Reference in New Issue
Block a user