{% load i18n %} var tag = this; tag.requestResources = function() { tag.parent.requestResources(); } function buildFormDataFile(form) { var formData = new FormData(); formData.append('_file', form.file.files[0]); formData.append('_data', JSON.stringify( { "title": form.title.value.trim(), "narrative": [{ "description": form.narrative.value.trim(), "language": form.language.value }], "categories": $('#categories').val() ? $('#categories').val() : [], "language": form.language.value, "organisation": "{{ user_org_id }}", "activity": form.activity.value, "iso_date": moment(form.iso_date.value).format('YYYY-MM-DD'), "url": form.url.value.trim(), "types": [], } )); return formData; } function buildFormDataURL(form) { return JSON.stringify( { "title": form.title.value.trim(), "narrative": [{ "description": form.narrative.value.trim(), "language": form.language.value }], "categories": $('#categories').val() ? $('#categories').val() : [], "language": form.language.value, "organisation": "{{ user_org_id }}", "activity": form.activity.value, "iso_date": moment(form.iso_date.value).format('YYYY-MM-DD'), "url": form.url.value.trim(), "types": [], } ); } function show_error_msg(target) { $(target+'.validation-error').removeClass('hidden'); } function validateForm() { var inputs = document.forms["res-creation-form"].elements; // hide existing error notices before checking $('.validation-error:not(.hidden)').addClass('hidden'); if (inputs.title.value == "" || (inputs.url.value == '' && inputs.file.files.length == 0)) { // missing one of the required fields... // set individual fields with an error class if (inputs.title.value == "") { show_error_msg('.title'); } if (inputs.url.value == '' && inputs.file.files.length == 0) { show_error_msg('.url'); show_error_msg('.file'); } // shake all visible validation error fields $('.validation-error:not(.hidden)').shake(); return false; } else { return true; } } tag.submit_resource = function(e) { e.preventDefault(); if (validateForm()) { var form = document.forms["res-creation-form"].elements; if (form.file.files.length == 0) { var xhr = $.ajax({ type: 'POST', url: "/en/editor/documents/", data: buildFormDataURL(form), contentType: "application/json; charset=utf-8", dataType: "json", headers: { 'X-CSRFTOKEN': '{{csrf_token}}' }, }).done(function (res) { tag.parent.buildHeaders(res); }).fail(function (err) { // log the error with Sentry?? }).always(function() { tag.closeAndClear(); }); } else { var xhr = $.ajax({ type: 'POST', url: "/en/editor/documents/", data: buildFormDataFile(form), cache: false, processData: false, contentType: false, headers: { 'X-CSRFTOKEN': '{{csrf_token}}' }, }).done(function (res) { tag.parent.buildHeaders(res); }).fail(function (err) { // log the error with Sentry?? }).always(function() { tag.closeAndClear(); }); } } }; tag.closeAndClear = function() { // close the modal $('#resource-add-modal').modal("hide"); // clear the form $('#res-creation-form')[0].reset(); // hide existing error notices before checking $('.validation-error:not(.hidden)').addClass('hidden'); }