109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
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 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(FormService.orderItems([
|
|
{
|
|
type: 'flag',
|
|
description: 'This is a help tooltip',
|
|
properties: {
|
|
key: 'flags',
|
|
label: 'Form Type Flags',
|
|
order: 3,
|
|
|
|
// dropdown && flags
|
|
options: [{key: 'alpha', value: 'A'}, {key: 'beta', value: 'B'}, {key: 'gamma', value: 'C'}, {key: 'delta', value: 'D'}]
|
|
}, constraints: {
|
|
optional: true
|
|
}
|
|
}, {
|
|
type: 'dropdown',
|
|
description: 'Cool dropdown',
|
|
properties: {
|
|
key: 'type',
|
|
label: 'Dropdown',
|
|
order: 2,
|
|
methods: ['insert'],
|
|
|
|
// dropdown
|
|
options: [{key: 'hello', value: 'Hallo'}, {key: 'world', value: 'World'}, {key: 'and', value: 'And'}, {key: 'u', value: 'You'}]
|
|
}, constraints: {
|
|
optional: false
|
|
}
|
|
}, {
|
|
type: 'hidden',
|
|
description: 'ID',
|
|
properties: {
|
|
key: 'ID',
|
|
order: 1
|
|
}, constraints: {
|
|
optional: false
|
|
}
|
|
}, {
|
|
type: 'textbox',
|
|
description: 'This is a very long box. Fill it!',
|
|
properties: {
|
|
key: 'textarea',
|
|
label: 'Textareaaaa',
|
|
order: 4,
|
|
specialization: 'world',
|
|
methods: ['insert']
|
|
}, constraints: {
|
|
optional: false
|
|
}
|
|
}, {
|
|
type: 'checkbox',
|
|
description: 'This is a very long box. Fill it!',
|
|
properties: {
|
|
key: 'checki',
|
|
label: 'To be or not to be?',
|
|
order: 5
|
|
}, constraints: {
|
|
optional: false
|
|
}
|
|
}
|
|
]), null), 10);
|
|
}
|
|
|
|
private static orderItems(list: QuestionInterface[]): QuestionInterface[] {
|
|
function compare(a: any, b: any) {
|
|
if (a.properties.order < b.properties.order)
|
|
return -1;
|
|
if (a.properties.order > b.properties.order)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
list.sort(compare);
|
|
return list;
|
|
}
|
|
} |