<commit_before>"use strict";

function GeoField(options) {
    var self = this;
    var defaultLocation = options.defaultLocation;

    defaultLocation = new google.maps.LatLng(
        parseFloat(defaultLocation.lat),
        parseFloat(defaultLocation.lng)
    );

    this.zoom = options.zoom;
    this.srid = options.srid;
    this.sourceField = $(options.sourceSelector);
    this.addressField = $(options.addressSelector);
    this.latLngField = $(options.latLngDisplaySelector);
    this.geocoder = new google.maps.Geocoder();

    this.geoWarningClassName = 'wagtailgeowidget__geo-warning';
    this.geoSuccessClassName = 'wagtailgeowidget__geo-success';

    this.initMap(options.mapEl, defaultLocation);
    this.initEvents();

    this.setMapPosition(defaultLocation);
    this.updateLatLng(defaultLocation);

    this.checkVisibility(function() {
        google.maps.event.trigger(self.map, 'resize');
        var coords = $(self.latLngField).val();
        self.updateMapFromCoords(coords)
    });
}

GeoField.prototype.initMap = function(mapEl, defaultLocation) {
    var map = new google.maps.Map(mapEl, {
        zoom: this.zoom,
        center: defaultLocation
    });

    var marker = new google.maps.Marker({
        position: defaultLocation,
        map: map,
        draggable: true
    });

    this.map = map;
    this.marker = marker;
}

GeoField.prototype.initEvents = function() {
    var self = this;
    var autocomplete = new google.maps.places.Autocomplete(this.addressField[0]);

    google.maps.event.addListener(this.marker, "dragend", function(event) {
        self.setMapPosition(event.latLng);
        self.updateLatLng(event.latLng);
        self.writeLocation(event.latLng);
    });

    this.latLngField.on("input", function(e) {
        var coords = $(this).val();
        self.updateMapFromCoords(coords);
    });

    this.addressField.on("keydown", function(e) {
        if (e.keyCode === 13) {
            e.preventDefault();
            e.stopPropagation();
            self.geocodeSearch($(this).val());
        }
    });

    this.addressField.on("input", function(e) {
        clearTimeout(self._timeoutId);

        var query = $(this).val();

        if (query === "") {
            self.clearWarning();
            self.clearSuccess();
            return;
        }

        self._timeoutId = setTimeout(function() {
            self.geocodeSearch(query);
        }, 400);
    });
}

GeoField.prototype.displayWarning = function(msg) {
    var warningMsg = document.createElement('p');

    this.clearSuccess();
    this.clearWarning();

    warningMsg.className = 'help-block help-warning ' + this.geoWarningClassName;
    warningMsg.innerHTML = msg;

    $(warningMsg).insertAfter(self.addressField);
}

GeoField.prototype.displaySuccess = function(msg) {
    var self = this;
    var successMessage;

    clearTimeout(self._successTimeout);

    self.clearSuccess();
    self.clearWarning();

    successMessage = document.createElement('p');
    successMessage.className = 'help-block help-info ' + self.geoSuccessClassName;
    successMessage.innerHTML = 'Address has been successfully geo-coded';

    $(successMessage).insertAfter(self.addressField);

    self._successTimeout = setTimeout(function() {
        self.clearSuccess();
    }, 3000);
}

GeoField.prototype.clearWarning = function() {
    $('.' + this.geoWarningClassName).remove();
}

GeoField.prototype.clearSuccess = function() {
    $('.' + this.geoSuccessClassName).remove();
}

GeoField.prototype.checkVisibility = function(callback) {
    var self = this;
    var intervalId = setInterval(function() {
        var visible = $(self.map.getDiv()).is(':visible')
        if (!visible) {
            return;
        }

        clearInterval(intervalId);
        callback();
    }, 1000);
}

GeoField.prototype.geocodeSearch = function(query) {
    var self = this;

    this.geocoder.geocode({'address': query}, function(results, status) {
        if (status === google.maps.GeocoderStatus.ZERO_RESULTS || !results.length) {
            self.displayWarning('Could not geocode adddress. The map may not be in sync with the address entered.');
            return;
        }

        if (status !== google.maps.GeocoderStatus.OK) {
            self.displayWarning('Google Maps Error: '+status);
            return;<commit_msg>Added location to warning message<commit_after>"use strict";

function GeoField(options) {
    var self = this;
    var defaultLocation = options.defaultLocation;

    defaultLocation = new google.maps.LatLng(
        parseFloat(defaultLocation.lat),
        parseFloat(defaultLocation.lng)
    );

    this.zoom = options.zoom;
    this.srid = options.srid;
    this.sourceField = $(options.sourceSelector);
    this.addressField = $(options.addressSelector);
    this.latLngField = $(options.latLngDisplaySelector);
    this.geocoder = new google.maps.Geocoder();

    this.geoWarningClassName = 'wagtailgeowidget__geo-warning';
    this.geoSuccessClassName = 'wagtailgeowidget__geo-success';

    this.initMap(options.mapEl, defaultLocation);
    this.initEvents();

    this.setMapPosition(defaultLocation);
    this.updateLatLng(defaultLocation);

    this.checkVisibility(function() {
        google.maps.event.trigger(self.map, 'resize');
        var coords = $(self.latLngField).val();
        self.updateMapFromCoords(coords)
    });
}

GeoField.prototype.initMap = function(mapEl, defaultLocation) {
    var map = new google.maps.Map(mapEl, {
        zoom: this.zoom,
        center: defaultLocation
    });

    var marker = new google.maps.Marker({
        position: defaultLocation,
        map: map,
        draggable: true
    });

    this.map = map;
    this.marker = marker;
}

GeoField.prototype.initEvents = function() {
    var self = this;
    var autocomplete = new google.maps.places.Autocomplete(this.addressField[0]);

    google.maps.event.addListener(this.marker, "dragend", function(event) {
        self.setMapPosition(event.latLng);
        self.updateLatLng(event.latLng);
        self.writeLocation(event.latLng);
    });

    this.latLngField.on("input", function(e) {
        var coords = $(this).val();
        self.updateMapFromCoords(coords);
    });

    this.addressField.on("keydown", function(e) {
        if (e.keyCode === 13) {
            e.preventDefault();
            e.stopPropagation();
            self.geocodeSearch($(this).val());
        }
    });

    this.addressField.on("input", function(e) {
        clearTimeout(self._timeoutId);

        var query = $(this).val();

        if (query === "") {
            self.clearWarning();
            self.clearSuccess();
            return;
        }

        self._timeoutId = setTimeout(function() {
            self.geocodeSearch(query);
        }, 400);
    });
}

GeoField.prototype.displayWarning = function(msg) {
    var warningMsg = document.createElement('p');

    this.clearSuccess();
    this.clearWarning();

    warningMsg.className = 'help-block help-warning ' + this.geoWarningClassName;
    warningMsg.innerHTML = msg;

    $(warningMsg).insertAfter(self.addressField);
}

GeoField.prototype.displaySuccess = function(msg) {
    var self = this;
    var successMessage;

    clearTimeout(self._successTimeout);

    self.clearSuccess();
    self.clearWarning();

    successMessage = document.createElement('p');
    successMessage.className = 'help-block help-info ' + self.geoSuccessClassName;
    successMessage.innerHTML = 'Address has been successfully geo-coded';

    $(successMessage).insertAfter(self.addressField);

    self._successTimeout = setTimeout(function() {
        self.clearSuccess();
    }, 3000);
}

GeoField.prototype.clearWarning = function() {
    $('.' + this.geoWarningClassName).remove();
}

GeoField.prototype.clearSuccess = function() {
    $('.' + this.geoSuccessClassName).remove();
}

GeoField.prototype.checkVisibility = function(callback) {
    var self = this;
    var intervalId = setInterval(function() {
        var visible = $(self.map.getDiv()).is(':visible')
        if (!visible) {
            return;
        }

        clearInterval(intervalId);
        callback();
    }, 1000);
}

GeoField.prototype.geocodeSearch = function(query) {
    var self = this;

    this.geocoder.geocode({'address': query}, function(results, status) {
        if (status === google.maps.GeocoderStatus.ZERO_RESULTS || !results.length) {
            self.displayWarning(
                'Could not geocode adddress "' + query + '". '+
                'The map may not be in sync with the address entered.'
            );
            return;
        }

        if (status !== google.maps.GeocoderStatus.OK) {
            self.displayWarning('Google Maps Error: '+status);
            return;
{'fraction_non_alphanumeric': 0.13382687927107062, 'fraction_numerical': 0.0031890660592255125, 'mean_word_length': 2.8261437908496734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4261201', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2533, 'n_words': 433}