<commit_before>M.wrap('github/jillix/editor/v0.0.1/editor.js', function (require, module, exports) {

// TODO warn when window closes and unsaved changes exists

var View = require('github/jillix/view/v0.0.1/view');

var statusText = [
    /*0*/
    "Document has changed",
    /*1*/
    "Document is saving...",
    /*2*/
    "All changes saved",
    /*3*/
    "An Error occurred while saving, please try again",
    /*4*/
    "Your changes are not saved!",
    /*5*/
    "Please wait till document is saved!",
    /*6*/
    "Document is loading...",
    /*7*/
    "Error while loading",
    /*8*/
    "Empty response"
];

function setupAce () {
    var self = this;
    
    self.editor = ace.edit("aceEdit");
    self.session = self.editor.getSession();
    self.changed = 0;
    self.saving = false;
    
    //setup editor
    self.editor.setTheme("ace/theme/textmate");
    
    // set font size
    self.editor.setFontSize(13);
    
    //add ctrl-s command
    self.editor.commands.addCommand({
    
        name: "save",
        bindKey: {
        
            win: "Ctrl-S",
            mac: "Command-S",
            sender: "editor"
        },
        exec: function () {
            saveDocument.call(self);
        }
    });
    
    // save automatically after 1s, if doc has changed
    self.session.on("change", function() {
        self.changed = 1;
        self.info.innerHTML = statusText[0];
    });
}

function saveDocument() {
    var self = this;
    
    if(self.model && self.changed === 1 && !self.saving && !self.loading) {
        
        self.saving = true;
        self.changed = 2;
        self.info.innerHTML = statusText[1];
        
        // save data to db
        var query = {
            q: {_id: self.data._id},
            d: JSON.parse(self.editor.getValue())
        };
        
        self.model.update(query, function (err) {
            
            if (err) {
                self.changed = 1;
                self.info.innerHTML = statusText[3];
            }
            else if (self.changed == 2) {
                
                self.changed = 0;
                self.info.innerHTML = statusText[2];
            }
            
            self.saving = false;
        });
    }
}

function load () {
    var self = this;
    self.loading = 1;
    
    if (self.session) {
        
        // set mode
        self.session.setMode("ace/mode/json");
        
        // set status text
        self.info.innerHTML = statusText[6];
        
        // get model and read data
        self.view.model(getModelFromUrl(), function (err, model) {
            
            if (err || !model) {
                self.info.innerHTML = statusText[7];
                self.loading = 0;
                self.session.setValue(err || statusText[7]);
                return;
            }
            
            var query = {
                q: {_id: location.pathname.split('/').splice(3).join('/')}
            };
            
            self.model = model;
            
            // load data from db into editor
            model.read(query, function (err, data) {
                
                if (err || !data || !data[0]) {
                    data = err = err ? [err.toString()] : [statusText[8]];
                }
                
                self.data = data[0];
                self.session.setValue(JSON.stringify(data[0], null, 4) + '\n');
                
                // set status text
                self.info.innerHTML = err ? statusText[7] : statusText[2];
                self.loading = 0;
                self.changed = 0;
            });
        });
    }
}

function getModelFromUrl (model) {
    return location.pathname.split('/')[2];
}

function init () {
    var self = this;
    self.load = load;
    self.loading = 0;
    config = self.mono.config.data;
    
    // init view<commit_msg>config was a magic global variable.<commit_after>M.wrap('github/jillix/editor/v0.0.1/editor.js', function (require, module, exports) {

// TODO warn when window closes and unsaved changes exists

var View = require('github/jillix/view/v0.0.1/view');

var statusText = [
    /*0*/
    "Document has changed",
    /*1*/
    "Document is saving...",
    /*2*/
    "All changes saved",
    /*3*/
    "An Error occurred while saving, please try again",
    /*4*/
    "Your changes are not saved!",
    /*5*/
    "Please wait till document is saved!",
    /*6*/
    "Document is loading...",
    /*7*/
    "Error while loading",
    /*8*/
    "Empty response"
];

function setupAce () {
    var self = this;
    
    self.editor = ace.edit("aceEdit");
    self.session = self.editor.getSession();
    self.changed = 0;
    self.saving = false;
    
    //setup editor
    self.editor.setTheme("ace/theme/textmate");
    
    // set font size
    self.editor.setFontSize(13);
    
    //add ctrl-s command
    self.editor.commands.addCommand({
    
        name: "save",
        bindKey: {
        
            win: "Ctrl-S",
            mac: "Command-S",
            sender: "editor"
        },
        exec: function () {
            saveDocument.call(self);
        }
    });
    
    // save automatically after 1s, if doc has changed
    self.session.on("change", function() {
        self.changed = 1;
        self.info.innerHTML = statusText[0];
    });
}

function saveDocument() {
    var self = this;
    
    if(self.model && self.changed === 1 && !self.saving && !self.loading) {
        
        self.saving = true;
        self.changed = 2;
        self.info.innerHTML = statusText[1];
        
        // save data to db
        var query = {
            q: {_id: self.data._id},
            d: JSON.parse(self.editor.getValue())
        };
        
        self.model.update(query, function (err) {
            
            if (err) {
                self.changed = 1;
                self.info.innerHTML = statusText[3];
            }
            else if (self.changed == 2) {
                
                self.changed = 0;
                self.info.innerHTML = statusText[2];
            }
            
            self.saving = false;
        });
    }
}

function load () {
    var self = this;
    self.loading = 1;
    
    if (self.session) {
        
        // set mode
        self.session.setMode("ace/mode/json");
        
        // set status text
        self.info.innerHTML = statusText[6];
        
        // get model and read data
        self.view.model(getModelFromUrl(), function (err, model) {
            
            if (err || !model) {
                self.info.innerHTML = statusText[7];
                self.loading = 0;
                self.session.setValue(err || statusText[7]);
                return;
            }
            
            var query = {
                q: {_id: location.pathname.split('/').splice(3).join('/')}
            };
            
            self.model = model;
            
            // load data from db into editor
            model.read(query, function (err, data) {
                
                if (err || !data || !data[0]) {
                    data = err = err ? [err.toString()] : [statusText[8]];
                }
                
                self.data = data[0];
                self.session.setValue(JSON.stringify(data[0], null, 4) + '\n');
                
                // set status text
                self.info.innerHTML = err ? statusText[7] : statusText[2];
                self.loading = 0;
                self.changed = 0;
            });
        });
    }
}

function getModelFromUrl (model) {
    return location.pathname.split('/')[2];
}

function init () {

    var self = this
      , config = self.mono.config.data;

    self.load = load;
    self.loading = 0;
    
    // init view
{'fraction_non_alphanumeric': 0.13624083365495948, 'fraction_numerical': 0.01183584201723916, 'mean_word_length': 1.341566265060241, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '160092', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2243, 'n_words': 518}