<commit_before>/*
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/** Functions for working with the UI elements. */

/** Class with objects for the main UI elements. */
function UiElements() {
  this.appbarElement = $('.app-bar');
  this.darkbgElement = $('.navdrawer-bg');
  this.mainMenuContainer = $('#main-menu');
  this.languageContainer = $('#language-menu');
  this.searchContainer = $('.search-container');
  this.mapContainer = $('.map');
  this.infoBox = $('.infobox');
  this.locationDialog = $('#location-dialog');
  this.locationNavBar = $('#location-nav-bar');
}

var ui;

/** Set up actions on UI elements. */
function setUpUI() {
  // Create global var for the UI elements.
  ui = new UiElements();

  $('#nav_google').click(menuHide);
  $('#nav_bing').click(menuHide);
  $('#nav_osm').click(menuHide);
  $('#nav_apple').click(menuHide);
  $('#nav_apps').click(menuHide);
  $('.nav_dismiss').click(menuHide);

  $('.infobox .pushpin-button').click(togglePushPin);

  $('#menu-button').click(uiClick);
  $('#search-button').click(uiClick);
  $('.bottom-bar > button').click(uiClick);
  $('#main-menu > .promote-layer > li').click(uiClick);
  $('#language-menu > .promote-layer > li').click(uiClick);

  // The dark shading used when showing the menu has a click action,
  // but normally it ignores clicks - it only receives them when it
  // has the open class applied.
  ui.darkbgElement.click(menuHide);

  // Use a mouseover event for button highlighting, because hover doesn't
  // work on mobile.
  if (!isMobile()) {
    $('button').mouseover(function() {$(this).addClass('highlight')});
    $('button').mouseout(function() {$(this).removeClass('highlight')});
  }
  if (navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('mac os') != -1) {
    $('<li>').append(
        $('<a>').attr('id', 'nav_apple'))
        .insertBefore($('#nav_feedback').closest('li'));
  }
  if (navigator.userAgent.toLowerCase().indexOf('android') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('blackberry') != -1) {
    $('<li>').append(
        $('<a>').attr('id', 'nav_apps'))
        .insertBefore($('#nav_feedback').closest('li'));
  }
  loadText();
}

/** Load the localised text into the UI elements. */
function loadText() {
  // Set localised text into the UI
  $('#nav_help').text(messages.get('ui-help'));
  $('#nav_satellite').text(messages.get('ui-satellite'));
  $('#nav_language').text(messages.get('ui-language'));
  $('#nav_google').text(messages.get('google-maps'));
  $('#nav_bing').text(messages.get('bing-maps'));
  $('#nav_osm').text(messages.get('osm-maps'));
  $('#nav_feedback').text(messages.get('ui-feedback'));
  $('#nav_github').text(messages.get('ui-github'));
  // nav_dismiss uses a class because there are two of them.
  $('.nav_dismiss').text(messages.get('dismiss'));

  $('#nav_apple').text(messages.get('apple-maps'));
  $('#nav_apps').text(messages.get('apps'));

  $('.search-input').attr('placeholder', messages.get('input-prompt'));
}


/** Handle a click on a UI element. */
function uiClick(e) {
  var clickedId = null;
  if ('target' in e) {
    clickedId = e.target.id;
  }
  if (clickedId === null) {
    return;
  }
  // Menu button.
  if (clickedId === 'menu-button') {
    menuShow();
  }
  // Search button.
  if (clickedId === 'search-button') {
    ui.searchContainer.toggleClass('open');
    if (ui.searchContainer.hasClass('open')) {
      $('.search-input').focus();
    } else {
      $('.search-input').blur();
    }
  }

  if (clickedId == 'location') {
    locationZoom();
  }
  if (clickedId == 'compass') {
    ui.mapContainer.toggleClass('hide');
    ui.infoBox.toggleClass('hide');
    if (ui.mapContainer.hasClass('hide')) {
      // Map is hidden, so compass should be revealed.
      compass.checkOperation();
    }
  }
  if (clickedId == 'nav_help') {
    //helpStart();
    menuHide();
    Help.start();
  }
  if (clickedId === 'nav_satellite') {
    // Change the map to/from satellite imagery.
    map.toggleImagery();
    menuHide();
  }
  if (clickedId === 'nav_language') {
    ui.mainMenuContainer.removeClass('open');
    ui.languageContainer.addClass('open');
  }
  if (clickedId.indexOf('lang_') == 0) {
    ui.languageContainer.removeClass('open');
    menuHide();
    // Change the language!
    var lang = clickedId.substr(5);
    if (messages.setLanguage(lang)) {
      loadText();
      if (displayedCode !== null) {
        displayCodeInformation();
        displayCodeMapCompass(displayedCode);
      }
    }
  }
  if (clickedId === 'nav_feedback') {
    menuHide();
    commentShow();
  }
}

/**
 * Update the location buttons classes depending on the visibility of the
 * code and user location.
 */
function updateLocationButton() {
  if (typeof google == 'undefined' || typeof google.maps == 'undefined') {
    return;
  }
  if (displayedCode.code === null) {
    return;
  }
  var button = $('#location');

  var mapBounds = map.map.getBounds();
  // There's always a code.
  var codeCenter = new google.maps.LatLng(
      displayedCode.codeArea.latitudeCenter,
      displayedCode.codeArea.longitudeCenter);
  if (deviceLatLng === null) {
    // We don't have a location for the device, so we're done.
    button.removeClass('location-zoom');
    button.addClass('code-zoom');
    return;
  }
  var userLocation = new google.maps.LatLng(deviceLatLng[0], deviceLatLng[1]);
  // Now we have both locations, so we can decide what to do with them.
  // If they're both in view, zoom to the code, if the code is view zoom to
  // user, and if user is in view zoom to both.
  if (mapBounds.contains(userLocation) && mapBounds.contains(codeCenter)) {
    button.removeClass('location-zoom');
    button.addClass('code-zoom');
  } else if (mapBounds.contains(codeCenter)) {
    button.addClass('location-zoom');
    button.removeClass('code-zoom');
  } else {
    button.removeClass('location-zoom');
    button.removeClass('code-zoom');
  }
}

/** Set the destination URLs for all the alternative map providers. */
function setMapProviderUrls(lat, lng, code) {
  code = encodeURIComponent(code);
  if (typeof lat === 'undefined') {
    $('#nav_google').attr('href', null);
    $('#nav_bing').attr('href', null);
    $('#nav_osm').attr('href', null);
    $('#nav_apple').attr('href', null);
    $('#nav_apps').attr('href', null);
  } else {
    $('#nav_google').attr(
        'href',
        'https://www.google.com/maps/place/' + lat + ',' + lng + '/@' + lat +
        ',' + lng + ',17z');
    $('#nav_bing').attr(
        'href',
        'http://www.bing.com/maps/?v=2&cp=' + lat + '~' + lng +
        '&style=h&lvl=19&sp=Point.' + lat + '_' + lng + '_' + code + '___');
    //
    $('#nav_osm').attr(
        'href',
        'http://www.openstreetmap.org/?mlat=' + lat + '&mlon=' + lng +
        '&zoom=19');
    $('#nav_apple').attr(
        'href',
        'http://maps.apple.com/?daddr=' + lat + ',' + lng + '&ll=');
    $('#nav_apps').attr(
        'href', 'geo:0,0?q=' + lat + ',' + lng + '(' + code + ')');
  }
}

/** Show the main menu. */
function menuShow() {
  ui.appbarElement.addClass('open');
  ui.mainMenuContainer.addClass('open');
  ui.darkbgElement.addClass('open');
}

/** Hide the main menu - called after handling a click on a menu element. */
function menuHide() {
  ui.languageContainer.removeClass('open');
  ui.appbarElement.removeClass('open');
  ui.mainMenuContainer.removeClass('open');
  ui.darkbgElement.removeClass('open');
}

function locationPromptDisplay() {
  var dialog = new Dialog(
      'location', $('<p>').text(messages.get('location-prompt')),
      locationDismissCallback);
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          locationDismissCallback();
      }));
}
function locationDismissCallback() {
  Dialog.remove('location');
  locationListener.getCurrentLocation();
}

function browserFeaturesDisplay() {
  var dialog = new Dialog(
      'browser', $('<section>').append($('<p>').html(messages.get('browser-problem-msg'))));
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          dialog.remove();
      }));
}

function noLocationDisplay(code) {
  //ui.locationDialog.innerHTML = messages.get('extend-failure-msg', {OLC: code});
  //ui.locationNavBar.innerHTML = '<button onclick="noLocationDismiss();" ' +
  //                   'class="dismiss"></button>';
  //ui.locationDialog.addClass('open');
  //ui.locationNavBar.addClass('open');
  var dialog = new Dialog('nolocation',
      $('<section>').append($('<p>').html(messages.get('extend-failure-msg', {OLC: code}))));
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          dialog.remove();
      }));
}

function commentShow() {
  var dialog = new Dialog('comment',
      $('<section>').append($('<p>').html(messages.get('feedback-detail')))
      .append($('<textarea>')));
  dialog.addButton($('<button>').addClass('dismiss').click(commentControls));
  dialog.addButton($('<button>').addClass('upload').click(commentControls));<commit_msg>Fix no location error dialog<commit_after>/*
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/** Functions for working with the UI elements. */

/** Class with objects for the main UI elements. */
function UiElements() {
  this.appbarElement = $('.app-bar');
  this.darkbgElement = $('.navdrawer-bg');
  this.mainMenuContainer = $('#main-menu');
  this.languageContainer = $('#language-menu');
  this.searchContainer = $('.search-container');
  this.mapContainer = $('.map');
  this.infoBox = $('.infobox');
  this.locationDialog = $('#location-dialog');
  this.locationNavBar = $('#location-nav-bar');
}

var ui;

/** Set up actions on UI elements. */
function setUpUI() {
  // Create global var for the UI elements.
  ui = new UiElements();

  $('#nav_google').click(menuHide);
  $('#nav_bing').click(menuHide);
  $('#nav_osm').click(menuHide);
  $('#nav_apple').click(menuHide);
  $('#nav_apps').click(menuHide);
  $('.nav_dismiss').click(menuHide);

  $('.infobox .pushpin-button').click(togglePushPin);

  $('#menu-button').click(uiClick);
  $('#search-button').click(uiClick);
  $('.bottom-bar > button').click(uiClick);
  $('#main-menu > .promote-layer > li').click(uiClick);
  $('#language-menu > .promote-layer > li').click(uiClick);

  // The dark shading used when showing the menu has a click action,
  // but normally it ignores clicks - it only receives them when it
  // has the open class applied.
  ui.darkbgElement.click(menuHide);

  // Use a mouseover event for button highlighting, because hover doesn't
  // work on mobile.
  if (!isMobile()) {
    $('button').mouseover(function() {$(this).addClass('highlight')});
    $('button').mouseout(function() {$(this).removeClass('highlight')});
  }
  if (navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('mac os') != -1) {
    $('<li>').append(
        $('<a>').attr('id', 'nav_apple'))
        .insertBefore($('#nav_feedback').closest('li'));
  }
  if (navigator.userAgent.toLowerCase().indexOf('android') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
      navigator.userAgent.toLowerCase().indexOf('blackberry') != -1) {
    $('<li>').append(
        $('<a>').attr('id', 'nav_apps'))
        .insertBefore($('#nav_feedback').closest('li'));
  }
  loadText();
}

/** Load the localised text into the UI elements. */
function loadText() {
  // Set localised text into the UI
  $('#nav_help').text(messages.get('ui-help'));
  $('#nav_satellite').text(messages.get('ui-satellite'));
  $('#nav_language').text(messages.get('ui-language'));
  $('#nav_google').text(messages.get('google-maps'));
  $('#nav_bing').text(messages.get('bing-maps'));
  $('#nav_osm').text(messages.get('osm-maps'));
  $('#nav_feedback').text(messages.get('ui-feedback'));
  $('#nav_github').text(messages.get('ui-github'));
  // nav_dismiss uses a class because there are two of them.
  $('.nav_dismiss').text(messages.get('dismiss'));

  $('#nav_apple').text(messages.get('apple-maps'));
  $('#nav_apps').text(messages.get('apps'));

  $('.search-input').attr('placeholder', messages.get('input-prompt'));
}


/** Handle a click on a UI element. */
function uiClick(e) {
  var clickedId = null;
  if ('target' in e) {
    clickedId = e.target.id;
  }
  if (clickedId === null) {
    return;
  }
  // Menu button.
  if (clickedId === 'menu-button') {
    menuShow();
  }
  // Search button.
  if (clickedId === 'search-button') {
    ui.searchContainer.toggleClass('open');
    if (ui.searchContainer.hasClass('open')) {
      $('.search-input').focus();
    } else {
      $('.search-input').blur();
    }
  }

  if (clickedId == 'location') {
    locationZoom();
  }
  if (clickedId == 'compass') {
    ui.mapContainer.toggleClass('hide');
    ui.infoBox.toggleClass('hide');
    if (ui.mapContainer.hasClass('hide')) {
      // Map is hidden, so compass should be revealed.
      compass.checkOperation();
    }
  }
  if (clickedId == 'nav_help') {
    //helpStart();
    menuHide();
    Help.start();
  }
  if (clickedId === 'nav_satellite') {
    // Change the map to/from satellite imagery.
    map.toggleImagery();
    menuHide();
  }
  if (clickedId === 'nav_language') {
    ui.mainMenuContainer.removeClass('open');
    ui.languageContainer.addClass('open');
  }
  if (clickedId.indexOf('lang_') == 0) {
    ui.languageContainer.removeClass('open');
    menuHide();
    // Change the language!
    var lang = clickedId.substr(5);
    if (messages.setLanguage(lang)) {
      loadText();
      if (displayedCode !== null) {
        displayCodeInformation();
        displayCodeMapCompass(displayedCode);
      }
    }
  }
  if (clickedId === 'nav_feedback') {
    menuHide();
    commentShow();
  }
}

/**
 * Update the location buttons classes depending on the visibility of the
 * code and user location.
 */
function updateLocationButton() {
  if (typeof google == 'undefined' || typeof google.maps == 'undefined') {
    return;
  }
  if (displayedCode.code === null) {
    return;
  }
  var button = $('#location');

  var mapBounds = map.map.getBounds();
  // There's always a code.
  var codeCenter = new google.maps.LatLng(
      displayedCode.codeArea.latitudeCenter,
      displayedCode.codeArea.longitudeCenter);
  if (deviceLatLng === null) {
    // We don't have a location for the device, so we're done.
    button.removeClass('location-zoom');
    button.addClass('code-zoom');
    return;
  }
  var userLocation = new google.maps.LatLng(deviceLatLng[0], deviceLatLng[1]);
  // Now we have both locations, so we can decide what to do with them.
  // If they're both in view, zoom to the code, if the code is view zoom to
  // user, and if user is in view zoom to both.
  if (mapBounds.contains(userLocation) && mapBounds.contains(codeCenter)) {
    button.removeClass('location-zoom');
    button.addClass('code-zoom');
  } else if (mapBounds.contains(codeCenter)) {
    button.addClass('location-zoom');
    button.removeClass('code-zoom');
  } else {
    button.removeClass('location-zoom');
    button.removeClass('code-zoom');
  }
}

/** Set the destination URLs for all the alternative map providers. */
function setMapProviderUrls(lat, lng, code) {
  code = encodeURIComponent(code);
  if (typeof lat === 'undefined') {
    $('#nav_google').attr('href', null);
    $('#nav_bing').attr('href', null);
    $('#nav_osm').attr('href', null);
    $('#nav_apple').attr('href', null);
    $('#nav_apps').attr('href', null);
  } else {
    $('#nav_google').attr(
        'href',
        'https://www.google.com/maps/place/' + lat + ',' + lng + '/@' + lat +
        ',' + lng + ',17z');
    $('#nav_bing').attr(
        'href',
        'http://www.bing.com/maps/?v=2&cp=' + lat + '~' + lng +
        '&style=h&lvl=19&sp=Point.' + lat + '_' + lng + '_' + code + '___');
    //
    $('#nav_osm').attr(
        'href',
        'http://www.openstreetmap.org/?mlat=' + lat + '&mlon=' + lng +
        '&zoom=19');
    $('#nav_apple').attr(
        'href',
        'http://maps.apple.com/?daddr=' + lat + ',' + lng + '&ll=');
    $('#nav_apps').attr(
        'href', 'geo:0,0?q=' + lat + ',' + lng + '(' + code + ')');
  }
}

/** Show the main menu. */
function menuShow() {
  ui.appbarElement.addClass('open');
  ui.mainMenuContainer.addClass('open');
  ui.darkbgElement.addClass('open');
}

/** Hide the main menu - called after handling a click on a menu element. */
function menuHide() {
  ui.languageContainer.removeClass('open');
  ui.appbarElement.removeClass('open');
  ui.mainMenuContainer.removeClass('open');
  ui.darkbgElement.removeClass('open');
}

function locationPromptDisplay() {
  var dialog = new Dialog(
      'location', $('<p>').text(messages.get('location-prompt')),
      locationDismissCallback);
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          locationDismissCallback();
      }));
}
function locationDismissCallback() {
  Dialog.remove('location');
  locationListener.getCurrentLocation();
}

function browserFeaturesDisplay() {
  var dialog = new Dialog(
      'browser', $('<section>').append($('<p>').html(messages.get('browser-problem-msg'))));
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          dialog.remove();
      }));
}

function noLocationDisplay(code) {
  var dialog = new Dialog('nolocation',
      $('<section>').append($('<p>').html(messages.get('extend-failure-msg', {OLC: code}))));
  dialog.addButton(
      $('<button>').addClass('dismiss').click(function() {
          dialog.remove();
      }));
}

function commentShow() {
  var dialog = new Dialog('comment',
      $('<section>').append($('<p>').html(messages.get('feedback-detail')))
      .append($('<textarea>')));
  dialog.addButton($('<button>').addClass('dismiss').click(commentControls));
  dialog.addButton($('<button>').addClass('upload').click(commentControls));
{'fraction_non_alphanumeric': 0.19672470978441128, 'fraction_numerical': 0.0024875621890547263, 'mean_word_length': 3.62757793764988, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 8, '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': '6459063', 'n_tokens_mistral': 6048, 'n_tokens_neox': 6037, 'n_words': 1281}