/
Data.js
141 lines (117 loc) · 4.44 KB
/
Data.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Aria.classDefinition({
$classpath : "ariadoc.guides.mall.Data",
$singleton : true,
$dependencies : ["ariadoc.guides.mall.Beans", "aria.core.JsonValidator", "ariadoc.guides.mall.Random"],
$events : {
"dataChange" : "Raised when the data model is updated"
},
$constructor : function () {
var court = {};
aria.core.JsonValidator.normalize({
json : court,
beanName : "ariadoc.guides.mall.Beans.Court"
});
court.counters = ariadoc.guides.mall.Random.generateCounters(10);
court.slots = ariadoc.guides.mall.Random.generateSlots(10);
this.internalData = court;
},
$prototype : {
getViewData : function () {
var data = this.internalData;
// Recursive listener on the datamodel
aria.utils.Json.addListener(data, null, {
fn : this.stateChange,
scope : this
}, false, true);
return data;
},
stateChange : function (change) {
var counter = change.dataHolder;
if (change.dataName === "assignedTo") {
if (counter.slot) {
this.moveCounter(counter, change.oldValue, change.newValue);
} else {
this.assignCounter(counter, change.newValue);
}
} else if ("owners.employees.people".indexOf(change.dataName) !== -1) {
counter.people = counter.owners + counter.employees;
if (counter.slot) {
// because contraints might have changed
this.updateSlot(counter.slot);
}
} else {
if (counter.slot) {
this.updateSlot(counter.slot);
}
}
this.$raiseEvent("dataChange");
},
assignCounter : function (counter, to) {
this.freeSlot(to);
counter.assignedTo = to;
counter.slot = this.internalData.slots[to];
counter.slot.counter = counter;
// Notify neighbours
this.updateSlot(counter.slot);
},
freeSlot : function (position) {
var slot = this.internalData.slots[position];
slot.usable = true;
var oldCounter = slot.counter;
if (oldCounter) {
oldCounter.assignedTo = -1;
oldCounter.slot = null;
}
this.internalData.slots[position].counter = null;
},
updateSlot : function (slot) {
for (var i = 0; i < slot.neighbours.length; i += 1) {
var near = slot.neighbours[i];
if (near.counter) {
if (this.hasCostraints(slot.counter, near.counter)) {
// Remove it
this.freeSlot(near.position);
slot.usable = false;
}
}
}
},
hasCostraints : function (newCounter, counter) {
// Closed counters cannot have neighbours
if (!counter.open) {
for (var i = 0; i < counter.slot.neighbours.length; i += 1) {
var near = counter.slot.neighbours[i];
if (near.counter) {
return true;
}
}
} else if (newCounter && !newCounter.open) {
var newHasneighbours = false;
for (var i = 0; i < newCounter.slot.neighbours.length; i += 1) {
var near = newCounter.slot.neighbours[i];
if (near.counter) {
newHasneighbours = true;
}
this.freeSlot(i);
}
return newHasneighbours;
}
// No pizza shop near counters with >10 people
if (counter.type === "pizza") {
for (var i = 0; i < counter.slot.neighbours.length; i += 1) {
var near = counter.slot.neighbours[i];
if (near.counter && near.counter.people > 10) {
return true;
}
}
}
return false;
},
moveCounter : function (counter, from, to) {
this.freeSlot(from);
if (to !== -1) {
this.assignCounter(counter, to);
}
}
}
});