Fix wp-query-menu entrance directive not able to downgrade

pull/6263/head
Oliver Günther 7 years ago
parent 7b79fe3f0a
commit 94232c590c
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 2
      frontend/app/angular4-modules.ts
  2. 203
      frontend/app/components/wp-query-menu/wp-query-menu.directive.ts
  3. 21
      frontend/app/components/wp-query-menu/wp-query-menu.ng2.test.ts
  4. 154
      frontend/app/components/wp-query-menu/wp-query-menu.service.ts

@ -202,7 +202,6 @@ import {WorkPackageFormQueryGroupComponent} from 'core-components/wp-form-group/
import {WorkPackageFormAttributeGroupComponent} from 'core-components/wp-form-group/wp-attribute-group.component';
import {WorkPackageRelationsService} from 'core-components/wp-relations/wp-relations.service';
import {UrlParamsHelperService} from 'core-components/wp-query/url-params-helper';
import {WpQueryMenuDirective} from 'core-components/wp-query-menu/wp-query-menu.directive';
import {AttributeHelpTextComponent} from 'core-components/common/help-texts/attribute-help-text.component';
import {AttributeHelpTextModal} from 'core-components/common/help-texts/attribute-help-text.modal';
import {AttributeHelpTextsService} from 'core-components/common/help-texts/attribute-help-text.service';
@ -293,7 +292,6 @@ import {UploadProgressComponent} from 'core-components/common/notifications/uplo
ConfigurationService,
upgradeService('currentProject', CurrentProjectService),
QueryMenuService,
WpQueryMenuDirective,
// Split view
upgradeService('firstRoute', FirstRouteService),
PathHelperService,

@ -1,203 +0,0 @@
// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2018 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 {WorkPackagesListChecksumService} from 'core-components/wp-list/wp-list-checksum.service';
import {LinkHandling} from 'core-components/common/link-handling/link-handling';
import {
QueryMenuEvent,
QueryMenuService
} from 'core-components/wp-query-menu/wp-query-menu.service';
import {StateService, Transition, TransitionService} from '@uirouter/core';
import {Directive, ElementRef, Inject, OnInit} from '@angular/core';
import {$stateToken} from 'core-app/angular4-transition-utils';
export const QUERY_MENU_ITEM_TYPE = 'query-menu-item';
@Directive({
selector: '[wp-query-menu]'
})
export class WpQueryMenuDirective implements OnInit {
private currentQueryId:string|null = null;
private uiRouteStateName = 'work-packages.list';
private container:ng.IAugmentedJQuery;
private $element:JQuery;
constructor(protected elementRef:ElementRef,
@Inject($stateToken) protected $state:StateService,
protected $transitions:TransitionService,
protected queryMenu:QueryMenuService,
protected wpListChecksumService:WorkPackagesListChecksumService) {
}
public ngOnInit() {
this.$element = jQuery(this.elementRef.nativeElement);
this.container = this.$element.parent().find('ul.menu-children');
this.$transitions.onStart({}, (transition:Transition) => {
const queryId = transition.params('to').queryId;
this.onQueryIdChanged(queryId);
});
this.queryMenu
.on('remove')
.subscribe((e:QueryMenuEvent) => this.removeItem(e));
this.queryMenu
.on('add')
.subscribe((e:QueryMenuEvent) => this.addItem(e));
this.queryMenu
.on('rename')
.subscribe((e:QueryMenuEvent) => this.renameItem(e));
this.container.on('click', `.${QUERY_MENU_ITEM_TYPE}`, (event) => {
if (LinkHandling.isClickedWithModifier(event) || LinkHandling.isOutsideAngular()) {
return true;
}
this.switchOrReload(jQuery(event.target));
event.preventDefault();
return false;
});
}
public onQueryIdChanged(queryId:string|null) {
this.currentQueryId = queryId;
this.setSelectedState();
}
private removeItem(e:QueryMenuEvent) {
const item = this.findItem(e.queryId);
this.setSelectedState();
}
private setSelectedState() {
// Set WP menu to selected if no current query id set
if (this.currentQueryId) {
this.$element.removeClass('selected');
}
// Update all queries children
const queries = this.container.find('.query-menu-item');
queries.toggleClass('selected', false);
if (this.currentQueryId) {
queries.filter(`#wp-query-menu-item-${this.currentQueryId}`).addClass('selected');
}
}
private renameItem(e:QueryMenuEvent) {
this.findItem(e.queryId)
.find('.menu-item--title')
.text(e.label!);
}
private addItem(e:QueryMenuEvent) {
const item = this.buildItem(e);
const previous = this.previousMenuItem(e.label!);
if (previous) {
jQuery(item).insertAfter(previous);
} else {
this.container.append(item);
}
this.setSelectedState();
}
private buildItem(e:QueryMenuEvent) {
const li = document.createElement('li');
const link = document.createElement('a');
link.id = `wp-query-menu-item-${e.queryId}`;
link.classList.add(QUERY_MENU_ITEM_TYPE);
link.dataset.queryId = e.queryId;
const span = document.createElement('span');
span.classList.add('menu-item--title', 'ellipsis');
span.textContent = e.label!;
link.appendChild(span);
li.appendChild(link);
return li;
}
private findItem(queryId:string) {
return this.container.find(`#wp-query-menu-item-${queryId}`);
}
private switchOrReload(item:JQuery) {
const queryId = item.data('queryId').toString();
let opts = {reload: false};
if (queryId === this.currentQueryId) {
this.wpListChecksumService.clear();
opts.reload = true;
}
this.$state.go(
this.uiRouteStateName,
{query_props: null, query_id: queryId },
opts
);
}
/**
* previousMenuItem
*
* Returns the menu item within the factories's container that has a title
* alphabetically before the provided title. The considered menu items have
* the type (css class) this factory is responsible for.
*
* Params
* * title: The string used for comparing.
*/
public previousMenuItem(title:string):ng.IAugmentedJQuery|null {
const allItems = this.container.find('li');
if (allItems.length === 0) {
return null;
}
let previousElement = angular.element(allItems[allItems.length - 1]);
let i = allItems.length - 2;
for (i; i >= 0; i--) {
if ((title > previousElement.find('a').attr('title')) ||
(previousElement.find('.' + QUERY_MENU_ITEM_TYPE).length === 0)) {
return previousElement;
}
else {
previousElement = angular.element(allItems[i]);
}
}
return previousElement;
}
}

@ -28,9 +28,8 @@
/*jshint expr: true*/
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {QueryMenuService} from 'core-components/wp-query-menu/wp-query-menu.service';
import {WpQueryMenuDirective} from 'core-components/wp-query-menu/wp-query-menu.directive';
import {Component} from '@angular/core';
import {WorkPackagesListChecksumService} from 'core-components/wp-list/wp-list-checksum.service';
import {TransitionService} from '@uirouter/core';
@ -38,21 +37,23 @@ import {$stateToken} from 'core-app/angular4-transition-utils';
@Component({
template: `
<div id="main-menu-work-packages-wrapper">
<a id="main-menu-work-packages" wp-query-menu>Work packages</a>
<li>
<div id="main-menu-work-packages-wrapper">
<a id="main-menu-work-packages">Work packages</a>
</div>
<ul class="menu-children"></ul>'
</div>
</li>
`
})
class WpQueryMenuTestComponent { }
describe('wp-query-menu directive', () => {
describe('wp-query-menu', () => {
let app:WpQueryMenuTestComponent;
let fixture:ComponentFixture<WpQueryMenuTestComponent>
let fixture:ComponentFixture<WpQueryMenuTestComponent>;
let element:JQuery;
let menuContainer:JQuery;
let queryMenuService = new QueryMenuService();
let queryMenuService:QueryMenuService;
let transitionCallback:(id:any) => any;
const $transitionStub = {
@ -68,8 +69,7 @@ describe('wp-query-menu directive', () => {
// noinspection JSIgnoredPromiseFromCall
return TestBed.configureTestingModule({
declarations: [
WpQueryMenuTestComponent,
WpQueryMenuDirective
WpQueryMenuTestComponent
],
providers: [
{ provide: $stateToken, useValue: { go: (...args:any[]) => undefined } },
@ -79,6 +79,7 @@ describe('wp-query-menu directive', () => {
]
}).compileComponents()
.then(() => {
queryMenuService = TestBed.get(QueryMenuService);
fixture = TestBed.createComponent(WpQueryMenuTestComponent);
app = fixture.debugElement.componentInstance;
element = jQuery(fixture.elementRef.nativeElement);

@ -26,9 +26,13 @@
// See doc/COPYRIGHT.rdoc for more details.
//++
import {input} from 'reactivestates';
import {distinctUntilChanged, filter} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Inject, Injectable} from '@angular/core';
import {StateService, Transition, TransitionService} from '@uirouter/core';
import {$stateToken} from 'core-app/angular4-transition-utils';
import {LinkHandling} from 'core-components/common/link-handling/link-handling';
import {WorkPackagesListChecksumService} from 'core-components/wp-list/wp-list-checksum.service';
export const QUERY_MENU_ITEM_TYPE = 'query-menu-item';
export type QueryMenuEvent = {
event:'add' | 'remove' | 'rename';
@ -39,29 +43,151 @@ export type QueryMenuEvent = {
@Injectable()
export class QueryMenuService {
private events = input<QueryMenuEvent>();
private currentQueryId:string|null = null;
private uiRouteStateName = 'work-packages.list';
private container = jQuery('#main-menu-work-packages').parent().find('ul.menu-children');
constructor(@Inject($stateToken) protected $state:StateService,
protected $transitions:TransitionService,
protected wpListChecksumService:WorkPackagesListChecksumService) {
this.$transitions.onStart({}, (transition:Transition) => {
const queryId = transition.params('to').query_id;
this.onQueryIdChanged(queryId);
});
this.container.on('click', `.${QUERY_MENU_ITEM_TYPE}`, (event) => {
if (LinkHandling.isClickedWithModifier(event) || LinkHandling.isOutsideAngular()) {
return true;
}
this.switchOrReload(jQuery(event.target));
event.preventDefault();
return false;
});
}
/**
* Add a query menu item
*/
public add(name:string, path:string, queryId:string) {
this.events.putValue({event: 'add', queryId: queryId, path: path, label: name});
const item = this.buildItem(queryId, name);
const previous = this.previousMenuItem(name);
if (previous) {
jQuery(item).insertAfter(previous);
} else {
this.container.append(item);
}
this.setSelectedState();
}
public rename(queryId:string, name:string) {
this.events.putValue({event: 'rename', queryId: queryId, label: name});
this.findItem(queryId)
.find('.menu-item--title')
.text(name);
}
public remove(queryId:string) {
this.events.putValue({event: 'remove', queryId: queryId, label: queryId});
this.removeItem(queryId);
}
public onQueryIdChanged(queryId:string|null) {
this.currentQueryId = queryId;
this.setSelectedState();
}
private removeItem(queryId:string) {
const item = this.findItem(queryId);
this.setSelectedState();
}
private setSelectedState() {
// Set WP menu to selected if no current query id set
if (this.currentQueryId) {
jQuery('#main-menu-work-packages').removeClass('selected');
}
// Update all queries children
const queries = this.container.find('.query-menu-item');
queries.toggleClass('selected', false);
if (this.currentQueryId) {
queries.filter(`#wp-query-menu-item-${this.currentQueryId}`).addClass('selected');
}
}
private buildItem(queryId:string, name:string) {
const li = document.createElement('li');
const link = document.createElement('a');
link.id = `wp-query-menu-item-${queryId}`;
link.classList.add(QUERY_MENU_ITEM_TYPE);
link.dataset.queryId = queryId;
const span = document.createElement('span');
span.classList.add('menu-item--title', 'ellipsis');
span.textContent = name;
link.appendChild(span);
li.appendChild(link);
return li;
}
private findItem(queryId:string) {
return this.container.find(`#wp-query-menu-item-${queryId}`);
}
private switchOrReload(item:JQuery) {
const queryId = item.data('queryId').toString();
let opts = {reload: false};
if (queryId === this.currentQueryId) {
this.wpListChecksumService.clear();
opts.reload = true;
}
this.$state.go(
this.uiRouteStateName,
{query_props: null, query_id: queryId },
opts
);
}
public on(type:string) {
return this.events
.values$()
.pipe(
filter((e:QueryMenuEvent) => e.event === type),
distinctUntilChanged()
);
/**
* previousMenuItem
*
* Returns the menu item within the factories's container that has a title
* alphabetically before the provided title. The considered menu items have
* the type (css class) this factory is responsible for.
*
* Params
* * title: The string used for comparing.
*/
public previousMenuItem(title:string):ng.IAugmentedJQuery|null {
const allItems = this.container.find('li');
if (allItems.length === 0) {
return null;
}
let previousElement = angular.element(allItems[allItems.length - 1]);
let i = allItems.length - 2;
for (i; i >= 0; i--) {
if ((title > previousElement.find('a').attr('title')) ||
(previousElement.find('.' + QUERY_MENU_ITEM_TYPE).length === 0)) {
return previousElement;
}
else {
previousElement = angular.element(allItems[i]);
}
}
return previousElement;
}
}

Loading…
Cancel
Save