# all files / Github/diagram-js/lib/draw/ Styles.js

100% Statements 17/17
85.71% Branches 12/14
100% Functions 5/5
100% Lines 17/17

    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

    1295×

    1295×

    1295×
    2751×

    2751×

    1295×

    33645×
    1295×
    1295×

    33645×
    19149×

    33645×

    1295×
    26878×
    14486×
    14486×

    26878×

     import {
      isArray,
      assign,
      reduce
    } from 'min-dash';

    /**
     * A component that manages shape styles
     */
    export default function Styles() {

      var defaultTraits = {

        'no-fill': {
          fill: 'none'
        },
        'no-border': {
          strokeOpacity: 0.0
        },
        'no-events': {
          pointerEvents: 'none'
        }
      };

      var self = this;

      /**
       * Builds a style definition from a className, a list of traits and an object of additional attributes.
       *
       * @param  {String} className
       * @param  {Array<String>} traits
       * @param  {Object} additionalAttrs
       *
       * @return {Object} the style defintion
       */
      this.cls = function(className, traits, additionalAttrs) {
        var attrs = this.style(traits, additionalAttrs);

        return assign(attrs, { 'class': className });
      };

      /**
       * Builds a style definition from a list of traits and an object of additional attributes.
       *
       * @param  {Array<String>} traits
       * @param  {Object} additionalAttrs
       *
       * @return {Object} the style defintion
       */
      this.style = function(traits, additionalAttrs) {

        if (!isArray(traits) && !additionalAttrs) {
          additionalAttrs = traits;
          traits = [];
        }

        var attrs = reduce(traits, function(attrs, t) {
          return assign(attrs, defaultTraits[t] || {});
        }, {});

        return additionalAttrs ? assign(attrs, additionalAttrs) : attrs;
      };

      this.computeStyle = function(custom, traits, defaultStyles) {
        if (!isArray(traits)) {
          defaultStyles = traits;
          traits = [];
        }

        return self.style(traits || [], assign({}, defaultStyles, custom || {}));
      };
    }