<commit_before>CESIUM_BASE_URL = '/cesium/Cesium/';

var CesiumSwitcher = function(opt_options) {
  var options = opt_options || {};
  var control = this;

  var enabled = false;
  var ol3d = undefined;

  var element = document.createElement('div');
  element.className = 'CesiumSwitcher ol-unselectable';

  var draw = function() {
    $(element).on('click touchend', $.proxy(onClick, control));
    control.setMode(this.enabled);
  };

  var onClick = function(evt) {
    control.setMode(!this.enabled);
    evt.preventDefault();
  };

  ol.control.Control.call(this, {
    element: element,
    target: options.target
  });

  draw();
};

ol.inherits(CesiumSwitcher, ol.control.Control);


/**
 * Sets the 3d mode.
 * @param {bool} mode - Enabled or not
 */
CesiumSwitcher.prototype.setMode = function(mode) {
  if (mode) {
    this.element.innerHTML = '<img src="../../images/2d.png"/>';
    $(this).triggerHandler('cesium_enable');
    this.loadCesium();
  } else {
    this.element.innerHTML = '<img src="../../images/3d.png"/>';
    if (this.ol3d) {
      $(this).triggerHandler('cesium_disable');
      this.ol3d.setEnabled(false);
      this.getMap().getView().setRotation(0);
    }
  }

  this.enabled = mode;
};


/**
 * Loads cesium.
 */
CesiumSwitcher.prototype.loadCesium = function() {
  if (typeof Cesium === 'undefined') {
    var cesium = document.createElement('script');
    cesium.src = '/cesium/Cesium/Cesium.js';
    document.body.appendChild(cesium);
  }

  var _this = this;

  // poor man's dom check
  var check = setInterval(function() {
    if (typeof Cesium !== 'undefined') {
      clearInterval(check);
      _this.loadOL3cesium();
    }
  }, 100);
};


/**
 * Loads ol3-cesium.
 */
CesiumSwitcher.prototype.loadOL3cesium = function() {
  if (typeof olcs === 'undefined') {
    var ol3cesium = document.createElement('script');
    ol3cesium.src = '/cesium/ol3cesium.js';
    document.body.appendChild(ol3cesium);
  }

  var _this = this;

  // poor man's dom check
  var check = setInterval(function() {
    if (typeof olcs !== 'undefined') {
      clearInterval(check);
      _this.enableCesium();
    }
  }, 100);
};


/**
 * Enbables ol3-cesium.
 */
CesiumSwitcher.prototype.enableCesium = function() {
  if (!this.ol3d) {
    var map = this.getMap();

    this.ol3d = new olcs.OLCesium({map: map});
    var scene = this.ol3d.getCesiumScene();
    var terrainProvider = new Cesium.CesiumTerrainProvider({
      url: '//assets.agi.com/stk-terrain/world'
    });
    scene.terrainProvider = terrainProvider;
    scene.globe.depthTestAgainstTerrain = true;
  }

  this.ol3d.setEnabled(true);<commit_msg>ol-CesiumSwitcher: use onload function for lazy initialization<commit_after>CESIUM_BASE_URL = '/cesium/Cesium/';

var CesiumSwitcher = function(opt_options) {
  var options = opt_options || {};
  var control = this;

  var enabled = false;
  var ol3d = undefined;

  var element = document.createElement('div');
  element.className = 'CesiumSwitcher ol-unselectable';

  var draw = function() {
    $(element).on('click touchend', $.proxy(onClick, control));
    control.setMode(this.enabled);
  };

  var onClick = function(evt) {
    control.setMode(!this.enabled);
    evt.preventDefault();
  };

  ol.control.Control.call(this, {
    element: element,
    target: options.target
  });

  draw();
};

ol.inherits(CesiumSwitcher, ol.control.Control);


/**
 * Sets the 3d mode.
 * @param {bool} mode - Enabled or not
 */
CesiumSwitcher.prototype.setMode = function(mode) {
  if (mode) {
    this.element.innerHTML = '<img src="../../images/2d.png"/>';
    $(this).triggerHandler('cesium_enable');
    this.loadCesium();
  } else {
    this.element.innerHTML = '<img src="../../images/3d.png"/>';
    if (this.ol3d) {
      $(this).triggerHandler('cesium_disable');
      this.ol3d.setEnabled(false);
      this.getMap().getView().setRotation(0);
    }
  }

  this.enabled = mode;
};


/**
 * Loads cesium.
 */
CesiumSwitcher.prototype.loadCesium = function() {
  var _this = this;

  if (typeof Cesium === 'undefined') {
    var cesium = document.createElement('script');
    cesium.src = '/cesium/Cesium/Cesium.js';
    cesium.onload = function() {
      _this.loadOL3cesium();
    };
    document.body.appendChild(cesium);
  }
};


/**
 * Loads ol3-cesium.
 */
CesiumSwitcher.prototype.loadOL3cesium = function() {
  var _this = this;

  if (typeof olcs === 'undefined') {
    var ol3cesium = document.createElement('script');
    ol3cesium.src = '/cesium/ol3cesium.js';
    ol3cesium.onload = function() {
      _this.enableCesium();
    };
    document.body.appendChild(ol3cesium);
  }
};


/**
 * Enbables ol3-cesium.
 */
CesiumSwitcher.prototype.enableCesium = function() {
  if (!this.ol3d) {
    var map = this.getMap();

    this.ol3d = new olcs.OLCesium({map: map});
    var scene = this.ol3d.getCesiumScene();
    var terrainProvider = new Cesium.CesiumTerrainProvider({
      url: '//assets.agi.com/stk-terrain/world'
    });
    scene.terrainProvider = terrainProvider;
    scene.globe.depthTestAgainstTerrain = true;
  }

  this.ol3d.setEnabled(true);
{'fraction_non_alphanumeric': 0.164576802507837, 'fraction_numerical': 0.008816614420062695, 'mean_word_length': 3.247088186356073, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6533668', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1748, 'n_words': 309}