// Menu Highlight Scripts const menuItems = document.getElementsByClassName('nav-link'); for(let i = 0; i < menuItems.length; i++) { if(menuItems[i].pathname == window.location.pathname) { menuItems[i].classList.add('active'); } } const dropdownItems = document.getElementsByClassName('dropdown-item'); for(let i = 0; i< dropdownItems.length; i++) { if(dropdownItems[i].pathname == window.location.pathname) { dropdownItems[i].classList.add('active'); $( "#" + dropdownItems[i].id ).closest( '.dropdown' ).find('.dropdown-toggle').addClass('active'); } } // General Post Method Form Processing Script $('form.form-post').submit(function(event) { var $form = $(this); var data = $form.serialize(); var url = $(this).attr('action'); var rel_success = $(this).data('rel-success'); $.ajax({ url: url, type: 'POST', data: data, dataType: 'json', success: function(response) { window.location.href = rel_success; }, error: function(response) { error_response(response); } }); event.preventDefault(); }); // Form Upload Questions - Special case, needs to handle files. $('form[name=form-upload-questions]').submit(function(event) { var $form = $(this); var data = new FormData($form[0]); var file = $('input[name=data_file]')[0].files[0] data.append('file', file) $.ajax({ url: window.location.pathname, type: 'POST', data: data, processData: false, contentType: false, success: function(response) { window.location.reload(); }, error: function(response) { error_response(response); } }); event.preventDefault(); }); // Edit and Delete Test Button Handlers $('.delete-test').click(function(event) { let _id = $(this).data('_id') $.ajax({ url: `/admin/tests/delete/`, type: 'POST', data: JSON.stringify({'_id': _id}), contentType: 'application/json', success: function(response) { window.location.href = '/admin/tests/'; }, error: function(response){ error_response(response); }, }); event.preventDefault(); }); // Edit Dataset Button Handlers $('.edit-question-dataset').click(function(event) { var filename = $(this).data('filename'); var action = $(this).data('action'); var disabled = $(this).hasClass('disabled'); if ( !disabled ) { $.ajax({ url: `/admin/settings/questions/${action}/`, type: 'POST', data: JSON.stringify({'filename': filename}), contentType: 'application/json', success: function(response) { window.location.reload(); }, error: function(response){ error_response(response); }, }); }; event.preventDefault(); }); function error_response(response) { var alert = $("#alert-box"); alert.html(''); if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) { alert.html(` `); } else if (response.responseJSON.error instanceof Array) { for (var i = 0; i < response.responseJSON.error.length; i ++) { alert.html(` `); } } } // Dismiss Cookie Alert $('#dismiss-cookie-alert').click(function(event){ $.ajax({ url: '/cookies/', type: 'GET', data: { time: Date.now() }, dataType: 'json', success: function(response){ console.log(response); }, error: function(response){ console.log(response); } }) event.preventDefault(); }) // Script for Resutlt Actions $('.result-action-buttons').click(function(event){ var _id = $(this).data('_id') if ($(this).data('result-action') == 'generate') { $.ajax({ url: '/admin/certificate/', type: 'POST', data: JSON.stringify({'_id': _id}), contentType: 'application/json', dataType: 'html', success: function(response) { var display_window = window.open(); display_window.document.write(response); }, error: function(response){ error_response(response); }, }); } else { var action = $(this).data('result-action') $.ajax({ url: window.location.href, type: 'POST', data: JSON.stringify({'_id': _id, 'action': action}), contentType: 'application/json', success: function(response) { if (action == 'delete') { window.location.href = '/admin/results/'; } else window.location.reload(); }, error: function(response){ error_response(response); }, }); } });