/*! * @license * Copyright 2016 Alfresco Software, Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { Component, Input, Output, EventEmitter } from '@angular/core'; import { AlfrescoAuthenticationService, CONTEXT_MENU_DIRECTIVES, CONTEXT_MENU_PROVIDERS } from 'ng2-alfresco-core'; import { ALFRESCO_DATATABLE_DIRECTIVES, ObjectDataTableAdapter } from 'ng2-alfresco-datatable'; /** *
{{data | json}}
{{data}}
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() showData: boolean = true; @Input() contextRoot: string = 'alfresco'; @Input() servicePath: string = 'service'; @Input() contentType: string = 'TEXT'; @Output() onSuccess = new EventEmitter(); data: any = undefined; showError: boolean = false; /** * Constructor * @param auth */ constructor(public authService: AlfrescoAuthenticationService) { } ngOnChanges(changes) { if (this.showData) { this.clean(); } return new Promise((resolve, reject) => { this.authService.getAlfrescoApi().webScript.executeWebScript('GET', this.scriptPath, this.scriptArgs, this.contextRoot, this.servicePath).then((webScriptdata) => { this.data = webScriptdata; if (this.showData) { if (this.contentType === 'DATATABLE') { this.data = this.showDataAsDataTable(webScriptdata); } } this.onSuccess.emit(this.data); resolve(); }, function (error) { console.log('Error' + error); reject(); }); }); } /** * show the data in a ng2-alfresco-datatable * * @param data * * @retutns the data as datatable */ showDataAsDataTable(data: any) { let datatableData: any = null; try { if (!data.schema) { data.schema = ObjectDataTableAdapter.generateSchema(data.data); } if (data.schema && data.schema.length > 0) { this.data = new ObjectDataTableAdapter(data.data, data.schema); } } catch (e) { console.log('error during the cast as datatable'); } return datatableData; } clean() { this.data = undefined; } isDataTableContent() { return this.contentType === 'DATATABLE'; } }