<commit_before>var Bind = require("github/jillix/bind");
var Events = require("github/jillix/events");

module.exports = function(config) {

    var self = this;
    Events.call(self, config);

    $(self.dom).on("submit", "form", function (e) {

        e.preventDefault();

        var $form = $(this);
        var serializedForm = {};

        $form.find("[data-field]").each(function () {
            var $element = $(this);
            var how = $element.attr("data-value") || "val";
            var params = $element.attr("data-params");
            var field = $element.attr("data-field");
            var value;

            if (!params) {
                value = $element[how]();
            } else {
                value = $element[how](params);
            }

            serializedForm[field] = value;
        });

        self.emit(config.eventName || "serializedForm", serializedForm);
    });

    self.fillForm = function (data) {

        if (data.length > 1) { return alert("You can't edit multiple lists at same time."); }
        config.onFill = config.onFill || {};

        for (var i in config.onFill.binds) {
            var bindObj = config.onFill.binds[i];
            bindObj.context = self.dom;
            Bind.call(self, bindObj, data);
        }
    };
};<commit_msg>Fixed fillForm and added errors showing.<commit_after>var Bind = require("github/jillix/bind");
var Events = require("github/jillix/events");

module.exports = function(config) {

    var self = this;
    Events.call(self, config);

    $(self.dom).on("submit", "form", function (e) {

        e.preventDefault();

        var $form = $(this);
        var serializedForm = {};

        $form.find("[data-field]").each(function () {
            var $element = $(this);
            var how = $element.attr("data-value") || "val";
            var params = $element.attr("data-params");
            var field = $element.attr("data-field");
            var value;

            if (!params) {
                value = $element[how]();
            } else {
                value = $element[how](params);
            }

            serializedForm[field] = value;
        });

        self.emit(config.eventName || "serializedForm", serializedForm);
    });

    self.fillForm = function (data) {

        self.clearErrors();

        if (!data) {
            self.showError("No list selected.");
            return;
        }

        if (data.length > 1) {
            self.showError("You can't edit multiple lists at same time.");
            return;
        }

        config.onFill = config.onFill || {};

        for (var i in config.onFill.binds) {
            var bindObj = config.onFill.binds[i];
            bindObj.context = self.dom;

            // TODO When will be added this into bind?
            //      Doesn't support multiple attrs
            if (bindObj.attr && bindObj.attr[0].name === "value") {
                var value = bindObj.attr[0].value;
                if (typeof value === "object") {
                    // TODO Doesn't support dot notation
                    value = data[0][value.source];
                }
                $(bindObj.target, self.dom).val(value);
            }
            else {
                Bind.call(self, bindObj, data[0]);
            }
        }
    };

    self.showError = function (err) {
        if (err) {
            var $newAlert = $("<div>");
            $newAlert.addClass("alert fade in danger alert-error");
            $newAlert.append("<button type='button' class='close' data-dismiss='alert'>×</button>");
            $newAlert.append(err);

            $("form", self.dom).before($newAlert);
            $newAlert.fadeIn();
            $("form", self.dom).hide();
            return;
        }

        $("form", self.dom).show();
        $(".alert-error", self.dom).remove();
    };

    self.clearErrors = function () {
        $("form", self.dom).show();
        $(".alert-error", self.dom).remove();
    };
};
{'fraction_non_alphanumeric': 0.15871421396283275, 'fraction_numerical': 0.0015067805123053742, 'mean_word_length': 1.705842391304348, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '2412412', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1106, 'n_words': 241}