<commit_before>"use strict";

var m          = require("mithril"),
    moment     = require("moment"),
    fuzzy      = require("fuzzysearch"),
    debounce   = require("lodash.debounce"),
    get        = require("lodash.get"),
    capitalize = require("lodash.capitalize"),
    slug       = require("sluggo"),

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

    css = require("./nav.css");

module.exports = {
    controller : function() {
        var ctrl = this,

            schema = db.child("schemas/" + m.route.param("schema"));

        ctrl.page = 0;

        ctrl.schema  = null;
        ctrl.content = null;
        ctrl.results = null;
        ctrl.hide    = false;

        schema.on("value", function(snap) {
            ctrl.schema = snap.val();
            ctrl.schema.key = snap.key();

            m.redraw();
        });

        // Go get initial data
        db.child("content/" + ctrl.schema.key).orderByChild("published").on("value", function(snap) {
            var content = [];

            snap.forEach(function(record) {
                var data = record.val();

                data.key       = record.key();
                data.created   = moment.utc(data.created);
                data.updated   = moment.utc(data.updated_at);
                data.published = data.published ? moment.utc(data.published) : null;
                data.search    = slug(data.name, { separator : "" });

                content.push(data);
            });

            ctrl.content = content;

            m.redraw();
        });

        // Event handlers
        ctrl.add = function() {
            var result;

            result = db.child("content/" + ctrl.schema.key).push({
                name    : "New " + ctrl.schema.name,
                created : db.TIMESTAMP
            });

            m.route("/content/" + ctrl.schema.key + "/" + result.key());
        };

        ctrl.hide = function() {
            ctrl.hidden = !ctrl.hidden;
        };

        ctrl.change = function(page, e) {
            e.preventDefault();

            ctrl.page = page;
        };

        // m.redraw calls are necessary due to debouncing, this function
        // may not be executing during a planned redraw cycle
        ctrl.filter = debounce(function(input) {
            if(input.length < 2) {
                ctrl.results = false;

                return m.redraw();
            }

            input = slug(input);

            ctrl.results = ctrl.content.filter(function(content) {
                return fuzzy(input, content.search);
            });

            m.redraw();
        }, 100);

        ctrl.remove = function(data) {
            var ref = db.child("content").child(ctrl.schema.key).child(data.key);

            if(window.confirm("Remove " + data.name + "?")) {
                ref.remove().catch(console.error.bind(console));
            }
        };
    },

    view : function(ctrl) {
        var route   = m.route(),
            content = ctrl.results || ctrl.content || [];
        
        if(!m.route.param("id")) {
            document.title = capitalize(ctrl.schema.name);
        }

        return m("div", { class : css[ctrl.hidden ? "hidden" : "nav"] },
            m(".head", { class : css.filter },
                m("input", {
                    class       : css.text,
                    placeholder : "Search...",
                    oninput     : m.withAttr("value", ctrl.filter)
                }),
                m("div", {
                        class   : ctrl.hidden ? css.show : css.hide,
                        onclick : ctrl.hide
                    },
                    m("span", ctrl.hidden ? "show" : "hide")
                )
            ),
            m("div", { class : css.body },
                m("ul", { class : css.list },
                    content.map(function(data) {
                        var url = "/content/" + ctrl.schema.key + "/" + data.key,
                            cssClass = css.item;

                        if(data.published && route.indexOf(url) === 0) {
                            cssClass = css.activePublished;
                        } else {
                            if(route.indexOf(url) === 0) {
                                cssClass = css.active;
                            } else if(data.published) {
                                cssClass = css.published;
                            }
                        }

                        return m("li", { class : cssClass },
                            m("a", {
                                    class  : css.anchor,
                                    href   : "/content/" + ctrl.schema.key + "/" + data.key,
                                    href   : "/content/" + ctrl.schema.key + "/" + data.key,
                                    config : m.route
                                },
                                m("h3", { class : css.heading }, data.name),
                                m("p", { class : css.date },<commit_msg>Track creation user<commit_after>"use strict";

var m          = require("mithril"),
    moment     = require("moment"),
    fuzzy      = require("fuzzysearch"),
    debounce   = require("lodash.debounce"),
    get        = require("lodash.get"),
    capitalize = require("lodash.capitalize"),
    slug       = require("sluggo"),

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

    css = require("./nav.css");

module.exports = {
    controller : function() {
        var ctrl = this,

            schema = db.child("schemas/" + m.route.param("schema"));

        ctrl.page = 0;

        ctrl.schema  = null;
        ctrl.content = null;
        ctrl.results = null;
        ctrl.hide    = false;

        schema.on("value", function(snap) {
            ctrl.schema = snap.val();
            ctrl.schema.key = snap.key();

            m.redraw();
        });

        // Go get initial data
        db.child("content/" + ctrl.schema.key).orderByChild("published").on("value", function(snap) {
            var content = [];

            snap.forEach(function(record) {
                var data = record.val();

                data.key       = record.key();
                data.created   = moment.utc(data.created);
                data.updated   = moment.utc(data.updated_at);
                data.published = data.published ? moment.utc(data.published) : null;
                data.search    = slug(data.name, { separator : "" });

                content.push(data);
            });

            ctrl.content = content;

            m.redraw();
        });

        // Event handlers
        ctrl.add = function() {
            var result;

            result = db.child("content/" + ctrl.schema.key).push({
                name       : "New " + ctrl.schema.name,
                created_at : db.TIMESTAMP,
                created_by : db.getAuth().uid
            });

            m.route("/content/" + ctrl.schema.key + "/" + result.key());
        };

        ctrl.hide = function() {
            ctrl.hidden = !ctrl.hidden;
        };

        ctrl.change = function(page, e) {
            e.preventDefault();

            ctrl.page = page;
        };

        // m.redraw calls are necessary due to debouncing, this function
        // may not be executing during a planned redraw cycle
        ctrl.filter = debounce(function(input) {
            if(input.length < 2) {
                ctrl.results = false;

                return m.redraw();
            }

            input = slug(input);

            ctrl.results = ctrl.content.filter(function(content) {
                return fuzzy(input, content.search);
            });

            m.redraw();
        }, 100);

        ctrl.remove = function(data) {
            var ref = db.child("content").child(ctrl.schema.key).child(data.key);

            if(window.confirm("Remove " + data.name + "?")) {
                ref.remove().catch(console.error.bind(console));
            }
        };
    },

    view : function(ctrl) {
        var route   = m.route(),
            content = ctrl.results || ctrl.content || [];
        
        if(!m.route.param("id")) {
            document.title = capitalize(ctrl.schema.name);
        }

        return m("div", { class : css[ctrl.hidden ? "hidden" : "nav"] },
            m(".head", { class : css.filter },
                m("input", {
                    class       : css.text,
                    placeholder : "Search...",
                    oninput     : m.withAttr("value", ctrl.filter)
                }),
                m("div", {
                        class   : ctrl.hidden ? css.show : css.hide,
                        onclick : ctrl.hide
                    },
                    m("span", ctrl.hidden ? "show" : "hide")
                )
            ),
            m("div", { class : css.body },
                m("ul", { class : css.list },
                    content.map(function(data) {
                        var url = "/content/" + ctrl.schema.key + "/" + data.key,
                            cssClass = css.item;

                        if(data.published && route.indexOf(url) === 0) {
                            cssClass = css.activePublished;
                        } else {
                            if(route.indexOf(url) === 0) {
                                cssClass = css.active;
                            } else if(data.published) {
                                cssClass = css.published;
                            }
                        }

                        return m("li", { class : cssClass },
                            m("a", {
                                    class  : css.anchor,
                                    href   : "/content/" + ctrl.schema.key + "/" + data.key,
                                    config : m.route
                                },
                                m("h3", { class : css.heading }, data.name),
                                m("p", { class : css.date },
{'fraction_non_alphanumeric': 0.13820404890759672, 'fraction_numerical': 0.0016035277610743637, 'mean_word_length': 1.1168858718710224, 'pattern_counts': {'":': 0, '<': 5, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5499774', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2502, 'n_words': 476}