{"size": 5742, "ext": "js", "max_stars_repo_path": "bundles/statistics/statsgrid2016/instance.js", "max_stars_repo_name": "okauppinen/oskari", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "bundles/statistics/statsgrid2016/instance.js", "max_issues_repo_name": "okauppinen/oskari", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "bundles/statistics/statsgrid2016/instance.js", "max_forks_repo_name": "okauppinen/oskari", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.3832335329, "max_line_length": 109, "alphanum_fraction": 0.5139324277}
/**
 * @class Oskari.statistics.statsgrid.StatsGridBundleInstance
 *
 * Sample extension bundle definition which inherits most functionalty
 * from DefaultExtension class.
 *
 */
Oskari.clazz.define(
    'Oskari.statistics.statsgrid.StatsGridBundleInstance',
    /**
     * @static constructor function
     */

    function () {
        // these will be used for this.conf if nothing else is specified (handled by DefaultExtension)
        this.defaultConf = {
            name: 'StatsGrid',
            sandbox: 'sandbox',
            stateful: true,
            tileClazz: 'Oskari.userinterface.extension.DefaultTile',
            viewClazz: 'Oskari.statistics.statsgrid.StatsView'
        };
    }, {
        afterStart: function (sandbox) {
            var me = this;
            // create the StatisticsService for handling ajax calls and common functionality.
            var statsService = Oskari.clazz.create('Oskari.statistics.statsgrid.StatisticsService', sandbox);
            sandbox.registerService(statsService);
            me.statsService = statsService;

            var conf = this.getConfiguration() || {};
            statsService.addDatasource(conf.sources);

            this.getTile().setEnabled(this.hasData());
            if(this.state) {
                this.setState(this.state);
            }

            var tile = this.getTile();
            var cel = tile.container;

            if (!cel.hasClass('statsgrid2016')) {
                cel.addClass('statsgrid2016');
            }
        },
        eventHandlers: {
            'StatsGrid.IndicatorEvent' : function(evt) {
                this.statsService.notifyOskariEvent(evt);
            },
            'StatsGrid.RegionsetChangedEvent' : function(evt) {
                this.statsService.notifyOskariEvent(evt);
            },
            'StatsGrid.RegionSelectedEvent' : function(evt) {
                this.statsService.notifyOskariEvent(evt);
            },
            'StatsGrid.ActiveIndicatorChangedEvent' : function(evt) {
                this.statsService.notifyOskariEvent(evt);
            },
            'UIChangeEvent' : function() {
                // close/tear down tge ui when receiving the event
                var sandbox = this.getSandbox();
                sandbox.postRequestByName('userinterface.UpdateExtensionRequest', [this, 'close']);
            },
            /**
             * @method userinterface.ExtensionUpdatedEvent
             */
            'userinterface.ExtensionUpdatedEvent': function (event) {

                if (event.getExtension().getName() !== this.getName() || !this.hasData()) {
                    // not me/no data -> do nothing
                    return;
                }

                var isShown = event.getViewState() !== 'close';
                this.getView().prepareMode(isShown, this.getConfiguration());
            },
            /**
             * @method MapLayerEvent
             * @param {Oskari.mapframework.event.common.MapLayerEvent} event
             *
             */
            MapLayerEvent: function (event) {
                // Enable tile when stats layer is available
                this.getTile().setEnabled(this.hasData());
            }
        },
        hasData: function () {
            return this.statsService.getDatasource().length
                && this.statsService.getRegionsets().length;
        },

        /**
         * Sets the map state to one specified in the parameter. State is bundle specific, check the
         * bundle documentation for details.
         *
         * @method setState
         * @param {Object} state bundle state as JSON
         */
        setState: function (state) {
            state = state || {};
            var service = this.statsService.getStateService();
            service.reset();
            if(state.regionset) {
                service.setRegionset(state.regionset);
            }
            if(state.indicators) {
                state.indicators.forEach(function(ind) {
                    service.addIndicator(ind.ds, ind.id, ind.selections);
                });
            }
            if(state.active) {
                service.setActiveIndicator(state.active);
            }
            // if state says view was visible fire up the UI, otherwise close it
            var sandbox = this.getSandbox();
            var uimode = state.view ? 'attach' : 'close';
            sandbox.postRequestByName('userinterface.UpdateExtensionRequest', [this, uimode]);
        },

            /*
 {
     "indicators": [{
         "ds": 1,
         "id": 5,
         "selections": {
             "sex": "male",
             "year": "1991"
         }
     }, {
         "ds": 1,
         "id": 6,
         "selections": {
             "sex": "male",
             "year": "1994"
         }
     }],
     "regionset": 7,
     "active": "1_6_{\"sex\":\"male\",\"year\":\"1994\"}",
     "view" : true
 }
            */
        getState: function () {
            var service = this.statsService.getStateService();
            var state = {
                indicators : [],
                regionset : service.getRegionset(),
                view : this.getView().isVisible
            };
            service.getIndicators().forEach(function(ind) {
                state.indicators.push({
                    ds : ind.datasource,
                    id : ind.indicator,
                    selections : ind.selections
                });
            });
            var active = service.getActiveIndicator();
            if(active) {
                state.active = active.hash
            }
            return state;
        }

    }, {
        extend: ['Oskari.userinterface.extension.DefaultExtension']
    }
);
