<commit_before>/*
 * Copyright 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 * Please see distribution for license.
 */
$.register_module({
    name: 'og.analytics.Grid',
    dependencies: ['og.api.text', 'og.common.events', 'og.analytics.Data', 'og.analytics.CellMenu'],
    obj: function () {
        var module = this, row_height = 21, title_height = 31, set_height = 24, logging = 'logLevel',
            templates = null, default_col_width = 175, HTML = 'innerHTML', scrollbar = (function () {
                var html = '<div style="width: 100px; height: 100px; position: absolute; \
                    visibility: hidden; overflow: auto; left: -10000px; z-index: -10000; bottom: 100px" />';
                return 100 - $(html).appendTo('body').append('<div />').find('div').css('height', '200px').width();
            })(), do_not_expand = {DOUBLE: null, NODE_ID: null, POSITION_ID: null, STRING: null};
        var available = (function () {
            var nodes;
            var all = function (total) {
                for (var result = [], lcv = 0; lcv < total; lcv += 1) result.push(lcv);
                return result;
            };
            var unravel = function (arr, result) {
                var start = arr[0], end = arr[1], children = arr[2], last_end = null,
                    i, j, len = children.length, child, curr_start, curr_end;
                result.push(start);
                if (!nodes[start]) return result;
                for (i = 0; i < len; i += 1) {
                    child = children[i]; curr_start = child[0]; curr_end = child[1];
                    for (j = (last_end || start) + 1; j < curr_start; j += 1) result.push(j);
                    last_end = start = curr_end;
                    unravel(child, result);
                }
                while (++start <= end) result.push(start);
                return result;
            };
            return function (meta) {
                nodes = meta.nodes
                return meta.structure.length ? unravel(meta.structure, []) : all(meta.data_rows);
            };
        })();
        var background = function (sets, width, bg_color) {
            var columns = sets.reduce(function (acc, set) {return acc.concat(set.columns);}, []),
                height = row_height, pixels = [], lcv, fg_color = 'dadcdd', dots = columns
                    .reduce(function (acc, col) {return acc.concat([[bg_color, col.width - 1], [fg_color, 1]]);}, []);
            for (lcv = 0; lcv < height - 1; lcv += 1) Array.prototype.push.apply(pixels, dots);
            pixels.push([fg_color, width]);
            return BMP.rle8(width, height, pixels);
        };
        var col_css = function (id, sets, offset) {
            var partial_width = 0, columns = sets.reduce(function (acc, set) {return acc.concat(set.columns);}, []),
                total_width = columns.reduce(function (acc, val) {return val.width + acc;}, 0);
            return columns.map(function (val, idx) {
                var css = {
                    prefix: id, index: idx + (offset || 0),
                    left: partial_width, right: total_width - partial_width - val.width
                };
                return (partial_width += val.width), css;
            });
        };
        var compile_templates = function (handler) {
            var grid = this, css = og.api.text({url: module.html_root + 'analytics/grid/og.analytics.grid_tash.css'}),
                header = og.api.text({module: 'og.analytics.grid.header_tash'}),
                container = og.api.text({module: 'og.analytics.grid.container_tash'}),
                loading = og.api.text({module: 'og.analytics.loading_tash'}),
                row = og.api.text({module: 'og.analytics.grid.row_tash'}), compile = Handlebars.compile;
            $.when(css, header, container, loading, row).then(function (css, header, container, loading, row) {
                templates = {
                    css: compile(css), header: compile(header),
                    container: compile(container), loading: compile(loading), row: compile(row)
                };
                handler.call(grid);
            });
        };
        var Grid = function (config) {
            var grid = this;
            grid.config = config || {};
            grid.elements = {empty: true, parent: $(config.selector).html('&nbsp;instantiating grid...')};
            grid.formatter = new og.analytics.Formatter(grid);
            grid.id = '#' + og.common.id('grid');
            grid.meta = null;
            grid.source = config.source;
            grid.updated = (function (last, delta) {
                return function (time) {
                    return time ? (last ? ((delta = time - last), (last = time), delta) : ((last = time), 0)) : delta;
                };
            })(null, 0);
            if (templates) init_data.call(grid); else compile_templates.call(grid, init_data);
        };
        var init_data = function () {
            var grid = this, config = grid.config;
            grid.busy = (function (busy) {
                return function (value) {return busy = typeof value !== 'undefined' ? value : busy;};
            })(false);
            grid.elements.parent.html(templates.loading({text: 'creating view client...'}));
            grid.dataman = new og.analytics.Data(grid.source, {bypass: false, label: 'grid'})
                .on('meta', init_grid, grid).on('data', render_rows, grid)
                .on('disconnect', function () {grid.selector.clear(); grid.clipboard.clear();})
                .on('fatal', function (error) {
                    grid.kill(), grid.elements.parent.html('&nbsp;fatal error: ' + error), grid.fire('fatal');
                })
                .on('types', function (types) {
                    grid.views = {selected: config.source.type || 'portfolio'};
                    grid.views.rest = Object.keys(types)
                        .filter(function (key) {return types[key] && key !== grid.views.selected;});
                    if (grid.elements.empty) return; else render_header.call(grid);
                });
            grid.clipboard = new og.analytics.Clipboard(grid);
        };
        var init_elements = function () {
            var grid = this, config = grid.config, elements, in_timeout, out_timeout, stall = 15,
                last_x, last_y, page_x, page_y, last_corner, cell // cached values for hover events;
            var hoverin_handler = function (event, silent) {
                (page_x = event.pageX), (page_y = event.pageY);
                if (grid.selector.busy()) return last_x = last_y = last_corner = null;
                if (page_x === last_x && page_y === last_y) return;
                var scroll_left = grid.elements.scroll_body.scrollLeft(),
                    scroll_top = grid.elements.scroll_body.scrollTop(),
                    fixed_width = grid.meta.columns.width.fixed, corner, corner_cache,
                    x = page_x - grid.offset.left + (page_x > fixed_width ? scroll_left : 0),
                    y = page_y - grid.offset.top + scroll_top - grid.meta.header_height,
                    rectangle = {top_left: (corner = grid.nearest_cell(x, y)), bottom_right: corner},
                    selection = grid.selector.selection(rectangle);
                if (!selection || last_corner === (corner_cache = JSON.stringify(corner))) return;
                if (!(cell = grid.cell(selection))) return hoverout_handler(); // cell is undefined
                if (!cell.value.v) return hoverout_handler(); // cell is empty
                cell.top = corner.top - scroll_top + grid.meta.header_height + grid.offset.top;
                cell.right = corner.right - (page_x > fixed_width ? scroll_left : 0);
                last_corner = corner_cache; last_x = page_x; last_y = page_y;
                if (!silent) grid.fire('cellhoverin', cell);
            };
            var hoverout_handler = function () {
                if (!last_x) return; else (last_x = last_y = last_corner = null), grid.fire('cellhoverout', cell);
            };
            (elements = grid.elements).style = $('<style type="text/css" />').appendTo('head');
            elements.parent.unbind().html(templates.container({id: grid.id.substring(1)}))
                .on('click', '.OG-g-h-set-name .og-js-viewchange', function (event) {
                    return grid.fire('viewchange', $(this).html().toLowerCase()), false;
                })
                .on('click', '.OG-g-h-set-name .og-dropdown', function (event) {
                    return $('.OG-g-h-set-name .og-menu').toggle(), false;
                })
                .on('click', '.OG-g-h-set-name .og-sparklines', function (event) {
                    return $(this).toggleClass('og-active').find('span')
                        .html((grid.config.sparklines = !grid.config.sparklines) ? 'ON' : 'OFF'), false;
                })
                .on('mousedown', function (event) {
                    var $target = $(event.target), row;
                    event.preventDefault();
                    if (!$target.is('.node')) return grid.fire('mousedown', event), void 0;
                    grid.meta.nodes[row = +$target.attr('data-row')] = !grid.meta.nodes[row];
                    grid.resize().selector.clear();
                    return false; // kill bubbling if it's a node
                })
                .on('contextmenu', '.OG-g-sel, .OG-g-cell', function (event) { // for cells
                    hoverin_handler(event, true); // silently run hoverin_handler to set cell
                    return grid.fire('contextmenu', cell);
                })
                .on('contextmenu', '.OG-g-h-cols', function (event) { // for column headers
                    hoverin_handler(event, true); // silently run hoverin_handler to set cell
                    var col = cell.col, columns = grid.meta.columns;
                    return grid.fire('contextmenu', ['description', 'header', 'type']
                        .reduce(function (acc, key) {return (acc[key] = columns[key + 's'][col]), acc;}, {col: col}));<commit_msg>contextmenu even includes original DOM event and separate cell and col arguments for handlers<commit_after>/*
 * Copyright 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 * Please see distribution for license.
 */
$.register_module({
    name: 'og.analytics.Grid',
    dependencies: ['og.api.text', 'og.common.events', 'og.analytics.Data', 'og.analytics.CellMenu'],
    obj: function () {
        var module = this, row_height = 21, title_height = 31, set_height = 24, logging = 'logLevel',
            templates = null, default_col_width = 175, HTML = 'innerHTML', scrollbar = (function () {
                var html = '<div style="width: 100px; height: 100px; position: absolute; \
                    visibility: hidden; overflow: auto; left: -10000px; z-index: -10000; bottom: 100px" />';
                return 100 - $(html).appendTo('body').append('<div />').find('div').css('height', '200px').width();
            })(), do_not_expand = {DOUBLE: null, NODE_ID: null, POSITION_ID: null, STRING: null};
        var available = (function () {
            var nodes;
            var all = function (total) {
                for (var result = [], lcv = 0; lcv < total; lcv += 1) result.push(lcv);
                return result;
            };
            var unravel = function (arr, result) {
                var start = arr[0], end = arr[1], children = arr[2], last_end = null,
                    i, j, len = children.length, child, curr_start, curr_end;
                result.push(start);
                if (!nodes[start]) return result;
                for (i = 0; i < len; i += 1) {
                    child = children[i]; curr_start = child[0]; curr_end = child[1];
                    for (j = (last_end || start) + 1; j < curr_start; j += 1) result.push(j);
                    last_end = start = curr_end;
                    unravel(child, result);
                }
                while (++start <= end) result.push(start);
                return result;
            };
            return function (meta) {
                nodes = meta.nodes
                return meta.structure.length ? unravel(meta.structure, []) : all(meta.data_rows);
            };
        })();
        var background = function (sets, width, bg_color) {
            var columns = sets.reduce(function (acc, set) {return acc.concat(set.columns);}, []),
                height = row_height, pixels = [], lcv, fg_color = 'dadcdd', dots = columns
                    .reduce(function (acc, col) {return acc.concat([[bg_color, col.width - 1], [fg_color, 1]]);}, []);
            for (lcv = 0; lcv < height - 1; lcv += 1) Array.prototype.push.apply(pixels, dots);
            pixels.push([fg_color, width]);
            return BMP.rle8(width, height, pixels);
        };
        var col_css = function (id, sets, offset) {
            var partial_width = 0, columns = sets.reduce(function (acc, set) {return acc.concat(set.columns);}, []),
                total_width = columns.reduce(function (acc, val) {return val.width + acc;}, 0);
            return columns.map(function (val, idx) {
                var css = {
                    prefix: id, index: idx + (offset || 0),
                    left: partial_width, right: total_width - partial_width - val.width
                };
                return (partial_width += val.width), css;
            });
        };
        var compile_templates = function (handler) {
            var grid = this, css = og.api.text({url: module.html_root + 'analytics/grid/og.analytics.grid_tash.css'}),
                header = og.api.text({module: 'og.analytics.grid.header_tash'}),
                container = og.api.text({module: 'og.analytics.grid.container_tash'}),
                loading = og.api.text({module: 'og.analytics.loading_tash'}),
                row = og.api.text({module: 'og.analytics.grid.row_tash'}), compile = Handlebars.compile;
            $.when(css, header, container, loading, row).then(function (css, header, container, loading, row) {
                templates = {
                    css: compile(css), header: compile(header),
                    container: compile(container), loading: compile(loading), row: compile(row)
                };
                handler.call(grid);
            });
        };
        var Grid = function (config) {
            var grid = this;
            grid.config = config || {};
            grid.elements = {empty: true, parent: $(config.selector).html('&nbsp;instantiating grid...')};
            grid.formatter = new og.analytics.Formatter(grid);
            grid.id = '#' + og.common.id('grid');
            grid.meta = null;
            grid.source = config.source;
            grid.updated = (function (last, delta) {
                return function (time) {
                    return time ? (last ? ((delta = time - last), (last = time), delta) : ((last = time), 0)) : delta;
                };
            })(null, 0);
            if (templates) init_data.call(grid); else compile_templates.call(grid, init_data);
        };
        var init_data = function () {
            var grid = this, config = grid.config;
            grid.busy = (function (busy) {
                return function (value) {return busy = typeof value !== 'undefined' ? value : busy;};
            })(false);
            grid.elements.parent.html(templates.loading({text: 'creating view client...'}));
            grid.dataman = new og.analytics.Data(grid.source, {bypass: false, label: 'grid'})
                .on('meta', init_grid, grid).on('data', render_rows, grid)
                .on('disconnect', function () {grid.selector.clear(); grid.clipboard.clear();})
                .on('fatal', function (error) {
                    grid.kill(), grid.elements.parent.html('&nbsp;fatal error: ' + error), grid.fire('fatal');
                })
                .on('types', function (types) {
                    grid.views = {selected: config.source.type || 'portfolio'};
                    grid.views.rest = Object.keys(types)
                        .filter(function (key) {return types[key] && key !== grid.views.selected;});
                    if (grid.elements.empty) return; else render_header.call(grid);
                });
            grid.clipboard = new og.analytics.Clipboard(grid);
        };
        var init_elements = function () {
            var grid = this, config = grid.config, elements, in_timeout, out_timeout, stall = 15,
                last_x, last_y, page_x, page_y, last_corner, cell // cached values for hover events;
            var hoverin_handler = function (event, silent) {
                (page_x = event.pageX), (page_y = event.pageY);
                if (grid.selector.busy()) return last_x = last_y = last_corner = null;
                if (page_x === last_x && page_y === last_y) return;
                var scroll_left = grid.elements.scroll_body.scrollLeft(),
                    scroll_top = grid.elements.scroll_body.scrollTop(),
                    fixed_width = grid.meta.columns.width.fixed, corner, corner_cache,
                    x = page_x - grid.offset.left + (page_x > fixed_width ? scroll_left : 0),
                    y = page_y - grid.offset.top + scroll_top - grid.meta.header_height,
                    rectangle = {top_left: (corner = grid.nearest_cell(x, y)), bottom_right: corner},
                    selection = grid.selector.selection(rectangle);
                if (!selection || last_corner === (corner_cache = JSON.stringify(corner))) return;
                if (!(cell = grid.cell(selection))) return hoverout_handler(); // cell is undefined
                if (!cell.value.v) return hoverout_handler(); // cell is empty
                cell.top = corner.top - scroll_top + grid.meta.header_height + grid.offset.top;
                cell.right = corner.right - (page_x > fixed_width ? scroll_left : 0);
                last_corner = corner_cache; last_x = page_x; last_y = page_y;
                if (!silent) grid.fire('cellhoverin', cell);
            };
            var hoverout_handler = function () {
                if (!last_x) return; else (last_x = last_y = last_corner = null), grid.fire('cellhoverout', cell);
            };
            (elements = grid.elements).style = $('<style type="text/css" />').appendTo('head');
            elements.parent.unbind().html(templates.container({id: grid.id.substring(1)}))
                .on('click', '.OG-g-h-set-name .og-js-viewchange', function (event) {
                    return grid.fire('viewchange', $(this).html().toLowerCase()), false;
                })
                .on('click', '.OG-g-h-set-name .og-dropdown', function (event) {
                    return $('.OG-g-h-set-name .og-menu').toggle(), false;
                })
                .on('click', '.OG-g-h-set-name .og-sparklines', function (event) {
                    return $(this).toggleClass('og-active').find('span')
                        .html((grid.config.sparklines = !grid.config.sparklines) ? 'ON' : 'OFF'), false;
                })
                .on('mousedown', function (event) {
                    var $target = $(event.target), row;
                    event.preventDefault();
                    if (!$target.is('.node')) return grid.fire('mousedown', event), void 0;
                    grid.meta.nodes[row = +$target.attr('data-row')] = !grid.meta.nodes[row];
                    grid.resize().selector.clear();
                    return false; // kill bubbling if it's a node
                })
                .on('contextmenu', '.OG-g-sel, .OG-g-cell', function (event) { // for cells
                    hoverin_handler(event, true); // silently run hoverin_handler to set cell
                    return grid.fire('contextmenu', event, cell, null);
                })
                .on('contextmenu', '.OG-g-h-cols', function (event) { // for column headers
                    hoverin_handler(event, true); // silently run hoverin_handler to set cell
                    var col = cell.col, columns = grid.meta.columns;
                    return grid.fire('contextmenu', event, null, ['description', 'header', 'type']
                        .reduce(function (acc, key) {return (acc[key] = columns[key + 's'][col]), acc;}, {col: col}));
{'fraction_non_alphanumeric': 0.1461169327196161, 'fraction_numerical': 0.006463617667221624, 'mean_word_length': 1.8404728789986091, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '261833', 'n_tokens_mistral': 5894, 'n_tokens_neox': 5548, 'n_words': 1378}