Passed
Push — develop ( 9a1b4b...96b452 )
by Xaver
03:40
created

lib/datadistributor.js   A

Complexity

Total Complexity 24
Complexity/F 1.2

Size

Lines of Code 103
Function Count 20

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 24
eloc 60
mnd 4
bc 4
fnc 20
dl 0
loc 103
rs 10
bpm 0.2
cpm 1.2
noi 0
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A datadistributor.js ➔ removeFilter 0 7 2
A datadistributor.js ➔ notifyObservers 0 5 2
B datadistributor.js ➔ refresh 0 19 6
A datadistributor.js ➔ add 0 7 2
A datadistributor.js ➔ remove 0 5 4
A datadistributor.js ➔ watchFilters 0 11 3
A datadistributor.js ➔ setData 0 4 1
A datadistributor.js ➔ addFilter 0 17 4
1
define(['filters/nodefilter'], function (NodeFilter) {
2
  'use strict';
3
4
  return function () {
5
    var targets = [];
6
    var filterObservers = [];
7
    var filters = [];
8
    var filteredData;
9
    var data;
10
11
    function remove(d) {
12
      targets = targets.filter(function (e) {
13
        return d !== e;
14
      });
15
    }
16
17
    function add(d) {
18
      targets.push(d);
19
20
      if (filteredData !== undefined) {
21
        d.setData(filteredData);
22
      }
23
    }
24
25
    function setData(d) {
26
      data = d;
27
      refresh();
28
    }
29
30
    function refresh() {
31
      if (data === undefined) {
32
        return;
33
      }
34
35
      var filter = filters.reduce(function (a, f) {
36
        return function (d) {
37
          return a(d) && f.run(d);
38
        };
39
      }, function () {
40
        return true;
41
      });
42
43
      filteredData = new NodeFilter(filter)(data);
44
45
      targets.forEach(function (t) {
46
        t.setData(filteredData);
47
      });
48
    }
49
50
    function notifyObservers() {
51
      filterObservers.forEach(function (d) {
52
        d.filtersChanged(filters);
53
      });
54
    }
55
56
    function addFilter(d) {
57
      var newItem = true;
58
59
      filters.forEach(function (f) {
60
        if (f.getKey && f.getKey() === d.getKey()) {
61
          removeFilter(f);
62
          newItem = false;
63
        }
64
      });
65
66
      if (newItem) {
67
        filters.push(d);
68
        notifyObservers();
69
        d.setRefresh(refresh);
70
        refresh();
71
      }
72
    }
73
74
    function removeFilter(d) {
75
      filters = filters.filter(function (e) {
76
        return d !== e;
77
      });
78
      notifyObservers();
79
      refresh();
80
    }
81
82
    function watchFilters(d) {
83
      filterObservers.push(d);
84
85
      d.filtersChanged(filters);
86
87
      return function () {
88
        filterObservers = filterObservers.filter(function (e) {
89
          return d !== e;
90
        });
91
      };
92
    }
93
94
    return {
95
      add: add,
96
      remove: remove,
97
      setData: setData,
98
      addFilter: addFilter,
99
      removeFilter: removeFilter,
100
      watchFilters: watchFilters
101
    };
102
  };
103
});
104