<commit_before>import React from 'react';
import ReactDOM from 'react-dom';
import _ from 'underscore';
import FormSavedNotice from '../form_saved_notice.jsx';
import TinyMCE from 'react-tinymce';
import Dropzone from 'react-dropzone';
import Lightbox from 'react-images';
import Loader from 'react-loader';

var classNames = require('classnames');
var docIcon = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '')+'/images/doc-icon.png';

var SectionFields = React.createClass({
	getInitialState: function() { 
		var loadedInfo = JSON.parse(this.props.loadInfo);
		var fields = [];

		_.each(loadedInfo['fields'], function(fieldObj) {
			_.each(fieldObj, function(field) {
				var fieldContent = "",fieldContentId,fieldLinks = [], fieldFiles = [];

				if (field['savedContent'].length) {
					_.each(field['savedContent'], function(content) {
						if (content['type'] == "text") {
							fieldContentId = content['id'];
							fieldContent = content['content'];
						}
						else if (content['type'] == "link") {
							fieldLinks.push({'id':content['id'],'uri':content['uri']});
						}
						else {
							fieldFiles.push({'id':content['id'],'uri':content['uri'],'saved_type':content['type']});
						}
					});
				}
				fields.push({'id':field['id'],
					'title':field['field_title'], 
					'required':field['required'], 
					'field_content_id': fieldContentId,
					'content':fieldContent,
					'links':fieldLinks,
					'files':fieldFiles
				});
			});
		});

		return {
            fields: fields
        };
	},
	render: function() {
		var contentId = this.props.contentId;
		var sectionId = this.props.sectionId;

		return(
			<form>

				{
					this.state.fields.map(function(field, i) {
                        return (
                        	<Field
                            key={i} 
                            field_id={field['id']}
                            field_content_id={field['field_content_id']}
                            title={field['title']} 
                            required={field['required']} 
                            content={field['content']}
                            links={field['links']}
                            files={field['files']}
                            content_id={contentId}
                            section_id={sectionId}/>
                        );
                    })
                }
			</form>


		)
	}
});

var Field = React.createClass({
	getInitialState() {
    	return {
    		field_content_id: this.props.field_content_id,
    		links: this.props.links,
      		content: "",
      		success: undefined,
      		error: {}
    	};
  	},
	handleChange: function(e) {
        this.setState({
      		content: e.target.getContent()
    	});

    	var data = {};
    	if (this.state.field_content_id) { 
    		data['id'] = this.state.field_content_id;
    	}
    	data['type'] = "text";
    	data['content'] = this.state.content;
    	data['content_id'] = this.props.content_id;
    	data['template_section_field_id'] = this.props.field_id;

    	$.ajax({
            type: 'POST',
            url: '/artifact/field',
            data: data,
            dataType: 'json',
        })
        .success(function(result) {
        	if (result['id']) {
                this.setState({ field_content_id: result['id'] });
            }
            this.setState({success:"true"});
        }.bind(this))
        .error(function(result) {
            var error = result.responseJSON;
            this.setState({ error: error });
        }.bind(this));

    },
    removeLink: function(e){
       
        var item = e.target.id;

        var data = {};
        data['id'] = this.state.links[item]['id'];

        $.ajax({
            type: 'DELETE',
            url: '/artifact/field/delete',
            data: data,
            dataType: 'json',
        })
        .success(function(result) {
            if (result['success']) {
                this.state.links.splice(item, 1);
                this.setState({
                    links: this.state.links
                });
                this.setState({success: "true"});
            }
        }.bind(this))
        .error(function(result) {
            var error = result.responseJSON;
            if (error) {
                var error = result.responseJSON;
                this.setState({ error: error });
            }
        }.bind(this)); 

  	},
    saveChange: function(e) {
        var data = {};
        var field_content_id = e.target.id;

        if (field_content_id) { 
            data['id'] = field_content_id;
        }
        data['type'] = "link";
        data['uri'] = $.trim(e.target.value);
        data['content_id'] = this.props.content_id;
        data['template_section_field_id'] = this.props.field_id;

        $.ajax({
            type: 'POST',
            url: '/artifact/field',
            data: data,
            dataType: 'json',
        })
        .success(function(result) {

            if (result['id']) {
                this.setState({ field_content_id: result['id'] });

                this.state.links.push({'id':result['id'],'uri':result['uri']});
            }

            this.setState({success:true});
        }.bind(this))
        .error(function(result) {
            var error = result.responseJSON;
            this.setState({ error: error });
        }.bind(this));
    },
	render: function() {
		var fieldId = this.props.field_id;
		var contentId = this.props.content_id;
		var sectionId = this.props.section_id;
		var removeLink = this.removeLink;<commit_msg>Prevent autosave if no link content has been added.<commit_after>import React from 'react';
import ReactDOM from 'react-dom';
import _ from 'underscore';
import FormSavedNotice from '../form_saved_notice.jsx';
import TinyMCE from 'react-tinymce';
import Dropzone from 'react-dropzone';
import Lightbox from 'react-images';
import Loader from 'react-loader';

var classNames = require('classnames');
var docIcon = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '')+'/images/doc-icon.png';

var SectionFields = React.createClass({
	getInitialState: function() { 
		var loadedInfo = JSON.parse(this.props.loadInfo);
		var fields = [];

		_.each(loadedInfo['fields'], function(fieldObj) {
			_.each(fieldObj, function(field) {
				var fieldContent = "",fieldContentId,fieldLinks = [], fieldFiles = [];

				if (field['savedContent'].length) {
					_.each(field['savedContent'], function(content) {
						if (content['type'] == "text") {
							fieldContentId = content['id'];
							fieldContent = content['content'];
						}
						else if (content['type'] == "link") {
							fieldLinks.push({'id':content['id'],'uri':content['uri']});
						}
						else {
							fieldFiles.push({'id':content['id'],'uri':content['uri'],'saved_type':content['type']});
						}
					});
				}
				fields.push({'id':field['id'],
					'title':field['field_title'], 
					'required':field['required'], 
					'field_content_id': fieldContentId,
					'content':fieldContent,
					'links':fieldLinks,
					'files':fieldFiles
				});
			});
		});

		return {
            fields: fields
        };
	},
	render: function() {
		var contentId = this.props.contentId;
		var sectionId = this.props.sectionId;

		return(
			<form>

				{
					this.state.fields.map(function(field, i) {
                        return (
                        	<Field
                            key={i} 
                            field_id={field['id']}
                            field_content_id={field['field_content_id']}
                            title={field['title']} 
                            required={field['required']} 
                            content={field['content']}
                            links={field['links']}
                            files={field['files']}
                            content_id={contentId}
                            section_id={sectionId}/>
                        );
                    })
                }
			</form>


		)
	}
});

var Field = React.createClass({
	getInitialState() {
    	return {
    		field_content_id: this.props.field_content_id,
    		links: this.props.links,
      		content: "",
      		success: undefined,
      		error: {}
    	};
  	},
	handleChange: function(e) {
        this.setState({
      		content: e.target.getContent()
    	});

    	var data = {};
    	if (this.state.field_content_id) { 
    		data['id'] = this.state.field_content_id;
    	}
    	data['type'] = "text";
    	data['content'] = this.state.content;
    	data['content_id'] = this.props.content_id;
    	data['template_section_field_id'] = this.props.field_id;

    	$.ajax({
            type: 'POST',
            url: '/artifact/field',
            data: data,
            dataType: 'json',
        })
        .success(function(result) {
        	if (result['id']) {
                this.setState({ field_content_id: result['id'] });
            }
            this.setState({success:"true"});
        }.bind(this))
        .error(function(result) {
            var error = result.responseJSON;
            this.setState({ error: error });
        }.bind(this));

    },
    removeLink: function(e){
       
        var item = e.target.id;

        var data = {};
        data['id'] = this.state.links[item]['id'];

        $.ajax({
            type: 'DELETE',
            url: '/artifact/field/delete',
            data: data,
            dataType: 'json',
        })
        .success(function(result) {
            if (result['success']) {
                this.state.links.splice(item, 1);
                this.setState({
                    links: this.state.links
                });
                this.setState({success: "true"});
            }
        }.bind(this))
        .error(function(result) {
            var error = result.responseJSON;
            if (error) {
                var error = result.responseJSON;
                this.setState({ error: error });
            }
        }.bind(this)); 

  	},
    saveChange: function(e) {

        if (e.target.value) {
            var data = {};
            var field_content_id = e.target.id;

            if (field_content_id) { 
                data['id'] = field_content_id;
            }
            data['type'] = "link";
            data['uri'] = $.trim(e.target.value);
            data['content_id'] = this.props.content_id;
            data['template_section_field_id'] = this.props.field_id;

            $.ajax({
                type: 'POST',
                url: '/artifact/field',
                data: data,
                dataType: 'json',
            })
            .success(function(result) {

                if (result['id']) {
                    this.setState({ field_content_id: result['id'] });

                    this.state.links.push({'id':result['id'],'uri':result['uri']});
                }

                this.setState({success:true});
            }.bind(this))
            .error(function(result) {
                var error = result.responseJSON;
                this.setState({ error: error });
            }.bind(this));
        }
    },
	render: function() {
		var fieldId = this.props.field_id;
		var contentId = this.props.content_id;
		var sectionId = this.props.section_id;
		var removeLink = this.removeLink;
{'fraction_non_alphanumeric': 0.15341861490957212, 'fraction_numerical': 0.000176444640494045, 'mean_word_length': 1.7601655709763817, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6198856', 'n_tokens_mistral': 3601, 'n_tokens_neox': 3244, 'n_words': 547}