Error during the deserialization of {{data}} as {{contentType}}
`,
directives: [ALFRESCO_DATATABLE_DIRECTIVES, CONTEXT_MENU_DIRECTIVES],
providers: [CONTEXT_MENU_PROVIDERS]
})
export class WebscriptComponent {
@Input()
scriptPath: string;
@Input()
scriptArgs: any;
@Input()
contextRoot: string = 'alfresco';
@Input()
servicePath: string = 'service';
@Input()
contentType: string = 'TEXT';
@Output()
onSuccess = new EventEmitter();
data: any = undefined;
show: boolean = false;
/**
* Constructor
* @param auth
*/
constructor(public authService: AlfrescoAuthenticationService) {
}
ngOnChanges(changes) {
this.clean();
return new Promise((resolve, reject) => {
this.authService.getAlfrescoApi().webScript.executeWebScript('GET', this.scriptPath, this.scriptArgs, this.contextRoot, this.servicePath).then((data) => {
if (this.contentType === 'JSON') {
this.show = this.showDataAsJSON(data);
} else if (this.contentType === 'DATATABLE') {
this.show = this.showDataAsDataTable(data);
} else {
this.show = this.showDataAsHTML(data);
}
this.onSuccess.emit(data);
resolve();
}, function (error) {
console.log('Error' + error);
reject();
});
});
}
/**
* Parserize and show the data id data is a JSON
*
* @param data
*
* @retutns boolean true if the component is able to Show the data as JSON
*/
showDataAsJSON(data: any) {
let jsonShow = true;
try {
this.data = JSON.stringify(data);
let wrapper = document.getElementById('webscript-data');
wrapper.innerHTML = this.data;
} catch (e) {
jsonShow = false;
}
return jsonShow;
}
/**
* Parserize and show the data id data is a html/xml
*
* @param data
*
* @retutns boolean true if the component is able to Show the data as html/xml
*/
showDataAsHTML(data: any) {
let htmlShow = false;
let domParser = new DOMParser();
if (domParser.parseFromString(data, 'text/xml')) {
let wrapper = document.getElementById('webscript-data');
wrapper.innerHTML = data;
this.data = data;
htmlShow = true;
}
return htmlShow;
}
/**
* show the data in a ng2-alfresco-datatable
*
* @param data
*
* @retutns boolean true if the component is able to Show the data as datatable
*/
showDataAsDataTable(data: any) {
let datatableShow = true;
try {
if (!data.schema) {
data.schema = ObjectDataTableAdapter.generationSchema(data.data[0]);
}
if (data.schema && data.schema.length > 0) {
this.data = new ObjectDataTableAdapter(data.data, data.schema);
} else {
datatableShow = false;
}
} catch (e) {
datatableShow = false;
}
return datatableShow;
}
clean() {
let wrapper = document.getElementById('webscript-data');
wrapper.innerHTML = '';
this.data = undefined;
}
isDataTableContent() {
return this.contentType === 'DATATABLE' && this.show;
}
}