<commit_before>Oskari.clazz.category('Oskari.statistics.bundle.statsgrid.StatsView', 'municipality-table', {
    /**
     * @method createStatsOut
     * @param
     * {obj}
     * container
     *           to where slick grid and pull downs will be appended
     * Get Sotka data and show it in slcik grid
     */
    createStatsOut : function(container) {

        // indicators (meta data)
        this.indicators = [];
        // indicator params are select-elements
        // (indicator drop down select and year & gender selects)
        this.prepareIndicatorParams(container);

        // stop events so that they don't affect other parts of the site (i.e. map)
        container.on("keyup", function(e) {
            e.stopPropagation();
        });
        container.on("keydown", function(e) {
            e.stopPropagation();
        });

    },
    /**
     * @method prepareIndicatorParams
     * @param container element where indicator-selector should be added
     */
    prepareIndicatorParams : function(container) {
        
        var selectors = jQuery('<div class="selectors-container"></div>');
        container.append(selectors);
        
        // Indicators 
        // success -> createIndicators      
        this.getSotkaIndicators(container);
        // Regions: success createMunicipalityGrid
        this.getSotkaRegionData(container);
    },
    /**
     * Fetch region data - we need to know all the regions / municipalities 
     * @method getSotkaRegionData
     */
    getSotkaRegionData : function(container) {
        var me = this;
        // call ajax function (params: url, successFallback, errorCallback)
        me.fetchData(
            me.instance.getSandbox().getAjaxUrl() + 'action_route=GetSotkaData&action=regions&version=1.1',
            // success callback
            function(regionData){
                if (regionData) {
                    // get the actual data
                    //me.createMunicipalitySlickGrid(container, indicator, genders, years, indicatorMeta, regionData);
                    me.createMunicipalitySlickGrid(container, regionData);
                } else {
                    me.instance.showMessage(me.instance.getLocalization('sotka').errorTitle,
                        me.instance.getLocalization('sotka').regionDataError);
                }
            }, 
            // error callback
            function(jqXHR, textStatus){
                    me.instance.showMessage(me.instance.getLocalization('sotka').errorTitle,
                        me.instance.getLocalization('sotka').regionDataXHRError);
            }
        );
    },

    /**
     * Create initial grid using just one column: municipality
     * @method createMunicipalitySlickGrid
     */
    createMunicipalitySlickGrid : function(container, regiondata) {
        var me = this;
        var grid;
        var gridContainer = jQuery('<div id="municipalGrid" class="municipal-grid"></div>');
        // clear and append municipal-grid container
        container.find('.municipal-grid').remove();
        container.append(gridContainer);
        // add one column
        var columns = [{
            id : "municipality",
            name : this.instance.getLocalization("sotka").municipality,
            field : "municipality",
            sortable : true
        }];

        // options
        var options = {
            enableCellNavigation : true,
            enableColumnReorder : false,
            multiColumnSort: true
        };
        var data = [];
        var rowId = 0;
        // loop through regiondata and find all the municipalities
        for (var i = 0; i < regiondata.length; i++) {
            var indicData = regiondata[i];

            if (indicData["category"] == 'KUNTA') {
                // add new row with id and name of municipality
                data[rowId] = {
                    id : indicData.id, 
                    municipality: indicData.title[Oskari.getLang()]
                }
                rowId++;
            }

        }
        // metadata provider for data view
        var groupItemMetadataProvider = new Slick.Data.GroupItemMetadataProvider();
        // dataview for the grid
        dataView = new Slick.Data.DataView({
            groupItemMetadataProvider : groupItemMetadataProvider,
            inlineFilters : true
        });
        // when the row changes re-render that row
        dataView.onRowsChanged.subscribe(function(e, args) {
            grid.invalidateRows(args.rows);
            grid.render();
        });
        // Grid
        grid = new Slick.Grid(gridContainer, dataView, columns, options);

        var sortcol = "json_number";
        var sortdir = 1;
        // when user sorts this grid according to selected column
        // we need to provide sort-function
        grid.onSort.subscribe(function(e, args) {
            var cols = args.sortCols;

            dataView.sort(function (dataRow1, dataRow2) {
                for (var i = 0, l = cols.length; i < l; i++) {
                    var field = cols[i].sortCol.field;
                    var sign = cols[i].sortAsc ? 1 : -1;
                    var value1 = dataRow1[field], value2 = dataRow2[field];
                    var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
                    if (result != 0) {
                        return result;
                    }
                }
                return 0;
            });
            grid.invalidate();
            grid.render();
        });

        grid.onHeaderClick.subscribe(function(e, args) {
            var columnId = args.column.id,
                geoStatsValues = [],
                columnData,
                gstats;

            if(!columnId.match(/^indicator/)) return;

            for(var i = 0; i < grid.getDataLength(); ++i) {
                var val = Number(grid.getDataItem(i)[columnId]);
                if(val) geoStatsValues.push(val);
            }
            gstats = new geostats(geoStatsValues);

            var visClasses = "360,175,160,210,435,13,27,26,366,208,196,166,15,272,30,363,346," +
            "223,130,32,400,139,111,258,325,171,106,103,34,18,101,306,349,375,381,132,319,232," +
            "340,50,278,19,192,188,379,87,238,220,83,297,209,56,127,440,226,307,148,69,125,417," +
            "420,356,291,302,48,233,410,157,150,387,116,334,53,449,24,110,54,23,176,174,4,392,181," +
            "361,144,274,301,399,321,424,342,191,395,300,38,430,405,224,304,93,264,382,214,422,183," +
            "385,352,318,431,402,299,295,45,266,343,320,273,217,163,143,314,423,12,338,280,370,316," +
            "115,374,76,108,421,285,358,444,371,436,290,173,324,373,250,330,70,384,78,283,212,11,213," +
            "80,293,398,348,77,82,303,35,123,68,328,216,268,350,75,64,102,243,383,99,305,331,22,798,354,"+
            "251,241,451,136,246,376,394,154,854,66,84,310,269,61,427,153,793,29,263,62,432,37,369,403," +
            "28,260,141,237,178,72,118,252,206,179,9,311,119,296,368,355,43,114,231,138,135,107,5,357," +
            "156,36,255,200,235,259,59,221,39,262,336,2,195,182,126,57,265,52,359,271,247,795,219,100," +
            "63,201,197,21,95,131,397,112,145,164,445,186,244,404,207|794,167,257,121,204,73,198,732,113," +
            "25,288,187,353,242,234,41,282,284,44,452,239,408,33,351,193,49,60,117,414,88,388,312,797,313," +
            "344,65,425,796,323,97,92,448,248,122,133,393,94,91,317,202,253,55,142,228|294,339,151,347,327," +
            "406,58,184,74,292,152,161,177|85,261,389,413,378|20,46";

            var statsParams = {
                VIS_ID: -1,
                VIS_NAME: "tilastoalueet:kunnat2013",
                VIS_ATTR: "kuntakoodi",
                VIS_CLASSES: visClasses,
                VIS_COLORS: "choro:edf8e9|bae4b3|74c476|31a354|006d2c"
            };

            var sandbox = me.instance.getSandbox();
            var eventBuilder = sandbox.getEventBuilder('MapStats.StatsVisualizationChangeEvent');
            if(eventBuilder) {
                var event = eventBuilder(me._layer, statsParams);
                sandbox.notifyAll(event);
            }
        });

        // notify dataview that we are starting to update data
        dataView.beginUpdate();
        // set municipality data
        dataView.setItems(data);
        // notify data view that we have updated data
        dataView.endUpdate();
        // invalidate() -> the values in the grid are not correct -> invalidate
        grid.invalidate();
        // render the grid
        grid.render();
        // remember the grid object.
        this.grid = grid;
        this.dataView = dataView;

    },
<commit_msg>Changed the ids of VIS_CLASSES to be of kuntakoodi type (added leading zeros).<commit_after>Oskari.clazz.category('Oskari.statistics.bundle.statsgrid.StatsView', 'municipality-table', {
    /**
     * @method createStatsOut
     * @param
     * {obj}
     * container
     *           to where slick grid and pull downs will be appended
     * Get Sotka data and show it in slcik grid
     */
    createStatsOut : function(container) {

        // indicators (meta data)
        this.indicators = [];
        // indicator params are select-elements
        // (indicator drop down select and year & gender selects)
        this.prepareIndicatorParams(container);

        // stop events so that they don't affect other parts of the site (i.e. map)
        container.on("keyup", function(e) {
            e.stopPropagation();
        });
        container.on("keydown", function(e) {
            e.stopPropagation();
        });

    },
    /**
     * @method prepareIndicatorParams
     * @param container element where indicator-selector should be added
     */
    prepareIndicatorParams : function(container) {
        
        var selectors = jQuery('<div class="selectors-container"></div>');
        container.append(selectors);
        
        // Indicators 
        // success -> createIndicators      
        this.getSotkaIndicators(container);
        // Regions: success createMunicipalityGrid
        this.getSotkaRegionData(container);
    },
    /**
     * Fetch region data - we need to know all the regions / municipalities 
     * @method getSotkaRegionData
     */
    getSotkaRegionData : function(container) {
        var me = this;
        // call ajax function (params: url, successFallback, errorCallback)
        me.fetchData(
            me.instance.getSandbox().getAjaxUrl() + 'action_route=GetSotkaData&action=regions&version=1.1',
            // success callback
            function(regionData){
                if (regionData) {
                    // get the actual data
                    //me.createMunicipalitySlickGrid(container, indicator, genders, years, indicatorMeta, regionData);
                    me.createMunicipalitySlickGrid(container, regionData);
                } else {
                    me.instance.showMessage(me.instance.getLocalization('sotka').errorTitle,
                        me.instance.getLocalization('sotka').regionDataError);
                }
            }, 
            // error callback
            function(jqXHR, textStatus){
                    me.instance.showMessage(me.instance.getLocalization('sotka').errorTitle,
                        me.instance.getLocalization('sotka').regionDataXHRError);
            }
        );
    },

    /**
     * Create initial grid using just one column: municipality
     * @method createMunicipalitySlickGrid
     */
    createMunicipalitySlickGrid : function(container, regiondata) {
        var me = this;
        var grid;
        var gridContainer = jQuery('<div id="municipalGrid" class="municipal-grid"></div>');
        // clear and append municipal-grid container
        container.find('.municipal-grid').remove();
        container.append(gridContainer);
        // add one column
        var columns = [{
            id : "municipality",
            name : this.instance.getLocalization("sotka").municipality,
            field : "municipality",
            sortable : true
        }];

        // options
        var options = {
            enableCellNavigation : true,
            enableColumnReorder : false,
            multiColumnSort: true
        };
        var data = [];
        var rowId = 0;
        // loop through regiondata and find all the municipalities
        for (var i = 0; i < regiondata.length; i++) {
            var indicData = regiondata[i];

            if (indicData["category"] == 'KUNTA') {
                // add new row with id and name of municipality
                data[rowId] = {
                    id : indicData.id, 
                    municipality: indicData.title[Oskari.getLang()]
                }
                rowId++;
            }

        }
        // metadata provider for data view
        var groupItemMetadataProvider = new Slick.Data.GroupItemMetadataProvider();
        // dataview for the grid
        dataView = new Slick.Data.DataView({
            groupItemMetadataProvider : groupItemMetadataProvider,
            inlineFilters : true
        });
        // when the row changes re-render that row
        dataView.onRowsChanged.subscribe(function(e, args) {
            grid.invalidateRows(args.rows);
            grid.render();
        });
        // Grid
        grid = new Slick.Grid(gridContainer, dataView, columns, options);

        var sortcol = "json_number";
        var sortdir = 1;
        // when user sorts this grid according to selected column
        // we need to provide sort-function
        grid.onSort.subscribe(function(e, args) {
            var cols = args.sortCols;

            dataView.sort(function (dataRow1, dataRow2) {
                for (var i = 0, l = cols.length; i < l; i++) {
                    var field = cols[i].sortCol.field;
                    var sign = cols[i].sortAsc ? 1 : -1;
                    var value1 = dataRow1[field], value2 = dataRow2[field];
                    var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
                    if (result != 0) {
                        return result;
                    }
                }
                return 0;
            });
            grid.invalidate();
            grid.render();
        });

        grid.onHeaderClick.subscribe(function(e, args) {
            var columnId = args.column.id,
                geoStatsValues = [],
                columnData,
                gstats;

            if(!columnId.match(/^indicator/)) return;

            for(var i = 0; i < grid.getDataLength(); ++i) {
                var val = Number(grid.getDataItem(i)[columnId]);
                if(val) geoStatsValues.push(val);
            }
            gstats = new geostats(geoStatsValues);

            var visClasses = "360,175,160,210,435,013,027,026,366,208,196,166,015,272,030,363,346," +
            "223,130,032,400,139,111,258,325,171,106,103,034,018,101,306,349,375,381,132,319,232," +
            "340,050,278,019,192,188,379,087,238,220,083,297,209,056,127,440,226,307,148,069,125,417," +
            "420,356,291,302,048,233,410,157,150,387,116,334,053,449,024,110,054,023,176,174,4,392,181," +
            "361,144,274,301,399,321,424,342,191,395,300,038,430,405,224,304,093,264,382,214,422,183," +
            "385,352,318,431,402,299,295,045,266,343,320,273,217,163,143,314,423,012,338,280,370,316," +
            "115,374,076,108,421,285,358,444,371,436,290,173,324,373,250,330,070,384,078,283,212,011,213," +
            "080,293,398,348,077,082,303,035,123,068,328,216,268,350,075,064,102,243,383,099,305,331,022,798,354,"+
            "251,241,451,136,246,376,394,154,854,066,084,310,269,061,427,153,793,029,263,062,432,037,369,403," +
            "028,260,141,237,178,072,118,252,206,179,9,311,119,296,368,355,043,114,231,138,135,107,5,357," +
            "156,036,255,200,235,259,059,221,039,262,336,2,195,182,126,057,265,052,359,271,247,795,219,100," +
            "063,201,197,021,095,131,397,112,145,164,445,186,244,404,207|794,167,257,121,204,073,198,732,113," +
            "025,288,187,353,242,234,041,282,284,044,452,239,408,033,351,193,049,060,117,414,088,388,312,797,313," +
            "344,065,425,796,323,097,092,448,248,122,133,393,094,091,317,202,253,055,142,228|294,339,151,347,327," +
            "406,058,184,074,292,152,161,177|085,261,389,413,378|020,046";

            var statsParams = {
                VIS_ID: -1,
                VIS_NAME: "ows:kunnat2013",
                VIS_ATTR: "kuntakoodi",
                VIS_CLASSES: visClasses,
                VIS_COLORS: "choro:edf8e9|bae4b3|74c476|31a354|006d2c"
            };

            var sandbox = me.instance.getSandbox();
            var eventBuilder = sandbox.getEventBuilder('MapStats.StatsVisualizationChangeEvent');
            if(eventBuilder) {
                var event = eventBuilder(me._layer, statsParams);
                sandbox.notifyAll(event);
            }
        });

        // notify dataview that we are starting to update data
        dataView.beginUpdate();
        // set municipality data
        dataView.setItems(data);
        // notify data view that we have updated data
        dataView.endUpdate();
        // invalidate() -> the values in the grid are not correct -> invalidate
        grid.invalidate();
        // render the grid
        grid.render();
        // remember the grid object.
        this.grid = grid;
        this.dataView = dataView;

    },

{'fraction_non_alphanumeric': 0.12768271046757854, 'fraction_numerical': 0.11572139872946832, 'mean_word_length': 2.020048392671967, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '6982505', 'n_tokens_mistral': 6635, 'n_tokens_neox': 5185, 'n_words': 922}