<commit_before>_ = require "underscore"
HasParent = require "../../common/has_parent"
PlotWidget = require "../../common/plot_widget"
properties = require "../../common/properties"
wmts = require "./wmts_tile_source"
ImagePool = require "./image_pool"
{logger} = require "../../common/logging"

class TileRendererView extends PlotWidget

  trace: (msg) ->
    console.warn "TileLayerView :: " + msg.toString()

  get_extent: () ->
    return [@x_range.get('start'), @y_range.get('start'), @x_range.get('end'), @y_range.get('end')]

  _doubletap: (e) =>
    extent = @get_extent()
    [xmin, ymin, xmax, ymax] = extent

    x_percent = (e.bokeh.plot_x - xmin) / (xmax - xmin)
    y_percent = (e.bokeh.plot_y - ymin) / (ymax - ymin)

    zoom_level = @mget('tile_source').get_closest_level_by_extent(extent, @map_frame.get('height'), @map_frame.get('width'))

    if e.srcEvent.shiftKey
      zoom_level = zoom_level - 1
    else
      zoom_level = zoom_level + 1

    new_resolution = @mget('tile_source').resolutions[zoom_level]
    new_xrange = new_resolution * @map_frame.get('width')
    new_yrange = new_resolution * @map_frame.get('height')
    nxmin = e.bokeh.plot_x - (x_percent * new_xrange)
    nymin = e.bokeh.plot_y - (y_percent * new_yrange)
    nxmax = xmin + new_xrange
    nymax = ymin + new_yrange
    new_extent = @mget('tile_source').snap_to_zoom([nxmin, nymin, nxmax, nymax], @map_frame.get('height'), @map_frame.get('width'), zoom_level)
    @x_range.set('start', new_extent[0])
    @y_range.set('start', new_extent[1])
    @x_range.set('end', new_extent[2])
    @y_range.set('end', new_extent[3])

  _set_data: () ->
    @pool = new ImagePool()
    @map_plot = @plot_view.model
    @map_canvas = @plot_view.canvas_view.ctx
    @map_frame = @plot_view.frame
    @x_range = @map_plot.get('x_range')
    @x_mapper = this.map_frame.get('x_mappers')['default']
    @y_range = @map_plot.get('y_range')
    @y_mapper = this.map_frame.get('y_mappers')['default']
    # @renderer.listenTo(@renderer, 'doubletap', @_doubletap)

  _map_data: () ->
    @initial_extent = @get_extent()
    console.dir(@mget('tile_source'))
    console.warn(@mget('tile_source'))
    console.warn(@mget('tile_source'))
    zoom_level = @mget('tile_source').get_level_by_extent(@initial_extent, @map_frame.get('height'), @map_frame.get('width'))
    new_extent = @mget('tile_source').snap_to_zoom(@initial_extent, @map_frame.get('height'), @map_frame.get('width'), zoom_level)
    @x_range.set('start', new_extent[0])
    @y_range.set('start', new_extent[1])
    @x_range.set('end', new_extent[2])
    @y_range.set('end', new_extent[3])

  _on_tile_load: (e) =>
    tile_data = e.target.tile_data
    tile_data.img = e.target
    tile_data.current = true
    @_render_tile(tile_data.cache_key)
    @mget('tile_source').tiles[tile_data.cache_key] = tile_data

  _on_tile_cache_load: (e) =>
    tile_data = e.target.tile_data
    tile_data.img = e.target
    @mget('tile_source').tiles[tile_data.cache_key] = tile_data

  _on_tile_error: (e) =>
    return ''

  _is_valid_tile: (x, y, z) ->

    if y < 0 || y > 1 << z || x < 0 || x > 1 << z
      return false

    return true

  _create_tile: (x, y, z, bounds, cache_only=false) ->
    tile = @pool.pop()

    if cache_only
      tile.onload = @_on_tile_cache_load
    else
      tile.onload = @_on_tile_load
<commit_msg>swap order of on_tile_load render vs. cache tile<commit_after>_ = require "underscore"
HasParent = require "../../common/has_parent"
PlotWidget = require "../../common/plot_widget"
properties = require "../../common/properties"
wmts = require "./wmts_tile_source"
ImagePool = require "./image_pool"
{logger} = require "../../common/logging"

class TileRendererView extends PlotWidget

  trace: (msg) ->
    console.warn "TileLayerView :: " + msg.toString()

  get_extent: () ->
    return [@x_range.get('start'), @y_range.get('start'), @x_range.get('end'), @y_range.get('end')]

  _doubletap: (e) =>
    extent = @get_extent()
    [xmin, ymin, xmax, ymax] = extent

    x_percent = (e.bokeh.plot_x - xmin) / (xmax - xmin)
    y_percent = (e.bokeh.plot_y - ymin) / (ymax - ymin)

    zoom_level = @mget('tile_source').get_closest_level_by_extent(extent, @map_frame.get('height'), @map_frame.get('width'))

    if e.srcEvent.shiftKey
      zoom_level = zoom_level - 1
    else
      zoom_level = zoom_level + 1

    new_resolution = @mget('tile_source').resolutions[zoom_level]
    new_xrange = new_resolution * @map_frame.get('width')
    new_yrange = new_resolution * @map_frame.get('height')
    nxmin = e.bokeh.plot_x - (x_percent * new_xrange)
    nymin = e.bokeh.plot_y - (y_percent * new_yrange)
    nxmax = xmin + new_xrange
    nymax = ymin + new_yrange
    new_extent = @mget('tile_source').snap_to_zoom([nxmin, nymin, nxmax, nymax], @map_frame.get('height'), @map_frame.get('width'), zoom_level)
    @x_range.set('start', new_extent[0])
    @y_range.set('start', new_extent[1])
    @x_range.set('end', new_extent[2])
    @y_range.set('end', new_extent[3])

  _set_data: () ->
    @pool = new ImagePool()
    @map_plot = @plot_view.model
    @map_canvas = @plot_view.canvas_view.ctx
    @map_frame = @plot_view.frame
    @x_range = @map_plot.get('x_range')
    @x_mapper = this.map_frame.get('x_mappers')['default']
    @y_range = @map_plot.get('y_range')
    @y_mapper = this.map_frame.get('y_mappers')['default']
    # @renderer.listenTo(@renderer, 'doubletap', @_doubletap)

  _map_data: () ->
    @initial_extent = @get_extent()
    console.dir(@mget('tile_source'))
    console.warn(@mget('tile_source'))
    console.warn(@mget('tile_source'))
    zoom_level = @mget('tile_source').get_level_by_extent(@initial_extent, @map_frame.get('height'), @map_frame.get('width'))
    new_extent = @mget('tile_source').snap_to_zoom(@initial_extent, @map_frame.get('height'), @map_frame.get('width'), zoom_level)
    @x_range.set('start', new_extent[0])
    @y_range.set('start', new_extent[1])
    @x_range.set('end', new_extent[2])
    @y_range.set('end', new_extent[3])

  _on_tile_load: (e) =>
    tile_data = e.target.tile_data
    tile_data.img = e.target
    tile_data.current = true
    @mget('tile_source').tiles[tile_data.cache_key] = tile_data
    @_render_tile(tile_data.cache_key)

  _on_tile_cache_load: (e) =>
    tile_data = e.target.tile_data
    tile_data.img = e.target
    @mget('tile_source').tiles[tile_data.cache_key] = tile_data

  _on_tile_error: (e) =>
    return ''

  _is_valid_tile: (x, y, z) ->

    if y < 0 || y > 1 << z || x < 0 || x > 1 << z
      return false

    return true

  _create_tile: (x, y, z, bounds, cache_only=false) ->
    tile = @pool.pop()

    if cache_only
      tile.onload = @_on_tile_cache_load
    else
      tile.onload = @_on_tile_load

{'fraction_non_alphanumeric': 0.16797395293769424, 'fraction_numerical': 0.004143850821370431, 'mean_word_length': 3.8444444444444446, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, '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': '368003', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2596, 'n_words': 419}