summaryrefslogtreecommitdiff
path: root/src/scripts/controllers/modes/object.ts
blob: e922433e8faac4e064b146dc3d04ba9ec3ae8b98 (plain)
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
import {AppMode, MapController, InteractionLevel} from "../mapcontroller";
import {MapView} from "../../views/mapview";
import * as $ from "jquery";


/**
 * Class responsible for rendering object mode and handling its UI interactions.
 */
export class ObjectModeController {
    public currentObject: IDCObject;
    public objectType: string;
    public currentRack: IRack;
    public currentPSU: IPSU;
    public currentCoolingItem: ICoolingItem;
    public currentObjectTile: ITile;

    private mapController: MapController;
    private mapView: MapView;


    constructor(mapController: MapController) {
        this.mapController = mapController;
        this.mapView = this.mapController.mapView;
    }

    /**
     * Performs the necessary setup actions and enters object mode.
     *
     * @param tile A reference to the tile containing the rack that was selected.
     */
    public enterMode(tile: ITile) {
        this.currentObjectTile = tile;
        this.mapView.grayLayer.currentObjectTile = tile;
        this.currentObject = tile.object;
        this.objectType = tile.objectType;

        // Show the corresponding sub-menu of object mode
        $(".object-sub-menu").hide();

        switch (this.objectType) {
            case "RACK":
                $("#rack-sub-menu").show();
                this.currentRack = <IRack>this.currentObject;
                $("#rack-name-input").val(this.currentRack.name);
                this.populateNodeList();

                break;

            case "PSU":
                $("#psu-sub-menu").show();
                this.currentPSU = <IPSU>this.currentObject;

                break;

            case "COOLING_ITEM":
                $("#cooling-item-sub-menu").show();
                this.currentCoolingItem = <ICoolingItem>this.currentObject;

                break;
        }

        this.mapView.grayLayer.drawRackLevel();
        MapController.hideAndShowMenus("#object-menu");
        this.scrollToBottom();

        if (this.mapController.appMode === AppMode.SIMULATION) {
            this.mapController.simulationController.transitionFromRoomToRack();
        }
    }

    /**
     * Leaves object mode and transfers to room mode.
     */
    public goToRoomMode() {
        this.mapController.interactionLevel = InteractionLevel.ROOM;
        this.mapView.grayLayer.hideRackLevel();
        MapController.hideAndShowMenus("#room-menu");
        this.mapController.roomModeController.enterMode(this.mapController.roomModeController.currentRoom);

        if (this.mapController.appMode === AppMode.SIMULATION) {
            this.mapController.simulationController.transitionFromRackToRoom();
        }
    }

    /**
     * Connects all DOM event listeners to their respective element targets.
     */
    public setupEventListeners() {
        // Handler for saving a new rack name
        $("#rack-name-save").on("click", () => {
            this.currentRack.name = $("#rack-name-input").val();
            this.mapController.api.updateRack(this.mapView.simulation.id,
                this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                this.mapController.objectModeController.currentObjectTile.id, this.currentRack).then(
                () => {
                    this.mapController.showInfoBalloon("Rack name saved", "info");
                });
        });

        let nodeListContainer = $(".node-list-container");

        // Handler for the 'add' button of each machine slot of the rack
        nodeListContainer.on("click", ".add-node", (event: JQueryEventObject) => {
            // Convert the DOM element index to a JS array index
            let index = this.currentRack.machines.length - $(event.target).closest(".node-element").index() - 1;

            // Insert an empty machine at the selected position
            this.mapController.api.addMachineToRack(this.mapView.simulation.id,
                this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                this.mapController.objectModeController.currentObjectTile.id, {
                    id: -1,
                    rackId: this.currentRack.id,
                    position: index,
                    tags: [],
                    cpuIds: [],
                    gpuIds: [],
                    memoryIds: [],
                    storageIds: []
                }).then((data: IMachine) => {
                this.currentRack.machines[index] = data;
                this.populateNodeList();
                this.mapView.dcObjectLayer.draw();
            });

            event.stopPropagation();
        });

        // Handler for the 'remove' button of each machine slot of the rack
        nodeListContainer.on("click", ".remove-node", (event: JQueryEventObject) => {
            let target = $(event.target);
            MapController.showConfirmDeleteDialog("machine", () => {
                // Convert the DOM element index to a JS array index
                let index = this.currentRack.machines.length - target.closest(".node-element").index() - 1;

                this.mapController.api.deleteMachine(this.mapView.simulation.id,
                    this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                    this.mapController.objectModeController.currentObjectTile.id,
                    index).then(() => {
                    this.currentRack.machines[index] = null;
                    this.populateNodeList();
                    this.mapView.dcObjectLayer.draw();
                });
            });
            event.stopPropagation();
        });

        // Handler for every node element, triggering node mode
        nodeListContainer.on("click", ".node-element", (event: JQueryEventObject) => {
            let domIndex = $(event.target).closest(".node-element").index();
            let index = this.currentRack.machines.length - domIndex - 1;
            let machine = this.currentRack.machines[index];

            if (machine != null) {
                this.mapController.interactionLevel = InteractionLevel.NODE;

                // Gray out the other nodes
                $(event.target).closest(".node-list-container").children(".node-element").each((nodeIndex: number, element: Element) => {
                    if (nodeIndex !== domIndex) {
                        $(element).children(".node-element-overlay").removeClass("hidden");
                    } else {
                        $(element).children(".node-element-overlay").addClass("hidden");
                    }
                });

                this.mapController.nodeModeController.enterMode(machine);
            }
        });

        // Handler for rack deletion button
        $("#rack-deletion").on("click", () => {
            MapController.showConfirmDeleteDialog("rack", () => {
                this.mapController.api.deleteRack(this.mapView.simulation.id,
                    this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                    this.mapController.objectModeController.currentObjectTile.id).then(() => {
                    this.currentObjectTile.object = undefined;
                    this.currentObjectTile.objectType = undefined;
                    this.currentObjectTile.objectId = undefined;
                    this.mapView.redrawMap();
                    this.goToRoomMode();
                });
            });
        });

        // Handler for PSU deletion button
        $("#psu-deletion").on("click", () => {
            MapController.showConfirmDeleteDialog("PSU", () => {
                this.mapController.api.deletePSU(this.mapView.simulation.id,
                    this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                    this.mapController.objectModeController.currentObjectTile.id).then(() => {
                    this.mapView.redrawMap();
                    this.goToRoomMode();
                });
                this.currentObjectTile.object = undefined;
                this.currentObjectTile.objectType = undefined;
                this.currentObjectTile.objectId = undefined;
            });
        });

        // Handler for Cooling Item deletion button
        $("#cooling-item-deletion").on("click", () => {
            MapController.showConfirmDeleteDialog("cooling item", () => {
                this.mapController.api.deleteCoolingItem(this.mapView.simulation.id,
                    this.mapView.currentDatacenter.id, this.mapController.roomModeController.currentRoom.id,
                    this.mapController.objectModeController.currentObjectTile.id).then(() => {
                    this.mapView.redrawMap();
                    this.goToRoomMode();
                });
                this.currentObjectTile.object = undefined;
                this.currentObjectTile.objectType = undefined;
                this.currentObjectTile.objectId = undefined;
            });
        });
    }

    public updateNodeComponentOverlays(): void {
        if (this.currentRack === undefined || this.currentRack.machines === undefined) {
            return;
        }

        for (let i = 0; i < this.currentRack.machines.length; i++) {
            if (this.currentRack.machines[i] === null) {
                continue;
            }

            let container = this.mapController.appMode === AppMode.CONSTRUCTION ? ".construction" : ".simulation";
            let element = $(container + " .node-element").eq(this.currentRack.machines.length - i - 1);
            if (this.currentRack.machines[i].cpus.length !== 0) {
                element.find(".overlay-cpu").addClass("hidden");
            } else {
                element.find(".overlay-cpu").removeClass("hidden");
            }
            if (this.currentRack.machines[i].gpus.length !== 0) {
                element.find(".overlay-gpu").addClass("hidden");
            } else {
                element.find(".overlay-gpu").removeClass("hidden");
            }
            if (this.currentRack.machines[i].memories.length !== 0) {
                element.find(".overlay-memory").addClass("hidden");
            } else {
                element.find(".overlay-memory").removeClass("hidden");
            }
            if (this.currentRack.machines[i].storages.length !== 0) {
                element.find(".overlay-storage").addClass("hidden");
            } else {
                element.find(".overlay-storage").removeClass("hidden");
            }
        }
    }

    /**
     * Dynamically generates and inserts HTML code for every node in the current rack.
     */
    private populateNodeList(): void {
        let type, content;
        let container = $(".node-list-container");

        // Remove any previously present node elements
        container.children().remove(".node-element");

        for (let i = 0; i < this.currentRack.machines.length; i++) {
            // Depending on whether the current machine slot is filled, allow removing or adding a new machine by adding
            // the appropriate button next to the machine slot
            type = (this.currentRack.machines[i] == null ? "glyphicon-plus add-node" : "glyphicon-remove remove-node");
            content =
                '<div class="node-element" data-id="' + (this.currentRack.machines[i] === null ?
                    "" : this.currentRack.machines[i].id) + '">' +
                '  <div class="node-element-overlay hidden"></div>' +
                '  <a class="node-element-btn glyphicon ' + type + '" href="javascript:void(0)"></a>' +
                '  <div class="node-element-number">' + (i + 1) + '</div>';
            if (this.currentRack.machines[i] !== null) {
                content +=
                    '<div class="node-element-content">' +
                    '  <img src="img/app/node-cpu.png">' +
                    '  <img src="img/app/node-gpu.png">' +
                    '  <img src="img/app/node-memory.png">' +
                    '  <img src="img/app/node-storage.png">' +
                    '  <img src="img/app/node-network.png">' +
                    '  <div class="icon-overlay overlay-cpu hidden"></div>' +
                    '  <div class="icon-overlay overlay-gpu hidden"></div>' +
                    '  <div class="icon-overlay overlay-memory hidden"></div>' +
                    '  <div class="icon-overlay overlay-storage hidden"></div>' +
                    '  <div class="icon-overlay overlay-network"></div>' +
                    '</div>';
            }
            content += '</div>';
            // Insert the generated machine slot into the DOM
            container.prepend(content);
        }

        this.updateNodeComponentOverlays();
    }

    private scrollToBottom(): void {
        let scrollContainer = $('.node-list-container');
        scrollContainer.scrollTop(scrollContainer[0].scrollHeight);
    }
}