<commit_before>define(function (require) {
    var $ = require('jquery');
    var _ = require('underscore');
    var Backbone = require('util/backbone-helper');
    var Hbs = require('handlebars');
    var Mn = require('backbone.marionette');
    var Q = require('q');
    var Radio = require('backbone.radio');
    var pageChannel = Radio.channel('page');
    var courseChannel = Radio.channel('course');
    var template = require('text!templates/modifyCategory.hbs');
    var alertTemplate = require('text!templates/alert-block.hbs');
    
    var Course = require('course/course');

    return Mn.ItemView.extend({
        tagName: 'div',
        className: 'modifyCategory modal-dialog  modal-lg',
        template: Hbs.compile(template),
        ui: {
            'categoryName' : '.className',
            'categoryWeight' : '.classWeight',
            'parentCategory' : '.classParent',
            'ok' : '.save',
            'cancel' : '.error'
        },

        initialize: function(options) {
            this.model = courseChannel.request('current:course');
            console.log(this.model);
        },

        /**
         * Hide the modify category dialog on initial load. Also,
         * create a dropdown menu for testing purposes that allows
         * the user to select which category to modify.
         *
         * @author <NAME>
         */
        onShow : function(){
            var ui = this.ui;

            var categories = this.model.get('categories');

            var catValues = [];
            catValues.push(''); 
            _.forEach(categories, function(category) {
                catValues.push(category.name);
            });

            $.each(catValues, function(key, value) {   
                 ui.category
                     .append($("<option></option>")
                     .text(value)); 
            });
        },
        events : {
            'click @ui.modifyCategoryButton' :  'showModifyCategory',
            'click @ui.ok' :  'saveModifyCategory',
            'click @ui.cancel' :  'closeModifyCategory'
        },
        /**
         * Show the modify category dialog. Use the data from the
         * course model to preload the fields for the chosen category.
         *
         * @author <NAME>
         */
        showModifyCategory : function() {
            var ui = this.ui;

            var reqCatName = ui.category.val();

            var categories = this.model.get('categories');
            var category = $.grep(categories, function(e){ return e.name == reqCatName; })[0];

            var catValues = [];
            catValues.push('');
            _.forEach(categories, function(category) {
                catValues.push(category.name);
            });

            $.each(catValues, function(key, value) {
                if (value != category.name) {
                    ui.parentCategory
                     .append($("<option></option>")
                     .text(value)); 
                }
            });

            var path = category.path.split('#');
            ui.parentCategory.val(path[path.length - 2]).attr("selected");

            ui.categoryName.val(category.name);
            ui.categoryWeight.val(category.weight);

            ui.dialog.show();
            ui.modifyCategoryButton.hide();
            ui.catSelector.hide();
        },
        /**
         * Gather the new values from the modify category dialog and
         * save them to the database.
         *
         * @author <NAME>
         */
        saveModifyCategory : function() {
            var ui = this.ui;
            var self = this;

            var reqCatName = ui.category.val();

            var categories = this.model.get('categories');
            var category = $.grep(categories, function(e){ return e.name == reqCatName; })[0];

            category.name = ui.categoryName.val();
            category.weight = ui.categoryWeight.val();

            var newParent = $.grep(categories, function(e){ return e.name == ui.parentCategory.val(); })[0];

            category.path = newParent ? newParent.path + "#" + category.name : "#" + category.name;

            Backbone.emulateHTTP = true;
            this.model.set("categories", categories);
            this.model.save().then(function() {
                self.closeModifyCategory();
            });
        },
        /**
         * Hide the modify category dialog. Bring the user back to the
         * screen to select which dialog to modify, and re-fetch
         * the newly updated data.
         *
         * @author <NAME>
         */
        closeModifyCategory : function() {
            var self = this;

            this.ui.dialog.hide();
        },
    })

    // Router.route("modifyCategory", "home", function() {
    //     console.log("Yes");
    //     var course = new Course;
    //     var modifyCatView = new ModifyCategoryView({
    //         model: course
    //     });
    //     PopupRegion.show(modifyCatView);
    // });
});<commit_msg>Updating modCat<commit_after>define(function (require) {
    var $ = require('jquery');
    var _ = require('underscore');
    var Backbone = require('util/backbone-helper');
    var Hbs = require('handlebars');
    var Mn = require('backbone.marionette');
    var Q = require('q');
    var Radio = require('backbone.radio');
    var pageChannel = Radio.channel('page');
    var courseChannel = Radio.channel('course');
    var template = require('text!templates/modifyCategory.hbs');
    var alertTemplate = require('text!templates/alert-block.hbs');
    
    var Course = require('course/course');

    return Mn.ItemView.extend({
        tagName: 'div',
        className: 'modifyCategory modal-dialog modal-lg',
        template: Hbs.compile(template),
        ui: {
            'categoryName' : '.className',
            'categoryWeight' : '.classWeight',
            'parentCategory' : '.classParent',
            'ok' : '.save',
            'cancel' : '.error'
        },

        initialize: function(options) {
            this.model = courseChannel.request('current:course');
            this.alertTemplate = Hbs.compile(alertTemplate);
            console.log(this.model);
        },

        /**
         * Hide the modify category dialog on initial load. Also,
         * create a dropdown menu for testing purposes that allows
         * the user to select which category to modify.
         *
         * @author <NAME>
         */
        onShow : function(){
            var ui = this.ui;

            var categories = this.model.get('categories');

            var catValues = [];
            catValues.push(''); 
            _.forEach(categories, function(category) {
                catValues.push(category.name);
            });

            $.each(catValues, function(key, value) {   
                 ui.category
                     .append($("<option></option>")
                     .text(value)); 
            });
        },
        events : {
            'click @ui.modifyCategoryButton' :  'showModifyCategory',
            'click @ui.ok' :  'saveModifyCategory',
            'click @ui.cancel' :  'closeModifyCategory'
        },
        /**
         * Show the modify category dialog. Use the data from the
         * course model to preload the fields for the chosen category.
         *
         * @author <NAME>
         */
        showModifyCategory : function() {
            var ui = this.ui;

            var reqCatName = ui.category.val();

            var categories = this.model.get('categories');
            var category = $.grep(categories, function(e){ return e.name == reqCatName; })[0];

            var catValues = [];
            catValues.push('');
            _.forEach(categories, function(category) {
                catValues.push(category.name);
            });

            $.each(catValues, function(key, value) {
                if (value != category.name) {
                    ui.parentCategory
                     .append($("<option></option>")
                     .text(value)); 
                }
            });

            var path = category.path.split('#');
            ui.parentCategory.val(path[path.length - 2]).attr("selected");

            ui.categoryName.val(category.name);
            ui.categoryWeight.val(category.weight);

            ui.dialog.show();
            ui.modifyCategoryButton.hide();
            ui.catSelector.hide();
        },
        /**
         * Gather the new values from the modify category dialog and
         * save them to the database.
         *
         * @author <NAME>
         */
        saveModifyCategory : function() {
            var ui = this.ui;
            var self = this;

            var reqCatName = ui.category.val();

            var categories = this.model.get('categories');
            var category = $.grep(categories, function(e){ return e.name == reqCatName; })[0];

            category.name = ui.categoryName.val();
            category.weight = ui.categoryWeight.val();

            var newParent = $.grep(categories, function(e){ return e.name == ui.parentCategory.val(); })[0];

            category.path = newParent ? newParent.path + "#" + category.name : "#" + category.name;

            Backbone.emulateHTTP = true;
            this.model.set("categories", categories);
            this.model.save().then(function() {
                self.closeModifyCategory();
            });
        },
        /**
         * Hide the modify category dialog. Bring the user back to the
         * screen to select which dialog to modify, and re-fetch
         * the newly updated data.
         *
         * @author <NAME>
         */
        closeModifyCategory : function() {
            var self = this;
            Q(this.model.save()).then(function(res) {
                console.dir(['modify category save result:', res]);
                var modalRegion = pageChannel.request('modalRegion');
                modalRegion.hideModal();
                courseChannel.command('updateCourses');
            },
            function(err) {
                self.ui.error.html(self.alertTemplate({message: err.responseText}));
                self.ui.saveButton.button('reset');
            }).done();
        },
    })
});
{'fraction_non_alphanumeric': 0.13209202573601092, 'fraction_numerical': 0.0007798791187365958, 'mean_word_length': 1.6695290137913088, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '969647', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2595, 'n_words': 593}