All files / addon/components nucleus-button.js

94.23% Statements 49/52
84.38% Branches 27/32
100% Functions 14/14
94.23% Lines 49/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453                                                                                                    23x                     28x                     4x                   4x 4x 4x                       23x                     4x 4x                   33x                     33x                     33x                     31x                   29x                   29x                   33x                     31x                   51x 51x                     5x                   33x                 33x                                                                                                                         51x                       32x                     32x                     32x                     33x                   33x 33x                     33x 33x                     33x 33x                                         51x 51x                           51x                     9x 9x       9x       9x 9x   9x 6x 6x 4x   2x     6x 6x         9x          
import {
  classNames,
  attributeBindings,
  classNameBindings,
  tagName,
  layout as templateLayout,
} from '@ember-decorators/component';
import defaultProp from '@freshworks/core/utils/default-decorator';
 
import { or, equal } from '@ember/object/computed';
import { run } from '@ember/runloop';
import Component from '@ember/component';
import { computed } from '@ember/object';
import layout from "../templates/components/nucleus-button";
import { BUTTON_STATE, ICON_VARIANT_MAP } from "../constants/nucleus-button";
import safeSet from "../utils/safe-set";
 
/**
  __Usage:__
 
  [Refer component page](/docs/components/nucleus-button)
 
  @class Nucleus Button
  @namespace Components
  @extends Ember.Component
  @public
*/
@templateLayout(layout)
@tagName('button')
@classNames('nucleus-button')
@classNameBindings(
  'active',
  'block:nucleus-button--block',
  '_sizeClass',
  '_typeClass',
  'customClass',
  '_iconClass',
  'active:is-active'
)
@attributeBindings('_disabled:disabled', '_buttonType:type', '_label:aria-label', 'autofocus')
class NucleusButton extends Component {
  /**
  * Default button text
  *
  * @field label
  * @type string|null
  * @default null
  * @public
  */
  @defaultProp
  label = null;
 
  /**
  * Button sizes: `default` & `mini`
  *
  * @field size
  * @type string
  * @default null
  * @public
  */
  @defaultProp
  size = null;
 
  /**
  * Icon sizes: `mini`, `small`, `medium`, `large`
  *
  * @field iconSize
  * @type string
  * @default null
  * @public
  */
  @defaultProp
  iconSize = null;
 
  /**
  * _iconSize
  *
  * @computed _iconSize
  * @private
  */
  @computed('iconSize', 'size')
  get _iconSize() {
    let iconSize = this.get('iconSize');
    let defaultSize = this.get('size') ? this.get('size') : 'small';
    return iconSize ? iconSize : defaultSize;
  }
 
  /**
  * Button display variants: `primary`, `secondary`, `danger`, `text` & `link`
  *
  * @field variant
  * @type string
  * @default 'primary'
  * @public
  */
  @defaultProp
  variant = 'primary';
 
  /**
  * Attribute bound to icon variant
  *
  * @field _iconVariant
  * @type string
  * @private
  */
  @computed('variant')
  get _iconVariant() {
    let variant = this.get('variant');
    return ICON_VARIANT_MAP[variant];
  }
 
  /**
  * Attribute bound to button type
  *
  * @field _buttonType
  * @type string
  * @private
  */
  _buttonType = 'button';
 
  /**
  * Class name `active` added if set to true
  *
  * @field active
  * @type boolean
  * @default false
  * @public
  */
  @defaultProp
  active = false;
 
  /**
  * Flag to set autofocus
  *
  * @field autofocus
  * @type boolean
  * @default false
  * @public
  */
  @defaultProp
  autofocus = false;
 
  /**
  * Flag to set button as full width block.
  *
  * @field block
  * @type boolean
  * @default false
  * @public
  */
  @defaultProp
  block = false;
 
  /**
  * Icon name used in svg-jar.
  *
  * @field icon
  * @type string|null
  * @public
  */
  @defaultProp
  icon = null;
 
  /**
  * Specifies it is an icon-only button
  *
  * @field iconOnly
  * @type boolean
  * @public
  */
 @defaultProp
 iconOnly = false;
 
  /**
  * Custom class names to be added to the button.
  *
  * @field customClass
  * @type string
  * @public
  */
  @defaultProp
  customClass = null;
 
  /**
  * Flag to set disabled attribute
  *
  * @field disabled
  * @type boolean
  * @default false
  * @public
  */
  @defaultProp
  disabled = false;
 
  /**
  * _disabled
  *
  * @computed _disabled
  * @private
  */
  @computed('disabled', '_isPending')
  get _disabled() {
    let isDisabled = this.get('disabled');
    return isDisabled ? isDisabled : this.get('_isPending');
  }
 
  /**
  * Function Arguments for `onClick` action
  *
  * @field args
  * @type string|number|object
  * @public
  */
  @defaultProp
  args = null;
 
  /**
  * Timeout after which the default label replaces fulfilled/rejected label.
  *
  * @field labelTimeout
  * @type number
  * @default 100
  * @public
  */
  labelTimeout = 1000;
 
  /**
  * Internal button _buttonState management utility
  *
  * @field _buttonState
  * @type string
  * @private
  */
  _buttonState = BUTTON_STATE.DEFAULT;
 
  /**
  * _isPending
  *
  * @field _isPending
  * @type boolean
  * @private
  */
  @equal('_buttonState', BUTTON_STATE.PENDING)
  _isPending;
 
  /**
  * _isFulfilled
  *
  * @field _isFulfilled
  * @type boolean
  * @private
  */
  @equal('_buttonState', BUTTON_STATE.FULFILLED)
  _isFulfilled;
 
  /**
  * _isRejected
  *
  * @field _isRejected
  * @type boolean
  * @private
  */
  @equal('_buttonState', BUTTON_STATE.REJECTED)
  _isRejected;
 
  /**
  * _isLoading
  *
  * @field _isLoading
  * @type boolean
  * @private
  */
  @or('_isPending', '_isFulfilled', '_isRejected')
  _isLoading;
 
  /**
  * To display animated checkmark
  *
  * @field _isLoadingComplete
  * @type boolean
  * @private
  */
  @or('_isFulfilled', '_isRejected')
  _isLoadingComplete;
 
  /**
  * Show loading animation only if custom state labels are not specified
  *
  * @field _isShowLoading
  * @type boolean
  * @private
  */
  @computed('_isLoading', 'pendingLabel', 'fulfilledLabel', 'rejectedLabel')
  get _isShowLoading() {
    return !(this.get('pendingLabel') || this.get('fulfilledLabel') || this.get('rejectedLabel'));
  }
 
  /**
  * Label to be displayed during Promise pending state
  *
  * @field pendingLabel
  * @type string
  * @default undefined
  * @public
  */
  @defaultProp
  pendingLabel = '';
 
  /**
  * Label to be displayed during Promise fulfilled state, a.k.a success label
  *
  * @field successLabel
  * @type string
  * @default undefined
  * @public
  */
  @defaultProp
  fulfilledLabel = '';
 
  /**
  * Label to be displayed during Promise rejected state, a.k.a failure label
  *
  * @field failureLabel
  * @type string
  * @default undefined
  * @public
  */
  @defaultProp
  rejectedLabel = '';
 
  /**
  * Optional aria-label attribute
  *
  * @field ariaLabel
  * @type string
  * @default null
  * @public
  */
  @defaultProp
  ariaLabel = null;
 
  /**
  * _sizeClass
  *
  * @computed _sizeClass
  * @private
  */
  @computed('size')
  get _sizeClass() {
    let size = this.get('size');
    return size ? `nucleus-button--${size}` : null;
  }
 
  /**
  * _typeClass
  *
  * @computed _typeClass
  * @private
  */
  @computed('variant')
  get _typeClass() {
    let type = this.get('variant');
    return type ? `nucleus-button--${type}` : 'nucleus-button--primary';
  }
 
  /**
  * _iconClass
  *
  * @computed _iconClass
  * @private
  */
  @computed('iconOnly')
  get _iconClass() {
    let iconButton = this.get('iconOnly');
    return iconButton ? `nucleus-button--iconOnly` : null;
  }
 
  /**
  * `onClick` action handler
  *
  * @field onClick
  * @type function
  * @public
  */
  @defaultProp
  onClick = null;
 
  /**
  * _buttonText
  *
  * @computed _buttonText
  * @private
  */
  @computed('_buttonState', 'label')
  get _buttonText() {
    let state = this._buttonState;
    return state === 'default'
    ? this.get('label')
    : this.get(`${state}Label`);
  }
 
  /**
  * _label
  *
  * @field _label
  * @type function
  * @private
  */
  @computed('_buttonText', 'ariaLabel', 'icon')
  get _label() {
    return this.get('ariaLabel') || this.get('_buttonText') || this.get('icon');
  }
 
  /**
  * click
  *
  * @method click
  * @public
  *
  */
  click() {
    let action = this.get('onClick');
    Iif(this.get('type') === "toggle") {
      this.set('active',!this.get('active'));
    }
 
    Iif (action === null || action === undefined) {
      return;
    }
 
    Eif (!this.get('_isPending')) {
      let promise = (action)(this.get('args'));
 
      if (promise && typeof promise.then === 'function') {
        safeSet(this, '_buttonState', BUTTON_STATE.PENDING);
        promise.then(() => {
          safeSet(this, '_buttonState', BUTTON_STATE.FULFILLED);
        }, () => {
          safeSet(this, '_buttonState', BUTTON_STATE.REJECTED);
        })
        .finally(() => {
          run.later(() => {
            safeSet(this, '_buttonState', BUTTON_STATE.DEFAULT)
          }, this.labelTimeout);
        });
      }
    }
    return false;
  }
}
 
export default NucleusButton;