Files
alfresco-ng2-components/ng2-components/ng2-activiti-tasklist/src/components/activiti-tasklist.component.ts
2016-12-08 16:45:56 +00:00

250 lines
7.3 KiB
TypeScript

/*!
* @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, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { AlfrescoTranslationService } from 'ng2-alfresco-core';
import { ObjectDataTableAdapter, DataTableAdapter, DataRowEvent, ObjectDataRow } from 'ng2-alfresco-datatable';
import { ActivitiTaskListService } from './../services/activiti-tasklist.service';
import { TaskQueryRequestRepresentationModel } from '../models/filter.model';
declare let componentHandler: any;
@Component({
moduleId: module.id,
selector: 'activiti-tasklist',
templateUrl: './activiti-tasklist.component.html',
styleUrls: ['./activiti-tasklist.component.css']
})
export class ActivitiTaskList implements OnInit, OnChanges {
@Input()
appId: string;
@Input()
processDefinitionKey: string;
@Input()
state: string;
@Input()
assignment: string;
@Input()
sort: string;
@Input()
name: string;
requestNode: TaskQueryRequestRepresentationModel;
@Input()
data: DataTableAdapter;
@Output()
rowClick: EventEmitter<string> = new EventEmitter<string>();
@Output()
onSuccess: EventEmitter<any> = new EventEmitter<any>();
@Output()
onError: EventEmitter<any> = new EventEmitter<any>();
currentInstanceId: string;
private defaultSchemaColumn: any[] = [
{type: 'text', key: 'id', title: 'Id'},
{type: 'text', key: 'name', title: 'Name', cssClass: 'full-width name-column', sortable: true},
{type: 'text', key: 'formKey', title: 'Form Key', sortable: true},
{type: 'text', key: 'created', title: 'Created', sortable: true}
];
constructor(private translate: AlfrescoTranslationService,
public activiti: ActivitiTaskListService) {
if (translate) {
translate.addTranslationFolder('ng2-activiti-tasklist', 'node_modules/ng2-activiti-tasklist/dist/src');
}
}
ngOnInit() {
if (!this.data) {
this.data = this.initDefaultSchemaColumns();
}
this.reload();
}
ngOnChanges(changes: SimpleChanges) {
if (this.isPropertyChanged(changes)) {
this.reload();
}
}
private isPropertyChanged(changes: SimpleChanges): boolean {
let changed: boolean = false;
let appId = changes['appId'];
let processDefinitionKey = changes['processDefinitionKey'];
let state = changes['state'];
let sort = changes['sort'];
let name = changes['name'];
let assignment = changes['assignment'];
if (appId && appId.currentValue) {
changed = true;
} else if (processDefinitionKey && processDefinitionKey.currentValue) {
changed = true;
} else if (state && state.currentValue) {
changed = true;
} else if (sort && sort.currentValue) {
changed = true;
} else if (name && name.currentValue) {
changed = true;
} else if (assignment && assignment.currentValue) {
changed = true;
}
return changed;
}
public reload() {
this.requestNode = this.createRequestNode();
this.load(this.requestNode);
}
/**
* Return an initDefaultSchemaColumns instance with the default Schema Column
* @returns {ObjectDataTableAdapter}
*/
initDefaultSchemaColumns(): ObjectDataTableAdapter {
return new ObjectDataTableAdapter(
[],
this.defaultSchemaColumn
);
}
private load(requestNode: TaskQueryRequestRepresentationModel) {
this.activiti.getTotalTasks(requestNode).subscribe(
(res) => {
requestNode.size = res.total;
this.activiti.getTasks(requestNode).subscribe(
(response) => {
let instancesRow = this.createDataRow(response);
this.renderInstances(instancesRow);
this.selectFirst();
this.onSuccess.emit(response);
}, (error) => {
console.error(error);
this.onError.emit(error);
});
}, (err) => {
console.error(err);
this.onError.emit(err);
});
}
/**
* Create an array of ObjectDataRow
* @param instances
* @returns {ObjectDataRow[]}
*/
private createDataRow(instances: any[]): ObjectDataRow[] {
let instancesRows: ObjectDataRow[] = [];
instances.forEach((row) => {
instancesRows.push(new ObjectDataRow({
id: row.id,
name: row.name,
created: row.created
}));
});
return instancesRows;
}
/**
* Render the instances list
*
* @param instances
*/
private renderInstances(instances: any[]) {
instances = this.optimizeNames(instances);
this.data.setRows(instances);
}
/**
* Select the first instance of a list if present
*/
selectFirst() {
if (!this.isListEmpty()) {
this.currentInstanceId = this.data.getRows()[0].getValue('id');
} else {
this.currentInstanceId = null;
}
}
/**
* Return the current id
* @returns {string}
*/
getCurrentId(): string {
return this.currentInstanceId;
}
/**
* Check if the list is empty
* @returns {ObjectDataTableAdapter|boolean}
*/
isListEmpty(): boolean {
return this.data === undefined ||
(this.data && this.data.getRows() && this.data.getRows().length === 0);
}
/**
* Emit the event rowClick passing the current task id when the row is clicked
* @param event
*/
onRowClick(event: DataRowEvent) {
let item = event;
this.currentInstanceId = item.value.getValue('id');
this.rowClick.emit(this.currentInstanceId);
}
/**
* Optimize name field
* @param istances
* @returns {any[]}
*/
private optimizeNames(istances: any[]) {
istances = istances.map(t => {
t.obj.name = t.obj.name || 'Nameless task';
if (t.obj.name.length > 50) {
t.obj.name = t.obj.name.substring(0, 50) + '...';
}
return t;
});
return istances;
}
private createRequestNode() {
let requestNode = {
appDefinitionId: this.appId,
processDefinitionKey: this.processDefinitionKey,
text: this.name,
assignment: this.assignment,
state: this.state,
sort: this.sort
};
return new TaskQueryRequestRepresentationModel(requestNode);
}
}