diff --git a/RIGS/hs.py b/RIGS/hs.py index db6c0177..b7da7638 100644 --- a/RIGS/hs.py +++ b/RIGS/hs.py @@ -100,6 +100,7 @@ class EventChecklistEdit(generic.UpdateView): ec = models.EventChecklist.objects.get(pk=pk) context['event'] = ec.event context['edit'] = True + context['vehicles_length'] = range(len(self.object.vehicles)) return context class EventChecklistCreate(generic.CreateView): @@ -131,6 +132,7 @@ class EventChecklistCreate(generic.CreateView): epk = self.kwargs.get('pk') event = models.Event.objects.get(pk=epk) context['event'] = event + context['vehicles_length'] = range(2) return context def get_success_url(self): diff --git a/RIGS/models.py b/RIGS/models.py index 4de900e9..d22d35aa 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -646,7 +646,7 @@ class EventChecklist(models.Model, RevisionMixin): event = models.OneToOneField('Event', on_delete=models.CASCADE) # General - power_mic = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='checklist', blank=True, null=True, + power_mic = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='checklist', verbose_name="Power MIC", on_delete=models.CASCADE, help_text="Who is the Power MIC?") vehicles = models.JSONField(help_text="List vehicles and their drivers", default=dict, null=False) diff --git a/RIGS/static/js/autocompleter.js b/RIGS/static/js/autocompleter.js index 10967478..eaa34554 100644 --- a/RIGS/static/js/autocompleter.js +++ b/RIGS/static/js/autocompleter.js @@ -1 +1 @@ -$(document).ready((function(){function e(e){targetObject=$("#"+e.attr("id")+"-update"),update_url=$("option:selected",e).data("update_url"),""==update_url?targetObject.attr("disabled",!0):(targetObject.attr("href",update_url),targetObject.attr("disabled",!1))}clearSelectionLabel="(no selection)",$(".selectpicker").each((function(){var t={ajax:{url:$(this).data("sourceurl"),type:"GET",dataType:"json",data:{term:"{{{q}}}"}},locale:{emptyTitle:""},clearOnEmpty:!1,preprocessData:function(e){var t,a=e.length,l=[];if(l.push({text:clearSelectionLabel,value:"",data:{update_url:"",subtext:""}}),a)for(t=0;t").attr("value","").text(clearSelectionLabel).data("update_url","")),$(this).selectpicker().ajaxSelectPicker(t),$(this).change((function(){e($(this))})),e($(this))})),$("#modal").on("hide.bs.modal",(function(e){null!=modaltarget&&""!=modalobject&&function(e,t,a,l){e.find("option").remove(),e.append($("").attr("value",t).text(a).data("update_url",l)),e.selectpicker("render"),e.selectpicker("refresh"),e.selectpicker("val",t),e.change()}($(modaltarget),modalobject[0].pk,modalobject[0].fields.name,modalobject[0].update_url)}))})); \ No newline at end of file +function changeSelectedValue(e,t,a,l){e.find("option").remove(),e.append($("").attr("value",t).text(a).data("update_url",l)),e.selectpicker("render"),e.selectpicker("refresh"),e.selectpicker("val",t),e.change()}function refreshUpdateHref(e){targetObject=$("#"+e.attr("id")+"-update"),update_url=$("option:selected",e).data("update_url"),""==update_url?targetObject.attr("disabled",!0):(targetObject.attr("href",update_url),targetObject.attr("disabled",!1))}function initPicker(e){console.log("called!");var t={ajax:{url:e.data("sourceurl"),type:"GET",dataType:"json",data:{term:"{{{q}}}"}},locale:{emptyTitle:""},clearOnEmpty:!1,preprocessData:function(e){var t,a=e.length,l=[];if(l.push({text:clearSelectionLabel,value:"",data:{update_url:"",subtext:""}}),a)for(t=0;t").attr("value","").text(clearSelectionLabel).data("update_url","")),e.selectpicker().ajaxSelectPicker(t),e.change((function(){refreshUpdateHref(e)})),refreshUpdateHref(e)}$(document).ready((function(){clearSelectionLabel="(no selection)",$(".selectpicker").each((function(){initPicker($(this))})),$("#modal").on("hide.bs.modal",(function(e){null!=modaltarget&&""!=modalobject&&changeSelectedValue($(modaltarget),modalobject[0].pk,modalobject[0].fields.name,modalobject[0].update_url)}))})); \ No newline at end of file diff --git a/RIGS/static/js/src/autocompleter.js b/RIGS/static/js/src/autocompleter.js index 214472fa..cc1c9938 100644 --- a/RIGS/static/js/src/autocompleter.js +++ b/RIGS/static/js/src/autocompleter.js @@ -1,41 +1,37 @@ -$(document).ready(function() { -clearSelectionLabel = '(no selection)'; - function changeSelectedValue(obj,pk,text,update_url) { //Pass in JQuery object and new parameters //console.log('Changing selected value'); obj.find('option').remove(); //Remove all the available options obj.append( //Add the new option $("") - .attr("value",pk) - .text(text) - .data('update_url',update_url) - ); - obj.selectpicker('render'); //Re-render the UI - obj.selectpicker('refresh'); //Re-render the UI - obj.selectpicker('val', pk); //Set the new value to be selected - obj.change(); //Trigger the change function manually + .attr("value",pk) + .text(text) + .data('update_url',update_url) + ); + obj.selectpicker('render'); //Re-render the UI + obj.selectpicker('refresh'); //Re-render the UI + obj.selectpicker('val', pk); //Set the new value to be selected + obj.change(); //Trigger the change function manually } function refreshUpdateHref(obj) { - //console.log('Refreshing Update URL'); + //console.log('Refreshing Update URL'); targetObject = $('#'+obj.attr('id')+'-update'); - update_url = $('option:selected', obj).data('update_url'); + update_url = $('option:selected', obj).data('update_url'); - if (update_url=="") { //Probably "clear selection" has been chosen - // console.log('Trying to disable'); - targetObject.attr('disabled', true); - } else { - targetObject.attr('href', update_url); - targetObject.attr('disabled', false); - } + if (update_url=="") { //Probably "clear selection" has been chosen + // console.log('Trying to disable'); + targetObject.attr('disabled', true); + } else { + targetObject.attr('href', update_url); + targetObject.attr('disabled', false); + } } - -$(".selectpicker").each(function() { - +function initPicker(obj) { + console.log('called!'); var options = { ajax: { - url: $(this).data('sourceurl'), + url: obj.data('sourceurl'), type: 'GET', dataType: 'json', // Use "{{{q}}}" as a placeholder and Ajax Bootstrap Select will @@ -76,29 +72,32 @@ $(".selectpicker").each(function() { } }; - $(this).prepend($("") + obj.prepend($("") .attr("value",'') .text(clearSelectionLabel) .data('update_url','')); //Add "clear selection" option - $(this).selectpicker().ajaxSelectPicker(options); //Initiaise selectPicker + obj.selectpicker().ajaxSelectPicker(options); //Initiaise selectPicker - $(this).change(function(){ //on change, update the edit button href + obj.change(function(){ //on change, update the edit button href // console.log('Selectbox Changed'); - refreshUpdateHref($(this)); + refreshUpdateHref(obj); }); - refreshUpdateHref($(this)); //Ensure href is correct at the beginning + refreshUpdateHref(obj); //Ensure href is correct at the beginning +} -}); +$(document).ready(function() { + clearSelectionLabel = '(no selection)'; -//When update/edit modal box submitted + $(".selectpicker").each(function(){initPicker($(this))}); + + //When update/edit modal box submitted $('#modal').on('hide.bs.modal', function (e) { if (modaltarget != undefined && modalobject != "") { //Update the selector with new values changeSelectedValue($(modaltarget),modalobject[0]['pk'],modalobject[0]['fields']['name'],modalobject[0]['update_url']); } }); - }); diff --git a/RIGS/templates/event_checklist_form.html b/RIGS/templates/event_checklist_form.html index bb797f00..6ae3b8d8 100644 --- a/RIGS/templates/event_checklist_form.html +++ b/RIGS/templates/event_checklist_form.html @@ -63,12 +63,25 @@ var name = current.name.split('_')[0]; result[index] = result[index] || {}; var nested = result[index] || {}; - nested[name] = current.value; + if(current.value): + nested[name] = current.value; result[index] = nested; return result; }, {}); $({{form.vehicles.id_for_label}}).val(JSON.stringify(post)); }); + $('#vehicle-add').on('click', function (event) { + event.preventDefault(); + var newID = Number($('#vehiclest').attr('data-pk')); + $('#vehicles_new').clone().attr('style', "").attr('id', 'vehicles_' + newID).appendTo('#vehiclest'); + $('#vehicles_' + newID).find('select,input').attr('name', function(){ + return this.name.split('_')[0] + '_' + newID; + }).attr('data-serialize', 'true'); + $('#vehicles_' + newID).find('select').addClass('selectpicker'); + $('#vehicles_' + newID).find('.selectpicker').selectpicker('refresh'); + $(".selectpicker").each(function(){initPicker($(this))}); + $('#vehiclest').attr('data-pk', newID + 1); + }); }); {% endblock %} @@ -111,7 +124,7 @@ + value="{{ form.vehicles.value }}"/>
| Driver | ||||
|---|---|---|---|---|
| - | + | + | - + | + {# TODO Delete functionality#} |