# all files / clique/view/SelectionInfo/ index.js

23.29% Statements 17/73
11.11% Branches 4/36
5.88% Functions 1/17
17.65% Lines 12/68
1 branch Ignored

    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
    1551×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     import _ from 'underscore';
    import Backbone from 'backbone';

    import selectionInfo from './selectionInfo.jade';

    const colors = {
      white: 'default',
      blue: 'primary',
      green: 'success',
      purple: 'info',
      orange: 'warning',
      red: 'danger',
      clear: 'link'
    };

    const processButtons = function (specs) {
      return _.map(specs || [], function (button) {
        return {
          label: _.isFunction(button.label) ? button.label : _.constant(button.label),
          cssClass: _.uniqueId('ident-'),
          color: colors[button.color] || 'default',
          icon: button.icon,
          repeat: _.isUndefined(button.repeat) ? false : button.repeat,
          callback: button.callback || _.noop,
          show: _.isFunction(button.show) ? button.show : _.constant(_.isUndefined(button.show) ? true : button.show)
        };
      });
    };

    const SelectionInfo = Backbone.View.extend({
      initialize: function (options) {
        var debRender;

        options = options || {};
        this.graph = options.graph;
        this.nav = _.isUndefined(options.nav) ? true : options.nav;
        this.metadata = _.isUndefined(options.metadata) ? true : options.metadata;

        this.nodeButtons = processButtons(options.nodeButtons);
        this.selectionButtons = processButtons(options.selectionButtons);

        debRender = _.debounce(this.render, 100);

        this.listenTo(this.model, 'change', debRender);
        this.listenTo(this.model, 'focused', debRender);
        this.listenTo(this.graph, 'change', debRender);
      },

      render: function () {
        let focused;
        let renderTemplate;

        renderTemplate = _.bind(function (node) {
          this.$el.html(selectionInfo({
            node: node,
            degree: node ? this.graph.degree(node.key()) : -1,
            selectionSize: this.model.size(),
            nav: this.nav,
            metadata: this.metadata,
            nodeButtons: this.nodeButtons,
            selectionButtons: this.selectionButtons
          }));

          _.each(this.nodeButtons, _.bind(function (spec) {
            this.$(`button.${spec.cssClass}`).on('click', _.bind(function () {
              var render = _.bind(spec.callback, this)(this.graph.adapter.getAccessor(this.model.focused()));
              if (render) {
                this.render();
              }
            }, this));
          }, this));

          _.each(this.selectionButtons, _.bind(function (spec) {
            this.$(`button.${spec.cssClass}`).on('click', _.bind(function () {
              let render;
              let selectionAccessors;

              selectionAccessors = _.map(this.model.items(), this.graph.adapter.getAccessor, this.graph.adapter);

              if (spec.repeat) {
                render = _.any(_.map(selectionAccessors, _.bind(spec.callback, this)));
              } else {
                render = _.bind(spec.callback, this)(selectionAccessors, this.graph.adapter.getAccessor(this.model.focused()));
              }

              if (render) {
                this.render();
              }
            }, this));
          }, this));

          this.$('a.prev')
          .on('click', _.bind(function () {
            this.model.focusLeft();
          }, this));

          this.$('a.next')
          .on('click', _.bind(function () {
            this.model.focusRight();
          }, this));
        }, this);

        focused = this.model.focused();

        if (!focused) {
          renderTemplate(focused);
        } else {
          this.graph.adapter.findNodeByKey(focused)
          .then(renderTemplate);
        }
      }
    });

    SelectionInfo.hideNode = function (node) {
      node.setAttribute('selected', false);
      node.clearAttribute('root');
      this.graph.removeNode(node.key());
    };

    SelectionInfo.deleteNode = function (node) {
      var doDelete = !node.getData('deleted');
      if (doDelete) {
        node.setData('deleted', true);
        _.bind(SelectionInfo.hideNode, this)(node);
      } else {
        node.clearData('deleted');
      }

      return !doDelete;
    };

    SelectionInfo.expandNode = function (node) {
      this.graph.addNeighborhood(node);
    };

    SelectionInfo.collapseNode = function (node) {
      let loners;
      let accessors;

      // Find all neighbors of the node that have exactly one
      // neighbor.
      loners = _.filter(this.graph.neighbors(node.key()), function (nbr) {
        return _.size(this.graph.neighbors(nbr)) === 1;
      }, this);

      // Extract the accessor objects for these nodes.
      accessors = _.map(loners, this.graph.adapter.getAccessor, this.graph.adapter);

      // Hide them.
      _.each(accessors, SelectionInfo.hideNode, this);
    };

    export default SelectionInfo;