File

projects/core/src/lib/action-toggle/action-toggle.ts

Description

ActionToggle used to create action with toggle functionality, e.g. checkbox, toggle slider

Example

const toggle = new ActionToggle({ checked: true });

Or

const toggle = actionFactory.createToggle({ checked: true });

Or

const toggle = actionFactory.createToggle().check();

Extends

ActionAbstract

Index

Properties
Methods

Constructor

constructor(options: ActionToggleOptions, component?: Type<ActionToggleComponentImpl>)

Public constructor used to instantiate ActionToggle

Parameters :
Name Type Optional Description
options ActionToggleOptions No

Options for ActionToggle

component Type<ActionToggleComponentImpl> Yes

Optional custom Component

Properties

Readonly changes$
Type : Observable<ActionToggleOptions>
Inherited from ActionAbstract
Defined in ActionAbstract:47

Observable notifies subscriptions on following changes: title, icon, visibility, disabled, checked

Readonly fire$
Type : Observable<ActionToggleEvent>
Inherited from ActionAbstract
Defined in ActionAbstract:42

Observable notifying subscriptions on the change of checked state

Readonly ariaLabel$
Type : Observable<string>
Inherited from ActionAbstract
Defined in ActionAbstract:124

Observable that notifies subscribers when the ariaLabel changes.

Readonly disabled$
Type : Observable<boolean>
Inherited from ActionAbstract
Defined in ActionAbstract:115

Observable that notifies subscriptions when disabled state changes

Readonly icon$
Type : Observable<string>
Inherited from ActionAbstract
Defined in ActionAbstract:106

Observable that notifies subscriptions when icon changes

Readonly state$
Type : Observable<ActionState>
Inherited from ActionAbstract
Defined in ActionAbstract:120

Observable that notifies subscriptions when action state changes e.g. Active, Inactive, Destroyed

Readonly title$
Type : Observable<string>
Inherited from ActionAbstract
Defined in ActionAbstract:102

Observable that notifies subscriptions when title changes

Readonly visible$
Type : Observable<boolean>
Inherited from ActionAbstract
Defined in ActionAbstract:111

Observable that notifies subscriptions when visibility state changes (visible or hidden)

Methods

check
check()

Will set checked status to true It will not produce second notification if already checked

Example:

toggle.check();
isChecked
isChecked()

Returns boolean defining whether action is checked

Example:

const isChecked = toggle.isChecked();
Returns : boolean
isUnchecked
isUnchecked()

Returns boolean defining whether action is unchecked

Example:

const isUnchecked = toggle.isUnchecked();
Returns : boolean
trigger
trigger()
Inherited from ActionAbstract
Defined in ActionAbstract:95

Will toggle current checked state when invoked Should be called in view component on click

Example:

toggle.trigger();
uncheck
uncheck()

Will set checked status to false It will not produce second notification if already unchecked

Example:

toggle.uncheck();
activate
activate()
Inherited from ActionAbstract
Defined in ActionAbstract:272

Will activate all observables in current action, unless action is already destroyed

deactivate
deactivate()
Inherited from ActionAbstract
Defined in ActionAbstract:285

Will deactivate all observables in current action, unless action is already destroyed

destroy
destroy()
Inherited from ActionAbstract
Defined in ActionAbstract:298

Will set action state to Destroyed, which will complete all observables

disable
disable()
Inherited from ActionAbstract
Defined in ActionAbstract:427

Will disable action, if prevously enabled

enable
enable()
Inherited from ActionAbstract
Defined in ActionAbstract:419

Will enable action, if prevously disabled

getAriaLabel
getAriaLabel()
Inherited from ActionAbstract
Defined in ActionAbstract:355

Returns current action ariaLabel

Returns : string
getForcedComponent
getForcedComponent()
Inherited from ActionAbstract
Defined in ActionAbstract:451

Returns a Component, that is provided as forced component via action constructor This component should be used by ActionOutletDirective, to represent the action in DOM, instead the component, provided via Angular Injector

Returns : Type | undefined
getIcon
getIcon()
Inherited from ActionAbstract
Defined in ActionAbstract:372

Returns current action icon

Returns : string
getParent
getParent()
Inherited from ActionAbstract
Defined in ActionAbstract:458

Returns current parent of the action

getTitle
getTitle()
Inherited from ActionAbstract
Defined in ActionAbstract:338

Returns current action title

Returns : string
hide
hide()
Inherited from ActionAbstract
Defined in ActionAbstract:387

Will nide the action, if previously visible

isActive
isActive()
Inherited from ActionAbstract
Defined in ActionAbstract:307

Returns boolean defining whether action has state ActionState.Active

Returns : boolean
isDestroyed
isDestroyed()
Inherited from ActionAbstract
Defined in ActionAbstract:321

Returns boolean defining whether action has state ActionState.Destroyed

Returns : boolean
isDisabled
isDisabled()
Inherited from ActionAbstract
Defined in ActionAbstract:435

Returns boolean defining whether action is disabled

Returns : boolean
isEnabled
isEnabled()
Inherited from ActionAbstract
Defined in ActionAbstract:442

Returns boolean defining whether action is enabled

Returns : boolean
isHidden
isHidden()
Inherited from ActionAbstract
Defined in ActionAbstract:412

Returns boolean defining whether action is hidden

Returns : boolean
isInactive
isInactive()
Inherited from ActionAbstract
Defined in ActionAbstract:314

Returns boolean defining whether action has state ActionState.Inactive

Returns : boolean
isVisible
isVisible()
Inherited from ActionAbstract
Defined in ActionAbstract:405

Returns boolean defining whether action is visible

Returns : boolean
setAriaLabel
setAriaLabel(ariaLabel: string)
Inherited from ActionAbstract
Defined in ActionAbstract:347

Will set the new ariaLabel and notify all ariaLabel subscribers

Parameters :
Name Type Optional Description
ariaLabel string No

The new action title

setIcon
setIcon(icon: string)
Inherited from ActionAbstract
Defined in ActionAbstract:364

Will set the new icon and notify all icon subscriptions

Parameters :
Name Type Optional Description
icon string No

The new action icon

setTitle
setTitle(title: string)
Inherited from ActionAbstract
Defined in ActionAbstract:330

Will set the new title and notify all title subscriptions

Parameters :
Name Type Optional Description
title string No

The new action title

setVisibility
setVisibility(visibility: boolean)
Inherited from ActionAbstract
Defined in ActionAbstract:397

Will show or hide the action depending from the provided visibility boolean

Parameters :
Name Type Optional Description
visibility boolean No

The new visibility

show
show()
Inherited from ActionAbstract
Defined in ActionAbstract:379

Will show the action, if previously hidden

import { Type } from '@angular/core';
import { BehaviorSubject, Observable, merge } from 'rxjs';
import { map } from 'rxjs/operators';

import { ActionAbstract } from '../action-abstract/action-abstract';
import { ActionToggleComponentImpl, ActionToggleEvent, ActionToggleOptions } from './action-toggle.model';

/**
 * Default options for `ActionToggle`
 * Extended by provided options in action `constructor`
 */
const defaultToggleOptions: ActionToggleOptions = {
  checked: false,
};

/**
 * `ActionToggle` used to create action with toggle functionality,
 * *e.g. checkbox, toggle slider*
 *
 * ## Example
 *
```typescript
const toggle = new ActionToggle({ checked: true });
```
 *
 * **Or**
 *
```typescript
const toggle = actionFactory.createToggle({ checked: true });
```
 *
 * **Or**
 *
```typescript
const toggle = actionFactory.createToggle().check();
```
 */
export class ActionToggle extends ActionAbstract<ActionToggleOptions, ActionToggleEvent> {
  /**
   * `Observable` notifying subscriptions on the change of `checked` state
   */
  readonly fire$: Observable<ActionToggleEvent>;
  /**
   * `Observable` notifies subscriptions on following changes:
   * *title, icon, visibility, disabled, checked*
   */
  readonly changes$: Observable<ActionToggleOptions>;

  /**
   * `BehaviorSubject`, holding the current checked state
   */
  protected fire: BehaviorSubject<boolean>;

  /**
   * Public `constructor` used to instantiate `ActionToggle`
   *
   * @param options Options for `ActionToggle`
   * @param component Optional custom `Component`
   */
  constructor(options: ActionToggleOptions = defaultToggleOptions, component?: Type<ActionToggleComponentImpl>) {
    super({ ...defaultToggleOptions, ...options }, component);

    this.fire = new BehaviorSubject(!!this.options.checked);

    this.fire$ = this.handleLivecycleDistinct(this.fire.asObservable(), false).pipe(
      map(checked => ({ action: this, checked })),
    );

    this.changes$ = this.handleLivecycle(
      merge(
        this.title$.pipe(map(title => <ActionToggleOptions>{ title })),
        this.icon$.pipe(map(icon => <ActionToggleOptions>{ icon })),
        this.visible$.pipe(map(visible => <ActionToggleOptions>{ visible })),
        this.disabled$.pipe(map(disabled => <ActionToggleOptions>{ disabled })),
        this.fire.pipe(map(checked => <ActionToggleOptions>{ checked })),
      ),
    );

    if (this.options.callback) {
      this.fire$.subscribe(this.options.callback);
    }
  }

  /**
     * Will toggle current checked state when invoked
     * Should be called in view component on click
     *
     * #### Example:
```typescript
toggle.trigger();
```
     *
     * @method trigger
     */
  trigger(): this {
    this.fire.next(!this.fire.getValue());
    return this;
  }

  /**
     * Will set checked status to `true`
     * It will **not** produce second notification if already checked
     *
     * #### Example:
```typescript
toggle.check();
```
     *
     * @method check
     */
  check(): this {
    this.fire.next(true);
    return this;
  }

  /**
     * Will set checked status to `false`
     * It will **not** produce second notification if already unchecked
     *
     * #### Example:
```typescript
toggle.uncheck();
```
     *
     * @method uncheck
     */
  uncheck(): this {
    this.fire.next(false);
    return this;
  }

  /**
     * Returns boolean defining whether action is checked
     *
     * #### Example:
```typescript
const isChecked = toggle.isChecked();
```
     *
     * @method isChecked
     */
  isChecked(): boolean {
    return this.fire.getValue();
  }

  /**
     * Returns boolean defining whether action is unchecked
     *
     * #### Example:
```typescript
const isUnchecked = toggle.isUnchecked();
```
     */
  isUnchecked(): boolean {
    return !this.fire.getValue();
  }
}

results matching ""

    No results matching ""