Skip to content

Commit

Permalink
Merge branch 'master' into feature/menu-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mishkolesnikov authored Mar 28, 2018
2 parents 3cf33cc + bbd86aa commit 8cf7393
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 81 deletions.
10 changes: 6 additions & 4 deletions docs/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'style-loader!./styles/styles.scss';
import { Subscription } from 'rxjs/Subscription';
import { DocsService } from './docs/docs.service';
import { Analytics } from './docs/utils/analytics.service';
import { NbWindow } from '@nebular/theme';

@Component({
selector: 'ngd-app-root',
Expand All @@ -23,7 +24,8 @@ export class NgdAppComponent implements AfterViewInit, OnDestroy, OnInit {
private fragmentSubscription: Subscription;


constructor(private docsService: DocsService,
constructor(private window: NbWindow,
private docsService: DocsService,
private router: Router,
private route: ActivatedRoute,
private elementRef: ElementRef,
Expand All @@ -48,13 +50,13 @@ export class NgdAppComponent implements AfterViewInit, OnDestroy, OnInit {
if (el) {
el.scrollIntoView();
if (new RegExp(/themes/i).test(this.router.url)) {
window.scrollBy(0, -235); // header + theme-header
this.window.scrollBy(0, -235); // header + theme-header
} else {
window.scrollBy(0, -80);
this.window.scrollBy(0, -80);
}
}
} else {
window.scrollTo(0, 0);
this.window.scrollTo(0, 0);
}
}

Expand Down
7 changes: 4 additions & 3 deletions docs/app/docs/page/page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Component, OnDestroy } from '@angular/core';

import { NbMenuService } from '@nebular/theme';
import { NbMenuService, NbWindow } from '@nebular/theme';
import { Subscription } from 'rxjs/Subscription';
import { Title } from '@angular/platform-browser';

Expand All @@ -30,7 +30,8 @@ export class NgdPageComponent implements OnDestroy {

private menuSubscription: Subscription;

constructor(private menuService: NbMenuService,
constructor(private window: NbWindow,
private menuService: NbMenuService,
private titleService: Title) {

this.menuSubscription = this.menuService.onItemSelect()
Expand All @@ -48,7 +49,7 @@ export class NgdPageComponent implements OnDestroy {
this.currentItem.children[0].block !== 'theme';

this.titleService.setTitle(`Nebular Documentation - ${event.item.data.name}`);
window.scrollTo(0, 0);
this.window.scrollTo(0, 0);
}

ngOnDestroy() {
Expand Down
16 changes: 9 additions & 7 deletions docs/app/docs/utils/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {Injectable} from '@angular/core';
import {Router, NavigationEnd} from '@angular/router';
import {Location} from '@angular/common';

import {filter} from 'rxjs/operator/filter';
import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Location } from '@angular/common';

import { filter } from 'rxjs/operator/filter';
import { NbWindow } from '@nebular/theme';
declare const ga: any;

@Injectable()
export class Analytics {
private _enabled: boolean;

constructor(private _location: Location, private _router: Router) {
this._enabled = window.location.href.indexOf('akveo.github.io') >= 0;
constructor(private window: NbWindow,
private _location: Location,
private _router: Router) {
this._enabled = this.window.location.href.indexOf('akveo.github.io') >= 0;
}

trackPageViews() {
Expand Down
7 changes: 4 additions & 3 deletions docs/app/homepage/homepage.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Title } from '@angular/platform-browser';
import * as ImageComparison from 'image-comparison';
import 'style-loader!image-comparison/src/ImageComparison.css';
import { Subject } from 'rxjs/Rx';
import { NbSpinnerService } from '@nebular/theme';
import { NbSpinnerService, NbWindow } from '@nebular/theme';

@Component({
selector: 'ngd-homepage',
Expand Down Expand Up @@ -95,7 +95,8 @@ export class NgdHomepageComponent implements AfterViewInit, OnInit {
currentSectionId: string = 'home';
highlightMenu$ = new Subject();

constructor(private renderer: Renderer2,
constructor(private window: NbWindow,
private renderer: Renderer2,
private spinnerService: NbSpinnerService,
private titleService: Title) {
this.renderer.setProperty(document.body, 'scrollTop', 0);
Expand Down Expand Up @@ -170,7 +171,7 @@ export class NgdHomepageComponent implements AfterViewInit, OnInit {
}

private calculateBgTopOffcet() {
let bgScrollTop = window.pageYOffset / this.getScrollSpeedRatio();
let bgScrollTop = this.window.pageYOffset / this.getScrollSpeedRatio();
if (bgScrollTop > NgdHomepageComponent.BG_IMAGE_HEIGHT) {
bgScrollTop = NgdHomepageComponent.BG_IMAGE_HEIGHT;
}
Expand Down
18 changes: 16 additions & 2 deletions e2e/menu.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,24 @@ describe('nb-menu', () => {
});

it('hidden menu item should not be present', () => {
expect(element(hiddenMenuItem).isPresent()).toBe(false);
expect(element(hiddenMenuItem).isPresent()).toBeFalsy();
});

it('hidden submenu item should not be present', () => {
expect(element(hiddenSubmenuItem).isPresent()).toBeFalsy()
expect(element(hiddenSubmenuItem).isPresent()).toBeFalsy();
})

it('should add query string to url', () => {
element.all(menu1).first().click()
.then(() => {
expect(browser.getCurrentUrl()).toContain('param=1');
})
});

it('should add query string to url (navigate home)', () => {
element(homeButton).click()
.then(() => {
expect(browser.getCurrentUrl()).toContain('param=2');
})
});
});
2 changes: 2 additions & 0 deletions src/app/menu-test/menu-test.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class NbMenuTestComponent implements OnInit, OnDestroy {
title: 'Menu #1',
link: '/menu/1',
icon: 'nb-keypad',
queryParams: { param: 1 },
},
{
title: 'Menu #2',
Expand Down Expand Up @@ -193,6 +194,7 @@ export class NbMenuTestComponent implements OnInit, OnDestroy {
{
title: 'Menu #3.3.2',
link: '/menu/3/3/2',
queryParams: { param: 2 },
home: true,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { NbMenuItem } from '../menu/menu.service';
import { NbThemeService } from '../../services/theme.service';
import { NbPopoverAdjustment, NbPopoverPlacement } from '../popover/helpers/model';
import { NbContextMenuComponent } from './context-menu.component';
import { NbPositioningHelper } from '../popover/helpers/positioning.helper';
import { NbAdjustmentHelper } from '../popover/helpers/adjustment.helper';
import { NbTriggerHelper } from '../popover/helpers/trigger.helper';

/**
* Full featured context menu directive.
Expand Down Expand Up @@ -91,11 +94,21 @@ export class NbContextMenuDirective implements OnInit, OnDestroy {
constructor(hostRef: ElementRef,
themeService: NbThemeService,
componentFactoryResolver: ComponentFactoryResolver,
positioningHelper: NbPositioningHelper,
adjustmentHelper: NbAdjustmentHelper,
triggerHelper: NbTriggerHelper,
@Inject(PLATFORM_ID) platformId) {
/**
* Initialize popover with all the important inputs.
* */
this.popover = new NbPopoverDirective(hostRef, themeService, componentFactoryResolver, platformId);
this.popover = new NbPopoverDirective(hostRef,
themeService,
componentFactoryResolver,
positioningHelper,
adjustmentHelper,
triggerHelper,
platformId,
);
this.popover.content = NbContextMenuComponent;
this.popover.placement = NbPopoverPlacement.BOTTOM;
}
Expand Down
5 changes: 5 additions & 0 deletions src/framework/theme/components/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export function convertToBoolProperty(val: any): boolean {
}

export function getElementHeight (el) {
/**
*
* TODO: Move helpers in separate common module.
* TODO: Provide window through di token.
* */
const style = window.getComputedStyle(el);
const marginTop = parseInt(style.getPropertyValue('margin-top'), 10);
const marginBottom = parseInt(style.getPropertyValue('margin-bottom'), 10);
Expand Down
10 changes: 6 additions & 4 deletions src/framework/theme/components/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AfterViewInit, Component, ComponentFactoryResolver, ElementRef, HostBinding, HostListener, Input, OnDestroy,
Renderer2, ViewChild, ViewContainerRef, OnInit, ComponentFactory, Inject, PLATFORM_ID,
} from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import { isPlatformBrowser } from '@angular/common';
import { Router, NavigationEnd } from '@angular/router';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
Expand All @@ -18,6 +18,7 @@ import { takeWhile } from 'rxjs/operators/takeWhile';
import { convertToBoolProperty } from '../helpers';
import { NbThemeService } from '../../services/theme.service';
import { NbSpinnerService } from '../../services/spinner.service';
import { NbWindow, NbDocument } from '../../theme.options';

/**
* A container component which determines a content position inside of the layout.
Expand Down Expand Up @@ -292,8 +293,9 @@ export class NbLayoutComponent implements AfterViewInit, OnInit, OnDestroy {
protected elementRef: ElementRef,
protected renderer: Renderer2,
protected router: Router,
protected window: NbWindow,
protected document: NbDocument,
@Inject(PLATFORM_ID) protected platformId: Object,
@Inject(DOCUMENT) protected document: any,
) {

this.themeService.onThemeChange()
Expand Down Expand Up @@ -333,9 +335,9 @@ export class NbLayoutComponent implements AfterViewInit, OnInit, OnDestroy {
}));
this.spinnerService.load();

// trigger first time so that after the change we have the initial value
if (isPlatformBrowser(this.platformId)) {
this.themeService.changeWindowWidth(window.innerWidth);
// trigger first time so that after the change we have the initial value
this.themeService.changeWindowWidth(this.window.innerWidth);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</span>
<a *ngIf="menuItem.link && !menuItem.url && !menuItem.children && !menuItem.group"
[routerLink]="menuItem.link"
[queryParams]="menuItem.queryParams"
[fragment]="menuItem.fragment"
[attr.target]="menuItem.target"
[attr.title]="menuItem.title"
Expand Down
10 changes: 7 additions & 3 deletions src/framework/theme/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { filter } from 'rxjs/operators/filter';

import { NbMenuInternalService, NbMenuItem, NbMenuService, NbMenuBag } from './menu.service';
import { convertToBoolProperty, getElementHeight } from '../helpers';
import { NbWindow } from '../../theme.options';

function sumSubmenuHeight(item: NbMenuItem) {
return item.expanded
Expand Down Expand Up @@ -206,7 +207,10 @@ export class NbMenuComponent implements OnInit, AfterViewInit, OnDestroy {
private alive: boolean = true;
private autoCollapseValue: boolean = false;

constructor(private menuInternalService: NbMenuInternalService, private router: Router) { }
constructor(private window: NbWindow,
private menuInternalService: NbMenuInternalService,
private router: Router) {
}

ngOnInit() {
this.menuInternalService
Expand Down Expand Up @@ -292,11 +296,11 @@ export class NbMenuComponent implements OnInit, AfterViewInit, OnDestroy {

if (homeItem) {
if (homeItem.link) {
this.router.navigate([homeItem.link]);
this.router.navigate([homeItem.link], { queryParams: homeItem.queryParams });
}

if (homeItem.url) {
window.location.href = homeItem.url;
this.window.location.href = homeItem.url;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/framework/theme/components/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { ReplaySubject } from 'rxjs/ReplaySubject';
import { share } from 'rxjs/operators/share';
import { Params } from '@angular/router';

export interface NbMenuBag { tag: string; item: NbMenuItem }

Expand Down Expand Up @@ -87,6 +88,10 @@ export abstract class NbMenuItem {
* @type {boolean}
*/
group?: boolean;
/** Map of query parameters
*@type {Params}
*/
queryParams?: Params;
parent?: NbMenuItem;
selected?: boolean;
data?: any;
Expand Down
38 changes: 22 additions & 16 deletions src/framework/theme/components/popover/helpers/adjustment.helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Injectable } from '@angular/core';

import { NbWindow } from '../../../theme.options';
import { NbPositioningHelper } from './positioning.helper';
import { NbPopoverAdjustment, NbPopoverPlacement, NbPopoverPosition } from './model';

Expand All @@ -20,8 +23,12 @@ const NB_ORDERED_PLACEMENTS = {
],
};

@Injectable()
export class NbAdjustmentHelper {

constructor(private positioningHelper: NbPositioningHelper, private window: NbWindow) {
}

/**
* Calculated {@link NbPopoverPosition} based on placed element, host element,
* placed element placement and adjustment strategy.
Expand All @@ -33,18 +40,18 @@ export class NbAdjustmentHelper {
*
* @return {NbPopoverPosition} calculated position.
* */
static adjust(placed: ClientRect,
host: ClientRect,
placement: NbPopoverPlacement,
adjustment: NbPopoverAdjustment): NbPopoverPosition {
adjust(placed: ClientRect,
host: ClientRect,
placement: NbPopoverPlacement,
adjustment: NbPopoverAdjustment): NbPopoverPosition {
const placements = NB_ORDERED_PLACEMENTS[adjustment].slice();
const ordered = NbAdjustmentHelper.orderPlacements(placement, placements);
const ordered = this.orderPlacements(placement, placements);
const possible = ordered.map(pl => ({
position: NbPositioningHelper.calcPosition(placed, host, pl),
position: this.positioningHelper.calcPosition(placed, host, pl),
placement: pl,
}));

return NbAdjustmentHelper.chooseBest(placed, possible);
return this.chooseBest(placed, possible);
}

/**
Expand All @@ -55,8 +62,8 @@ export class NbAdjustmentHelper {
*
* @return {NbPopoverPosition} calculated position.
* */
private static chooseBest(placed: ClientRect, possible: NbPopoverPosition[]): NbPopoverPosition {
return possible.find(adjust => NbAdjustmentHelper.inViewPort(placed, adjust)) || possible.shift();
private chooseBest(placed: ClientRect, possible: NbPopoverPosition[]): NbPopoverPosition {
return possible.find(adjust => this.inViewPort(placed, adjust)) || possible.shift();
}

/**
Expand All @@ -67,11 +74,11 @@ export class NbAdjustmentHelper {
*
* @return {boolean} true if placed element completely viewport.
* */
private static inViewPort(placed: ClientRect, position: NbPopoverPosition): boolean {
return position.position.top - window.pageYOffset > 0
&& position.position.left - window.pageXOffset > 0
&& position.position.top + placed.height < window.innerHeight + window.pageYOffset
&& position.position.left + placed.width < window.innerWidth + window.pageXOffset;
private inViewPort(placed: ClientRect, position: NbPopoverPosition): boolean {
return position.position.top - this.window.pageYOffset > 0
&& position.position.left - this.window.pageXOffset > 0
&& position.position.top + placed.height < this.window.innerHeight + this.window.pageYOffset
&& position.position.left + placed.width < this.window.innerWidth + this.window.pageXOffset;
}

/**
Expand All @@ -95,8 +102,7 @@ export class NbAdjustmentHelper {
* ]);
* ```
* */
private static orderPlacements(placement: NbPopoverPlacement,
placements: NbPopoverPlacement[]): NbPopoverPlacement[] {
private orderPlacements(placement: NbPopoverPlacement, placements: NbPopoverPlacement[]): NbPopoverPlacement[] {
const index = placements.indexOf(placement);
const start = placements.splice(index, placements.length);
return start.concat(...placements);
Expand Down
Loading

0 comments on commit 8cf7393

Please sign in to comment.