summaryrefslogblamecommitdiffstats
path: root/modules-available/roomplanner/clientscript.js
blob: b7bd5d555ec01b9a15767a59924370128a5c8b98 (plain) (tree)
1
2
3
4
5
6
7
8
9





                                   
                      

                  

                                







































































                                                                                                      
                                                     

                           
                                                     

 






                                                                            
 
                                                           
 



                                       
                                         


                                                     


                                                                              

                                                                                                                           
 
                                
 


                             
 
 




                                             
 
 
/**
 * Pop-Up to select a machine
 *
 * Copyright 2016 Christian Klinger
 * */

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

selectMachinInitialized = false;



function renderMachineEntry(item, escape) {
    machineCache[item.machineuuid] = item;
    return '<div class="machine-entry">'
            //+ ' <div class="machine-logo"><i class="glyphicon glyphicon-hdd"></i></div>'
            + ' <div class="machine-body">'
            + '    <div class="machine-entry-header"> ' + escape(item.hostname) + '</div>'
            + '          <table class="table table-sm">'
            +               '<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 loadMachines(query, callback) {
    console.log('queryMachines(' + query + ')');
    if (query.length < 2) return callback();
    $.ajax({
        url: '?do=roomplanner&action=getmachines&query=' + encodeURIComponent(query),
        type: 'GET',
        error: function() {
            console.log('error while doing ajax call');
            callback();
        },
        success: function(res) {
            console.log('success ajax call');
            var json = JSON.parse(res);
            json.machines.forEach(function (v,i,a){
                a[i].combined = v.machineuuid + " " + v.hostname + " " + v.clientip + " " + v.macaddr;
            });
            return callback(json.machines);
        }
    });
}


var searchSettings = {
    plugins : ["remove_button"],
    valueField: 'machineuuid',
    searchField: "combined",
    //labelField: "combined",
    openOnFocus: false,
    create: false,
    render : { option : renderMachineEntry, item: renderMachineEntry},
    load: loadMachines,
    maxItems: 1,
    sortField: 'clientip',
    sortDirection: 'asc',
    onChange: clearSubnetBox

}

var subnetSettings = {
    plugins : ["remove_button"],
    valueField: 'machineuuid',
    searchField: "combined",
    //labelField: "combined",
    openOnFocus: false,
    create: false,
    render : { option : renderMachineEntry, item: renderMachineEntry},
    load: loadMachines,
    maxItems: 1,
    sortField: 'clientip',
    sortDirection: 'asc',
    onChange: clearSearchBox

}

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

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

        $('#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 === undefined || bySubnet == "") ? bySearch : bySubnet;
        var result = {muuid: value.machineuuid, ip: value.clientip, mac_address : value.macaddr, hostname: value.hostname};

        currentCallback(result);

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

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