Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Replace columns autocompleter select2 with ng-select

Oliver Günther4a68ae74334c
diff --git a/app/assets/stylesheets/content/drag_and_drop.sass b/app/assets/stylesheets/content/drag_and_drop.sass
index 45173f68834..65540555d53 100644
--- a/app/assets/stylesheets/content/drag_and_drop.sass
+++ b/app/assets/stylesheets/content/drag_and_drop.sass
@@ -43,3 +43,5 @@
// added by cdk drag.
.cdk-drag-preview
@include d_n_d--preview
+ // Allow dragged items to appear before modals
+ z-index: 10000 !important
diff --git a/config/locales/js-en.yml b/config/locales/js-en.yml
index 13311d4275f..929f090faa9 100644
--- a/config/locales/js-en.yml
+++ b/config/locales/js-en.yml
@@ -852,7 +852,7 @@ en:
automatic: 'Automatic'
manually: 'Manually'
warning: 'You will lose your previous sorting when activating the automatic sorting mode.'
- columns_help_text: "Use the input above to add or remove columns to your table view. You can drag and drop the columns to reorder them."
+ columns_help_text: "Use the input above to add columns to your table view. You can drag and drop the columns to reorder them."
upsale:
attribute_highlighting: 'Need certain work packages to stand out from the mass?'
relation_columns: 'Need to see relations in the work package list?'
diff --git a/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.html b/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.html
index e900462bfb0..d5ab31b62b4 100644
--- a/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.html
+++ b/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.html
@@ -4,6 +4,11 @@
class="hidden-for-sighted">
</label>
+ <draggable-autocompleter [options]="availableColumnsOptions"
+ [selected]="selectedColumns"
+ (onChange)="updateSelected($event)">
+ </draggable-autocompleter>
+
<ng-container>
<input #select2Columns
type="hidden" />
diff --git a/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.ts b/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.ts
index 6e3d8a840dd..730719580b3 100644
--- a/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.ts
+++ b/frontend/src/app/components/wp-table/configuration-modal/tabs/columns-tab.component.ts
@@ -1,24 +1,21 @@
-import {AfterViewInit, Component, ElementRef, Injector, OnDestroy, ViewChild} from '@angular/core';
+import {Component, Injector, OnInit} from '@angular/core';
import {I18nService} from 'core-app/modules/common/i18n/i18n.service';
import {QueryColumn} from 'core-components/wp-query/query-column';
import {ConfigurationService} from 'core-app/modules/common/config/configuration.service';
import {WorkPackageViewColumnsService} from 'core-app/modules/work_packages/routing/wp-view-base/view-services/wp-view-columns.service';
import {TabComponent} from 'core-components/wp-table/configuration-modal/tab-portal-outlet';
import {BannersService} from "core-app/modules/common/enterprise/banners.service";
-
-interface ColumnLike {
- text:string;
- id:string;
-}
+import {DraggableOption} from "core-app/modules/common/draggable-autocomplete/draggable-autocomplete.component";
@Component({
templateUrl: './columns-tab.component.html'
})
-export class WpTableConfigurationColumnsTab implements TabComponent, AfterViewInit, OnDestroy {
+export class WpTableConfigurationColumnsTab implements TabComponent, OnInit {
+ public availableColumnsOptions = this.wpTableColumns.all.map(c => this.column2Like(c));
public availableColumns = this.wpTableColumns.all;
- public availableColumnsMap:{[id:string]: QueryColumn} = _.keyBy(this.availableColumns, c => c.id);
- public selectedColumns:ColumnLike[] = this.wpTableColumns.getColumns().map(c => this.column2Like(c));
+ public availableColumnsMap:{ [id:string]:QueryColumn } = _.keyBy(this.availableColumns, c => c.id);
+ public selectedColumns:DraggableOption[] = this.wpTableColumns.getColumns().map(c => this.column2Like(c));
public selectedColumnMap:{ [id:string]:boolean } = {};
public eeShowBanners:boolean = false;
@@ -33,8 +30,6 @@ export class WpTableConfigurationColumnsTab implements TabComponent, AfterViewIn
upsaleCheckOutLink: this.I18n.t('js.work_packages.table_configuration.upsale.check_out_link')
};
- @ViewChild('select2Columns', { static: true }) select2Columns:ElementRef;
-
constructor(readonly injector:Injector,
readonly I18n:I18nService,
readonly wpTableColumns:WorkPackageViewColumnsService,
@@ -46,79 +41,18 @@ export class WpTableConfigurationColumnsTab implements TabComponent, AfterViewIn
this.wpTableColumns.setColumnsById(this.selectedColumns.map(c => c.id));
}
- public setSelectedColumn(column:ColumnLike) {
- if (this.selectedColumnMap[column.id]) {
- this.selectedColumns.push(column);
- }
- else {
- _.remove(this.selectedColumns, (c:ColumnLike) => c.id === column.id);
- }
- }
-
- public updateSelect2Columns(event:any) {
- const current:string[] = event.val;
-
- this.selectedColumnMap = {};
- this.selectedColumns = current.map(id => {
- this.selectedColumnMap[id] = true;
- return this.column2Like(this.availableColumnsMap[id]!)
- });
- }
-
ngOnInit() {
this.eeShowBanners = this.bannerService.eeShowBanners;
- this.selectedColumns.forEach((c:ColumnLike) => {
+ this.selectedColumns.forEach((c:DraggableOption) => {
this.selectedColumnMap[c.id] = true;
});
}
- ngAfterViewInit() {
- this.setupSelect2();
- }
-
- ngOnDestroy() {
- const input = jQuery(this.select2Columns.nativeElement);
- input.select2('close');
- }
-
- setupSelect2() {
- const input = jQuery(this.select2Columns.nativeElement);
-
- input
- .on('change', this.updateSelect2Columns.bind(this))
- .select2({
- multiple: true,
- tags: false,
- initSelection: (_:any, callback:(data:any) => any) => callback(this.selectedColumns),
- query: (query:any) => {
- let results:ColumnLike[] = [];
-
- this.availableColumns.forEach(c => {
- if (query.term.length === 0 || c.name.toUpperCase().indexOf(query.term.toUpperCase()) >= 0) {
- results.push({id: c.id, text: c.name });
- }
- });
- query.callback({ results: results, more: false });
- }
- })
- // Need to initialize with some empty value in order to hit initSelection
- .select2('val', []);
-
- // Make it sortable
- input
- .select2("container")
- .find("ul.select2-choices")
- .sortable({
- containment: 'parent',
- start: function() { input.select2("onSortStart"); },
- update: function() { input.select2("onSortEnd"); }
- });
-
- input
- .select2('focus')
+ private column2Like(c:QueryColumn):DraggableOption {
+ return { id: c.id, name: c.name };
}
- private column2Like(c:QueryColumn):ColumnLike {
- return { id: c.id, text: c.name };
+ updateSelected(selected:DraggableOption[]) {
+ this.selectedColumns = selected;
}
}
diff --git a/frontend/src/app/helpers/dom/set-window-cursor.helper.ts b/frontend/src/app/helpers/dom/set-window-cursor.helper.ts
new file mode 100644
index 00000000000..129738fd353
--- /dev/null
+++ b/frontend/src/app/helpers/dom/set-window-cursor.helper.ts
@@ -0,0 +1,5 @@
+export namespace DomHelpers {
+ export function setBodyCursor(cursor:string, priority:'important'|'' = '') {
+ document.body.style.setProperty('cursor', cursor, priority);
+ }
+}
\ No newline at end of file
diff --git a/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.html b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.html
new file mode 100644
index 00000000000..2942842af23
--- /dev/null
+++ b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.html
@@ -0,0 +1,31 @@
+<div class="draggable-autocomplete--container">
+ <div cdkDropList
+ class="draggable-autocomplete--selected"
+ cdkDropListOrientation="horizontal"
+ (cdkDropListDropped)="onItemMoved($event)">
+ <div class="draggable-autocomplete--item"
+ cdkDrag
+ (cdkDragStarted)="startDragging($event)"
+ (cdkDragEnded)="stopDragging($event)"
+ *ngFor="let item of selected">
+ <span class="draggable-autocomplete--item-text"
+ [textContent]="item.name"></span>
+ <a (click)="remove(item)"
+ class="draggable-autocomplete--remove-item icon-remove"></a>
+ </div>
+ </div>
+
+ <ng-select #ngSelectComponent
+ [items]="availableOptions"
+ ngClass="draggable-autocomplete--input"
+ bindLabel="name"
+ [multiple]="false"
+ [virtualScroll]="true"
+ appendTo="body"
+ [placeholder]="text.placeholder"
+ [clearSearchOnAdd]="true"
+ [closeOnSelect]="true"
+ (open)="opened()"
+ (change)="select($event)">
+ </ng-select>
+</div>
diff --git a/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.sass b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.sass
new file mode 100644
index 00000000000..c945f07ead0
--- /dev/null
+++ b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.sass
@@ -0,0 +1,29 @@
+.draggable-autocomplete--selected
+ display: flex
+ padding-bottom: 1rem
+
+.draggable-autocomplete--item
+ padding: 5px
+ margin-right: 5px
+ min-width: 50px
+ border: 1px solid #ddd
+ background: #eee
+ cursor: move
+ font-size: 14px
+ display: flex
+ align-items: center
+ justify-content: space-between
+
+ .draggable-autocomplete--remove-item
+ font-size: 8px
+ color: #999
+ padding-left: 5px
+
+.cdk-drag-placeholder
+ opacity: 0
+
+.cdk-drag-animating
+ transition: transform 250ms cubic-bezier(0, 0, 0.2, 1)
+
+.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder)
+ transition: transform 250ms cubic-bezier(0, 0, 0.2, 1)
diff --git a/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.ts b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.ts
new file mode 100644
index 00000000000..36d4e71b4bc
--- /dev/null
+++ b/frontend/src/app/modules/common/draggable-autocomplete/draggable-autocomplete.component.ts
@@ -0,0 +1,98 @@
+import {ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output, ViewChild} from "@angular/core";
+import {I18nService} from "core-app/modules/common/i18n/i18n.service";
+import {NgSelectComponent} from "@ng-select/ng-select";
+import {CdkDragDrop, CdkDragEnd, CdkDragStart, moveItemInArray} from "@angular/cdk/drag-drop";
+import {DomHelpers} from "core-app/helpers/dom/set-window-cursor.helper";
+
+export interface DraggableOption {
+ name:string;
+ id:string;
+}
+
+@Component({
+ selector: 'draggable-autocompleter',
+ templateUrl: './draggable-autocomplete.component.html',
+ styleUrls: ['./draggable-autocomplete.component.sass'],
+ changeDetection: ChangeDetectionStrategy.OnPush
+})
+export class DraggableAutocompleteComponent implements OnInit {
+ /** Options to show in the autocompleter */
+ @Input() options:DraggableOption[];
+
+ /** Order list of selected items */
+ @Input('selected') _selected:DraggableOption[] = [];
+
+ /** Output when autocompleter changes values or items removed */
+ @Output() onChange = new EventEmitter<DraggableOption[]>();
+
+ /** List of items still available for selection */
+ availableOptions:DraggableOption[] = [];
+
+ @ViewChild('ngSelectComponent') public ngSelectComponent:NgSelectComponent;
+
+ text = {
+ placeholder: this.I18n.t('js.label_add_columns')
+ };
+
+ constructor(readonly I18n:I18nService) {
+ }
+
+ ngOnInit():void {
+ this.updateAvailableOptions();
+ }
+
+ select(item:DraggableOption|undefined) {
+ if (!item) {
+ return;
+ }
+
+ this.selected = [...this.selected, item];
+
+ // Remove selection
+ this.ngSelectComponent.clearModel();
+ }
+
+ remove(item:DraggableOption) {
+ this.selected = this.selected.filter(selected => selected.id !== item.id);
+ }
+
+ get selected() {
+ return this._selected;
+ }
+
+ set selected(val:DraggableOption[]) {
+ this._selected = val;
+ this.updateAvailableOptions();
+
+ this.onChange.emit(this.selected);
+ }
+
+ opened() {
+ // Force reposition as a workaround for BUG
+ // https://github.com/ng-select/ng-select/issues/1259
+ setTimeout(() => {
+ const component = this.ngSelectComponent as any;
+ if (component && component.dropdownPanel) {
+ component.dropdownPanel._updatePosition();
+ }
+ }, 25);
+ }
+
+ onItemMoved(event:CdkDragDrop<DraggableOption[]>) {
+ moveItemInArray(this.selected, event.previousIndex, event.currentIndex);
+ }
+
+
+ startDragging($event:CdkDragStart) {
+ DomHelpers.setBodyCursor('move', 'important');
+ }
+
+ stopDragging($event:CdkDragEnd) {
+ DomHelpers.setBodyCursor('auto');
+ }
+
+ private updateAvailableOptions() {
+ this.availableOptions = this.options
+ .filter(item => !this.selected.find(selected => selected.id === item.id));
+ }
+}
diff --git a/frontend/src/app/modules/common/openproject-common.module.ts b/frontend/src/app/modules/common/openproject-common.module.ts
index 999ebbf0cc2..1b744edc8d0 100644
--- a/frontend/src/app/modules/common/openproject-common.module.ts
+++ b/frontend/src/app/modules/common/openproject-common.module.ts
@@ -59,10 +59,7 @@ import {NoResultsComponent} from "core-app/modules/common/no-results/no-results.
import {DragDropModule} from "@angular/cdk/drag-drop";
import {UserAutocompleterComponent} from "app/modules/common/autocomplete/user-autocompleter.component";
import {ScrollableTabsComponent} from "core-app/modules/common/tabs/scrollable-tabs/scrollable-tabs.component";
-import {
- ContentTabsComponent,
- contentTabsSelector
-} from "core-app/modules/common/tabs/content-tabs/content-tabs.component";
+import {ContentTabsComponent} from "core-app/modules/common/tabs/content-tabs/content-tabs.component";
import {EditableToolbarTitleComponent} from "core-app/modules/common/editable-toolbar-title/editable-toolbar-title.component";
import {UserAvatarComponent} from "core-components/user/user-avatar/user-avatar.component";
import {EnterpriseBannerComponent} from "core-components/enterprise-banner/enterprise-banner.component";
@@ -86,7 +83,7 @@ import {CurrentProjectService} from "core-components/projects/current-project.se
import {CurrentUserService} from "core-components/user/current-user.service";
import {WorkPackageAutocompleterComponent} from "core-app/modules/common/autocomplete/wp-autocompleter.component";
import {TimeEntryWorkPackageAutocompleterComponent} from "core-app/modules/common/autocomplete/te-work-package-autocompleter.component";
-import {DynamicBootstrapper} from "core-app/globals/dynamic-bootstrapper";
+import {DraggableAutocompleteComponent} from "core-app/modules/common/draggable-autocomplete/draggable-autocomplete.component";
export function bootstrapModule(injector:Injector) {
// Ensure error reporter is run
@@ -96,7 +93,7 @@ export function bootstrapModule(injector:Injector) {
window.ErrorReporter.addContext((scope) => {
if (currentUser.isLoggedIn) {
- scope.setUser({name: currentUser.name, id: currentUser.userId, email: currentUser.mail});
+ scope.setUser({ name: currentUser.name, id: currentUser.userId, email: currentUser.mail });
}
if (currentProject.inProjectContext) {
@@ -198,6 +195,8 @@ export function bootstrapModule(injector:Injector) {
DynamicModule,
WorkPackageAutocompleterComponent,
+
+ DraggableAutocompleteComponent,
],
declarations: [
OpDatePickerComponent,
@@ -269,6 +268,7 @@ export function bootstrapModule(injector:Injector) {
VersionAutocompleterComponent,
WorkPackageAutocompleterComponent,
TimeEntryWorkPackageAutocompleterComponent,
+ DraggableAutocompleteComponent,
HomescreenNewFeaturesBlockComponent,
BoardVideoTeaserModalComponent
diff --git a/frontend/src/app/modules/common/resizer/resizer.component.ts b/frontend/src/app/modules/common/resizer/resizer.component.ts
index 55c924e9626..f696c77aad9 100644
--- a/frontend/src/app/modules/common/resizer/resizer.component.ts
+++ b/frontend/src/app/modules/common/resizer/resizer.component.ts
@@ -1,4 +1,5 @@
import {Component, EventEmitter, HostListener, Input, OnDestroy, Output} from "@angular/core";
+import {DomHelpers} from "core-app/helpers/dom/set-window-cursor.helper";
export interface ResizeDelta {
@@ -104,11 +105,11 @@ export class ResizerComponent implements OnDestroy {
}
private setResizeCursor() {
- this.setCursor(`${this.cursorClass} !important`);
+ DomHelpers.setBodyCursor(this.cursorClass, 'important');
}
private setAutoCursor() {
- this.setCursor('auto');
+ DomHelpers.setBodyCursor('auto');
}
private buildDelta(event:MouseEvent):ResizeDelta {
@@ -124,12 +125,4 @@ export class ResizerComponent implements OnDestroy {
}
};
}
-
- // Change cursor icon
- // This is handled via JS to ensure
- // that the cursor stays the same even when the mouse leaves the actual resizer.
- private setCursor(style:string) {
- document.getElementsByTagName("body")[0].setAttribute('style',
- `cursor: ${style}`);
- }
}
diff --git a/spec/features/work_packages/table/configuration_modal/column_spec.rb b/spec/features/work_packages/table/configuration_modal/column_spec.rb
index 04aec7196f4..14abb03a342 100644
--- a/spec/features/work_packages/table/configuration_modal/column_spec.rb
+++ b/spec/features/work_packages/table/configuration_modal/column_spec.rb
@@ -35,7 +35,7 @@ describe 'Work Package table configuration modal columns spec', js: true do
columns.remove 'Subject', save_changes: false
columns.add 'Project', save_changes: true
columns.expect_column_available 'Subject'
- columns.expect_column_not_selectable 'Project'
+ columns.expect_column_not_available 'Project'
expect(page).to have_selector('.wp-table--table-header', text: 'ID')
expect(page).to have_selector('.wp-table--table-header', text: 'PROJECT')
diff --git a/spec/support/components/work_packages/columns.rb b/spec/support/components/work_packages/columns.rb
index 3ecfd93d8df..f515976994d 100644
--- a/spec/support/components/work_packages/columns.rb
+++ b/spec/support/components/work_packages/columns.rb
@@ -31,6 +31,7 @@ module Components
class Columns
include Capybara::DSL
include RSpec::Matchers
+ include ::Components::NgSelectAutocompleteHelpers
attr_accessor :trigger_parent
@@ -41,35 +42,24 @@ module Components
def expect_column_not_available(name)
modal_open? or open_modal
- # Open select2
- find('.columns-modal--content .select2-input').click
- expect(page).to have_no_selector('.select2-result-label', text: name)
- find('.columns-modal--content .select2-input').send_keys :escape
- end
-
- def expect_column_not_selectable(name)
- modal_open? or open_modal
-
- # Open select2
- find('.columns-modal--content .select2-input').click
- expect(page).to have_no_selector('.select2-result-label', text: name)
- find('.columns-modal--content .select2-input').send_keys :escape
+ find('.columns-modal--content .draggable-autocomplete--input').click
+ expect(page).to have_no_selector('.ng-option', text: name, visible: :all)
+ find('.columns-modal--content .draggable-autocomplete--input').send_keys :escape
end
def expect_column_available(name)
modal_open? or open_modal
- # Open select2
- find('.columns-modal--content .select2-input').click
- expect(page).to have_selector('.select2-result-label', text: name)
- find('.columns-modal--content .select2-input').send_keys :escape
+ find('.columns-modal--content .draggable-autocomplete--input').click
+ expect(page).to have_selector('.ng-option', text: name, visible: :all)
+ find('.columns-modal--content .draggable-autocomplete--input').send_keys :escape
end
def add(name, save_changes: true)
modal_open? or open_modal
- find('.columns-modal--content .select2-input').click
- find('.select2-results .select2-result-label', text: name).click
+ select_autocomplete '.columns-modal--content .draggable-autocomplete--input',
+ query: name
apply if save_changes
end
@@ -78,8 +68,8 @@ module Components
modal_open? or open_modal
within_modal do
- container = find('.select2-search-choice', text: name)
- container.find('.select2-search-choice-close').click
+ container = find('.draggable-autocomplete--item', text: name)
+ container.find('.draggable-autocomplete--remove-item').click
end
apply if save_changes
@@ -87,7 +77,7 @@ module Components
def expect_checked(name)
within_modal do
- expect(page).to have_selector('.select2-search-choice', text: name)
+ expect(page).to have_selector('.draggable-autocomplete--item', text: name)
end
end
@@ -95,8 +85,8 @@ module Components
modal_open? or open_modal
within_modal do
- expect(page).to have_selector('.select2-search-choice', minimum: 1)
- page.all('.select2-search-choice-close').each do |el|
+ expect(page).to have_selector('.draggable-autocomplete--item', minimum: 1)
+ page.all('.draggable-autocomplete--remove-item').each do |el|
el.click
sleep 1
end