<commit_before>/*
 * ************************************************************************
 * Copyright (c), 2013 Next Century Corporation. All Rights Reserved.
 *
 * This software code is the exclusive property of Next Century Corporation and is
 * protected by United States and International laws relating to the protection
 * of intellectual property.  Distribution of this software code by or to an
 * unauthorized party, or removal of any of these notices, is strictly
 * prohibited and punishable by law.
 *
 * UNLESS PROVIDED OTHERWISE IN A LICENSE AGREEMENT GOVERNING THE USE OF THIS
 * SOFTWARE, TO WHICH YOU ARE AN AUTHORIZED PARTY, THIS SOFTWARE CODE HAS BEEN
 * ACQUIRED BY YOU "AS IS" AND WITHOUT WARRANTY OF ANY KIND.  ANY USE BY YOU OF
 * THIS SOFTWARE CODE IS AT YOUR OWN RISK.  ALL WARRANTIES OF ANY KIND, EITHER
 * EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE HEREBY EXPRESSLY
 * DISCLAIMED.
 *
 * PROPRIETARY AND CONFIDENTIAL TRADE SECRET MATERIAL NOT FOR DISCLOSURE OUTSIDE
 * OF NEXT CENTURY CORPORATION EXCEPT BY PRIOR WRITTEN PERMISSION AND WHEN
 * RECIPIENT IS UNDER OBLIGATION TO MAINTAIN SECRECY.
 */

$(function () {

    OWF.ready(function () {
        OWF.relayFile = 'js/eventing/rpc_relay.uncompressed.html';
        neon.query.SERVER_URL = $("#neon-server").val();

        var databaseName;
        var tableName;
        var filterKey;
        var clientId = OWF.getInstanceId();

        var options = ['latitude', 'longitude', 'color-by', 'size-by'];

        neon.eventing.messageHandler.subscribeToNeonEvents({
            activeDatasetChanged: onActiveDatasetChanged,
            filtersChanged: onFiltersChanged
        });

        var map;


        initialize();
        restoreState();

        function initialize() {
            map = new coreMap.Map("map");
            installOptionsPanels();
            setMapMappingFunctions();
            setLayerChangeListener();
            setApplyFiltersListener();
            map.draw();
            map.register("moveend", this, onMapMovement);
        }

        function onMapMovement() {
            var checked = $("#auto-filter").is(':checked');

            if(checked && neon.mapWidgetUtils.latitudeAndLongitudeAreSelected()){
                $("#map-redraw-button").attr("disabled", true);
                var filter = createFilterFromExtent();
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);
            }
            else{
                $("#map-redraw-button").removeAttr("disabled");
                var query = buildQuery();
                var stateObject = buildStateObject(query);
                neon.query.saveState(clientId, stateObject);
            }
        }

        function installOptionsPanels(){
            neon.toggle.createOptionsPanel("#options-panel");
            $("#functions-toggle-id").click(function() {
                $("#functions-panel").slideToggle("slow");
            });
        }

        function setMapMappingFunctions() {
            neon.mapWidgetUtils.addDropdownChangeListener("latitude", function (value) {
                map.latitudeMapping = value;
            });
            neon.mapWidgetUtils.addDropdownChangeListener("longitude", function (value) {
                map.longitudeMapping = value;
            });
            neon.mapWidgetUtils.addDropdownChangeListener("size-by", function (value) {
                if(value){
                    map.sizeMapping = value;
                }
                else{
                    map.sizeMapping = coreMap.Map.DEFAULT_SIZE_MAPPING;
                }
            });
            neon.mapWidgetUtils.addDropdownChangeListener("color-by", function (value) {
                map.colorMapping = value;
            });
        }

        function setLayerChangeListener() {
            neon.mapWidgetUtils.setLayerChangeListener(function () {
                if ($('#points').is(':checked')) {
                    map.toggleLayers();
                    $('#color-by-group').show();
                }
                else {
                    map.toggleLayers();
                    $('#color-by-group').hide();
                }
                var query = buildQuery();
                var stateObject = buildStateObject(query);
                neon.query.saveState(clientId, stateObject);
            });
        }

        function setApplyFiltersListener(){
            $('#map-redraw-button').click(function() {
                var filter = createFilterFromExtent();
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);
            });
            $('#map-reset-button').click(function() {
                map.reset();
                var filter = new neon.query.Filter().selectFrom(databaseName, tableName);
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);

            });
            $('#auto-filter').click(onMapMovement);
        }

        function createFilterFromExtent(){
            var lonField = neon.mapWidgetUtils.getLongitudeField();
            var latField = neon.mapWidgetUtils.getLatitudeField();

            var extent = map.getExtent();
            var leftClause = neon.query.where(lonField, ">=", extent.minimumLongitude);
            var rightClause = neon.query.where(lonField, "<=", extent.maximumLongitude);
            var bottomClause = neon.query.where(latField, ">=", extent.minimumLatitude);
            var topClause = neon.query.where(latField, "<=", extent.maximumLatitude);
            var filterClause = neon.query.and(leftClause, rightClause, bottomClause, topClause);

            //if the current extent includes the international date line
            if(extent.minimumLongitude && extent.maximumLongitude < 0) {
                leftClause = neon.query.where(lonField, ">=", extent.minimumLongitude + 360);
                var leftDateLine = neon.query.where(lonField, "<=", 180);
                var rightDateLine = neon.query.where(lonField, ">=", -180);

                var datelineClause = neon.query.or(neon.query.and(leftClause, leftDateLine), neon.query.and(rightClause, rightDateLine));
                filterClause = neon.query.and(topClause, bottomClause, datelineClause);
            }

            return new neon.query.Filter().selectFrom(databaseName, tableName).where(filterClause);
        }

        function onActiveDatasetChanged(message) {
            databaseName = message.database;
            tableName = message.table;
            map.reset();
            neon.query.registerForFilterKey(databaseName, tableName, function (filterResponse) {
                filterKey = filterResponse;
            });
            neon.query.getFieldNames(databaseName, tableName, neon.widget.MAP, populateFromColumns);
        }

        function populateFromColumns(data) {
            neon.dropdown.populateAttributeDropdowns(data, options, queryForMapData);
        }

        function onFiltersChanged() {
            queryForMapData();
        }

        function queryForMapData(){
            if(!neon.mapWidgetUtils.latitudeAndLongitudeAreSelected()){
                return;
            }

            var query = buildQuery();
            var stateObject = buildStateObject(query);
            neon.query.executeQuery(query, redrawMapData);
            neon.query.saveState(clientId, stateObject);
        }

        function buildQuery() {
            var query = new neon.query.Query().selectFrom(databaseName, tableName);
            appendGroupByClause(query);
            appendSizeByFieldClause(query);
            return query;
        }

        function appendGroupByClause(query) {
            var groupByFields = [neon.mapWidgetUtils.getLatitudeField(), neon.mapWidgetUtils.getLongitudeField()];
            var colorByField = neon.mapWidgetUtils.getColorByField();

            if (colorByField) {
                groupByFields.push(colorByField);
            }
            neon.query.Query.prototype.groupBy.apply(query, groupByFields);
        }

        function appendSizeByFieldClause(query) {
            // if a specific field to size the radius by was chosen, use that. otherwise use a generic count
            var sizeByField = neon.mapWidgetUtils.getSizeByField();
            if (sizeByField) {
                query.aggregate(neon.query.SUM, sizeByField, sizeByField);
            }
            else {
                query.aggregate(neon.query.COUNT, null, coreMap.Map.DEFAULT_SIZE_MAPPING);
            }
        }

        function redrawMapData(mapData){
            map.setData(mapData.data);
            map.draw();
        }

        function buildStateObject(query) {
            return {
                filterKey: filterKey,
                columns: neon.dropdown.getFieldNamesFromDropdown("latitude"),
                selectedLatitude: neon.mapWidgetUtils.getLatitudeField(),
                selectedLongitude: neon.mapWidgetUtils.getLongitudeField(),
                selectedColorBy: neon.mapWidgetUtils.getColorByField(),
                selectedSizeBy: neon.mapWidgetUtils.getSizeByField(),
                selectedLayer: neon.mapWidgetUtils.getLayer(),
                selectedExtent: map.getExtent(),
                autoFilterChecked: $("#auto-filter").is(':checked'),
                query: query
            };
        }

        function restoreState() {
            neon.query.getSavedState(clientId, function (data) {
                filterKey = data.filterKey;
                if(!filterKey){
                    return;
                }
                databaseName = data.filterKey.dataSet.databaseName;
                tableName = data.filterKey.dataSet.tableName;
                neon.dropdown.populateAttributeDropdowns(data.columns, options, queryForMapData);
                neon.dropdown.setDropdownInitialValue("latitude", data.selectedLatitude);
                neon.dropdown.setDropdownInitialValue("longitude", data.selectedLongitude);
                neon.dropdown.setDropdownInitialValue("color-by", data.selectedColorBy);
                neon.dropdown.setDropdownInitialValue("size-by", data.selectedSizeBy);

                _.each(options, function(selector) {
                    $('#' + selector).change();
                });

                if(data.autoFilterChecked){
                    $('#auto-filter').attr('checked', true);
                }

                neon.mapWidgetUtils.setLayer(data.selectedLayer);
                if(data.selectedLayer === "heatmap") {
                    map.toggleLayers();
                    $('#color-by-group').hide();
                    $('#heatmap').attr('checked', true);
                }
                map.zoomToExtent(data.selectedExtent);

                neon.query.executeQuery(data.query, redrawMapData);
            });
        }

    });
});
<commit_msg>Fixed bug in filter calculation for map<commit_after>/*
 * ************************************************************************
 * Copyright (c), 2013 Next Century Corporation. All Rights Reserved.
 *
 * This software code is the exclusive property of Next Century Corporation and is
 * protected by United States and International laws relating to the protection
 * of intellectual property.  Distribution of this software code by or to an
 * unauthorized party, or removal of any of these notices, is strictly
 * prohibited and punishable by law.
 *
 * UNLESS PROVIDED OTHERWISE IN A LICENSE AGREEMENT GOVERNING THE USE OF THIS
 * SOFTWARE, TO WHICH YOU ARE AN AUTHORIZED PARTY, THIS SOFTWARE CODE HAS BEEN
 * ACQUIRED BY YOU "AS IS" AND WITHOUT WARRANTY OF ANY KIND.  ANY USE BY YOU OF
 * THIS SOFTWARE CODE IS AT YOUR OWN RISK.  ALL WARRANTIES OF ANY KIND, EITHER
 * EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE HEREBY EXPRESSLY
 * DISCLAIMED.
 *
 * PROPRIETARY AND CONFIDENTIAL TRADE SECRET MATERIAL NOT FOR DISCLOSURE OUTSIDE
 * OF NEXT CENTURY CORPORATION EXCEPT BY PRIOR WRITTEN PERMISSION AND WHEN
 * RECIPIENT IS UNDER OBLIGATION TO MAINTAIN SECRECY.
 */

$(function () {

    OWF.ready(function () {
        OWF.relayFile = 'js/eventing/rpc_relay.uncompressed.html';
        neon.query.SERVER_URL = $("#neon-server").val();

        var databaseName;
        var tableName;
        var filterKey;
        var clientId = OWF.getInstanceId();

        var options = ['latitude', 'longitude', 'color-by', 'size-by'];

        neon.eventing.messageHandler.subscribeToNeonEvents({
            activeDatasetChanged: onActiveDatasetChanged,
            filtersChanged: onFiltersChanged
        });

        var map;


        initialize();
        restoreState();

        function initialize() {
            map = new coreMap.Map("map");
            installOptionsPanels();
            setMapMappingFunctions();
            setLayerChangeListener();
            setApplyFiltersListener();
            map.draw();
            map.register("moveend", this, onMapMovement);
        }

        function onMapMovement() {
            var checked = $("#auto-filter").is(':checked');

            if(checked && neon.mapWidgetUtils.latitudeAndLongitudeAreSelected()){
                $("#map-redraw-button").attr("disabled", true);
                var filter = createFilterFromExtent();
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);
            }
            else{
                $("#map-redraw-button").removeAttr("disabled");
                var query = buildQuery();
                var stateObject = buildStateObject(query);
                neon.query.saveState(clientId, stateObject);
            }
        }

        function installOptionsPanels(){
            neon.toggle.createOptionsPanel("#options-panel");
            $("#functions-toggle-id").click(function() {
                $("#functions-panel").slideToggle("slow");
            });
        }

        function setMapMappingFunctions() {
            neon.mapWidgetUtils.addDropdownChangeListener("latitude", function (value) {
                map.latitudeMapping = value;
            });
            neon.mapWidgetUtils.addDropdownChangeListener("longitude", function (value) {
                map.longitudeMapping = value;
            });
            neon.mapWidgetUtils.addDropdownChangeListener("size-by", function (value) {
                if(value){
                    map.sizeMapping = value;
                }
                else{
                    map.sizeMapping = coreMap.Map.DEFAULT_SIZE_MAPPING;
                }
            });
            neon.mapWidgetUtils.addDropdownChangeListener("color-by", function (value) {
                map.colorMapping = value;
            });
        }

        function setLayerChangeListener() {
            neon.mapWidgetUtils.setLayerChangeListener(function () {
                if ($('#points').is(':checked')) {
                    map.toggleLayers();
                    $('#color-by-group').show();
                }
                else {
                    map.toggleLayers();
                    $('#color-by-group').hide();
                }
                var query = buildQuery();
                var stateObject = buildStateObject(query);
                neon.query.saveState(clientId, stateObject);
            });
        }

        function setApplyFiltersListener(){
            $('#map-redraw-button').click(function() {
                var filter = createFilterFromExtent();
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);
            });
            $('#map-reset-button').click(function() {
                map.reset();
                var filter = new neon.query.Filter().selectFrom(databaseName, tableName);
                neon.eventing.owfEventPublisher.replaceFilter(filterKey, filter);

            });
            $('#auto-filter').click(onMapMovement);
        }

        function createFilterFromExtent(){
            var lonField = neon.mapWidgetUtils.getLongitudeField();
            var latField = neon.mapWidgetUtils.getLatitudeField();

            var extent = map.getExtent();

            var leftClause = neon.query.where(lonField, ">=", extent.minimumLongitude);
            var rightClause = neon.query.where(lonField, "<=", extent.maximumLongitude);
            var bottomClause = neon.query.where(latField, ">=", extent.minimumLatitude);
            var topClause = neon.query.where(latField, "<=", extent.maximumLatitude);
            var filterClause = neon.query.and(leftClause, rightClause, bottomClause, topClause);

            //Deal with different dateline crossing scenarios.
            if(extent.minimumLongitude < -180 && extent.maximumLongitude > 180){
                filterClause = neon.query.and(topClause, bottomClause);
            }
            else if(extent.minimumLongitude < -180) {
                leftClause = neon.query.where(lonField, ">=", extent.minimumLongitude + 360);
                var leftDateLine = neon.query.where(lonField, "<=", 180);
                var rightDateLine = neon.query.where(lonField, ">=", -180);
                var datelineClause = neon.query.or(neon.query.and(leftClause, leftDateLine), neon.query.and(rightClause, rightDateLine));
                filterClause = neon.query.and(topClause, bottomClause, datelineClause);
            }
            else if(extent.maximumLongitude > 180){
                rightClause = neon.query.where(lonField, "<=", extent.maximumLongitude - 360);
                var rightDateLine = neon.query.where(lonField, ">=", -180);
                var leftDateLine = neon.query.where(lonField, "<=", 180);
                var datelineClause = neon.query.or(neon.query.and(leftClause, leftDateLine), neon.query.and(rightClause, rightDateLine));
                filterClause = neon.query.and(topClause, bottomClause, datelineClause);
            }

            return new neon.query.Filter().selectFrom(databaseName, tableName).where(filterClause);
        }

        function onActiveDatasetChanged(message) {
            databaseName = message.database;
            tableName = message.table;
            map.reset();
            neon.query.registerForFilterKey(databaseName, tableName, function (filterResponse) {
                filterKey = filterResponse;
            });
            neon.query.getFieldNames(databaseName, tableName, neon.widget.MAP, populateFromColumns);
        }

        function populateFromColumns(data) {
            neon.dropdown.populateAttributeDropdowns(data, options, queryForMapData);
        }

        function onFiltersChanged() {
            queryForMapData();
        }

        function queryForMapData(){
            if(!neon.mapWidgetUtils.latitudeAndLongitudeAreSelected()){
                return;
            }

            var query = buildQuery();
            var stateObject = buildStateObject(query);
            neon.query.executeQuery(query, redrawMapData);
            neon.query.saveState(clientId, stateObject);
        }

        function buildQuery() {
            var query = new neon.query.Query().selectFrom(databaseName, tableName);
            appendGroupByClause(query);
            appendSizeByFieldClause(query);
            return query;
        }

        function appendGroupByClause(query) {
            var groupByFields = [neon.mapWidgetUtils.getLatitudeField(), neon.mapWidgetUtils.getLongitudeField()];
            var colorByField = neon.mapWidgetUtils.getColorByField();

            if (colorByField) {
                groupByFields.push(colorByField);
            }
            neon.query.Query.prototype.groupBy.apply(query, groupByFields);
        }

        function appendSizeByFieldClause(query) {
            // if a specific field to size the radius by was chosen, use that. otherwise use a generic count
            var sizeByField = neon.mapWidgetUtils.getSizeByField();
            if (sizeByField) {
                query.aggregate(neon.query.SUM, sizeByField, sizeByField);
            }
            else {
                query.aggregate(neon.query.COUNT, null, coreMap.Map.DEFAULT_SIZE_MAPPING);
            }
        }

        function redrawMapData(mapData){
            map.setData(mapData.data);
            map.draw();
        }

        function buildStateObject(query) {
            return {
                filterKey: filterKey,
                columns: neon.dropdown.getFieldNamesFromDropdown("latitude"),
                selectedLatitude: neon.mapWidgetUtils.getLatitudeField(),
                selectedLongitude: neon.mapWidgetUtils.getLongitudeField(),
                selectedColorBy: neon.mapWidgetUtils.getColorByField(),
                selectedSizeBy: neon.mapWidgetUtils.getSizeByField(),
                selectedLayer: neon.mapWidgetUtils.getLayer(),
                selectedExtent: map.getExtent(),
                autoFilterChecked: $("#auto-filter").is(':checked'),
                query: query
            };
        }

        function restoreState() {
            neon.query.getSavedState(clientId, function (data) {
                filterKey = data.filterKey;
                if(!filterKey){
                    return;
                }
                databaseName = data.filterKey.dataSet.databaseName;
                tableName = data.filterKey.dataSet.tableName;
                neon.dropdown.populateAttributeDropdowns(data.columns, options, queryForMapData);
                neon.dropdown.setDropdownInitialValue("latitude", data.selectedLatitude);
                neon.dropdown.setDropdownInitialValue("longitude", data.selectedLongitude);
                neon.dropdown.setDropdownInitialValue("color-by", data.selectedColorBy);
                neon.dropdown.setDropdownInitialValue("size-by", data.selectedSizeBy);

                _.each(options, function(selector) {
                    $('#' + selector).change();
                });

                if(data.autoFilterChecked){
                    $('#auto-filter').attr('checked', true);
                }

                neon.mapWidgetUtils.setLayer(data.selectedLayer);
                if(data.selectedLayer === "heatmap") {
                    map.toggleLayers();
                    $('#color-by-group').hide();
                    $('#heatmap').attr('checked', true);
                }
                map.zoomToExtent(data.selectedExtent);

                neon.query.executeQuery(data.query, redrawMapData);
            });
        }

    });
});

{'fraction_non_alphanumeric': 0.111091432366615, 'fraction_numerical': 0.002125304405578924, 'mean_word_length': 2.0215384615384617, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '4323223', 'n_tokens_mistral': 5974, 'n_tokens_neox': 5619, 'n_words': 1101}