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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
import {Util} from "../../util";
import {InteractionLevel, MapController, AppMode} from "../mapcontroller";
import {MapView} from "../../views/mapview";
import * as $ from "jquery";
export enum RoomInteractionMode {
DEFAULT,
ADD_RACK,
ADD_PSU,
ADD_COOLING_ITEM
}
export class RoomModeController {
public currentRoom: IRoom;
public roomInteractionMode: RoomInteractionMode;
private mapController: MapController;
private mapView: MapView;
private roomTypes: string[];
private roomTypeMap: IRoomTypeMap;
private availablePSUs: IPSU[];
private availableCoolingItems: ICoolingItem[];
constructor(mapController: MapController) {
this.mapController = mapController;
this.mapView = this.mapController.mapView;
this.mapController.api.getAllRoomTypes().then((roomTypes: string[]) => {
this.roomTypes = roomTypes;
this.roomTypeMap = {};
this.roomTypes.forEach((type: string) => {
this.mapController.api.getAllowedObjectsByRoomType(type).then((objects: string[]) => {
this.roomTypeMap[type] = objects;
});
});
this.populateRoomTypeDropdown();
});
// this.mapController.api.getAllPSUSpecs().then((specs: IPSU[]) => {
// this.availablePSUs = specs;
// });
//
// this.mapController.api.getAllCoolingItemSpecs().then((specs: ICoolingItem[]) => {
// this.availableCoolingItems = specs;
// });
this.roomInteractionMode = RoomInteractionMode.DEFAULT;
}
public enterMode(room: IRoom) {
this.currentRoom = room;
this.roomInteractionMode = RoomInteractionMode.DEFAULT;
this.mapView.roomTextLayer.setVisibility(false);
this.mapView.zoomInOnRoom(this.currentRoom);
$("#room-name-input").val(this.currentRoom.name);
MapController.hideAndShowMenus("#room-menu");
// Pre-select the type of the current room in the dropdown
let roomTypeDropdown = $("#roomtype-select");
roomTypeDropdown.find('option').prop("selected", "false");
let roomTypeIndex = this.roomTypes.indexOf(this.currentRoom.roomType);
if (roomTypeIndex !== -1) {
roomTypeDropdown.find('option[value="' + roomTypeIndex + '"]').prop("selected", "true");
} else {
roomTypeDropdown.val([]);
}
this.populateAllowedObjectTypes();
this.mapView.roomLayer.setClickable(false);
if (this.mapController.appMode === AppMode.SIMULATION) {
this.mapController.simulationController.transitionFromBuildingToRoom();
}
}
public goToBuildingMode() {
this.mapController.interactionLevel = InteractionLevel.BUILDING;
if (this.roomInteractionMode !== RoomInteractionMode.DEFAULT) {
this.roomInteractionMode = RoomInteractionMode.DEFAULT;
this.mapView.hoverLayer.setHoverItemVisibility(false);
$("#add-rack-btn").attr("data-active", "false");
$("#add-psu-btn").attr("data-active", "false");
$("#add-cooling-item-btn").attr("data-active", "false");
}
this.mapView.roomTextLayer.setVisibility(true);
this.mapView.zoomOutOnDC();
MapController.hideAndShowMenus("#building-menu");
this.mapView.roomLayer.setClickable(true);
if (this.mapController.appMode === AppMode.SIMULATION) {
this.mapController.simulationController.transitionFromRoomToBuilding();
}
}
public setupEventListeners(): void {
// Component buttons
let addRackBtn = $("#add-rack-btn");
let addPSUBtn = $("#add-psu-btn");
let addCoolingItemBtn = $("#add-cooling-item-btn");
let roomTypeDropdown = $("#roomtype-select");
addRackBtn.on("click", () => {
this.handleItemClick("RACK");
});
addPSUBtn.on("click", () => {
this.handleItemClick("PSU");
});
addCoolingItemBtn.on("click", () => {
this.handleItemClick("COOLING_ITEM");
});
// Handler for saving a new room name
$("#room-name-save").on("click", () => {
this.currentRoom.name = $("#room-name-input").val();
this.mapController.api.updateRoom(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom).then(() => {
this.mapView.roomTextLayer.draw();
this.mapController.showInfoBalloon("Room name saved", "info");
});
});
// Handler for room deletion button
$("#room-deletion").on("click", () => {
MapController.showConfirmDeleteDialog("room", () => {
this.mapController.api.deleteRoom(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom.id).then(() => {
let roomIndex = this.mapView.currentDatacenter.rooms.indexOf(this.currentRoom);
this.mapView.currentDatacenter.rooms.splice(roomIndex, 1);
this.mapView.redrawMap();
this.goToBuildingMode();
});
});
});
// Handler for the room type dropdown component
roomTypeDropdown.on("change", () => {
let newRoomType = this.roomTypes[roomTypeDropdown.val()];
if (!this.checkRoomTypeLegality(newRoomType)) {
roomTypeDropdown.val(this.roomTypes.indexOf(this.currentRoom.roomType));
this.mapController.showInfoBalloon("Room type couldn't be changed, illegal objects", "warning");
return;
}
this.currentRoom.roomType = newRoomType;
this.mapController.api.updateRoom(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom).then(() => {
this.populateAllowedObjectTypes();
this.mapView.roomTextLayer.draw();
this.mapController.showInfoBalloon("Room type changed", "info");
});
});
}
public handleCanvasMouseClick(gridPos: IGridPosition): void {
if (this.roomInteractionMode === RoomInteractionMode.DEFAULT) {
let tileIndex = Util.tileListPositionIndexOf(this.currentRoom.tiles, gridPos);
if (tileIndex !== -1) {
let tile = this.currentRoom.tiles[tileIndex];
if (tile.object !== undefined) {
this.mapController.interactionLevel = InteractionLevel.OBJECT;
this.mapController.objectModeController.enterMode(tile);
}
} else {
this.goToBuildingMode();
}
} else if (this.roomInteractionMode === RoomInteractionMode.ADD_RACK) {
this.addObject(this.mapView.hoverLayer.hoverTilePosition, "RACK");
} else if (this.roomInteractionMode === RoomInteractionMode.ADD_PSU) {
this.addObject(this.mapView.hoverLayer.hoverTilePosition, "PSU");
} else if (this.roomInteractionMode === RoomInteractionMode.ADD_COOLING_ITEM) {
this.addObject(this.mapView.hoverLayer.hoverTilePosition, "COOLING_ITEM");
}
}
private handleItemClick(type: string): void {
let addRackBtn = $("#add-rack-btn");
let addPSUBtn = $("#add-psu-btn");
let addCoolingItemBtn = $("#add-cooling-item-btn");
let allObjectContainers = $(".dc-component-container");
let objectTypes = [
{
type: "RACK",
mode: RoomInteractionMode.ADD_RACK,
btn: addRackBtn
},
{
type: "PSU",
mode: RoomInteractionMode.ADD_PSU,
btn: addPSUBtn
},
{
type: "COOLING_ITEM",
mode: RoomInteractionMode.ADD_COOLING_ITEM,
btn: addCoolingItemBtn
}
];
allObjectContainers.attr("data-active", "false");
if (this.roomInteractionMode === RoomInteractionMode.DEFAULT) {
this.mapView.hoverLayer.setHoverItemVisibility(true, type);
if (type === "RACK") {
this.roomInteractionMode = RoomInteractionMode.ADD_RACK;
addRackBtn.attr("data-active", "true");
} else if (type === "PSU") {
this.roomInteractionMode = RoomInteractionMode.ADD_PSU;
addPSUBtn.attr("data-active", "true");
} else if (type === "COOLING_ITEM") {
this.roomInteractionMode = RoomInteractionMode.ADD_COOLING_ITEM;
addCoolingItemBtn.attr("data-active", "true");
}
return;
}
let changed = false;
objectTypes.forEach((objectType: any, index: number) => {
if (this.roomInteractionMode === objectType.mode) {
if (changed) {
return;
}
if (type === objectType.type) {
this.roomInteractionMode = RoomInteractionMode.DEFAULT;
this.mapView.hoverLayer.setHoverItemVisibility(false);
objectType.btn.attr("data-active", "false");
} else {
objectTypes.forEach((otherObjectType, otherIndex: number) => {
if (index !== otherIndex) {
if (type === otherObjectType.type) {
this.mapView.hoverLayer.setHoverItemVisibility(true, type);
otherObjectType.btn.attr("data-active", "true");
this.roomInteractionMode = otherObjectType.mode;
}
}
});
}
changed = true;
}
});
}
private addObject(position: IGridPosition, type: string): void {
if (!this.mapView.roomLayer.checkHoverTileValidity(position)) {
return;
}
let tileList = this.mapView.mapController.roomModeController.currentRoom.tiles;
for (let i = 0; i < tileList.length; i++) {
if (tileList[i].position.x === position.x && tileList[i].position.y === position.y) {
if (type === "RACK") {
this.mapController.api.addRack(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom.id, tileList[i].id, {
id: -1,
objectType: "RACK",
name: "",
capacity: 42,
powerCapacityW: 5000
}).then((rack: IRack) => {
tileList[i].object = rack;
tileList[i].objectId = rack.id;
tileList[i].objectType = type;
this.mapView.dcObjectLayer.populateObjectList();
this.mapView.dcObjectLayer.draw();
this.mapView.updateScene = true;
});
} else if (type === "PSU") {
this.mapController.api.addPSU(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom.id, tileList[i].id, this.availablePSUs[0])
.then((psu: IPSU) => {
tileList[i].object = psu;
tileList[i].objectId = psu.id;
tileList[i].objectType = type;
this.mapView.dcObjectLayer.populateObjectList();
this.mapView.dcObjectLayer.draw();
this.mapView.updateScene = true;
});
} else if (type === "COOLING_ITEM") {
this.mapController.api.addCoolingItem(this.mapView.simulation.id,
this.mapView.currentDatacenter.id, this.currentRoom.id, tileList[i].id,
this.availableCoolingItems[0]).then((coolingItem: ICoolingItem) => {
tileList[i].object = coolingItem;
tileList[i].objectId = coolingItem.id;
tileList[i].objectType = type;
this.mapView.dcObjectLayer.populateObjectList();
this.mapView.dcObjectLayer.draw();
this.mapView.updateScene = true;
});
}
break;
}
}
}
/**
* Populates the room-type dropdown element with all available room types
*/
private populateRoomTypeDropdown(): void {
let dropdown = $("#roomtype-select");
this.roomTypes.forEach((type: string, index: number) => {
dropdown.append($('<option>').text(Util.toSentenceCase(type)).val(index));
});
}
/**
* Loads all object types that are allowed in the current room into the menu.
*/
private populateAllowedObjectTypes(): void {
let addObjectsLabel = $("#add-objects-label");
let noObjectsInfo = $("#no-objects-info");
let allowedObjectTypes = this.roomTypeMap[this.currentRoom.roomType];
$(".dc-component-container").addClass("hidden");
if (allowedObjectTypes === undefined || allowedObjectTypes === null || allowedObjectTypes.length === 0) {
addObjectsLabel.addClass("hidden");
noObjectsInfo.removeClass("hidden");
return;
}
addObjectsLabel.removeClass("hidden");
noObjectsInfo.addClass("hidden");
allowedObjectTypes.forEach((type: string) => {
switch (type) {
case "RACK":
$("#add-rack-btn").removeClass("hidden");
break;
case "PSU":
$("#add-psu-btn").removeClass("hidden");
break;
case "COOLING_ITEM":
$("#add-cooling-item-btn").removeClass("hidden");
break;
}
});
}
/**
* Checks whether a given room type can be assigned to the current room based on units already present.
*
* @param newRoomType The new room type to be validated
* @returns {boolean} Whether it is allowed to change the room's type to the new type
*/
private checkRoomTypeLegality(newRoomType: string): boolean {
let legality = true;
this.currentRoom.tiles.forEach((tile: ITile) => {
if (tile.objectType !== undefined && tile.objectType !== null && tile.objectType !== "" &&
this.roomTypeMap[newRoomType].indexOf(tile.objectType) === -1) {
legality = false;
}
});
return legality;
}
}
|