$(function() { var TAB_KEY_CODE = 9; var execution_environments; var file_types; var addFileForm = function(event) { event.preventDefault(); var element = $('#dummies').children().first().clone(); var html = $('
').append(element).html().replace(/index/g, new Date().getTime()); $('#files').append(html); $('#files li:last select[name*="file_type_id"]').val(getSelectedExecutionEnvironment().file_type_id); $('#files li:last select').chosen(window.CodeOcean.CHOSEN_OPTIONS); $('body, html').scrollTo('#add-file'); // if we collapse the file forms by default, we need to click on the new element in order to open it. // however, this crashes for more files (if we add several ones by clicking the add button more often), since the elements are probably not correctly added to the files list. //$('#files li:last>div:first>a>div').click(); }; var ajaxError = function() { $.flash.danger({ text: $('#flash').data('message-failure') }); }; var buildCheckboxes = function() { $('tbody tr').each(function(index, element) { var td = $('td.public', element); var checkbox = $('', { checked: td.data('value'), type: 'checkbox' }); td.on('click', function(event) { event.preventDefault(); checkbox.prop('checked', !checkbox.prop('checked')); }); td.html(checkbox); }); }; var discardFile = function(event) { event.preventDefault(); $(this).parents('li').remove(); }; var enableBatchUpdate = function() { $('thead .batch a').on('click', function(event) { event.preventDefault(); if (!$(this).data('toggled')) { $(this).data('toggled', true); $(this).text($(this).data('text')); buildCheckboxes(); } else { performBatchUpdate(); } }); }; var enableInlineFileCreation = function() { $('#add-file').on('click', addFileForm); $('#files').on('click', 'li .discard-file', discardFile); $('form.edit_exercise, form.new_exercise').on('submit', function() { $('#dummies').html(''); }); }; var findFileTypeByFileExtension = function(file_extension) { return _.find(file_types, function(file_type) { return file_type.file_extension === file_extension; }) || {}; }; var getSelectedExecutionEnvironment = function() { return _.find(execution_environments, function(execution_environment) { return execution_environment.id === parseInt($('#exercise_execution_environment_id').val()); }) || {}; }; var highlightCode = function() { $('pre code').each(function(index, element) { hljs.highlightBlock(element); }); }; var inferFileAttributes = function() { $(document).on('change', 'input[type="file"]', function() { var filename = $(this).val().split(/\\|\//g).pop(); var file_extension = '.' + filename.split('.')[1]; var file_type = findFileTypeByFileExtension(file_extension); var name = filename.split('.')[0]; var parent = $(this).parents('li'); parent.find('input[name*="name"]').val(name); parent.find('select[name*="file_type_id"]').val(file_type.id).trigger('chosen:updated'); }); }; var insertTabAtCursor = function(textarea) { var selection_start = textarea.get(0).selectionStart; var selection_end = textarea.get(0).selectionEnd; textarea.val(textarea.val().substring(0, selection_start) + "\t" + textarea.val().substring(selection_end)); textarea.get(0).selectionStart = selection_start + 1; textarea.get(0).selectionEnd = selection_start + 1; }; var observeFileRoleChanges = function() { $(document).on('change', 'select[name$="[role]"]', function() { var is_test_file = $(this).val() === 'teacher_defined_test'; var parent = $(this).parents('.panel'); var fields = parent.find('.test-related-fields'); if (is_test_file) { fields.slideDown(); } else { fields.slideUp(); parent.find('[name$="[feedback_message]"]').val(''); parent.find('[name$="[weight]"]').val(1); } }); }; var overrideTextareaTabBehavior = function() { $('.form-group textarea[name$="[content]"]').on('keydown', function(event) { if (event.which === TAB_KEY_CODE) { event.preventDefault(); insertTabAtCursor($(this)); } }); }; var performBatchUpdate = function() { var jqxhr = $.ajax({ data: { exercises: _.map($('tbody tr'), function(element) { return { id: $(element).data('id'), public: $('.public input', element).prop('checked') }; }) }, dataType: 'json', method: 'PUT' }); jqxhr.done(window.CodeOcean.refresh); jqxhr.fail(ajaxError); }; var toggleCodeHeight = function() { $('code').on('click', function() { $(this).css({ 'max-height': 'initial' }); }); }; var updateFileTemplates = function(fileType) { var jqxhr = $.ajax({ url: '/file_templates/by_file_type/' + fileType + '.json', dataType: 'json' }); jqxhr.done(function(response) { var noTemplateLabel = $('#noTemplateLabel').data('text'); var options = ""; for (var i = 0; i < response.length; i++) { options += "" } $("#code_ocean_file_file_template_id").find('option').remove().end().append($(options)); }); jqxhr.fail(ajaxError); } if ($.isController('exercises')) { if ($('table').isPresent()) { enableBatchUpdate(); } else if ($('.edit_exercise, .new_exercise').isPresent()) { execution_environments = $('form').data('execution-environments'); file_types = $('form').data('file-types'); // new MarkdownEditor('#exercise_instructions'); new MarkdownEditor('#exercise_description'); // todo: add an ace editor for each file enableInlineFileCreation(); inferFileAttributes(); observeFileRoleChanges(); overrideTextareaTabBehavior(); } else if ($('#files.jstree').isPresent()) { var fileTypeSelect = $('#code_ocean_file_file_type_id'); fileTypeSelect.on("change", function() {updateFileTemplates(fileTypeSelect.val())}); updateFileTemplates(fileTypeSelect.val()); } toggleCodeHeight(); if (window.hljs) { highlightCode(); } } });