summaryrefslogtreecommitdiffstats
path: root/dashboard/src/templates/dashboard/multiple_select_filter_widget.html
blob: 3a7e14820b41b8b46694531569d6c44891290eb3 (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
<style>
.object_class_wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    border: 0px;
}

.class_grid_wrapper {
    border: 0px;
    text-align: center;
    border-right: 1px;
    border-style: solid;
    border-color: grey;
}

.class_grid_wrapper:last-child {
    border-right: none;
}

.grid_wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr;
}

.grid-item {
    cursor: pointer;
    border: 1px solid #cccccc;
    border-radius: 5px;
    margin: 20px;
    height: 200px;
    padding: 7px;
    transition: border-color ease-in-out .1s,box-shadow ease-in-out .1s;
    box-shadow: 0 1px 1px rgba(0,0,0,.075);
}

.selected_node {
    border-color: #40c640;
    box-shadow: 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(109, 243, 76, 0.6);
    transition: border-color ease-in-out .1s,box-shadow ease-in-out .1s;
}

.disabled_node {
    cursor: not-allowed;
    background-color: #EFEFEF;
}

.disabled_node:hover {}

.cleared_node {
    background-color: #FFFFFF;
}

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

.dropdown_item {
    border: 1px;
    border-style: solid;
    border-color: lightgray;
    border-radius: 5px;
    margin: 20px;
    padding: 2px;
    grid-column: 1;
    display: grid;
    grid-template-columns: 1fr 3fr 1fr;
    justify-items: center;
}

.dropdown_item > button {
    margin: 2px;
    justify-self: end;
}

.dropdown_item > h5 {
    margin: auto;
}

.dropdown_item > input {
    padding: 7px;
    margin: 2px;
    width: 90%;
}

#dropdown_wrapper {
    display: grid;
    grid-template-columns: 4fr 5fr;
}
</style>

<input name="filter_field" id="filter_field" type="hidden"/>
<div id="grid_wrapper" class="grid_wrapper">
{% for object_class, object_list in display_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="{{ 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 %}Add{% else %}Select{% endif %}</button>
            </div>
        {% endfor %}
        </div>
    </div>
{% endfor %}
</div>

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

<script>
var initialized = false;
var inputs = [];
var graph_neighbors = {{ neighbors|safe }};
var filter_items = {{ filter_items|safe }};
var result = {};
var dropdown_count = 0;

{% if initial_value %}

var initial_value = {{ initial_value|safe }};


function make_selection( initial_data ){
    try_init();
    for(var item_class in initial_data) {
        var selected_items = initial_data[item_class];
        for( var node_id in selected_items ){
            var node = filter_items[node_id];
            var selection_data = selected_items[node_id]
            if( selection_data.selected ) {
                select(node);
                markAndSweep(node);
                updateResult(node);
            }
            if(node['multiple']){
                make_multiple_selection(node, selection_data);
            }
        }
    }
}

function make_multiple_selection(node, selection_data){
    prepop_data = selection_data.values;
    for(var k in prepop_data){
        var div = add_item_prepopulate(node, prepop_data[k]);
        updateObjectResult(node, div.id, prepop_data[k]);
    }
}

make_selection({{initial_value|safe}});

{% endif %}

function markAndSweep(root){
    for(var i in filter_items) {
        node = filter_items[i];
        node['marked'] = true; //mark all nodes
    }

    toCheck = [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
            var neighbors = graph_neighbors[node.id];
            for(var i in neighbors) {
                var neighId = neighbors[i];
                var neighbor = filter_items[neighId];
                toCheck.push(neighbor);
            }
        }
    }

    //now remove all nodes still marked
    for(var i in filter_items){
        node = filter_items[i];
        if(node['marked']){
            disable_node(node);
        }
    }
}

function process(node) {
    if(node['selected']) {
        markAndSweep(node);
    }
    else {  //TODO: make this not dumb
        var selected = []
        //remember the currently selected, then reset everything and reselect one at a time
        for(var nodeId in filter_items) {
            node = filter_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');
}

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');
}

function disable_node(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');
}

function processClick(id){
    try_init();
    var node = filter_items[id];
    if(!node['selectable'])
        return;

    if(node['multiple']){
        return processClickMultiple(node);
    } else {
        return processClickSingle(node);
    }
}

function processClickSingle(node){
    node['selected'] = !node['selected']; //toggle on click

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

function processClickMultiple(node){
    select(node);
    var div = add_node(node);
    process(node);
    updateObjectResult(node, div.id, "");
}

function add_node(node){
    return add_item_prepopulate(node, false);
}

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 make_remove_button(div, node){
    var button = document.createElement("BUTTON");
    button.type = "button";
    button.appendChild(document.createTextNode("Remove"));
    button.classList.add("btn-danger");
    button.classList.add("btn");
    button.onclick = function(){
        remove_dropdown(div.id, node.id);
    }
    return button;
}

function make_input(div, node, prepopulate){
    var input = document.createElement("INPUT");
    input.type = node.form.type;
    input.name = node.id + node.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 = node.form.placeholder;
    inputs.push(input);
    input.onchange = function() { updateObjectResult(node, div.id, input.value); restrictchars(this); };
    input.oninput = function() { restrictchars(this); };
    if(prepopulate)
        input.value = prepopulate;
    return input;
}

function add_item_prepopulate(node, prepopulate){
    var div = document.createElement("DIV");
    div.id = "dropdown_" + dropdown_count;
    div.classList.add("dropdown_item");
    dropdown_count++;
    var label = document.createElement("H5")
    label.appendChild(document.createTextNode(node['name']))
    div.appendChild(label);
    div.appendChild(make_input(div, node, prepopulate));
    div.appendChild(make_remove_button(div, node));
    document.getElementById("dropdown_wrapper").appendChild(div);
    return div;
}

function remove_dropdown(div_id, node_id){
    var div = document.getElementById(div_id);
    var node = filter_items[node_id]
    var parent = div.parentNode;
    div.parentNode.removeChild(div);
    delete result[node.class][node.id]['values'][div.id];

    //checks if we have removed last item in class
    if(jQuery.isEmptyObject(result[node.class][node.id]['values'])){
        delete result[node.class][node.id];
        clear(node);
    }
}
function updateResult(node){
    try_init();
    if(!node['multiple']){
        result[node.class][node.id] = {selected: node.selected, id: node.model_id}
        if(!node.selected)
            delete result[node.class][node.id];
    }
}

function updateObjectResult(node, childKey, childValue){
    try_init();
    if(!result[node.class][node.id])
        result[node.class][node.id] = {selected: true, id: node.model_id, values: {}}

    result[node.class][node.id]['values'][childKey] = childValue;
}

function try_init() {
    if(initialized) return;
    for(nodeId in filter_items) {
        var element = document.getElementById(nodeId);
        var node = filter_items[nodeId];
        result[node.class] = {}
        }
    initialized = true;
}

</script>