Cleanup Quick Booking Page
[pharos-tools.git] / dashboard / src / templates / booking / quick_deploy.html
1 {% extends "base.html" %}
2 {% load staticfiles %}
3 {% load bootstrap3 %}
4 {% block content %}
5 <style>
6     .grid_container {
7         display: grid;
8         grid-template-columns: repeat(12, 1fr);
9         padding: 30px;
10     }
11     .grid_element {
12         border-radius: 3px;
13         box-shadow: 0px 0px 7px 0px rgba(0,0,0,0.75);
14         margin: 10px;
15         padding: 7px;
16     }
17     .grid_element_wide {
18         grid-column-start: span 12;
19     }
20     .grid_element_half {
21         grid-column-start: span 6;
22     }
23     .grid_element_1third {
24         grid-column-start: span 4;
25     }
26     .grid_element_2third {
27         grid-column-start: span 8;
28     }
29 </style>
30 {% bootstrap_form_errors form type='non_fields' %}
31 <form id="quick_booking_form" action="/booking/quick/" method="POST" class="form">
32 {% csrf_token %}
33 <div class="grid_container">
34 <div class="grid_element host_select_pane grid_element_wide">
35 <p>Please select a host type you wish to book. Only available types are shown.</p>
36 {% bootstrap_field form.filter_field show_label=False %}
37 </div>
38 <div class="grid_element booking_info_pane grid_element_1third">
39     {% bootstrap_field form.purpose %}
40     {% bootstrap_field form.project %}
41     {% bootstrap_field form.length %}
42     <p style="display:inline;">Days: </p><output id="daysout" style="display:inline;">0</output>
43     <script>
44         document.getElementById("id_length").setAttribute("oninput", "daysout.value=this.value");
45         document.getElementById("daysout").value = document.getElementById("id_length").value;
46     </script>
47 </div>
48 <div class="grid_element collaborator_pane grid_element_1third">
49         <label>Collaborators</label>
50         {{ form.users }}
51 </div>
52 <div class="grid_element_1third">
53     <div class="configuration_pane grid_element">
54         {% bootstrap_field form.hostname %}
55         {% bootstrap_field form.image %}
56     </div>
57     <div class="configuration_pane grid_element">
58         <strong>OPNFV: (Optional)</strong>
59         {% bootstrap_field form.installer %}
60         {% bootstrap_field form.scenario %}
61     </div>
62 </div>
63 </div>
64 <script type="text/javascript">
65     var normalize = function(data)
66     {
67         //converts the top level keys in data to map to lists
68         var normalized = {}
69         for( var key in data ){
70             normalized[key] = [];
71             for( var subkey in data[key] ){
72                 normalized[key].push(data[key][subkey]);
73             }
74         }
75         return normalized;
76     }
77     var update_page_contents = function(response)
78     {
79         document.open();
80         document.write(response);
81         document.close();
82     }
83
84     //form hamdler code
85     submit_form = function()
86     {
87         //altered from initial prototype: form submits automatically,
88         //but needs formatting for multiple select field
89         var data = normalize(result);
90         data = JSON.stringify(data);
91         document.getElementById("filter_field").value = data;
92     }
93
94     var sup_image_dict = {{ image_filter|safe }};
95     var sup_installer_dict = {{ installer_filter|safe }};
96     var sup_scenario_dict = {{ scenario_filter|safe }};
97
98     function imageHider() {
99         var data = normalize(result);
100         var drop = document.getElementById("id_image");
101         for( var i=0; i < drop.length; i++ )
102         {
103             if ( drop.options[i].text == '---------' )
104             {
105                 drop.selectedIndex = i;
106             }
107         }
108
109         $('#id_image').children().hide();
110
111         var empty_map = {}
112
113         for ( var i=0; i < drop.childNodes.length; i++ )
114         {
115             var image_object = sup_image_dict[drop.childNodes[i].value];
116             if( image_object ) //weed out empty option
117             {
118                 var lab_pk = ""
119                 for( var j in data["labs"][0] )
120                 {
121                     if( j in {} ) { continue; }
122                     else { lab_pk = j; break; }
123                 }
124                 var host_pk = "";
125                 for( var j in data["hosts"][0] )
126                 {
127                     if( j in {} ) { continue; }
128                     else { host_pk = j; break; }
129                 }
130                 if( image_object.host_profile == host_pk && image_object.lab == lab_pk )
131                 {
132                     drop.childNodes[i].style.display = "inherit";
133                 }
134             }
135         }
136     }
137
138     $('#id_image').children().hide();
139     $('#id_installer').children().hide();
140     $('#id_scenario').children().hide();
141
142
143     Array.from(document.getElementsByClassName("grid-item-select-btn")).forEach(function(element) {
144         element.addEventListener('click', imageHider);
145     });
146
147     function installerHider() {
148         dropFilter("id_installer", sup_installer_dict, "id_image");
149         scenarioHider();
150     }
151     document.getElementById('id_image').addEventListener('change', installerHider);
152
153     function scenarioHider() {
154         dropFilter("id_scenario", sup_scenario_dict, "id_installer");
155     }
156     document.getElementById('id_installer').addEventListener('change', scenarioHider);
157
158     function dropFilter(target, target_filter, master) {
159         ob = document.getElementById(target);
160
161         for(var i=0; i<ob.options.length; i++) {
162             if ( ob.options[i].text == '---------' ) {
163                 ob.selectedIndex = i;
164                 }
165         }
166
167         targ_id = "#" + target;
168         $(targ_id).children().hide();
169         var drop = document.getElementById(master);
170         var opts = target_filter[drop.options[drop.selectedIndex].value];
171         if (!opts) {
172             opts = {};
173         }
174         var emptyMap = {}
175
176         var map = Object.create(null);
177         for (var i = 0; i < opts.length; i++) {
178             var j = opts[i];
179             map[j] = true;
180         }
181
182         for (var i = 0; i < document.getElementById(target).childNodes.length; i++) {
183             if (document.getElementById(target).childNodes[i].value in opts && !(document.getElementById(target).childNodes[i].value in emptyMap) ) {
184                 document.getElementById(target).childNodes[i].style.display = "inherit";
185             }
186         }
187     }
188 </script>
189     <button onclick="submit_form();" class="btn btn-success">Confirm</button>
190 </form>
191 <script>
192     //context vars
193     var prefill_host_selection = "{{host_select_field_prefill_data|default:""|safe}}";
194     var prefill_purpose = "{{prefill_purpose|default:""|safe}}";
195     var prefill_project = "{{prefill_project|default:""|safe}}";
196     var prefill_hostname = "{{prefill_hostname|default:""|safe}}";
197
198     //to handle prefill
199     function prefill_host_select_field(data)
200     {
201         //
202         if(data)
203         {
204             make_selection(data);
205         }
206     }
207
208     //call init functions
209     prefill_host_select_field(prefill_host_selection);
210 </script>
211 {% endblock %}