From 4a86235a8602106fd38411e8a9eeb587c1604a9f Mon Sep 17 00:00:00 2001 From: mauriziovitale84 Date: Fri, 15 Jul 2016 16:29:22 +0100 Subject: [PATCH] Add Tasklist filters --- .../activiti-tasklist.component.html | 8 ++ .../components/activiti-tasklist.component.ts | 61 +++++---- .../src/models/filter.model.ts | 57 +++++++++ .../src/services/activiti-tasklist.service.ts | 119 ++++++------------ 4 files changed, 139 insertions(+), 106 deletions(-) create mode 100644 ng2-components/ng2-alfresco-activiti-tasklist/src/models/filter.model.ts diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.html b/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.html index 883d9c28e2..45d54a5c0c 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.html +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.html @@ -1 +1,9 @@ + + \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.ts index 05db69546c..34b56a31f7 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.ts +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/components/activiti-tasklist.component.ts @@ -15,11 +15,13 @@ * limitations under the License. */ -import { Component, Input } from '@angular/core'; +import { Component, Input, OnInit} from '@angular/core'; import { AlfrescoTranslationService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; import { ALFRESCO_DATATABLE_DIRECTIVES, ObjectDataTableAdapter, DataTableAdapter } from 'ng2-alfresco-datatable'; import { ActivitiTaskListService } from './../services/activiti-tasklist.service'; - +import { FilterModel } from '../models/filter.model'; +import { Observer } from 'rxjs/Observer'; +import { Observable } from 'rxjs/Observable'; declare let componentHandler: any; declare let __moduleName: string; @@ -32,16 +34,18 @@ declare let __moduleName: string; providers: [ActivitiTaskListService] }) -export class ActivitiTaskList { +export class ActivitiTaskList implements OnInit { + + @Input() + data: DataTableAdapter; + + private filterObserver: Observer; + + filter$: Observable; tasks: ObjectDataTableAdapter; - @Input() - data: DataTableAdapter; - - @Input() - assignment: string; - + filtersList: Observable; /** * Constructor * @param auth @@ -50,26 +54,20 @@ export class ActivitiTaskList { constructor(private auth: AlfrescoAuthenticationService, private translate: AlfrescoTranslationService, private activiti: ActivitiTaskListService) { + this.filter$ = new Observable(observer => this.filterObserver = observer).share(); translate.addTranslationFolder('node_modules/ng2-alfresco-activiti-tasklist'); + } - if (auth.isLoggedIn('BPM')) { - activiti.getTaskListFilters().subscribe((resFilter) => { - let tasksListFilter = resFilter.data || []; - if (tasksListFilter.length === 0) { - activiti.createMyTaskFilter().subscribe(() => { - console.log('Default filters created'); - }); - activiti.getTasks(this.assignment).subscribe((res) => { - let tasks = res.data || []; - console.log(tasks); - this.loadTasks(tasks); - }); - } + ngOnInit() { + this.filtersList = this.activiti.getTaskListFilters().map(res => (res.data)); + + this.filter$.subscribe( (filter: FilterModel) => { + this.activiti.getTasks(filter).subscribe((res) => { + let tasks = res.data; + this.loadTasks(tasks); }); - } else { - console.error('User unauthorized'); - } + }); } /** @@ -81,6 +79,19 @@ export class ActivitiTaskList { this.tasks = new ObjectDataTableAdapter(tasks, this.data.getColumns()); } + /** + * Pass the selected filter as next + * @param filter + */ + public selectFilter(filter: FilterModel) { + this.filterObserver.next(filter); + } + + /** + * Optimize task name field + * @param tasks + * @returns {any[]} + */ private optimizeTaskName(tasks: any[]) { tasks = tasks.map(t => { t.name = t.name || 'Nameless task'; diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/models/filter.model.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/models/filter.model.ts new file mode 100644 index 0000000000..5aa422c1a3 --- /dev/null +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/models/filter.model.ts @@ -0,0 +1,57 @@ +/*! + * @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. + */ + +/** + * + * This object represent the filter. + * + * + * @returns {FilterModel} . + */ +export class FilterModel { + id: number; + name: string; + recent: boolean = false; + icon: string; + filter: FilterParamsModel; + + constructor(name: string, recent: boolean, icon: string, state: string, assignment: string) { + this.name = name; + this.recent = recent; + this.icon = icon; + this.filter = new FilterParamsModel(name, state, assignment); + } +} + +/** + * + * This object represent the parameters of a filter. + * + * + * @returns {FilterModel} . + */ +export class FilterParamsModel { + name: string; + state: string; + assignment: string; + + constructor(name: string, state: string, assignment: string) { + this.name = name; + this.state = state; + this.assignment = assignment; + } +} diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts index b98bfae039..56af454e54 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts @@ -18,19 +18,55 @@ import { Injectable } from '@angular/core'; import { Http, Headers, RequestOptions, Response } from '@angular/http'; import { Observable } from 'rxjs/Rx'; +import { FilterModel } from '../models/filter.model'; @Injectable() export class ActivitiTaskListService { - constructor(private http: Http) {} + constructor(private http: Http) { + } + /** + * Retrive all the Tasks filters + * @returns {Observable} + */ getTaskListFilters() { + return this.callApiTaskFilters(); + } + + /** + * Retrive all the tasks created in activiti + * @returns {any} + */ + getTasks(filter: FilterModel): Observable { + let data: any = {}; + data.filterId = filter.id; + data.filter = filter.filter; + data = JSON.stringify(data); + return this.callApiTasksFiltered(data); + } + + private callApiTasksFiltered(data: Object): Observable { + let url = 'http://localhost:9999/activiti-app/app/rest/filter/tasks'; + let headers = new Headers({ + 'Content-Type': 'application/json', + 'Cache-Control': 'no-cache' + }); + let options = new RequestOptions({headers: headers}); + + return this.http + .post(url, data, options) + .map((res: Response) => res.json()) + .catch(this.handleError); + } + + private callApiTaskFilters(): Observable { let url = 'http://localhost:9999/activiti-app/app/rest/filters/tasks'; let headers = new Headers({ 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' }); - let options = new RequestOptions({ headers: headers }); + let options = new RequestOptions({headers: headers}); return this.http .get(url, options) @@ -38,85 +74,6 @@ export class ActivitiTaskListService { .catch(this.handleError); } - createMyTaskFilter() { - let data = JSON.stringify({ - 'name': 'My Tasks', - 'index': 1, - 'icon': 'glyphicon-inbox', - 'filter': { - 'name': '', - 'sort': 'created-desc', - 'state': 'open', - 'assignment': 'assignee' - }, - 'appId': null - }); - return this.callApiCreateFilter(data); - } - - /** - * Retrive all the tasks created in activiti - * @returns {any} - */ - getTasks(assignment: string = 'assignee'): Observable { - let data = JSON.stringify({ - 'page': 0, - 'filterId': 1, - 'filter': { - 'name': '', - 'sort': 'created-desc', - 'state': 'open', - 'assignment': assignment - }, - 'appDefinitionId': null - }); - return this.callApiFilterTasks(data); - } - - getInvolvedTasks(): Observable { - let data = JSON.stringify({ - 'page': 0, - 'filterId': 3, - 'filter': { - 'name': '', - 'sort': 'created-desc', - 'state': 'open', - 'assignment': 'involved' - }, - 'appDefinitionId': null - }); - return this.callApiFilterTasks(data); - } - - private callApiCreateFilter(data: Object): Observable { - - let url = 'http://localhost:9999/activiti-app/app/rest/filters/tasks'; - let headers = new Headers({ - 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache' - }); - let options = new RequestOptions({ headers: headers }); - - return this.http - .post(url, data, options) - .map((res: Response) => res.json()) - .catch(this.handleError); - } - - private callApiFilterTasks(data: Object): Observable { - - let url = 'http://localhost:9999/activiti-app/app/rest/filter/tasks'; - let headers = new Headers({ - 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache' - }); - let options = new RequestOptions({ headers: headers }); - - return this.http - .post(url, data, options) - .map((res: Response) => res.json()) - .catch(this.handleError); - } /** * The method write the error in the console browser