aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/source-viewer/source.js
blob: 1ded0471cb77795aaf3e0bca70d75c7c7e5e7091 (plain)
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
define([
  'backbone'
], function (Backbone) {

  return Backbone.Model.extend({
    idAttribute: 'uuid',

    defaults: function () {
      return {
        hasSource: false,
        hasCoverage: false,
        hasDuplications: false,
        hasSCM: false
      };
    },

    key: function () {
      return this.get('key');
    },

    addMeta: function (meta) {
      var source = this.get('source'),
          metaIdx = 0,
          metaLine = meta[metaIdx];
      source.forEach(function (line) {
        while (metaLine != null && line.line > metaLine.line) {
          metaLine = meta[++metaIdx];
        }
        if (metaLine != null && line.line === metaLine.line) {
          _.extend(line, metaLine);
          metaLine = meta[++metaIdx];
        }
      });
      this.set({ source: source });
    },

    addDuplications: function (duplications) {
      var source = this.get('source');
      if (source != null) {
        source.forEach(function (line) {
          var lineDuplications = [];
          duplications.forEach(function (d, i) {
            var duplicated = false;
            d.blocks.forEach(function (b) {
              if (b._ref === '1') {
                var lineFrom = b.from,
                    lineTo = b.from + b.size;
                if (line.line >= lineFrom && line.line <= lineTo) {
                  duplicated = true;
                }
              }
            });
            lineDuplications.push(duplicated ? i + 1 : false);
          });
          line.duplications = lineDuplications;
        });
      }
      this.set({ source: source });
    }
  });

});