summaryrefslogtreecommitdiffstats
path: root/modules-available/roomplanner/clientscript.js
blob: 823c212a2520724a922f769bf4930a604232ac0a (plain) (blame)
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
/**
 * Pop-Up to select a machine
 *
 * AUTHOR: Christian Klinger
 * */

/* Map: uuid -> obj */
var machineCache = {};

var selectMachinInitialized = false;

var placedMachines = [];

var $modal, $selectizeSearch;
var currentCallback = false;

function makeCombinedFieldSingle(item)
{
	item.combined = (item.machineuuid + " " + item.hostname + " " + item.clientip + " " + item.macaddr + " " + item.macaddr.replace(/-/g, ':')).toLocaleLowerCase();
	item.sortField = (item.fixedlocationid === null ? 'a' : 'z') + item.hostname;
}

function makeCombinedField(machineArray)
{
   machineArray.forEach(function (v,i,a){
		makeCombinedFieldSingle(machineArray[i]);
   });
   return machineArray;
}

function renderMachineEntry(item, escape) {
    machineCache[item.machineuuid] = item;
    // console.log('rendering ' + item.machineuuid);
    // console.log('used uuids is ');
    // console.log(placedMachines);

    var extraClass = '';
    var extraText = '';
    if (item.otherroom) {
        extraText = ' (in ' + item.otherroom + ')';
        extraClass = 'used';
    } else if (item.fixedlocationid !== null) {
        extraText = ' (already placed)';
        extraClass = 'used';
    }
    return '<div class="machine-entry ' + extraClass +'">'
            //+ ' <div class="machine-logo"><i class="glyphicon glyphicon-hdd"></i></div>'
            + ' <div class="machine-body">'
            + '    <div class="machine-entry-header"> ' + escape(item.hostname) + extraText + '</div>'
            + '          <table>'
            +               '<tr><td>UUID:</td> <td>' +  escape(item.machineuuid) + '</td></tr>'
            +               '<tr><td>MAC: </td> <td>' +  escape(item.macaddr) + '</td></tr>'
            +               '<tr><td>IP:  </td> <td>' +  escape(item.clientip)    + '</td></tr>'
            + '          </table>'
            + '    </div>'
            + '</div>';
}

function renderMachineSelected(item, escape) {
   return '<div>' + escape(item.hostname) + '</div>';
}

var queryCache = {};

function filterCache(key, query) {
    return queryCache[key].filter(function (el) {
       return -1 !== el.combined.indexOf(query);
    });
}

function loadMachines(query, callback) {
    console.log('queryMachines(' + query + ')');
    if (query.length < 2) {
       callback();
       return;
    }
    query = query.toLocaleLowerCase();
    // See if we have a previous query in our cache that is a superset for this one
    for (var k in queryCache) {
        if (query.indexOf(k) !== -1) {
            callback(filterCache(k, query));
            return;
        }
    }
    $.ajax({
        url: '?do=roomplanner&action=getmachines&query=' + encodeURIComponent(query),
        type: 'GET',
        dataType: 'json',
        error: function() {
            console.log('error while doing ajax call');
            callback();
        },
        success: function(json) {
            console.log('success ajax call');
            var machines = makeCombinedField(json.machines);
            // Server cuts off at 100, so only cache if it contains less entries, as
            // the new, more specific query could return previously removed results.
            if (machines.length < 100) {
                queryCache[query] = machines;
            }
            callback(machines);
        }
    });
}

function clearSearchBox() {
    $selectizeSearch[0].selectize.setValue([], true);
    $selectizeSearch[0].selectize.clearCache();
}
function clearSubnetBox() {
    $selectizeSubnet[0].selectize.setValue([], true);
    $selectizeSubnet[0].selectize.clearCache();
}

function initSelectize() {
    if(!selectMachinInitialized) {
        console.log("initializing selectize");
        /* init modal */
        $modal = $('#selectMachineModal');

        /* for the search */
        $selectizeSearch = $('#machineSearchBox').selectize({
            plugins : ["remove_button"],
            valueField: 'machineuuid',
            searchField: "combined",
            openOnFocus: false,
            create: false,
            render : { option : renderMachineEntry, item: renderMachineSelected},
            load: loadMachines,
            maxItems: 1,
            sortField: 'sortField',
            sortDirection: 'asc',
            onChange: clearSubnetBox
        });


        /* for the subnet box */
        $selectizeSubnet = $('#subnetBox').selectize({
            options: subnetMachines,
            plugins : ["remove_button"],
            valueField: 'machineuuid',
            searchField: "combined",
            openOnFocus:  true,
            create: false,
            render : { option : renderMachineEntry, item: renderMachineSelected},
            maxItems: 1,
            sortField: 'sortField',
            sortDirection: 'asc',
            onChange: clearSearchBox
        });

        $('#selectMachineButton').on('click', onBtnSelect);

        selectMachinInitialized = true;
    }
}
function onBtnSelect() {
        /* check which one has a value */
        console.assert($selectizeSubnet.length === 1);
        console.assert($selectizeSearch.length === 1);

        var bySubnet = machineCache[$selectizeSubnet[0].selectize.getValue()];
        var bySearch = machineCache[$selectizeSearch[0].selectize.getValue()];

        var value = !bySubnet ? bySearch : bySubnet;
        value.fixedlocationid = -1;
        makeCombinedFieldSingle(value);

        var result = {muuid: value.machineuuid, ip: value.clientip, mac_address : value.macaddr, hostname: value.hostname};

        currentCallback(result);
        currentCallback = null;

        $modal.modal('hide');
        clearSubnetBox();
        clearSearchBox();
}

function onPcDelete(muuid) {
    var value = machineCache[muuid];
    if (!value) {
        subnetMachines.forEach(function (v, i, a) {
            if (subnetMachines[i] && subnetMachines[i].machineuuid === muuid) {
                value = subnetMachines[i];
            }
        });
        if (!value) return;
    }
    value.fixedlocationid = null;
    makeCombinedFieldSingle(value);
}

/* to be called from berryous' code */
function selectMachine(usedUuids, callback) {
    initSelectize();
    currentCallback = callback;
    placedMachines =  usedUuids;
    $modal.modal('show');
    $modal.one('hidden.bs.modal', function () {
        if (currentCallback) {
            currentCallback(false);
        }
    });
}