mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
#440 basic REST services support for dropdown widget
This commit is contained in:
@@ -15,7 +15,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component } from '@angular/core';
|
import { Component, OnInit, NgZone } from '@angular/core';
|
||||||
|
import { Observable } from 'rxjs/Rx';
|
||||||
|
import { Http } from '@angular/http';
|
||||||
|
import { ObjectUtils } from 'ng2-alfresco-core';
|
||||||
import { WidgetComponent } from './../widget.component';
|
import { WidgetComponent } from './../widget.component';
|
||||||
|
|
||||||
declare let __moduleName: string;
|
declare let __moduleName: string;
|
||||||
@@ -27,6 +30,53 @@ declare var componentHandler;
|
|||||||
templateUrl: './dropdown.widget.html',
|
templateUrl: './dropdown.widget.html',
|
||||||
styleUrls: ['./dropdown.widget.css']
|
styleUrls: ['./dropdown.widget.css']
|
||||||
})
|
})
|
||||||
export class DropdownWidget extends WidgetComponent {
|
export class DropdownWidget extends WidgetComponent implements OnInit {
|
||||||
|
|
||||||
|
constructor(private http: Http,
|
||||||
|
private zone: NgZone) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
if (this.field &&
|
||||||
|
this.field.optionType === 'rest' &&
|
||||||
|
this.field.restUrl &&
|
||||||
|
this.field.restIdProperty &&
|
||||||
|
this.field.restLabelProperty) {
|
||||||
|
|
||||||
|
let url = `${this.field.restUrl}`;
|
||||||
|
this.http.get(url).subscribe(
|
||||||
|
response => {
|
||||||
|
let json: any = response.json();
|
||||||
|
this.loadFromJson(json);
|
||||||
|
},
|
||||||
|
this.handleError
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: support 'restResponsePath'
|
||||||
|
private loadFromJson(json: any) {
|
||||||
|
if (json instanceof Array) {
|
||||||
|
let options = json.map(obj => {
|
||||||
|
return {
|
||||||
|
id: ObjectUtils.getValue(obj, this.field.restIdProperty).toString(),
|
||||||
|
name: ObjectUtils.getValue(obj, this.field.restLabelProperty).toString()
|
||||||
|
};
|
||||||
|
});
|
||||||
|
this.field.options = options;
|
||||||
|
this.field.updateForm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleError (error: any) {
|
||||||
|
// In a real world app, we might use a remote logging infrastructure
|
||||||
|
// We'd also dig deeper into the error to get a better message
|
||||||
|
let errMsg = (error.message) ? error.message :
|
||||||
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
||||||
|
console.error(errMsg); // log to console instead
|
||||||
|
return Observable.throw(errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -45,28 +45,25 @@ export interface FormFieldOption {
|
|||||||
|
|
||||||
export class FormFieldModel extends FormWidgetModel {
|
export class FormFieldModel extends FormWidgetModel {
|
||||||
|
|
||||||
private _fieldType: string;
|
|
||||||
private _id: string;
|
|
||||||
private _name: string;
|
|
||||||
private _type: string;
|
|
||||||
private _value: string;
|
private _value: string;
|
||||||
private _tab: string;
|
|
||||||
|
|
||||||
get fieldType(): string {
|
fieldType: string;
|
||||||
return this._fieldType;
|
id: string;
|
||||||
}
|
name: string;
|
||||||
|
type: string;
|
||||||
get id(): string {
|
required: boolean;
|
||||||
return this._id;
|
readOnly: boolean;
|
||||||
}
|
overrideId: boolean;
|
||||||
|
tab: string;
|
||||||
get name(): string {
|
colspan: number = 1;
|
||||||
return this._name;
|
options: FormFieldOption[] = [];
|
||||||
}
|
restUrl: string;
|
||||||
|
restResponsePath: string;
|
||||||
get type(): string {
|
restIdProperty: string;
|
||||||
return this._type;
|
restLabelProperty: string;
|
||||||
}
|
hasEmptyValue: boolean;
|
||||||
|
className: string;
|
||||||
|
optionType: string;
|
||||||
|
|
||||||
get value(): any {
|
get value(): any {
|
||||||
return this._value;
|
return this._value;
|
||||||
@@ -77,24 +74,27 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
this.updateForm();
|
this.updateForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
get tab(): string {
|
|
||||||
return this._tab;
|
|
||||||
}
|
|
||||||
|
|
||||||
colspan: number = 1;
|
|
||||||
options: FormFieldOption[] = [];
|
|
||||||
|
|
||||||
constructor(form: FormModel, json?: any) {
|
constructor(form: FormModel, json?: any) {
|
||||||
super(form, json);
|
super(form, json);
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
this._fieldType = json.fieldType;
|
this.fieldType = json.fieldType;
|
||||||
this._id = json.id;
|
this.id = json.id;
|
||||||
this._name = json.name;
|
this.name = json.name;
|
||||||
this._type = json.type;
|
this.type = json.type;
|
||||||
this._tab = json.tab;
|
this.required = <boolean> json.required;
|
||||||
|
this.readOnly = <boolean> json.readOnly;
|
||||||
|
this.overrideId = <boolean> json.overrideId;
|
||||||
|
this.tab = json.tab;
|
||||||
|
this.restUrl = json.restUrl;
|
||||||
|
this.restResponsePath = json.restResponsePath;
|
||||||
|
this.restIdProperty = json.restIdProperty;
|
||||||
|
this.restLabelProperty = json.restLabelProperty;
|
||||||
this.colspan = <number> json.colspan;
|
this.colspan = <number> json.colspan;
|
||||||
this.options = <FormFieldOption[]> json.options || [];
|
this.options = <FormFieldOption[]> json.options || [];
|
||||||
|
this.hasEmptyValue = <boolean> json.hasEmptyValue;
|
||||||
|
this.className = json.className;
|
||||||
|
this.optionType = json.optionType;
|
||||||
|
|
||||||
this._value = this.parseValue(json);
|
this._value = this.parseValue(json);
|
||||||
this.updateForm();
|
this.updateForm();
|
||||||
@@ -118,7 +118,7 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateForm() {
|
updateForm() {
|
||||||
/*
|
/*
|
||||||
This is needed due to Activiti reading dropdown values as string
|
This is needed due to Activiti reading dropdown values as string
|
||||||
but saving back as object: { id: <id>, name: <name> }
|
but saving back as object: { id: <id>, name: <name> }
|
||||||
|
Reference in New Issue
Block a user