projects/core/src/lib/action-toggle/action-toggle.ts
ActionToggle
used to create action with toggle functionality,
e.g. checkbox, toggle slider
const toggle = new ActionToggle({ checked: true });
Or
const toggle = actionFactory.createToggle({ checked: true });
Or
const toggle = actionFactory.createToggle().check();
Properties |
Methods |
constructor(options: ActionToggleOptions, component?: Type<ActionToggleComponentImpl>)
|
||||||||||||
Public
Parameters :
|
Readonly changes$ |
Type : Observable<ActionToggleOptions>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:47
|
|
Readonly fire$ |
Type : Observable<ActionToggleEvent>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:42
|
|
Readonly ariaLabel$ |
Type : Observable<string>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:124
|
|
Readonly disabled$ |
Type : Observable<boolean>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:115
|
|
Readonly icon$ |
Type : Observable<string>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:106
|
|
Readonly state$ |
Type : Observable<ActionState>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:120
|
|
Readonly title$ |
Type : Observable<string>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:102
|
|
Readonly visible$ |
Type : Observable<boolean>
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:111
|
|
check |
check()
|
Will set checked status to Example:
|
isChecked |
isChecked()
|
Returns boolean defining whether action is checked Example:
Returns :
boolean
|
isUnchecked |
isUnchecked()
|
Returns boolean defining whether action is unchecked Example:
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:
|
uncheck |
uncheck()
|
Will set checked status to Example:
|
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 |
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
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
Returns :
ActionGroup | undefined
|
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
Returns :
boolean
|
isDestroyed |
isDestroyed()
|
Inherited from
ActionAbstract
|
Defined in
ActionAbstract:321
|
Returns boolean defining whether action has state
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
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 :
|
setIcon | ||||||||
setIcon(icon: string)
|
||||||||
Inherited from
ActionAbstract
|
||||||||
Defined in
ActionAbstract:364
|
||||||||
Will set the new icon and notify all icon subscriptions
Parameters :
|
setTitle | ||||||||
setTitle(title: string)
|
||||||||
Inherited from
ActionAbstract
|
||||||||
Defined in
ActionAbstract:330
|
||||||||
Will set the new title and notify all title subscriptions
Parameters :
|
setVisibility | ||||||||
setVisibility(visibility: boolean)
|
||||||||
Inherited from
ActionAbstract
|
||||||||
Defined in
ActionAbstract:397
|
||||||||
Will show or hide the action depending from the provided visibility boolean
Parameters :
|
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();
}
}