<commit_before>(function($, window, document, undefined){

var selection = {}; // course_id => crns
window.selection = selection;

var updateFuse = new Utils.Fuse({
    delay: 250,
    execute: function(){
        var parameters = {};
        $.each(selection, function(cid, crns){
            parameters['selected_course_' + cid] = "checked";
            $.each(crns, function(i, crn){
                parameters['selected_course_' + cid + '_' + crn] = "checked";
            });
        });
        console.log('post', parameters);
        $.ajax(Utils.selectURL(), {
            type: "post",
            data: $.param(parameters),
            complete: function(){
            }
        });
    }
});

// lower level operations to modify the selection, should automatically
// handle syncing with the server
function addSectionToSelection(courseID, crn){
    courseID = parseInt(courseID, 10);
    crn = parseInt(crn, 10);
    updateFuse.stop();
    if(!selection[courseID])
        selection[courseID] = [];
    if($.inArray(crn, selection[courseID]) !== -1)
        return;
    selection[courseID].push(crn);
    updateFuse.start();
}

function removeSectionFromSelection(courseID, crn){
    courseID = parseInt(courseID, 10);
    crn = parseInt(crn, 10);
    if(!selection[courseID])
        return;
    updateFuse.stop();
    if($.inArray(crn, selection[courseID]) === -1)
        return;
    selection[courseID].removeItem(crn);
    if(selection[courseID].length === 0)
        delete selection[courseID];
    updateFuse.start();
}

function syncSelection(){
    $.ajax(Utils.selectionURL(), {
        type: 'GET',
        dataType: 'text',
        success: function(content, status, request){
            var sections = Utils.json(content);
            updateFuse.freeze();
            $.each(sections, function(){
                addSectionToSelection(this.course.id, this.crn);
            });
            updateFuse.thaw();
            // TODO: update
        },
        error: function(request, status, error){

        }
    })
}

// end selection handling

// event handling for #selected
function sectionChanged(){
    // update selection via ajax ???
    var crn = $(this).find('.crn').text(), courseID = $(this).attr('data-cid');

    if (this.checked){
        addSectionToSelection(courseID, crn);
        return;
    }

    removeSectionFromSelection(courseID, crn);
}

function courseChanged(evt){
    var $sections = $(this).parent().find('.section input[type=checkbox]');
    if (this.checked)
        $sections.attr('checked', 'checked');
    else
        $sections.removeAttr('checked');

    $sections.each(function(){
        sectionChanged.call(this, [evt]);
    });
}

// handles adding & removing selections by (un)checking the courses in #courses.
function addToSelected($course){
    console.log('add', $course.attr('data-cid'));
    var courseID = $course.attr('data-cid'),<commit_msg>Deselection of courses now work via JS.<commit_after>(function($, window, document, undefined){

var selection = {}; // course_id => crns
window.selection = selection;

var updateFuse = new Utils.Fuse({
    delay: 250,
    execute: function(){
        var parameters = {};
        $.each(selection, function(cid, crns){
            parameters['selected_course_' + cid] = "checked";
            $.each(crns, function(i, crn){
                parameters['selected_course_' + cid + '_' + crn] = "checked";
            });
        });
        console.log('post', parameters);
        $.ajax(Utils.selectURL(), {
            type: "post",
            data: $.param(parameters),
            complete: function(){
            }
        });
    }
});

// lower level operations to modify the selection, should automatically
// handle syncing with the server
function addSectionToSelection(courseID, crn){
    courseID = parseInt(courseID, 10);
    crn = parseInt(crn, 10);
    updateFuse.stop();
    if(!selection[courseID])
        selection[courseID] = [];
    if($.inArray(crn, selection[courseID]) !== -1)
        return;
    selection[courseID].push(crn);
    updateFuse.start();
}

function removeSectionFromSelection(courseID, crn){
    courseID = parseInt(courseID, 10);
    crn = parseInt(crn, 10);
    if(!selection[courseID])
        return;
    updateFuse.stop();
    if($.inArray(crn, selection[courseID]) === -1){
        console.log(crn, 'not in', courseID);
        return;
    }
    selection[courseID].removeItem(crn);
    if(selection[courseID].length === 0)
        delete selection[courseID];
    updateFuse.start();
}

function syncSelection(){
    $.ajax(Utils.selectionURL(), {
        type: 'GET',
        dataType: 'text',
        success: function(content, status, request){
            var sections = Utils.json(content);
            updateFuse.freeze();
            $.each(sections, function(){
                addSectionToSelection(this.course.id, this.crn);
            });
            updateFuse.thaw();
            // TODO: update
        },
        error: function(request, status, error){

        }
    })
}

// end selection handling

// event handling for #selected
function sectionChanged(evt){
    // update selection via ajax ???
    var crn = $(this).attr('data-crn'), courseID = $(this).attr('data-cid');

    if (this.checked){
        addSectionToSelection(courseID, crn);
        // check parent if not done so
        $(this).parent('.course').find('input[type=checkbox]:first').attr('checked', 'checked');
        return;
    }

    removeSectionFromSelection(courseID, crn);
}

function courseChanged(evt){
    var $sections = $(this).parent().find('.section input[type=checkbox]');
    if (this.checked)
        $sections.attr('checked', 'checked');
    else
        $sections.removeAttr('checked');

    $sections.each(function(){
        console.log($(this).attr('data-crn'), this);
        sectionChanged.call(this, evt);
    });
}

// handles adding & removing selections by (un)checking the courses in #courses.
function addToSelected($course){
    console.log('add', $course.attr('data-cid'));
    var courseID = $course.attr('data-cid'),
{'fraction_non_alphanumeric': 0.15574844415329184, 'fraction_numerical': 0.004585653455617425, 'mean_word_length': 2.6416219439475253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3208577', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1772, 'n_words': 377}