2017-01-29 23:51:08 +00:00
|
|
|
define(['filters/nodefilter'], function (NodeFilter) {
|
|
|
|
'use strict';
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2015-07-07 22:36:57 +00:00
|
|
|
return function () {
|
2016-05-22 11:23:43 +00:00
|
|
|
var targets = [];
|
|
|
|
var filterObservers = [];
|
|
|
|
var filters = [];
|
|
|
|
var filteredData;
|
|
|
|
var data;
|
2015-07-07 22:36:57 +00:00
|
|
|
|
|
|
|
function remove(d) {
|
2016-05-22 12:51:30 +00:00
|
|
|
targets = targets.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function add(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
targets.push(d);
|
2015-07-07 22:36:57 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (filteredData !== undefined) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.setData(filteredData);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setData(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
data = d;
|
|
|
|
refresh();
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
2016-05-22 12:51:30 +00:00
|
|
|
if (data === undefined) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-07-07 22:36:57 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var filter = filters.reduce(function (a, f) {
|
2015-07-08 19:17:00 +00:00
|
|
|
return function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return a(d) && f.run(d);
|
|
|
|
};
|
2016-05-22 12:51:30 +00:00
|
|
|
}, function () {
|
|
|
|
return true;
|
|
|
|
});
|
2015-07-08 19:17:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
filteredData = new NodeFilter(filter)(data);
|
2015-07-07 22:36:57 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
targets.forEach(function (t) {
|
2016-05-22 11:23:43 +00:00
|
|
|
t.setData(filteredData);
|
|
|
|
});
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function notifyObservers() {
|
2016-05-22 12:51:30 +00:00
|
|
|
filterObservers.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.filtersChanged(filters);
|
|
|
|
});
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function addFilter(d) {
|
2017-03-28 22:45:52 +00:00
|
|
|
var newItem = true;
|
|
|
|
|
|
|
|
filters.forEach(function (f) {
|
|
|
|
if (f.getKey && f.getKey() === d.getKey()) {
|
|
|
|
removeFilter(f);
|
|
|
|
newItem = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (newItem) {
|
|
|
|
filters.push(d);
|
|
|
|
notifyObservers();
|
|
|
|
d.setRefresh(refresh);
|
|
|
|
refresh();
|
|
|
|
}
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeFilter(d) {
|
2016-05-22 12:51:30 +00:00
|
|
|
filters = filters.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
2016-05-22 11:23:43 +00:00
|
|
|
notifyObservers();
|
|
|
|
refresh();
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function watchFilters(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
filterObservers.push(d);
|
2015-07-07 22:36:57 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
d.filtersChanged(filters);
|
2015-07-07 22:36:57 +00:00
|
|
|
|
|
|
|
return function () {
|
2016-05-22 12:51:30 +00:00
|
|
|
filterObservers = filterObservers.filter(function (e) {
|
|
|
|
return d !== e;
|
|
|
|
});
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-07-07 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return {
|
|
|
|
add: add,
|
|
|
|
remove: remove,
|
|
|
|
setData: setData,
|
|
|
|
addFilter: addFilter,
|
|
|
|
removeFilter: removeFilter,
|
2018-08-03 18:08:08 +00:00
|
|
|
watchFilters: watchFilters
|
2016-05-22 12:51:30 +00:00
|
|
|
};
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
|
|
|
});
|