<commit_before>var $body;
var $modal;
var $modalCover;
var $newComponentItem;
var $newComponentStep1;
var $newComponentStep2;
var $changedInput;
var $spinner;

$(document).ready(function() {
    $body = $('body');
    $modal = $('.history-modal');
    $modalCover = $('<div class="modal-cover">');
    $body.append($modalCover);
    $newComponentItem = $('.new-component-item');
    $newComponentTypePicker = $('.new-component');
    $newComponentTemplatePickers = $('.new-component-templates');
    $newComponentButton = $('.new-component-button');
    $spinner = $('<span class="spinner-in-field-icon"></span>');
    $body.bind('keyup', onKeyUp);

    $('.expand-collapse-icon').bind('click', toggleSubmodules);
    $('.visibility-options').bind('change', setVisibility);

    $('.unit-history ol a').bind('click', showHistoryModal);
    $modal.bind('click', hideModal);
    $modalCover.bind('click', hideHistoryModal);
    $modalCover.bind('click', hideModal);
    $('.assets .upload-button').bind('click', showUploadModal);
    $('.upload-modal .close-button').bind('click', hideModal);

    $body.on('click', '.embeddable-xml-input', function(){ $(this).select(); });

    $('.unit .item-actions .delete-button').bind('click', deleteUnit);
    $('.new-unit-item').bind('click', createNewUnit);
    $('.save-subsection').bind('click', saveSubsection);

    // autosave when a field is updated on the subsection page
    $body.on('keyup', '.subsection-display-name-input, .unit-subtitle, .policy-list-value', checkForNewValue);
    $('.subsection-display-name-input, .unit-subtitle, .policy-list-name, .policy-list-value').each(function(i) {
        this.val = $(this).val();
    });
    $("#start_date, #start_time, #due_date, #due_time").bind('change', autosaveInput);
    $('.sync-date, .remove-date').bind('click', autosaveInput);

    // making the unit list sortable
    $('.sortable-unit-list').sortable({
        axis: 'y',
        handle: '.drag-handle',
        update: onUnitReordered
    });

    // expand/collapse methods for optional date setters
    $('.set-date').bind('click', showDateSetter);
    $('.remove-date').bind('click', removeDateSetter);
    // add new/delete section
    $('.new-courseware-section-button').bind('click', addNewSection);
    $('.delete-section-button').bind('click', deleteSection);
    
    // add new/delete subsection
    $('.new-subsection-item').bind('click', addNewSubsection);
    $('.delete-subsection-button').bind('click', deleteSubsection);
    // add/remove policy metadata button click handlers
    $('.add-policy-data').bind('click', addPolicyMetadata);
    $('.remove-policy-data').bind('click', removePolicyMetadata);
    $body.on('click', '.policy-list-element .save-button', savePolicyMetadata);
    $body.on('click', '.policy-list-element .cancel-button', cancelPolicyMetadata);

    $('.sync-date').bind('click', syncReleaseDate);

    // import form setup
    $('.import .file-input').bind('change', showImportSubmit);
    $('.import .choose-file-button, .import .choose-file-button-inline').bind('click', function(e) {
        e.preventDefault();
        $('.import .file-input').click();
    });

    // Subsection reordering
    $('.subsection-list > ol').sortable({
        axis: 'y',
        handle: '.section-item .drag-handle',
        update: onSubsectionReordered
    });

    // Section reordering
    $('.courseware-overview').sortable({
        axis: 'y',
        handle: 'header .drag-handle',
        update: onSectionReordered
    });

    $('.new-course-button').bind('click', addNewCourse);

    // section name editing
    $('.section-name').bind('click', editSectionName);
    $('.edit-section-name-cancel').bind('click', cancelEditSectionName);
    $('.edit-section-name-save').bind('click', saveEditSectionName);

    // section date setting
    $('.set-publish-date').bind('click', setSectionScheduleDate);
    $('.edit-section-start-cancel').bind('click', cancelSetSectionScheduleDate);
    $('.edit-section-start-save').bind('click', saveSetSectionScheduleDate);

    // modal upload asset dialog. Bind it in the initializer otherwise multiple hanlders will get registered causing
    // pretty wacky stuff to happen
    $('.file-input').bind('change', startUpload);
    $('.upload-modal .choose-file-button').bind('click', showFileSelectionMenu);

    $body.on('click', '.section-published-date .edit-button', editSectionPublishDate);
    $body.on('click', '.section-published-date .schedule-button', editSectionPublishDate);
    $body.on('click', '.edit-subsection-publish-settings .save-button', saveSetSectionScheduleDate);
    $body.on('click', '.edit-subsection-publish-settings .cancel-button', hideModal)
    $body.on('change', '.edit-subsection-publish-settings .start-date', function() {
        if($('.edit-subsection-publish-settings').find('.start-time').val() == '') {
            $('.edit-subsection-publish-settings').find('.start-time').val('12:00am');    
        }
    });
    $('.edit-subsection-publish-settings').on('change', '.start-date, .start-time', function() {
        $('.edit-subsection-publish-settings').find('.save-button').show();
    });
});

function editSectionPublishDate(e) {
    e.preventDefault();
    $modal = $('.edit-subsection-publish-settings').show();
    $modal = $('.edit-subsection-publish-settings').show();
    $modal.attr('data-id', $(this).attr('data-id'));
    $modal.find('.start-date').val($(this).attr('data-date'));
    $modal.find('.start-time').val($(this).attr('data-time'));
    if($modal.find('.start-date').val() == '' && $modal.find('.start-time').val() == '') {
        $modal.find('.save-button').hide();
    }    
    $modal.find('.section-name').html('"' + $(this).closest('.courseware-section').find('.section-name-span').text() + '"');
    $modalCover.show();
}

function showImportSubmit(e) {
    var filepath = $(this).val();
    if(filepath.substr(filepath.length - 6, 6) == 'tar.gz') {
        $('.error-block').hide();
        $('.file-name').html($(this).val());
        $('.file-name-block').show();
        $('.import .choose-file-button').hide();
        $('.submit-button').show();
        $('.progress').show();
    } else {
        $('.error-block').html('File format not supported. Please upload a file with a <code>tar.gz</code> extension.').show();
    }
}

function syncReleaseDate(e) {
    e.preventDefault();
    $(this).closest('.notice').hide();
    $("#start_date").val("");
    $("#start_time").val("");
}

function addPolicyMetadata(e) {
    e.preventDefault();
    var template =$('#add-new-policy-element-template > li');
    var newNode = template.clone();
    var _parent_el = $(this).parent('ol:.policy-list');
    newNode.insertBefore('.add-policy-data');
    $('.remove-policy-data').bind('click', removePolicyMetadata);
    newNode.find('.policy-list-name').focus();
}

function editPolicyMetadata(e) {
    e.preventDefault();

    var $policyElement = $(this).parents('.policy-list-element');
    $policyElement.data('currentValues', [$policyElement.find('.policy-list-name').val(), $policyElement.find('.policy-list-value').val()]);
    $policyElement.addClass('new-policy-list-element').addClass('editing');
}

function savePolicyMetadata(e) {
    e.preventDefault();

    var $policyElement = $(this).parents('.policy-list-element');
    $('.save-subsection').click();
    $policyElement.removeClass('new-policy-list-element');
    $policyElement.find('.policy-list-name').attr('disabled', 'disabled');
    $policyElement.removeClass('editing');
}

function cancelPolicyMetadata(e) {
    e.preventDefault();

    var $policyElement = $(this).parents('.policy-list-element');
    if(!$policyElement.hasClass('editing')) {
        $policyElement.remove();
    } else {
        $policyElement.removeClass('new-policy-list-element');
        $policyElement.find('.policy-list-name').val($policyElement.data('currentValues')[0]);
        $policyElement.find('.policy-list-value').val($policyElement.data('currentValues')[1]);
    }
    $policyElement.removeClass('editing');
}

function removePolicyMetadata(e) {
    e.preventDefault();

    if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!'))<commit_msg>removed unused edit policy function<commit_after>var $body;
var $modal;
var $modalCover;
var $newComponentItem;
var $newComponentStep1;
var $newComponentStep2;
var $changedInput;
var $spinner;

$(document).ready(function() {
    $body = $('body');
    $modal = $('.history-modal');
    $modalCover = $('<div class="modal-cover">');
    $body.append($modalCover);
    $newComponentItem = $('.new-component-item');
    $newComponentTypePicker = $('.new-component');
    $newComponentTemplatePickers = $('.new-component-templates');
    $newComponentButton = $('.new-component-button');
    $spinner = $('<span class="spinner-in-field-icon"></span>');
    $body.bind('keyup', onKeyUp);

    $('.expand-collapse-icon').bind('click', toggleSubmodules);
    $('.visibility-options').bind('change', setVisibility);

    $('.unit-history ol a').bind('click', showHistoryModal);
    $modal.bind('click', hideModal);
    $modalCover.bind('click', hideHistoryModal);
    $modalCover.bind('click', hideModal);
    $('.assets .upload-button').bind('click', showUploadModal);
    $('.upload-modal .close-button').bind('click', hideModal);

    $body.on('click', '.embeddable-xml-input', function(){ $(this).select(); });

    $('.unit .item-actions .delete-button').bind('click', deleteUnit);
    $('.new-unit-item').bind('click', createNewUnit);
    $('.save-subsection').bind('click', saveSubsection);

    // autosave when a field is updated on the subsection page
    $body.on('keyup', '.subsection-display-name-input, .unit-subtitle, .policy-list-value', checkForNewValue);
    $('.subsection-display-name-input, .unit-subtitle, .policy-list-name, .policy-list-value').each(function(i) {
        this.val = $(this).val();
    });
    $("#start_date, #start_time, #due_date, #due_time").bind('change', autosaveInput);
    $('.sync-date, .remove-date').bind('click', autosaveInput);

    // making the unit list sortable
    $('.sortable-unit-list').sortable({
        axis: 'y',
        handle: '.drag-handle',
        update: onUnitReordered
    });

    // expand/collapse methods for optional date setters
    $('.set-date').bind('click', showDateSetter);
    $('.remove-date').bind('click', removeDateSetter);
    // add new/delete section
    $('.new-courseware-section-button').bind('click', addNewSection);
    $('.delete-section-button').bind('click', deleteSection);
    
    // add new/delete subsection
    $('.new-subsection-item').bind('click', addNewSubsection);
    $('.delete-subsection-button').bind('click', deleteSubsection);
    // add/remove policy metadata button click handlers
    $('.add-policy-data').bind('click', addPolicyMetadata);
    $('.remove-policy-data').bind('click', removePolicyMetadata);
    $body.on('click', '.policy-list-element .save-button', savePolicyMetadata);
    $body.on('click', '.policy-list-element .cancel-button', cancelPolicyMetadata);

    $('.sync-date').bind('click', syncReleaseDate);

    // import form setup
    $('.import .file-input').bind('change', showImportSubmit);
    $('.import .choose-file-button, .import .choose-file-button-inline').bind('click', function(e) {
        e.preventDefault();
        $('.import .file-input').click();
    });

    // Subsection reordering
    $('.subsection-list > ol').sortable({
        axis: 'y',
        handle: '.section-item .drag-handle',
        update: onSubsectionReordered
    });

    // Section reordering
    $('.courseware-overview').sortable({
        axis: 'y',
        handle: 'header .drag-handle',
        update: onSectionReordered
    });

    $('.new-course-button').bind('click', addNewCourse);

    // section name editing
    $('.section-name').bind('click', editSectionName);
    $('.edit-section-name-cancel').bind('click', cancelEditSectionName);
    $('.edit-section-name-save').bind('click', saveEditSectionName);

    // section date setting
    $('.set-publish-date').bind('click', setSectionScheduleDate);
    $('.edit-section-start-cancel').bind('click', cancelSetSectionScheduleDate);
    $('.edit-section-start-save').bind('click', saveSetSectionScheduleDate);

    // modal upload asset dialog. Bind it in the initializer otherwise multiple hanlders will get registered causing
    // pretty wacky stuff to happen
    $('.file-input').bind('change', startUpload);
    $('.upload-modal .choose-file-button').bind('click', showFileSelectionMenu);

    $body.on('click', '.section-published-date .edit-button', editSectionPublishDate);
    $body.on('click', '.section-published-date .schedule-button', editSectionPublishDate);
    $body.on('click', '.edit-subsection-publish-settings .save-button', saveSetSectionScheduleDate);
    $body.on('click', '.edit-subsection-publish-settings .cancel-button', hideModal)
    $body.on('change', '.edit-subsection-publish-settings .start-date', function() {
        if($('.edit-subsection-publish-settings').find('.start-time').val() == '') {
            $('.edit-subsection-publish-settings').find('.start-time').val('12:00am');    
        }
    });
    $('.edit-subsection-publish-settings').on('change', '.start-date, .start-time', function() {
        $('.edit-subsection-publish-settings').find('.save-button').show();
    });
});

function editSectionPublishDate(e) {
    e.preventDefault();
    $modal = $('.edit-subsection-publish-settings').show();
    $modal = $('.edit-subsection-publish-settings').show();
    $modal.attr('data-id', $(this).attr('data-id'));
    $modal.find('.start-date').val($(this).attr('data-date'));
    $modal.find('.start-time').val($(this).attr('data-time'));
    if($modal.find('.start-date').val() == '' && $modal.find('.start-time').val() == '') {
        $modal.find('.save-button').hide();
    }    
    $modal.find('.section-name').html('"' + $(this).closest('.courseware-section').find('.section-name-span').text() + '"');
    $modalCover.show();
}

function showImportSubmit(e) {
    var filepath = $(this).val();
    if(filepath.substr(filepath.length - 6, 6) == 'tar.gz') {
        $('.error-block').hide();
        $('.file-name').html($(this).val());
        $('.file-name-block').show();
        $('.import .choose-file-button').hide();
        $('.submit-button').show();
        $('.progress').show();
    } else {
        $('.error-block').html('File format not supported. Please upload a file with a <code>tar.gz</code> extension.').show();
    }
}

function syncReleaseDate(e) {
    e.preventDefault();
    $(this).closest('.notice').hide();
    $("#start_date").val("");
    $("#start_time").val("");
}

function addPolicyMetadata(e) {
    e.preventDefault();
    var template =$('#add-new-policy-element-template > li');
    var newNode = template.clone();
    var _parent_el = $(this).parent('ol:.policy-list');
    newNode.insertBefore('.add-policy-data');
    $('.remove-policy-data').bind('click', removePolicyMetadata);
    newNode.find('.policy-list-name').focus();
}

function savePolicyMetadata(e) {
    e.preventDefault();

    var $policyElement = $(this).parents('.policy-list-element');
    $('.save-subsection').click();
    $policyElement.removeClass('new-policy-list-element');
    $policyElement.find('.policy-list-name').attr('disabled', 'disabled');
    $policyElement.removeClass('editing');
}

function cancelPolicyMetadata(e) {
    e.preventDefault();

    var $policyElement = $(this).parents('.policy-list-element');
    if(!$policyElement.hasClass('editing')) {
        $policyElement.remove();
    } else {
        $policyElement.removeClass('new-policy-list-element');
        $policyElement.find('.policy-list-name').val($policyElement.data('currentValues')[0]);
        $policyElement.find('.policy-list-value').val($policyElement.data('currentValues')[1]);
    }
    $policyElement.removeClass('editing');
}

function removePolicyMetadata(e) {
    e.preventDefault();

    if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!'))
{'fraction_non_alphanumeric': 0.21214374225526642, 'fraction_numerical': 0.0012391573729863693, 'mean_word_length': 3.793881793881794, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4400664', 'n_tokens_mistral': 4854, 'n_tokens_neox': 4809, 'n_words': 759}