{"size": 2305, "ext": "js", "max_stars_repo_path": "src/js/internals/modules/database-search.js", "max_stars_repo_name": "lfamorim/harlan", "max_stars_repo_stars_event_min_datetime": "2015-06-20T06:22:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-21T06:16:54.000Z", "max_issues_repo_path": "src/js/internals/modules/database-search.js", "max_issues_repo_name": "lfamorim/harlan", "max_issues_repo_issues_event_min_datetime": "2016-05-07T11:17:17.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-12T00:47:17.000Z", "max_forks_repo_path": "src/js/internals/modules/database-search.js", "max_forks_repo_name": "lfamorim/harlan", "max_forks_repo_forks_event_min_datetime": "2015-06-20T20:08:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-21T22:25:32.000Z"}
{"max_stars_count": 25, "max_issues_count": 7, "max_forks_count": 21, "avg_line_length": 38.4166666667, "max_line_length": 111, "alphanum_fraction": 0.5197396963}
module.exports = controller => {

    controller.registerCall('databaseSearch::submit', args => {
        const form = args[0];
        const tableJNode = args[1];
        const databaseJNode = args[2];
        const section = args[3];

        const database = databaseJNode.attr('name');
        const table = tableJNode.attr('name');

        return e => {
            const loader = section.find('.display-loader');
            e.preventDefault();
            const formdata = form.serialize();
            controller.serverCommunication.call(`SELECT FROM '${database}'.'${table}'`, {
                data: formdata,
                success(doc) {
                    const args = [].concat(doc, database, table, databaseJNode, tableJNode, section, form);
                    controller.trigger('database::success', args);
                    controller.trigger(`database::success::${database}`, args);
                    controller.trigger(`database::success::${database}::${table}`, args);
                },
                error(x, h, r) {
                    const args = [].concat(x, h, r, database, table, databaseJNode, tableJNode, section, form);
                    controller.trigger('database::error', args);
                    controller.trigger(`database::error::${database}`, args);
                    controller.trigger(`database::error::${database}::${table}`, args);
                },
                beforeSend() {
                    loader.css('display', 'inline-block');
                },
                complete() {
                    loader.css('display', 'none');
                }
            });
        };
    });

    controller.registerTrigger('database::success', 'databaseSearch::success', (args, callback) => {
        callback();

        const doc = args[0];
        const database = args[1];
        const table = args[2];
        const databaseJNode = args[3];
        const tableJNode = args[4];
        const section = args[5];
        const form = args[6];

        const results = section.find('.results');
        const htmlNode = controller.call('xmlDocument', doc);
        htmlNode.find('.xml2html').data('form', form.serializeArray());

        results.empty().append(htmlNode);
        $('html, body').scrollTop(results.offset().top);
    });

};
