Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Expose fxFlex directive class to access via ContentChildren #266

@amcdnl

Description

@amcdnl

Requirement

I need to access fxFlex via ContentChildren from a adjacent directive to dynamically modify widths.

Use Case

I want to create a responsive split component that leverages flex-layout for the layout.

use

Code

Usage

<div fxLayout="row" ngxSplit="row">
  <div fxFlex="30%" ngxSplitArea>
    Left
  </div>
  <div fxFlex="5px">
    <a href="#" ngxSplitHandle>...</a>
  </div>
  <div fxFlex="70%" ngxSplitArea>
    Right
  </div>
</div>

split.directive.ts

import { 
  Directive, Input, ChangeDetectionStrategy, ContentChild, 
  ContentChildren, AfterContentInit, QueryList
} from '@angular/core';
import { SplitAreaDirective } from './split-area.directive';
import { SplitHandleDirective } from './split-handle.directive';

@Directive({
  selector: '[ngxSplit]',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    class: 'ngx-split'
  }
})
export class SplitDirective implements AfterContentInit {

  /*tslint:disable*/
  @Input('ngxSplit') 
  direction: string = 'vertial';
  /*tslint:enable*/

  @ContentChild(SplitHandleDirective) handle: SplitHandleDirective;
  @ContentChildren(SplitAreaDirective) areas: QueryList<SplitAreaDirective>;

  ngAfterContentInit(): void {
    console.log('ha', this.handle, this.areas); //<-- Need it here
    this.handle.drag.subscribe(pos => this.onDrag(pos));
  }

  onDrag({ x, y }): void {
    this.areas.forEach(area => {
      console.log('area', area); // TODO: Resize here
    });
  }

}

split-area.directive.ts

import { Directive, Input, ChangeDetectionStrategy, ContentChildren, QueryList } from '@angular/core';

@Directive({
  selector: '[ngxSplitArea]',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    class: 'ngx-split-area'
  }
})
export class SplitAreaDirective {

  @ContentChildren('[fxFlex]') layouts: QueryList<any>;

}

split-handle.directive.ts

import { Directive, ElementRef, Output, ChangeDetectionStrategy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/operator/switchMap';

@Directive({
  selector: '[ngxSplitHandle]',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    class: 'ngx-split-handle'
  }
})
export class SplitHandleDirective {

  @Output() drag: Observable<{ x: number, y: number }>;

  constructor(ref: ElementRef) {
    const getMouseEventPosition = (event: MouseEvent) => ({ x: event.clientX, y: event.clientY });

    const mousedown$ = Observable.fromEvent(ref.nativeElement, 'mousedown').map(getMouseEventPosition);
    const mousemove$ = Observable.fromEvent(document, 'mousemove').map(getMouseEventPosition);
    const mouseup$ = Observable.fromEvent(document, 'mouseup');
    
    this.drag = mousedown$.switchMap(mousedown =>
        mousemove$.map(mousemove => ({
          x: mousemove.x - mousedown.x,
          y: mousemove.y - mousedown.y
        }))
        .takeUntil(mouseup$)
      );
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhas prA PR has been created to address this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions