diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9285ba7..22ecb9c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,16 @@ - + + + + - - - - - + + + @@ -25,37 +26,107 @@ - - + + - - - - - - - - - - - - - - - - - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -64,93 +135,13 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -172,21 +163,24 @@ @@ -388,36 +382,6 @@ - @@ -655,8 +626,6 @@ @@ -685,55 +656,23 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -763,11 +702,13 @@ - + - + + + @@ -792,10 +733,10 @@ - - - - + + + + @@ -839,7 +780,7 @@ - + @@ -935,23 +876,6 @@ - - - - - - - - - - - - - - - - - @@ -988,14 +912,6 @@ - - - - - - - - @@ -1012,16 +928,6 @@ - - - - - - - - - - @@ -1040,76 +946,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1141,7 +983,7 @@ - + @@ -1154,13 +996,149 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/app/app.component.html b/src/app/app.component.html index ae6471b..c03d6ed 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1 +1,3 @@ +

{{formName}}

+

{{formDescription}}

\ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index fd8c6ba..cd5880a 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -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); + } }); } diff --git a/src/app/modules/dyn-form/dyn-form.component.ts b/src/app/modules/dyn-form/dyn-form.component.ts index bb1ad17..3db8063 100644 --- a/src/app/modules/dyn-form/dyn-form.component.ts +++ b/src/app/modules/dyn-form/dyn-form.component.ts @@ -11,7 +11,7 @@ import {FormControl, FormGroup} from '@angular/forms'; [question]="question" [form]="form" [type]="type" (onChange)="valueChange.emit(form.value)">
- +
` @@ -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 = new EventEmitter(); @Output() onSubmit: EventEmitter = new EventEmitter(); @@ -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]; } } diff --git a/src/app/modules/dyn-form/dyn-form.module.ts b/src/app/modules/dyn-form/dyn-form.module.ts index 67fd140..1a0388c 100644 --- a/src/app/modules/dyn-form/dyn-form.module.ts +++ b/src/app/modules/dyn-form/dyn-form.module.ts @@ -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 ], }) diff --git a/src/app/modules/dyn-form/services/question.service.ts b/src/app/modules/dyn-form/services/form.service.ts similarity index 73% rename from src/app/modules/dyn-form/services/question.service.ts rename to src/app/modules/dyn-form/services/form.service.ts index 8f91a0e..115ab40 100644 --- a/src/app/modules/dyn-form/services/question.service.ts +++ b/src/app/modules/dyn-form/services/form.service.ts @@ -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', diff --git a/src/app/modules/dyn-form/services/value.service.ts b/src/app/modules/dyn-form/services/value.service.ts index b1c383d..1c77256 100644 --- a/src/app/modules/dyn-form/services/value.service.ts +++ b/src/app/modules/dyn-form/services/value.service.ts @@ -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; } } \ No newline at end of file diff --git a/src/app/modules/dyn-form/types/form.interface.ts b/src/app/modules/dyn-form/types/form.interface.ts new file mode 100644 index 0000000..89ba4f4 --- /dev/null +++ b/src/app/modules/dyn-form/types/form.interface.ts @@ -0,0 +1,8 @@ +import {QuestionInterface} from './question.interface'; +export interface FormInterface { + description: string, + uri: string, + name: string, + questions: QuestionInterface[], + filters: QuestionInterface[] +} \ No newline at end of file diff --git a/src/app/services/http-cached.service.ts b/src/app/services/http-cached.service.ts index 6d67e82..de49a61 100644 --- a/src/app/services/http-cached.service.ts +++ b/src/app/services/http-cached.service.ts @@ -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); diff --git a/src/app/services/http.service.ts b/src/app/services/http.service.ts index c461192..a347a4b 100644 --- a/src/app/services/http.service.ts +++ b/src/app/services/http.service.ts @@ -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() {