<commit_before>use_tags(['div','table','tbody','tr','th','td','span','textarea','a','pre']);

var cache = {};   // map from index to date

var Cal = React.createClass({

    getInitialState: function() {
        var props = this.props.initial_state;
        // initial_data:
        // first : new Date( ...)  -- date of first sunday
        // month : data['month'],  -- month name
        //  year  : data['year'],
        //  data : data['data']  -- map from yyyy-mm-dd to text for that day
        //  changed : { a subset of data: map from yyyy-mm-dd to text for that day (updated, not saved)}
        //  last_touch : timestamp of last keystroke

        props['last_touch'] = new Date().getTime();
        return props;
    },
    save: function() {
        var url = '/cal';  // window.location.href;
        var that = this;
        var data = this.state.changed;
        fetch(url, {
            method: 'POST',
            headers: { 'Content-Type' : 'application/json' },
            body: JSON.stringify({data:data})
        })
        .then(function(response){
            if (response.ok) {
                that.touch();
                var changed = this.state.changed || {};
                if (Object.getOwnPropertyNames(changed).length == 0) {
                    that.setState({changed: false});
                }  else {
                    that.setState({changed: changed });
                }
            } else {
                console.log('response is not ok', response);
            }
        })
        .catch(function(err) {
            console.log("network error");
        })
    },
    load: function(from,to) {
        var url = '/cal/range/' + from.ymd() + '/' + to.ymd();
        var that = this;
        fetch(url, {
            headers: { 'Content-Type' : 'application/json' },
        }).then(function(res){
            return res.json();
        }).then(function(j){
            that.setState({ data: j, changed: false });
        }).catch(function(err) {
            console.log('error',err);
        })
    },
    reload: function() {
        var first = this.state.first;
        this.load(first.addDays(-42), first.addDays(83));
    },
    dt: function(i) {
        if (cache[i]) { return cache[i] };
        cache[i] = this.state.first.addDays(i);
        return cache[i];
    },
    edit: function(index, e) {
        if (e.target.getAttribute('href')) { return; }
        this.setState({ editing:index });
        this.touch();
    },
    cell: function(i) {
       var dt = this.dt(i);
       var cl = 'normal';
       if (dt.mon() != this.state.month) {
           cl = 'other';
       }
       return td( { className: cl, onClick: this.edit.bind(this,i) },
                  span( { className:'dt ', id: i}, dt.d()),
                  span( { html: wikify( this.state.data[ dt.ymd() ] ) })
                );
    },
    editcell: function(i) {
        var txt = this.state.data[this.dt(i).ymd()];
        var dt_class = this.state.changed ? 'changed' : 'saved';
        return td( { className:'edit'},
            span( { className:'dt ' + dt_class, id: i}, ''),
            textarea({autoFocus: true,
                id: i,
                className: ( this.state.changed ? 'changed' : 'saved' ),
                onKeyDown: this.touch,
                defaultValue: txt,
                onChange: this.handleChange })
               );
    },
    cells: function(from,to) {
        var x = [];
        var e = this.state.editing;
        for (i=from;i<to;i++) {
            if (e==i) {
              x.push( this.editcell(i) );
            } else {
              x.push( this.cell(i) );
            }
        }
        return x;
    },
    maybeSave: function() {
        if (this.state.changed) {
            this.save();
            return;
        }
        if (this.state.editing !== undefined) {
            var now = new Date().getTime();
            var last = this.state.last_touch;
            if (now - this.state.last_touch > 4000) {<commit_msg>saveAndStopEdit<commit_after>use_tags(['div','table','tbody','tr','th','td','span','textarea','a','pre']);

var cache = {};   // map from index to date

var Cal = React.createClass({

    getInitialState: function() {
        var props = this.props.initial_state;
        // initial_data:
        // first : new Date( ...)  -- date of first sunday
        // month : data['month'],  -- month name
        //  year  : data['year'],
        //  data : data['data']  -- map from yyyy-mm-dd to text for that day
        //  changed : { a subset of data: map from yyyy-mm-dd to text for that day (updated, not saved)}
        //  last_touch : timestamp of last keystroke

        props['last_touch'] = new Date().getTime();
        return props;
    },
    save: function() {
        var url = '/cal';  // window.location.href;
        var that = this;
        var data = this.state.changed;
        fetch(url, {
            method: 'POST',
            headers: { 'Content-Type' : 'application/json' },
            body: JSON.stringify({data:data})
        })
        .then(function(response){
            if (response.ok) {
                that.touch();
                var changed = this.state.changed || {};
                if (Object.getOwnPropertyNames(changed).length == 0) {
                    that.setState({changed: false});
                }  else {
                    that.setState({changed: changed });
                }
            } else {
                console.log('response is not ok', response);
            }
        })
        .catch(function(err) {
            console.log("network error");
        })
    },
    load: function(from,to) {
        var url = '/cal/range/' + from.ymd() + '/' + to.ymd();
        var that = this;
        fetch(url, {
            headers: { 'Content-Type' : 'application/json' },
        }).then(function(res){
            return res.json();
        }).then(function(j){
            that.setState({ data: j, changed: false });
        }).catch(function(err) {
            console.log('error',err);
        })
    },
    reload: function() {
        var first = this.state.first;
        this.load(first.addDays(-42), first.addDays(83));
    },
    dt: function(i) {
        if (cache[i]) { return cache[i] };
        cache[i] = this.state.first.addDays(i);
        return cache[i];
    },
    edit: function(index, e) {
        if (e.target.getAttribute('href')) { return; }
        this.setState({ editing:index });
        this.touch();
    },
    cell: function(i) {
       var dt = this.dt(i);
       var cl = 'normal';
       if (dt.mon() != this.state.month) {
           cl = 'other';
       }
       return td( { className: cl, onClick: this.edit.bind(this,i) },
                  span( { className:'dt ', id: i}, dt.d()),
                  span( { html: wikify( this.state.data[ dt.ymd() ] ) })
                );
    },
    editcell: function(i) {
        var txt = this.state.data[this.dt(i).ymd()];
        var dt_class = this.state.changed ? 'changed' : 'saved';
        return td( { className:'edit'},
            span(
                { className:'dt ' + dt_class,
                  id: i,
                  onClick: this.saveAndStopEdit
                }, ''),
            textarea({autoFocus: true,
                id: i,
                className: ( this.state.changed ? 'changed' : 'saved' ),
                onKeyDown: this.touch,
                defaultValue: txt,
                onChange: this.handleChange })
               );
    },
    cells: function(from,to) {
        var x = [];
        var e = this.state.editing;
        for (i=from;i<to;i++) {
            if (e==i) {
              x.push( this.editcell(i) );
            } else {
              x.push( this.cell(i) );
            }
        }
        return x;
    },
    saveAndStopEdit: function() {
        if (this.state.changed) {
            this.save();
        }
        this.setState({editing: undefined });
    },
    maybeSave: function() {
        if (this.state.changed) {
            this.save();
            return;
        }
        if (this.state.editing !== undefined) {
            var now = new Date().getTime();
            var last = this.state.last_touch;
            if (now - this.state.last_touch > 2000) {
{'fraction_non_alphanumeric': 0.15985489721886337, 'fraction_numerical': 0.002176541717049577, 'mean_word_length': 1.5162762397322787, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1155171', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2410, 'n_words': 529}