aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates/workflow/viewport-base.html
blob: d219386efc46ecebd45af092fb20a5f636f76475 (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
{% extends "base.html" %}
{% load staticfiles %}

{% load bootstrap4 %}

{% block content %}

<!-- Pagination -->
<div class="row mt-3">
    <div class="col">
        <nav>
            <ul class="pagination d-flex flex-row" id="topPagination">
                <li class="page-item flex-shrink-1 page-control">
                    <a class="page-link" href="#" id="gob" onclick="go('prev')">
                        <i class="fas fa-backward"></i> Back
                    </a>
                </li>
                <li class="page-item flex-grow-1 active">
                    <a class="page-link disabled" href="#">
                        Select <i class="far fa-check-square"></i>
                    </a>
                </li>
                <li class="page-item flex-grow-1">
                    <a class="page-link disabled" href="#">
                        Configure <i class="far fa-square"></i>
                    </a>
                </li>
                <li class="page-item flex-grow-1">
                    <a class="page-link disabled" href="#">
                        Information <i class="far fa-square"></i>
                    </a>
                </li>
                <li class="page-item flex-grow-1">
                    <a class="page-link disabled" href="#">
                        OPNFV <i class="far fa-square"></i>
                    </a>
                </li>
                <li class="page-item flex-grow-1">
                    <a class="page-link disabled" href="#">
                        Confirm <i class="far fa-square"></i>
                    </a>
                </li>
                <li class="page-item flex-shrink-1 page-control">
                    <a class="page-link text-right" href="#" id="gof" onclick="go('next')">
                        Next <i class="fas fa-forward"></i>
                    </a>
                </li>
            </ul>
        </nav>
    </div>
</div>
<!-- Top header -->
<div class="row px-4">
    <div class="col">
        <div id="iframe_header" class="row view-header">
            <div class="col-lg-12">
                <h1 class="d-inline-block" id="view_title"></h1>
                <span class="description text-muted" id="view_desc"></span>
                <p id="view_message"></p>
            </div>
            <script>
                function update_description(title, desc) {
                    document.getElementById("view_title").innerText = title;
                    document.getElementById("view_desc").innerText = desc;
                }

                function update_message(message, stepstatus) {
                    document.getElementById("view_message").innerText = message;
                    document.getElementById("view_message").className = "step_message";
                    document.getElementById("view_message").classList.add("message_" + stepstatus);
                }
            </script>
        </div>
    </div>
    <div class="col-auto align-self-center d-flex">
        <button id="cancel_btn" class="btn btn-danger ml-auto" onclick="cancel_wf()">Cancel</button>
    </div>
</div>
<div class="row d-flex flex-column flex-grow-1">
    <div class="container-fluid d-flex flex-column h-100">
        <div class="row d-flex flex-grow-1 p-4">
            <div class="col-12 d-flex border flex-grow-1 px-0">
                <iframe src="/wf/workflow" class="w-100 h-100" scrolling="yes" id="viewport-iframe"
                    frameBorder="0"></iframe>
            </div>
        </div>
    </div>
</div>
{% csrf_token %}

<script type="text/javascript">
    update_context();
    var step = 0;
    var page_count = 0;
    var context_data = false;

    function go(to) {
        step_on_leave();
        request_leave(to);
    }

    function request_leave(to) {
        $.ajax({
            type: "GET",
            url: "/wf/manager/",
            beforeSend: function (request) {
                request.setRequestHeader("X-CSRFToken",
                    $('input[name="csrfmiddlewaretoken"]').val());
            },
            success: function (data) {
                confirm_permission(to, data);
                update_page(data);
            }
        });
    }

    function confirm_permission(to, data) {
        if (errors_exist(data)) {
            if (to != "prev") {
                return;
            }
        }

        var problem = function () {
            alert("There was a problem");
        }
        //makes an asynch request
        req = new XMLHttpRequest();
        url = "/wf/workflow/?step=" + to;
        req.open("GET", url, true);
        req.onload = function (e) {
            if (req.readyState === 4) {
                if (req.status < 300) {
                    document.getElementById("viewport-iframe").srcdoc = this.responseText;
                } else {
                    problem();
                }
            } else {
                problem();
            }
        }
        req.onerror = problem;
        req.send();
    }

    function step_on_leave() {
        document.getElementById("viewport-iframe").contentWindow.step_on_leave();
    }

    function errors_exist(data) {
        var stat = data['steps'][data['active']]['valid'];
        if (stat >= 100 && stat < 200) {
            return true;
        } else {
            return false;
        }
    }

    function update_context() {
        $.ajax({
            type: "GET",
            url: "/wf/manager/",
            beforeSend: function (request) {
                request.setRequestHeader("X-CSRFToken",
                    $('input[name="csrfmiddlewaretoken"]').val());
            },
            success: function (data) {
                update_page(data);
            }
        });
    }

    function update_page(data) {
        context_data = data;
        update_breadcrumbs(data);
        if (data["workflow_count"] == 1) {
            document.getElementById("cancel_btn").innerText = "Exit Workflow";
        } else {
            document.getElementById("cancel_btn").innerText = "Return to Parent";
        }
    }

    function update_breadcrumbs(meta_json) {
        step = meta_json['active'];
        page_count = meta_json['steps'].length;
        if (step == 0) {
            var btn = document.getElementById("gob");
            btn.classList.add("invisible");
            btn.disabled = true;
        } else {
            var btn = document.getElementById("gob");
            btn.classList.remove("invisible");
            btn.disabled = false;
        }
        if (step == page_count - 1) {
            var btn = document.getElementById("gof");
            btn.classList.add("invisible");
            btn.disabled = true;
        } else {
            var btn = document.getElementById("gof");
            btn.classList.remove("invisible");
            btn.disabled = false;
        }
        //remove all children of breadcrumbs so we can redraw
        $("#topPagination").children().not(".page-control").remove();
        draw_steps(meta_json);
    }

    function draw_steps(meta_json) {
        for (var i = 0; i < meta_json["steps"].length; i++) {
            meta_json["steps"][i]["index"] = i;
            var step_btn = create_step(meta_json["steps"][i], i == meta_json["active"]);
            $("#topPagination li:last-child").before(step_btn);
        }
    }

    function create_step(step_json, active) {
        var step_dom = document.createElement("li");
        // First create the dom object depending on active or not
        if (active) {
            step_dom.className = "topcrumb active";
        } else {
            step_dom.className = "topcrumb";
        }
        $(step_dom).html(`<span class="d-flex align-items-center justify-content-center text-capitalize w-100">${step_json['title']}</span>`)
        var code = step_json['valid'];
        stat = "";
        msg = "";
        if (code < 100) {
            $(step_dom).children().first().append("<i class='ml-2 far fa-square'></i>")
            stat = "";
            msg = "";
        } else if (code < 200) {
            $(step_dom).children().first().append("<i class='ml-2 fas fa-minus-square'></i>")
            stat = "invalid";
            msg = step_json['message'];
        } else if (code < 300) {
            $(step_dom).children().first().append("<i class='ml-2 far fa-check-square'></i>")
            stat = "valid";
            msg = step_json['message'];
        }
        if (step_json['enabled'] == false) {
            step_dom.classList.add("disabled");
        }
        if (active) {
            update_message(msg, stat);
        }

        var step_number = step_json['index'];
        return step_dom;
    }

    function cancel_wf() {
        var form = $("#workflow_pop_form");
        var formData = form.serialize();
        var req = new XMLHttpRequest();
        req.open("POST", "/wf/workflow/finish/", false);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.onerror = function () {
            alert("problem occurred while trying to cancel current workflow");
        }
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                refresh_iframe();
            }
        };
        req.send(formData);
    }

    function refresh_iframe() {
        req = new XMLHttpRequest();
        url = "/wf/workflow/";
        req.open("GET", url, true);
        req.onload = function (e) {
            var doc = document.getElementById("viewport-iframe").contentWindow.document;
            doc.open();
            doc.write(this.responseText);
            doc.close();
        }
        req.send();
    }

    function write_iframe(contents) {
        document.getElementById("viewport-iframe").contentWindow.document.innerHTML = contents;
    }

    function redirect_root() {
        window.location.replace('/wf/');
    }

    function add_wf(type) {
        add_wf_internal(type, false);
    }

    function add_edit_wf(type, target) {
        add_wf_internal(type, target);
    }

    function add_wf_internal(type, itemid) {
        data = {
            "add": type
        };
        if (itemid) {
            data['target'] = itemid;
        }
        $.ajax({
            type: "POST",
            url: "/wf/manager/",
            data: data,
            beforeSend: function (request) {
                request.setRequestHeader("X-CSRFToken",
                    $('input[name="csrfmiddlewaretoken"]').val()
                );
            },
            success: refresh_wf_iframe()
        });
    }

    function refresh_wf_iframe() {
        window.location = window.location;
    }
</script>
<div class="d-none" id="workflow_pop_form_div">
    <form id="workflow_pop_form" action="/wf/workflow/finish/" method="post">
        {% csrf_token %}
    </form>
</div>
{% endblock content %}