OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/frontend/app/components/wp-fast-table/state/wp-table-columns.service.ts

239 lines
6.5 KiB

// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {
WorkPackageTableBaseService,
TableStateStates
} from './wp-table-base.service';
import {
States
} from '../../states.service';
import {opServicesModule} from '../../../angular-modules';
import {WPTableRowSelectionState} from '../wp-table.interfaces';
import {QueryColumn} from '../../api/api-v3/hal-resources/query-resource.service'
import {Observable} from 'rxjs/Observable';
import {WorkPackageTableColumns} from '../wp-table-columns'
import {WorkPackageTableBaseState} from '../wp-table-base';
import {QueryResource} from '../../api/api-v3/hal-resources/query-resource.service';
import {QuerySchemaResourceInterface} from '../../api/api-v3/hal-resources/query-schema-resource.service';
export class WorkPackageTableColumnsService extends WorkPackageTableBaseService {
protected stateName = 'columns' as TableStateStates;
constructor(protected states: States) {
super(states);
}
public initialize(query:QueryResource, schema?:QuerySchemaResourceInterface) {
let state = this.create(query, schema);
this.state.putValue(state);
}
public update(query:QueryResource, schema?:QuerySchemaResourceInterface) {
let currentState = this.currentState;
if (currentState) {
currentState.update(query, schema);
this.state.putValue(currentState);
} else {
this.initialize(query, schema);
}
}
protected create(query:QueryResource, schema?:QuerySchemaResourceInterface) {
return new WorkPackageTableColumns(query, schema)
}
/**
* Retrieve the QueryColumn objects for the selected columns
*/
public getColumns():any[] {
return (this.currentState && this.currentState.getColumns()) || [];
}
/**
* Return the index of the given column or -1 if it is not contained.
*/
public index(id:string):number {
return _.findIndex(this.getColumns(), column => column.id === id);
}
/**
* Return the previous column of the given column name
* @param name
*/
public previous(column:QueryColumn):QueryColumn|null {
let index = this.index(column.id);
if (index <= 0) {
return null;
}
return this.getColumns()[index - 1];
}
/**
* Return the next column of the given column name
* @param name
*/
public next(column:QueryColumn):QueryColumn|null {
let index = this.index(column.id);
if (index === -1 || this.isLast(name)) {
return null;
}
return this.getColumns()[index + 1];
}
/**
* Returns true if the column is the first selected
*/
public isFirst(column:QueryColumn):boolean {
return this.index(column.id) === 0;
}
/**
* Returns true if the column is the last selected
*/
public isLast(column:QueryColumn):boolean {
return this.index(column.id) === this.columnCount - 1;
}
/**
* Update the selected columns to a new set of columns.
*/
public setColumns(columns:QueryColumn[]) {
let currentState = this.currentState;
currentState.current = columns;
this.state.putValue(currentState);
}
/**
* Move the column at index {fromIndex} to {toIndex}.
* - If toIndex is larger than all columns, insert at the end.
* - If toIndex is less than zero, insert at the start.
*/
public moveColumn(fromIndex:number, toIndex:number) {
let columns = this.getColumns();
if (toIndex >= columns.length) {
toIndex = columns.length - 1;
}
if (toIndex < 0) {
toIndex = 0;
}
let element = columns[fromIndex];
columns.splice(fromIndex, 1);
columns.splice(toIndex, 0, element);
8 years ago
this.setColumns(columns);
}
/**
* Shift the given column name X indices,
* where X is the offset in indices (-1 = shift one to left)
*/
public shift(column:QueryColumn, offset:number) {
let index = this.index(column.id);
if (index === -1) {
return;
}
this.moveColumn(index, index + offset);
}
/**
* Add a new column to the selection at the given position
*/
public addColumn(id:string, position?:number) {
let columns = this.getColumns();
if (position === undefined) {
position = columns.length;
}
if (this.index(id) === -1) {
let newColumn = _.find(this.all, (column) => column.id === id);
if (!newColumn) {
throw "Column with provided name is not found";
}
columns.splice(position, 0, newColumn);
8 years ago
this.setColumns(columns);
}
}
/**
* Remove a column from the active list
*/
public removeColumn(column:QueryColumn) {
let index = this.index(column.id);
if (index !== -1) {
let columns = this.getColumns();
columns.splice(index, 1);
8 years ago
this.setColumns(columns);
}
}
// only exists to cast the state
protected get currentState():WorkPackageTableColumns {
return this.state.value as WorkPackageTableColumns;
}
/**
* Return the number of selected rows.
*/
public get columnCount():number {
return this.getColumns().length;
}
/**
* Get all available columns (regardless of whether they are selected already)
*/
public get all():QueryColumn[] {
return this.currentState.available || [];
}
/**
* Get columns not yet selected
*/
public get unused():QueryColumn[] {
return _.differenceBy(this.all, this.getColumns(), '$href');
}
}
opServicesModule.service('wpTableColumns', WorkPackageTableColumnsService);