<commit_before>"use strict";

var m          = require("mithril"),
    capitalize = require("lodash.capitalize"),

    children = require("../types/children"),

    watch  = require("../lib/watch"),
    db     = require("../lib/firebase"),
    update = require("../lib/update"),

    editor = require("./schema-edit/editor"),

    layout = require("./layout"),
    css    = require("./schema-edit.css");

module.exports = {
    controller : function() {
        var ctrl = this,
            id   = m.route.param("schema"),
            ref  = db.child("schemas/" + id);

        ctrl.ref     = ref;
        ctrl.schema  = null;
        ctrl.data    = {};
        ctrl.preview = {
            valid : true,
            value : ""
        };

        // Get Firebase data
        ref.on("value", function(snap) {
            ctrl.schema = snap.val();

            if(!ctrl.preview.value) {
                ctrl.preview.value = ctrl.schema.preview || "";
            }

            m.redraw();
        });

        // Event Handlers
        ctrl.previewChanged = function(e) {
            var el = e.target;

            ctrl.preview.valid = el.validity.valid;
            ctrl.preview.value = el.value;

            ref.child("preview").set(el.value);
        };

        watch(ref);
    },

    view : function(ctrl) {
        if(!ctrl.schema) {
            return m.component(layout);
        }

        return m.component(layout, {
            title   : "Edit - " + capitalize(ctrl.schema.name),
            content : m("div", { class : layout.css.content },
                m("div", { class : css.meta },
                    m("h3", "Metadata"),
                    m("label", { class : css.label, for : "preview" }, "Preview URL Base"),
                    m("input", {
                        id    : "preview",
                        class : css[ctrl.preview.valid ? "preview" : "previewError"],
                        type  : "url",
                        value : ctrl.preview.value || "",

                        oninput : ctrl.previewChanged,
                        config  : function(el, init) {
                            if(init) {
                                return;
                            }

                            ctrl.preview.valid = el.validity.valid;
                        }
                    }),
                    m("p", { class : css.note },
                        ctrl.preview.value ?
                            ctrl.preview.value + "-0IhUBgUFfhyLQ2m6s5x" :
                            null
                    )
                ),
                m("div", { class : css.contents },
                    m("div", { class : css.editor },
                        m("h3", "Field Definitions"),
                        m.component(editor, {
                            ref    : ctrl.ref,
                            source : ctrl.schema.source || "{\n\n}"
                        })
                    ),

                    m("div", { class : css.fields },
                        m("h3", "Preview"),
                        m.component(children, {
                            details : ctrl.schema.fields,
                            data    : ctrl.data,
                            path    : [],
                            state   : ctrl.data,
                            update  : update.bind(null, false, ctrl.data)
                        })
                    )
                )
            )
        });
    }
};<commit_msg>Finish porting over to fields name<commit_after>"use strict";

var m          = require("mithril"),
    capitalize = require("lodash.capitalize"),

    children = require("../types/children"),

    watch  = require("../lib/watch"),
    db     = require("../lib/firebase"),
    update = require("../lib/update"),

    editor = require("./schema-edit/editor"),

    layout = require("./layout"),
    css    = require("./schema-edit.css");

module.exports = {
    controller : function() {
        var ctrl = this,
            id   = m.route.param("schema"),
            ref  = db.child("schemas/" + id);

        ctrl.ref     = ref;
        ctrl.schema  = null;
        ctrl.data    = {};
        ctrl.preview = {
            valid : true,
            value : ""
        };

        // Get Firebase data
        ref.on("value", function(snap) {
            ctrl.schema = snap.val();

            if(!ctrl.preview.value) {
                ctrl.preview.value = ctrl.schema.preview || "";
            }

            m.redraw();
        });

        // Event Handlers
        ctrl.previewChanged = function(e) {
            var el = e.target;

            ctrl.preview.valid = el.validity.valid;
            ctrl.preview.value = el.value;

            ref.child("preview").set(el.value);
        };

        watch(ref);
    },

    view : function(ctrl) {
        if(!ctrl.schema) {
            return m.component(layout);
        }

        return m.component(layout, {
            title   : "Edit - " + capitalize(ctrl.schema.name),
            content : m("div", { class : layout.css.content },
                m("div", { class : css.meta },
                    m("h3", "Metadata"),
                    m("label", { class : css.label, for : "preview" }, "Preview URL Base"),
                    m("input", {
                        id    : "preview",
                        class : css[ctrl.preview.valid ? "preview" : "previewError"],
                        type  : "url",
                        value : ctrl.preview.value || "",

                        oninput : ctrl.previewChanged,
                        config  : function(el, init) {
                            if(init) {
                                return;
                            }

                            ctrl.preview.valid = el.validity.valid;
                        }
                    }),
                    m("p", { class : css.note },
                        ctrl.preview.value ?
                            ctrl.preview.value + "-0IhUBgUFfhyLQ2m6s5x" :
                            null
                    )
                ),
                m("div", { class : css.contents },
                    m("div", { class : css.editor },
                        m("h3", "Field Definitions"),
                        m.component(editor, {
                            ref    : ctrl.ref,
                            source : ctrl.schema.source || "{\n\n}"
                        })
                    ),

                    m("div", { class : css.fields },
                        m("h3", "Preview"),
                        m.component(children, {
                            fields : ctrl.schema.fields,
                            data   : ctrl.data,
                            path   : [],
                            state  : ctrl.data,
                            update : update.bind(null, false, ctrl.data)
                        })
                    )
                )
            )
        });
    }
};
{'fraction_non_alphanumeric': 0.13306567249784915, 'fraction_numerical': 0.002007456266131345, 'mean_word_length': 0.9396551724137931, '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': '5404386', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1699, 'n_words': 307}