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

69.23% Statements 18/26
50% Branches 8/16
100% Functions 5/5
69.23% Lines 18/26
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                                                                                                                                                                                      127×   127× 73×     54×                     127×   127× 73×     54×                     92× 92×   92×       87×     86×       86× 86×                                   87× 87×        
import Component from '@ember/component';
import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
import { isNone, isEmpty } from '@ember/utils';
import { classify } from '@ember/string';
import layout from '../templates/components/polaris-icon';
 
// TODO: look into importing icons properly.
export default Component.extend({
  tagName: 'span',
  classNames: ['Polaris-Icon'],
  classNameBindings: [
    'colorClass',
    'isColored:Polaris-Icon--isColored',
    'backdrop:Polaris-Icon--hasBackdrop'
  ],
  attributeBindings: [
    'accessibilityLabel:aria-label',
  ],
 
  layout,
 
  /**
   * The SVG contents to display in the icon
   * If the source doesn't have a slash in the name, it will look for Polaris
   * icons in the namespace specified by `sourcePath` property.
   *
   * @property source
   * @public
   * @type {string}
   * @default null
   */
  source: null,
 
  /**
   * Sets the color for the SVG fill
   *
   * @property color
   * @public
   * @type {string}
   * @default null
   */
  color: null,
 
  /**
   * Show a backdrop behind the icon
   *
   * @property backdrop
   * @public
   * @type {boolean}
   * @default false
   */
  backdrop: false,
 
  /**
   * Descriptive text to be read to screenreaders
   *
   * @property accessibilityLabel
   * @public
   * @type {string}
   * @default null
   */
  accessibilityLabel: null,
 
  /**
   * Path under which `ember-svg-jar` serves the Polaris SVG icons
   *
   * @property sourcePath
   * @public
   * @type {string}
   * @default 'polaris'
   */
  sourcePath: 'polaris',
 
  /**
   * Whether the component should leave space for an icon
   *
   * @property showPlaceholder
   * @private
   * @type {boolean}
   */
  showPlaceholder: equal('source', 'placeholder').readOnly(),
 
  /**
   * Class to apply to color the icon
   *
   * @property colorClass
   * @private
   * @type {string}
   */
  colorClass: computed('color', function() {
    let color = this.get('color');
 
    if (isEmpty(color)) {
      return null;
    }
 
    return `Polaris-Icon--color${classify(color)}`;
  }).readOnly(),
 
  /**
   * Whether a color has been specified for the icon
   *
   * @property isColored
   * @private
   * @type {boolean}
   */
  isColored: computed('color', function() {
    let color = this.get('color');
 
    if (isEmpty(color)) {
      return false;
    }
 
    return color !== 'white';
  }).readOnly(),
 
  /**
   * Final source for the icon SVG
   *
   * @property iconSource
   * @private
   * @type {string}
   */
  iconSource: computed('sourcePath', 'source', function() {
    let source = this.get('source');
    source = source.indexOf('/') === -1 ? `${ this.get('sourcePath') }/${ source }` : source;
 
    return source;
  }).readOnly(),
 
  removeSvgFills(elem) {
    if (isNone(elem)) {
      return;
    }
 
    Iif (!elem.hasChildNodes) {
        return;
    }
 
    let children = elem.childNodes;
    for (let i = 0, len = children.length; i < len; i++) {
      let child = children[i];
      if (child.tagName === 'g') {
        child.removeAttribute('fill');
        this.removeSvgFills(child);
      } else {
        let fill = child.getAttribute('fill');
        // This is what Shopify does too in @shopify/images/icon-loader.js
        let newFill = fill && fill.indexOf('#FFF') !== -1 ? 'currentColor' : '';
        child.setAttribute('fill', newFill);
      }
    }
  },
 
  /*
   * Lifecycle hooks.
   */
  didInsertElement() {
    this._super(...arguments);
    this.removeSvgFills(this.$('svg')[0]);
  }
});