summaryrefslogtreecommitdiffstats
path: root/dashboard/src/templates/dashboard/multiple_select_filter_widget.html
blob: d1929ef0374d00fd35ea928610a8413b950d4d9e (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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<style>
.object_class_wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    border: 0px;
}
.class_grid_wrapper {
    border: 0px;
    border-left: 1px;
    border-right: 1px;
    border-style: solid;
    border-color: grey;
    text-align: center;
}
.grid_wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr;
}
.grid-item {
    cursor: pointer;
    border:2px;
    border-style:none;
    border-color:black;
    border-radius: 5px;
    margin:20px;
    height: 200px;
    padding: 7px;
    box-shadow: 0px 0px 7px 0px rgba(0,0,0,0.75);
    transition-property: box-shadow, background-color;
    transition-duration: .2s;
}

.grid-item:hover {
    box-shadow: 0px 0px 14px 0px rgba(0,0,0,0.75);
    transition-property: box-shadow;
    transition-duration: .2s;

}

.selected_node {
    box-shadow: 0px 0px 14px 0px rgba(0,0,0,0.75);
    background-color: #CCECD7;
    transition-property: background-color;
    transition-duration: .2s;
}

.disabled_node {
    cursor: not-allowed;
    background-color: #EFEFEF;
    box-shadow: 0px 0px 1px 0px rgba(0,0,0,0.75);
    transition-property: box-shadow;
    transition-duration: .2s;
}

.disabled_node:hover {
    box-shadow: 0px 0px 1px 0px rgba(0,0,0,0.75);
}

.cleared_node {
    background-color: #FFFFFF;
}

.grid-item-header
{
    font-weight: bold;
    font-size: 20px;
    margin-top: 10px;
}

</style>
<input name="filter_field" id="filter_field" type="hidden"/>
<div id="grid_wrapper" class="grid_wrapper">
{% for object_class, object_list in filter_objects %}
<div class="class_grid_wrapper">
    <div style="display:inline-block;margin:auto">
        <h4>{{object_class}}</h4>
    </div>
    <div id="{{object_class}}" class="object_class_wrapper">
    {% for obj in object_list %}
    <div id="object_parent">
        <div id="{{ obj.id|default:'not_provided' }}" class="grid-item">
            <p class="grid-item-header">{{obj.name}}</p>
            <p class="grid-item-description">{{obj.description}}</p>
            <button type="button" class="btn btn-success grid-item-select-btn" onclick="processClick('{{obj.id}}', {% if obj.multiple %}true{% else %}false{% endif %});">{% if obj.multiple %}Add{% else %}Select{% endif %}</button>
        </div>
        <input type="hidden" name="{{obj.id}}_selected" value="false"/>
    </div>
    {% endfor %}
    </div>
    </div>
{% endfor %}
</div>

<div id="dropdown_wrapper">
</div>

<script>
var initialized = false;
var mapping = {{ mapping|safe }};
var items = {{ items|safe }};
var result = {};
var selection = {{selection_data|default_if_none:"null"|safe}};
var dropdown_count = 0;

{% if selection_data %}
make_selection({{selection_data|safe}});
{% endif %}

function make_selection( selection_data ){
    if(!initialized) {
        init();
    }
    for(var k in selection_data) {
        selected_items = selection_data[k];
        for( var item in selected_items ){
            var node = items[item];
            if(!node['multiple']){
                var input_value = selected_items[item];
                if( input_value != 'false' ) {
                    select(node);
                    markAndSweep(node);
                }
                var div = document.getElementById(item)
                var input = div.parentNode.getElementsByTagName("input")[0]
                input.value = input_value;
                updateResult(item);
            } else {
                make_multiple_selection(selected_items, item);
            }
        }
    }
}

function make_multiple_selection(data, item_class){
    var node = items[item_class];
    select(node);
    markAndSweep(node);
    prepop_data = data[item_class];
    for(var i=0; i<prepop_data.length; i++){
        var div = add_item_prepopulate(node, prepop_data[i]);
        updateObjectResult(div);
    }
}

function markAndSweep(root){
    for(var nodeId in items) {
        node = items[nodeId];
        node['marked'] = true; //mark all nodes
        //clears grey background of everything
    }

    toCheck = [];
    toCheck.push(root);

    while(toCheck.length > 0){
        node = toCheck.pop();
        if(!node['marked']) {
            //already visited, just continue
            continue;
        }
        node['marked'] = false; //mark as visited
        if(node['follow'] || node == root){ //add neighbors if we want to follow this node (labs)
            var mappingId = node.id
            var neighbors = mapping[mappingId];
            for(var neighId in neighbors) {
                neighId = neighbors[neighId];
                var neighbor = items[neighId];
                toCheck.push(neighbor);
            }
        }
    }

    //now remove all nodes still marked
    for(var nodeId in items){
        node = items[nodeId];
        if(node['marked']){
            disable(node);
        }
    }
}

function process(node) {
    if(node['selected']) {
        markAndSweep(node);
    }
    else {
        var selected = []
        //remember the currently selected, then reset everything and reselect one at a time
        for(var nodeId in items) {
            node = items[nodeId];
            if(node['selected']) {
                selected.push(node);
            }
            clear(node);

        }
        for(var i=0; i<selected.length; i++) {
            node = selected[i];
            select(node);
            markAndSweep(selected[i]);
        }
    }
}

function select(node) {
    elem = document.getElementById(node['id']);
    node['selected'] = true;
    elem.classList.remove('cleared_node')
    elem.classList.remove('disabled_node')
    elem.classList.add('selected_node')
    var input = elem.parentNode.getElementsByTagName("input")[0];
    input.disabled = false;
    input.value = true;
}

function clear(node) {
    elem = document.getElementById(node['id']);
    node['selected'] = false;
    node['selectable'] = true;
    elem.classList.add('cleared_node')
    elem.classList.remove('disabled_node')
    elem.classList.remove('selected_node')
    elem.parentNode.getElementsByTagName("input")[0].disabled = true;
}

function disable(node) {
    elem = document.getElementById(node['id']);
    node['selected'] = false;
    node['selectable'] = false;
    elem.classList.remove('cleared_node')
    elem.classList.add('disabled_node')
    elem.classList.remove('selected_node')
    elem.parentNode.getElementsByTagName("input")[0].disabled = true;
}

function processClick(id, multiple){
    if(!initialized){
        init();
    }
    var element = document.getElementById(id);
    var node = items[id];
    if(!node['selectable']){
        return;
    }
    if(multiple){
        return processClickMultipleObject(node);
    }
    node['selected'] = !node['selected']; //toggle on click

    if(node['selected']) {
        select(node);
    }
    else {
        clear(node);
    }
    process(node);
    updateResult(id);
}

function processClickMultipleObject(node){
    select(node);
    add_item(node);
    process(node);
}

function add_item(node){
    return add_item_prepopulate(node, {});
}

inputs = []

function restrictchars(input)
{
    if( input.validity.patternMismatch )
    {
        input.setCustomValidity("Only alphanumeric characters (a-z, A-Z, 0-9), underscore(_), and hyphen (-) are allowed");
        input.reportValidity();
    }

    input.value = input.value.replace(/([^A-Za-z0-9-_.])+/g, "");

    checkunique(input);
}

function checkunique(tocheck)
{
    val = tocheck.value;
    for( var i = 0; i < inputs.length; i++ )
    {
        if( inputs[i].value == val && inputs[i] != tocheck)
        {
            tocheck.setCustomValidity("All hostnames must be unique");
            tocheck.reportValidity();
            return;
        }
    }
    tocheck.setCustomValidity("");
}

function add_item_prepopulate(node, prepopulate){
    inputs = [];
    var div = document.createElement("DIV");
    div.class = node['id'];
    div.id = "dropdown_" + dropdown_count;
    dropdown_count++;
    var label = document.createElement("H5");
    label.style['display'] = 'inline';
    label.appendChild(document.createTextNode(node['name']));
    div.appendChild(label);
    for(var i=0; i<node['forms'].length; i++){
        form = node['forms'][i];
        var input = document.createElement("INPUT");
        input.type = form['type'];
        input.name = form['name'];
        input.pattern = "(?=^.{1,253}$)(^([A-Za-z0-9-_]{1,62}\.)*[A-Za-z0-9-_]{1,63})";
        input.title = "Only alphanumeric characters (a-z, A-Z, 0-9), underscore(_), and hyphen (-) are allowed"
        input.placeholder = form['placeholder'];
        inputs.push(input);
        input.onchange = function() { updateObjectResult(div); restrictchars(this); };
        input.oninput = function() { restrictchars(this); };
        if(form['name'] in prepopulate){
            input.value = prepopulate[form['name']];
        }
        div.appendChild(input);
    }
    //add class id to dropdown object
    var hiddenInput = document.createElement("INPUT");
    hiddenInput.type = "hidden";
    hiddenInput.name = "class";
    hiddenInput.value = node['id'];
    div.appendChild(hiddenInput);
    button = document.createElement("BUTTON");
    button.onclick = function(){
        remove_dropdown(div.id);
    }
    button.type = "button";
    button.appendChild(document.createTextNode("Remove"));
    div.appendChild(button);
    document.getElementById("dropdown_wrapper").appendChild(div);
    updateObjectResult(div);
    return div;
}

function remove_dropdown(id){
    var div = document.getElementById(id);
    var parent = div.parentNode;
    div.parentNode.removeChild(div);
    //checks if we have removed last item in class
    var deselect_class = true;
    var div_inputs = div.getElementsByTagName("input");
    var div_class = div_inputs[div_inputs.length-1].value;
    var result_class = document.getElementById(div_class).parentNode.parentNode.id;
    delete result[result_class][div.id];
    for(var i=0; i<parent.children.length; i++){
        var inputs = parent.children[i].getElementsByTagName("input");
        var object_class = "";
        for(var k=0; k<inputs.length; k++){
            if(inputs[k].name == "class"){
                object_class = inputs[k].value;
            }
        }
        if(object_class == div_class){
            deselect_class = false;
        }
    }
    if(deselect_class){
        clear(items[div_class]);
    }
}

function updateResult(nodeId){
    if(!initialized){
        init();
    }
    if(!items[nodeId]['multiple']){
        var node = document.getElementById(nodeId);
        var value = {}
        value[nodeId] = node.parentNode.getElementsByTagName("input")[0].value;
        result[node.parentNode.parentNode.id] = {};
        result[node.parentNode.parentNode.id][nodeId] = value;
    }
}

function updateObjectResult(parentElem){
    node_type = document.getElementById(parentElem.class).parentNode.parentNode.id;
    input = {};
    inputs = parentElem.getElementsByTagName("input");
    for(var i in inputs){
        var e = inputs[i];
        input[e.name] = e.value;
    }
    result[node_type][parentElem.id] = input;
}

function init() {
    for(nodeId in items) {
        element = document.getElementById(nodeId);
        node = items[nodeId];
        result[element.parentNode.parentNode.id] = {}
        }
    initialized = true;
}

</script>