Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gvm.determineOverlaps = function () {
- const tmp = {};
- const overlaps = {};
- // creates an empty object with each day of the week as key
- shortDays.forEach(day => {
- tmp[day] = [];
- overlaps[day] = [];
- });
- // filters the rules to each day
- gvm.rules().forEach(rule => rule.days().forEach(day => tmp[day].push(rule)));
- const keys = Object.keys(tmp);
- const values = Object.values(tmp);
- // loop over each day
- values.forEach((rules, index) => {
- // loops over all the rules of that specific day
- rules.forEach(rule => {
- // loops over all the other rules again to check if there are overlaps
- rules.filter(_rule => rule !== _rule).forEach(_rule => {
- if (_rule.start() <= rule.end() && _rule.end() >= rule.start() && !overlaps[keys[index]].includes(rule)) {
- overlaps[keys[index]].push(rule);
- };
- });
- });
- });
- return overlaps;
- };
- gvm.tijdelijk = function () {
- const obj = {};
- shortDays.forEach(day => obj[day] = []);
- //sorts the rules on the start time
- gvm.rules(gvm.rules().sort((a,b) => (a.start() > b.start()) ? 1 : ((b.start() > a.start()) ? -1 : 0)));
- const overlaps = gvm.determineOverlaps();
- const keys = Object.keys(overlaps);
- const values = Object.values(overlaps);
- values.forEach((overlapRules, index) => {
- overlapRules.forEach(overlapRule => {
- const tmp = [];
- overlapRules.filter(_overlapeRule => _overlapeRule.start() < overlapRule.end() && overlapRule.start() < _overlapeRule.end())
- .forEach(_overlapeRule => tmp.push(_overlapeRule));
- obj[keys[index]].push(tmp);
- });
- });
- return obj
- };
- gvm.createOverlaps = function () {
- const obj = gvm.tijdelijk();
- const keys = Object.keys(obj);
- const values = Object.values(obj)
- let tmp = [];
- values.forEach((days, index) => {
- const overlaps = [];
- days.forEach(rules => {
- const startTimes = rules.map(x => x.start());
- const endTimes = rules.map(x => x.end());
- const duplicated = overlaps.find(x => x.start() === Math.min(...startTimes) && x.end() === Math.max(...endTimes));
- if (duplicated === undefined) {
- overlaps.push(new Overlap(gvm, {
- start: Math.min(...startTimes),
- end: Math.max(...endTimes),
- days: [keys[index]],
- rules: ko.toJS(rules)
- }));
- };
- });
- tmp = tmp.concat(overlaps);
- });
- gvm.tmp(tmp)
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement