all files / addon/components/ polaris-badge.js

100% Statements 18/18
100% Branches 16/16
100% Functions 4/4
100% Lines 18/18
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                                                                                                                                                                                     
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isBlank } from '@ember/utils';
import { notEmpty } from '@ember/object/computed';
import { classify } from '@ember/string';
import layout from '../templates/components/polaris-badge';
 
const PROGRESS_LABELS = {
  incomplete: 'Incomplete',
  partiallyComplete: 'Partially complete',
  complete: 'Complete',
};
 
const STATUS_LABELS = {
  info: 'Info',
  success: 'Success',
  warning: 'Warning',
  attention: 'Attention',
};
 
/**
 * Polaris badge component.
 * See https://polaris.shopify.com/components/images-and-icons/badge
 */
export default Component.extend({
  tagName: 'span',
  classNames: ['Polaris-Badge'],
  classNameBindings: ['statusClass', 'progressClass'],
 
  layout,
 
  /*
   * Public attributes.
   */
  /**
   * The content to display inside the badge.
   *
   * This component can be used in block form,
   * in which case the block content will be used
   * instead of `text`
   *
   * @property text
   * @type {string}
   * @default: null
   */
  text: null,
 
  /**
   * Render a pip showing the progress of a given task.
   *
   * @property progress
   * @type {string}
   * @default: null
   */
  progress: null,
 
  /**
   * Set the color of the badge for the given status.
   *
   * @property status
   * @type {string}
   * @default: null
   */
  status: null,
 
  /*
   * Internal properties.
   */
  hasProgress: notEmpty('progress'),
  hasStatus: notEmpty('status'),
 
  progressDescription: computed('progress', function() {
    const progress = this.get('progress');
    if (isBlank(progress) || progress === 'default') {
      return null;
    }
 
    return PROGRESS_LABELS[progress];
  }).readOnly(),
 
  progressClass: computed('progress', function() {
    const progress = this.get('progress');
    if (isBlank(progress) || progress === 'default') {
      return null;
    }
 
    return `Polaris-Badge--progress${classify(progress)}`;
  }).readOnly(),
 
  statusDescription: computed('status', function() {
    const status = this.get('status');
    if (isBlank(status) || status === 'default') {
      return null;
    }
 
    return STATUS_LABELS[status];
  }).readOnly(),
 
  statusClass: computed('status', function() {
    const status = this.get('status');
    if (isBlank(status) || status === 'default') {
      return null;
    }
 
    return `Polaris-Badge--status${classify(status)}`;
  }).readOnly()
});