-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlanningList.js
113 lines (98 loc) · 3.29 KB
/
PlanningList.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var Planning = require('./Planning');
var _ = require('lodash');
/**
* PlanningList class
* Définit un objet planningList contenant une liste d'objets Plannings
*
* @class PlanningList
* @property {Array} plannings liste d'objets plannings
*/
function PlanningList() {
this.plannings = [];
}
PlanningList.prototype.size = function (planning) {
return this.plannings.length;
};
PlanningList.prototype.addPlanning = function (planning) {
if (planning instanceof Planning) {
this.plannings.push(planning);
} else {
throw {name: "addPlanning", type: "error", message: "L'élément que vous voulez ajouter n'est pas un Objet Planning"};
}
};
PlanningList.prototype.prospectiveConflicts = function (houseLocation) {
// group all meetings with the same location onto a new planning
var prospectiveConflicts = new Planning();
for (var i = 0; i < this.plannings.length; i++) {
for (var j = 0; j < this.plannings[i].size(); j++) {
if (this.plannings[i].meetings[j]['location'] === houseLocation) {
prospectiveConflicts.addMeeting(this.plannings[i].meetings[j]);
}
}
}
return prospectiveConflicts;
};
PlanningList.prototype.conflictsPlanning = function (prospectiveConflicts) {
var conflictsPlanning = new Planning();
var addedKeys = []; // array of already added keys to prevent duplicates
var len = prospectiveConflicts.size();
for (var i = 0; i < len; i++) {
for (var j = 0; j < len; j++) {
if ((i !== j) && (prospectiveConflicts.meetings[i].isOverlapping(prospectiveConflicts.meetings[j]))) {
if (!contains.call(addedKeys, i)) {
addedKeys.push(i);
conflictsPlanning.addMeeting(prospectiveConflicts.meetings[i]);
}
if (!contains.call(addedKeys, j)) {
addedKeys.push(j);
conflictsPlanning.addMeeting(prospectiveConflicts.meetings[j]);
}
}
}
}
return conflictsPlanning;
};
var contains = function (needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if (!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function (needle) {
var i = -1, index = -1;
for (i = 0; i < this.length; i++) {
var item = this[i];
if ((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};
function clone_object(o) {
var n = Object.create(
Object.getPrototypeOf(o),
Object.getOwnPropertyNames(o).reduce(
function (prev, cur) {
prev[cur] = Object.getOwnPropertyDescriptor(o, cur);
return prev;
},
{}
)
);
if (!Object.isExtensible(o)) {
Object.preventExtensions(n);
}
if (Object.isSealed(o)) {
Object.seal(n);
}
if (Object.isFrozen(o)) {
Object.freeze(n);
}
return n;
}
module.exports = PlanningList;