2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import { Action, IActionHandler } from '../../flux/action';
19 import { Dispatch } from '../../flux/store';
21 import { AddErrorInfoAction } from '../../actions/errorActions';
22 import { IApplicationStoreState } from '../../store/applicationStore';
24 export const RowDisabled = Symbol("RowDisabled");
25 import { DataCallback } from ".";
27 export interface IExternalTableState<TData> {
28 order: 'asc' | 'desc';
29 orderBy: string | null;
30 selected: any[] | null;
31 hiddenColumns: string[]
32 rows: (TData & { [RowDisabled]?: boolean })[];
38 filter: { [property: string]: string };
39 preFilter: { [property: string]: string };
42 export type ExternalMethodes<TData> = {
43 reloadAction: (dispatch: Dispatch, getAppState: () => IApplicationStoreState) => Promise<void | AddErrorInfoAction>;
44 createActions: (dispatch: Dispatch, skipRefresh?: boolean) => {
45 onRefresh: () => void;
46 onHandleRequestSort: (orderBy: string) => void;
47 onHandleExplicitRequestSort: (property: string, sortOrder: "asc" | "desc") => void;
48 onToggleFilter: (refresh?: boolean | undefined) => void;
49 onFilterChanged: (property: string, filterTerm: string) => void;
50 onHandleChangePage: (page: number) => void;
51 onHandleChangeRowsPerPage: (rowsPerPage: number | null) => void;
52 onHideColumns: (columnName: string[]) => void;
53 onShowColumns: (columnName: string[]) => void;
54 onClearFilters: () => void;
56 createPreActions: (dispatch: Dispatch, skipRefresh?: boolean) => {
57 onPreFilterChanged: (preFilter: {
58 [key: string]: string;
61 createProperties: (state: IApplicationStoreState) => IExternalTableState<TData>;
62 actionHandler: IActionHandler<IExternalTableState<TData>, Action>;
66 /** Create an actionHandler and actions for external table states. */
67 export function createExternal<TData>(callback: DataCallback<TData>, selectState: (appState: IApplicationStoreState) => IExternalTableState<TData>) : ExternalMethodes<TData> ;
68 export function createExternal<TData>(callback: DataCallback<TData>, selectState: (appState: IApplicationStoreState) => IExternalTableState<TData>, disableRow: (data: TData) => boolean) : ExternalMethodes<TData>;
69 export function createExternal<TData>(callback: DataCallback<TData>, selectState: (appState: IApplicationStoreState) => IExternalTableState<TData>, disableRow?: (data: TData) => boolean) : ExternalMethodes<TData> {
72 abstract class TableAction extends Action { }
75 class RequestSortAction extends TableAction {
76 constructor(public orderBy: string) {
81 class RequestExplicitSortAction extends TableAction {
82 constructor(public propertyName: string, public sortOrder: "asc" | "desc") {
87 class SetSelectedAction extends TableAction {
88 constructor(public selected: TData[] | null) {
93 class SetPageAction extends TableAction {
94 constructor(public page: number) {
99 class SetRowsPerPageAction extends TableAction {
100 constructor(public rowsPerPage: number) {
105 class SetPreFilterChangedAction extends TableAction {
106 constructor(public preFilter: { [key: string]: string }) {
111 class SetFilterChangedAction extends TableAction {
112 constructor(public filter: { [key: string]: string }) {
117 class SetShowFilterAction extends TableAction {
118 constructor(public show: boolean) {
123 class RefreshAction extends TableAction {
129 class SetResultAction extends TableAction {
130 constructor(public result: { page: number, total: number, rows: TData[] }) {
135 class HideColumnsAction extends TableAction{
136 constructor(public property: string[]){
141 class ShowColumnsAction extends TableAction{
142 constructor(public property: string[]){
149 //#region Action Handler
150 const externalTableStateInit: IExternalTableState<TData> = {
165 const externalTableStateActionHandler: IActionHandler<IExternalTableState<TData>> = (state = externalTableStateInit, action) => {
166 if (!(action instanceof TableAction)) return state;
167 if (action instanceof RefreshAction) {
172 } else if (action instanceof SetResultAction) {
177 ? action.result.rows.map((row: TData) => ({...row, [RowDisabled]: disableRow(row) }))
178 : action.result.rows,
179 total: action.result.total,
180 page: action.result.page,
182 } else if (action instanceof RequestSortAction) {
186 orderBy: state.orderBy === action.orderBy && state.order === 'desc' ? null : action.orderBy,
187 order: state.orderBy === action.orderBy && state.order === 'asc' ? 'desc' : 'asc',
189 } else if (action instanceof RequestExplicitSortAction) {
193 orderBy: action.propertyName,
194 order: action.sortOrder
197 else if (action instanceof SetShowFilterAction) {
201 showFilter: action.show
203 } else if (action instanceof SetPreFilterChangedAction) {
207 preFilter: action.preFilter
209 } else if (action instanceof SetFilterChangedAction) {
213 filter: action.filter
215 } else if (action instanceof SetPageAction) {
221 } else if (action instanceof SetRowsPerPageAction) {
225 rowsPerPage: action.rowsPerPage
228 else if (action instanceof HideColumnsAction){
230 //merge arrays, remove duplicates
231 const newArray = [...new Set([...state.hiddenColumns, ...action.property])]
232 state = {...state, hiddenColumns: newArray};
234 else if(action instanceof ShowColumnsAction){
236 const newArray = state.hiddenColumns.filter(el=> !action.property.includes(el));
237 state = {...state, hiddenColumns: newArray};
243 //const createTableAction(tableAction)
246 const reloadAction = (dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
247 dispatch(new RefreshAction());
248 const ownState = selectState(getAppState());
249 const filter = { ...ownState.preFilter, ...(ownState.showFilter && ownState.filter || {}) };
250 return Promise.resolve(callback(ownState.page, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter)).then(result => {
252 if (ownState.page > 0 && ownState.rowsPerPage * ownState.page > result.total) { //if result is smaller than the currently shown page, new search and repaginate
254 let newPage = Math.floor(result.total / ownState.rowsPerPage);
256 Promise.resolve(callback(newPage, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter)).then(result1 => {
257 dispatch(new SetResultAction(result1));
262 dispatch(new SetResultAction(result));
266 }).catch(error => dispatch(new AddErrorInfoAction(error)));
269 const createPreActions = (dispatch: Dispatch, skipRefresh: boolean = false) => {
271 onPreFilterChanged: (preFilter: { [key: string]: string }) => {
272 dispatch(new SetPreFilterChangedAction(preFilter));
273 (!skipRefresh) && dispatch(reloadAction);
278 const createActions = (dispatch: Dispatch, skipRefresh: boolean = false) => {
281 dispatch(reloadAction);
283 onHandleRequestSort: (orderBy: string) => {
284 dispatch((dispatch: Dispatch) => {
285 dispatch(new RequestSortAction(orderBy));
286 (!skipRefresh) && dispatch(reloadAction);
289 onHandleExplicitRequestSort: (property: string, sortOrder: "asc" | "desc") => {
290 dispatch((dispatch: Dispatch) => {
291 dispatch(new RequestExplicitSortAction(property, sortOrder));
292 (!skipRefresh) && dispatch(reloadAction);
295 onToggleFilter: (refresh?: boolean) => {
296 dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
297 const { showFilter } = selectState(getAppState());
298 dispatch(new SetShowFilterAction(!showFilter));
299 if (!skipRefresh && (refresh === undefined || refresh))
300 dispatch(reloadAction);
303 onFilterChanged: (property: string, filterTerm: string) => {
304 dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
305 let { filter } = selectState(getAppState());
306 filter = { ...filter, [property]: filterTerm };
307 dispatch(new SetFilterChangedAction(filter));
308 (!skipRefresh) && dispatch(reloadAction);
311 onHandleChangePage: (page: number) => {
312 dispatch((dispatch: Dispatch) => {
313 dispatch(new SetPageAction(page));
314 (!skipRefresh) && dispatch(reloadAction);
317 onHandleChangeRowsPerPage: (rowsPerPage: number | null) => {
318 dispatch((dispatch: Dispatch) => {
319 dispatch(new SetRowsPerPageAction(rowsPerPage || 10));
320 (!skipRefresh) && dispatch(reloadAction);
323 onHideColumns: (columnName: string[]) =>{
324 dispatch((dispatch: Dispatch) => {
325 dispatch(new HideColumnsAction(columnName));
328 onShowColumns: (columnName: string[]) =>{
329 dispatch((dispatch: Dispatch) => {
330 dispatch(new ShowColumnsAction(columnName));
333 onClearFilters: () => {
334 dispatch((dispatch: Dispatch) => {
336 dispatch(new SetFilterChangedAction(filter));
343 const createProperties = (state: IApplicationStoreState) => {
345 ...selectState(state)
350 reloadAction: reloadAction,
351 createActions: createActions,
352 createProperties: createProperties,
353 createPreActions: createPreActions,
354 actionHandler: externalTableStateActionHandler,