@alihbuzaid/ember-ui
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
133 lines (114 loc) • 3.2 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { computed, action } from '@ember/object';
import { not, equal } from '@ember/object/computed';
export default class ButtonComponent extends Component {
/**
* Inject abilities service.
*
* @memberof ButtonComponent
*/
abilities;
/**
* Determines if the button should be disabled
*
* @var {Boolean}
*/
get isDisabled() {
const { isLoading, disabled } = this.args;
return this.disabledByPermission || disabled || isLoading;
}
/**
* Determines if the button should be disabled
*
* @var {Boolean}
*/
isSecondary;
/**
* Determines if the button should be disabled
*
* @var {Boolean}
*/
isNotSecondary;
/**
* Determines if icon be displayed
*
* @var {Boolean}
*/
get showIcon() {
const { icon, isLoading } = this.args;
return icon && !isLoading;
}
/**
* The permission required.
*
* @memberof ButtonComponent
*/
permissionRequired;
/**
* If the button is disabled by permissions.
*
* @memberof ButtonComponent
*/
disabledByPermission = false;
/**
* Determines the visibility of the button
*
* @memberof ButtonComponent
*/
visible = true;
/**
* Determines if the button is disabled
*
* @memberof ButtonComponent
*/
disabled = false;
/**
* Creates an instance of ButtonComponent.
* @param {*} owner
* @param {*} { permission = null }
* @memberof ButtonComponent
*/
constructor(owner, { permission = null, disabled = false, visible = true }) {
super(...arguments);
this.permissionRequired = permission;
this.visible = visible;
if (!disabled && permission) {
this.disabledByPermission = permission && this.abilities.cannot(permission);
}
}
/**
* Setup this component
*
* @void
*/
setupComponent() {
const { onInsert } = this.args;
if (typeof onInsert === 'function') {
onInsert();
}
}
/**
* Dispatches the `onClick` event with all arguments.
* If button `this.isDisable` then event is not executed.
*
* @void
*/
onClick() {
const { onClick } = this.args;
if (this.isDisabled) {
return;
}
if (typeof onClick === 'function') {
onClick(...arguments);
}
}
onArgsChanged(el, [disabled = false, visible = true, permission = null]) {
this.permissionRequired = permission;
this.visible = visible;
if (!disabled && permission) {
this.disabledByPermission = permission && this.abilities.cannot(permission);
}
}
}