all files / addon/components/ polaris-progress-bar.js

100% Statements 15/15
100% Branches 6/6
100% Functions 3/3
100% Lines 15/15
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                                                                                                                       
import Component from '@ember/component';
import { computed } from '@ember/object';
import { classify, htmlSafe } from '@ember/string';
import layout from '../templates/components/polaris-progress-bar';
 
const allowedSizes = [
  'small',
  'medium',
  'large'
];
const defaultSize = 'medium';
 
/**
 * Polaris progress bar component.
 * See https://polaris.shopify.com/components/feedback-indicators/progress-bar
 */
export default Component.extend({
  classNames: ['Polaris-ProgressBar'],
  classNameBindings: ['sizeClass'],
 
  layout,
 
  /**
   * The progression of certain tasks
   *
   * @public
   * @property progress
   * @type {Number}
   * @default: 0
   */
  progress: 0,
 
  /**
   * Size of progressbar
   *
   * @public
   * @property size
   * @type {String}
   * @default: 'medium'
   */
  size: defaultSize,
 
  /*
   * Internal properties.
   */
  sizeClass: computed('size', function() {
    let size = this.get('size');
    if (allowedSizes.indexOf(size) === -1) {
      size = defaultSize;
    }
 
    return `Polaris-ProgressBar--size${ classify(size) }`;
  }).readOnly(),
 
  parsedProgress: computed('progress', function() {
    let progress = this.get('progress');
    let parsedProgress;
 
    if (progress < 0) {
      parsedProgress = 0;
    } else if (progress > 100) {
      parsedProgress = 100;
    } else {
      parsedProgress = progress;
    }
 
    return parsedProgress;
  }).readOnly(),
 
  progressStyle: computed('parsedProgress', function() {
    return htmlSafe(`width: ${ this.get('parsedProgress') }%;`);
  }).readOnly()
});