<commit_before>'use strict';

var R6MapsControls = (function($, window, document, R6MapsLangTerms, undefined) {
  var mapControl = $('#map-control'),
    objectiveControl = $('#objective-control'),
    floorControl = $('#floor-control'),
    zoomControl = $('#zoom-range'),
    SELECTED_CLASS = 'selected',
    ZOOMED_IN_FAR_CLASS = 'zoomed-in-far',
    ZOOMED_OUT_FAR_CLASS = 'zoomed-out-far';

  var populateMapOptions = function populateMapOptions(mapData) {
    var optionsAsString = '';

    for (var mapKey in mapData) {
      if (mapData.hasOwnProperty(mapKey)) {
        optionsAsString += '<option value="' + mapKey + '">' + mapData[mapKey].name + '</option>';
      }
    }
    mapControl.html(optionsAsString);
  };

  var getCurrentlySelectedMap = function getCurrentlySelectedMap() {
    return mapControl.val();
  };

  var trySelectMap = function trySelectMap(map) {
    trySelectOption(mapControl, map);
  };

  var setupMapChangeEvent = function setupMapChangeEvent(callback) {
    mapControl.on('change', callback);
  };

  var populateObjectiveOptions = function populateObjectiveOptions(objectives) {
    var options = '',
      objectiveTerms = R6MapsLangTerms.terms.objectives;

    options += '<option value="all">' + objectiveTerms.showAll + '</option>';
    objectives.forEach(function(objective) {
      options += '<option value="' + objective + '">' + objectiveTerms[objective] + '</option>';
    });
    objectiveControl.html(options);
  };

  var getCurrentlySelectedObjective = function getCurrentlySelectedObjective() {
    return objectiveControl.val();
  };

  var trySelectObjective = function trySelectObjective(objective) {
    trySelectOption(objectiveControl, objective);
  };

  var setupObjectiveChangeEvent = function setupObjectiveChangeEvent(callback) {
    objectiveControl.on('change', callback);
  };

  var populateFloorOptions = function populateFloorOptions(floors) {
    var buttonsAsString = '',
      classes = '',
      tooltip = '';

    floors.forEach(function(floor) {
      classes = '';
      classes += (floor.default) ? SELECTED_CLASS : '';
      tooltip = getFloorTooltip(floor.index);
      buttonsAsString += '<button data-index="' + floor.index + '" class="' + classes + '" title="' + tooltip + '">' + floor.name + '</button>';
    });
    floorControl.html(buttonsAsString);
  };

  var getCurrentlySelectedFloor = function getCurrentlySelectedFloor() {
    return floorControl.find('.selected').data('index');
  };

  var trySelectFloor = function trySelectFloor(floorIndex) {
    var selectedFloor = floorControl.find("[data-index='" + floorIndex + "']");

    if (selectedFloor.length) {
      floorControl.find('button').removeClass(SELECTED_CLASS);
      selectedFloor.addClass(SELECTED_CLASS);
      selectedFloor.trigger('click');
      return true;
    }
    return false;
  };

  var setupFloorChangeEvent = function setupFloorChangeEvent(callback) {
    floorControl.on('click', 'button', function(e) {
      floorControl.find('.' + SELECTED_CLASS + '').removeClass(SELECTED_CLASS);
      $(e.target).addClass(SELECTED_CLASS);
      callback();
    });
  };

  var setupFloorHotkeys = function setupFloorHotkeys(showSelectedFloorFn) {
    $(document).on('keydown', getHandleHotkeyFn(showSelectedFloorFn));
  };

  var getHandleHotkeyFn = function getHandleHotkeyFn(showSelectedFloorFn) {
    return function handleHotKey(e) {
      var keyCode = e.which;

      if (keyCode >= 48 && keyCode <= 53) {  // '0' through '1'
        if (trySelectFloor(keyCode - 48)) {
          showSelectedFloorFn();
        }
      } else if (keyCode == 192) { // '`'
        if (trySelectFloor(0)) {
          showSelectedFloorFn();
        }
      }
    };
  };

  var setupZoom = function setupZoom(map, mapElements) {
    map.panzoom({
      $zoomRange: zoomControl,
      minScale: 0.3,
      maxScale: 2.5
    });

    map.on('mousewheel', function(event) {
      zoomControl.val(+zoomControl.val() + (event.deltaY * 0.06));
      zoomControl.trigger('input');
      zoomControl.trigger('change');
    });

    zoomControl.on('change', getHandleZoomChangeFn(mapElements));
    zoomControl.on('input', getHandleZoomChangeFn(mapElements));

    // camera links were not working on touch devices:
    map.on('touchstart','a', function(e) {
      $(this).addClass('hover');
    });

    map.on('touchend','a', function(e) {
      $(this).removeClass('hover');
      this.click();
    });
  };

  var isZoomed = function isZoomed() {
    return (zoomControl.val() != 1);
  };

  var getHandleZoomChangeFn = function getHandleZoomChangeFn(mapElements) {
    return function handleZoomChange() {
      var zoomVal = zoomControl.val();

      if ( zoomVal > 1.6) {
        mapElements.addClass(ZOOMED_IN_FAR_CLASS);
      } else if ( zoomVal < 0.8 ) {
        mapElements.addClass(ZOOMED_OUT_FAR_CLASS);
      } else {
        mapElements.removeClass(ZOOMED_IN_FAR_CLASS);
        mapElements.removeClass(ZOOMED_OUT_FAR_CLASS);
      }
    };
  };

  var trySelectOption = function trySelectOption(selectEl, option) {
    var selectOption = selectEl.find('option[value="' + option + '"]');

    if (selectOption) {
      selectOption.prop('selected', true);
    }
  };

  var getFloorTooltip = function getFloorTooltip(floorIndex) {
    var shortcutTip = R6MapsLangTerms.terms.general.shortcutTip;

    if (floorIndex == 0) {
      return shortcutTip.replace('{shortcut}',"'0' or '~'");
    } else {
      return shortcutTip.replace('{shortcut}',floorIndex);
    }
  };

  return  {
    populateMapOptions: populateMapOptions,
    getCurrentlySelectedMap: getCurrentlySelectedMap,
    trySelectMap: trySelectMap,
    setupMapChangeEvent: setupMapChangeEvent,

    populateObjectiveOptions: populateObjectiveOptions,
    getCurrentlySelectedObjective: getCurrentlySelectedObjective,
    trySelectObjective: trySelectObjective,
    setupObjectiveChangeEvent: setupObjectiveChangeEvent,

    populateFloorOptions: populateFloorOptions,
    getCurrentlySelectedFloor: getCurrentlySelectedFloor,
    trySelectFloor: trySelectFloor,
    setupFloorChangeEvent: setupFloorChangeEvent,
    setupFloorHotkeys: setupFloorHotkeys,

    setupZoom: setupZoom,
    isZoomed: isZoomed
  };
})(window.jQuery, window, document, R6MapsLangTerms);
<commit_msg>Sort and select last for input options<commit_after>'use strict';

var R6MapsControls = (function($, window, document, R6MapsLangTerms, undefined) {
  var mapControl = $('#map-control'),
    objectiveControl = $('#objective-control'),
    floorControl = $('#floor-control'),
    zoomControl = $('#zoom-range'),
    SELECTED_CLASS = 'selected',
    ZOOMED_IN_FAR_CLASS = 'zoomed-in-far',
    ZOOMED_OUT_FAR_CLASS = 'zoomed-out-far';

  var populateMapOptions = function populateMapOptions(mapData) {
    var optionsAsString = '',
      initialMap = getCurrentlySelectedMap(),
      maps = [];

    for (var mapKey in mapData) {
      if (mapData.hasOwnProperty(mapKey)) {
        maps.push({ key: mapKey, name: mapData[mapKey].name })
      }
    }

    maps.sort(function(a,b) {
      if(a.name < b.name) return -1;
      if(a.name > b.name) return 1;
      return 0;
    });
    maps.forEach(function(map) {
      optionsAsString += '<option value="' + map.key + '">' + map.name + '</option>';
    });

    mapControl.html(optionsAsString);
    trySelectMap(initialMap);
  };

  var getCurrentlySelectedMap = function getCurrentlySelectedMap() {
    return mapControl.val();
  };

  var trySelectMap = function trySelectMap(map) {
    trySelectOption(mapControl, map);
  };

  var setupMapChangeEvent = function setupMapChangeEvent(callback) {
    mapControl.on('change', callback);
  };

  var populateObjectiveOptions = function populateObjectiveOptions(objectives) {
    var options = '',
      objectiveTerms = R6MapsLangTerms.terms.objectives,
      initialObjective = getCurrentlySelectedObjective();

    objectives.sort(function(a,b) {
      if(objectiveTerms[a] < objectiveTerms[b]) return -1;
      if(objectiveTerms[a] > objectiveTerms[b]) return 1;
      return 0;
    });

    options += '<option value="all">' + objectiveTerms.showAll + '</option>';
    objectives.forEach(function(objective) {
      options += '<option value="' + objective + '">' + objectiveTerms[objective] + '</option>';
    });
    objectiveControl.html(options);
    trySelectObjective(initialObjective);
  };

  var getCurrentlySelectedObjective = function getCurrentlySelectedObjective() {
    return objectiveControl.val();
  };

  var trySelectObjective = function trySelectObjective(objective) {
    trySelectOption(objectiveControl, objective);
  };

  var setupObjectiveChangeEvent = function setupObjectiveChangeEvent(callback) {
    objectiveControl.on('change', callback);
  };

  var populateFloorOptions = function populateFloorOptions(floors) {
    var buttonsAsString = '',
      classes = '',
      tooltip = '',
      initalFloor = getCurrentlySelectedFloor();

    floors.forEach(function(floor) {
      classes = '';
      classes += (floor.default) ? SELECTED_CLASS : '';
      tooltip = getFloorTooltip(floor.index);
      buttonsAsString += '<button data-index="' + floor.index + '" class="' + classes + '" title="' + tooltip + '">' + floor.name + '</button>';
    });
    floorControl.html(buttonsAsString);
    trySelectFloor(initalFloor);
  };

  var getCurrentlySelectedFloor = function getCurrentlySelectedFloor() {
    return floorControl.find('.selected').data('index');
  };

  var trySelectFloor = function trySelectFloor(floorIndex) {
    var selectedFloor = floorControl.find("[data-index='" + floorIndex + "']");

    if (selectedFloor.length) {
      floorControl.find('button').removeClass(SELECTED_CLASS);
      selectedFloor.addClass(SELECTED_CLASS);
      selectedFloor.trigger('click');
      return true;
    }
    return false;
  };

  var setupFloorChangeEvent = function setupFloorChangeEvent(callback) {
    floorControl.on('click', 'button', function(e) {
      floorControl.find('.' + SELECTED_CLASS + '').removeClass(SELECTED_CLASS);
      $(e.target).addClass(SELECTED_CLASS);
      callback();
    });
  };

  var setupFloorHotkeys = function setupFloorHotkeys(showSelectedFloorFn) {
    $(document).on('keydown', getHandleHotkeyFn(showSelectedFloorFn));
  };

  var getHandleHotkeyFn = function getHandleHotkeyFn(showSelectedFloorFn) {
    return function handleHotKey(e) {
      var keyCode = e.which;

      if (keyCode >= 48 && keyCode <= 53) {  // '0' through '1'
        if (trySelectFloor(keyCode - 48)) {
          showSelectedFloorFn();
        }
      } else if (keyCode == 192) { // '`'
        if (trySelectFloor(0)) {
          showSelectedFloorFn();
        }
      }
    };
  };

  var setupZoom = function setupZoom(map, mapElements) {
    map.panzoom({
      $zoomRange: zoomControl,
      minScale: 0.3,
      maxScale: 2.5
    });

    map.on('mousewheel', function(event) {
      zoomControl.val(+zoomControl.val() + (event.deltaY * 0.06));
      zoomControl.trigger('input');
      zoomControl.trigger('change');
    });

    zoomControl.on('change', getHandleZoomChangeFn(mapElements));
    zoomControl.on('input', getHandleZoomChangeFn(mapElements));

    // camera links were not working on touch devices:
    map.on('touchstart','a', function(e) {
      $(this).addClass('hover');
    });

    map.on('touchend','a', function(e) {
      $(this).removeClass('hover');
      this.click();
    });
  };

  var isZoomed = function isZoomed() {
    return (zoomControl.val() != 1);
  };

  var getHandleZoomChangeFn = function getHandleZoomChangeFn(mapElements) {
    return function handleZoomChange() {
      var zoomVal = zoomControl.val();

      if ( zoomVal > 1.6) {
        mapElements.addClass(ZOOMED_IN_FAR_CLASS);
      } else if ( zoomVal < 0.8 ) {
        mapElements.addClass(ZOOMED_OUT_FAR_CLASS);
      } else {
        mapElements.removeClass(ZOOMED_IN_FAR_CLASS);
        mapElements.removeClass(ZOOMED_OUT_FAR_CLASS);
      }
    };
  };

  var trySelectOption = function trySelectOption(selectEl, option) {
    var selectOption = selectEl.find('option[value="' + option + '"]');

    if (selectOption) {
      selectOption.prop('selected', true);
    }
  };

  var getFloorTooltip = function getFloorTooltip(floorIndex) {
    var shortcutTip = R6MapsLangTerms.terms.general.shortcutTip;

    if (floorIndex == 0) {
      return shortcutTip.replace('{shortcut}',"'0' or '~'");
    } else {
      return shortcutTip.replace('{shortcut}',floorIndex);
    }
  };

  return  {
    populateMapOptions: populateMapOptions,
    getCurrentlySelectedMap: getCurrentlySelectedMap,
    trySelectMap: trySelectMap,
    setupMapChangeEvent: setupMapChangeEvent,

    populateObjectiveOptions: populateObjectiveOptions,
    getCurrentlySelectedObjective: getCurrentlySelectedObjective,
    trySelectObjective: trySelectObjective,
    setupObjectiveChangeEvent: setupObjectiveChangeEvent,

    populateFloorOptions: populateFloorOptions,
    getCurrentlySelectedFloor: getCurrentlySelectedFloor,
    trySelectFloor: trySelectFloor,
    setupFloorChangeEvent: setupFloorChangeEvent,
    setupFloorHotkeys: setupFloorHotkeys,

    setupZoom: setupZoom,
    isZoomed: isZoomed
  };
})(window.jQuery, window, document, R6MapsLangTerms);

{'fraction_non_alphanumeric': 0.13267609873570138, 'fraction_numerical': 0.005117399157134257, 'mean_word_length': 3.6858251057827927, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3192080', 'n_tokens_mistral': 4139, 'n_tokens_neox': 3979, 'n_words': 700}