2015-07-08 21:17:00 +02:00
|
|
|
define(["filters/nodefilter"], function (NodeFilter) {
|
2015-07-08 00:36:57 +02:00
|
|
|
return function () {
|
2017-03-17 03:14:57 +01:00
|
|
|
var targets = [];
|
|
|
|
var filterObservers = [];
|
|
|
|
var filters = [];
|
|
|
|
var filteredData;
|
|
|
|
var data;
|
2015-07-08 00:36:57 +02:00
|
|
|
|
|
|
|
function remove(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
targets = targets.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
targets.push(d);
|
2015-07-08 00:36:57 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (filteredData !== undefined) {
|
|
|
|
d.setData(filteredData);
|
|
|
|
}
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setData(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
data = d;
|
|
|
|
refresh();
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
2017-03-17 03:14:57 +01:00
|
|
|
if (data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-08 00:36:57 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
var filter = filters.reduce(function (a, f) {
|
2015-07-08 21:17:00 +02:00
|
|
|
return function (d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return a(d) && f.run(d);
|
|
|
|
};
|
|
|
|
}, function () {
|
|
|
|
return true;
|
|
|
|
});
|
2015-07-08 21:17:00 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
filteredData = new NodeFilter(filter)(data);
|
2015-07-08 00:36:57 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
targets.forEach(function (t) {
|
|
|
|
t.setData(filteredData);
|
|
|
|
});
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function notifyObservers() {
|
2017-03-17 03:14:57 +01:00
|
|
|
filterObservers.forEach(function (d) {
|
|
|
|
d.filtersChanged(filters);
|
|
|
|
});
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addFilter(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
filters.push(d);
|
|
|
|
notifyObservers();
|
|
|
|
d.setRefresh(refresh);
|
|
|
|
refresh();
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeFilter(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
filters = filters.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
|
|
|
notifyObservers();
|
|
|
|
refresh();
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function watchFilters(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
filterObservers.push(d);
|
2015-07-08 00:36:57 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
d.filtersChanged(filters);
|
2015-07-08 00:36:57 +02:00
|
|
|
|
|
|
|
return function () {
|
2017-03-17 03:14:57 +01:00
|
|
|
filterObservers = filterObservers.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
|
|
|
};
|
2015-07-08 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return {
|
|
|
|
add: add,
|
|
|
|
remove: remove,
|
|
|
|
setData: setData,
|
|
|
|
addFilter: addFilter,
|
|
|
|
removeFilter: removeFilter,
|
|
|
|
watchFilters: watchFilters,
|
|
|
|
refresh: refresh
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|