Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. var Map = (function () {
  2. function Map(parent, template, proj, loc, zoom) {
  3. this.selection = d3.select(parent);
  4. this.loaded_tiles = {
  5. };
  6. this.template = template;
  7. this.parent = parent;
  8. var size = Mouse.element_size(this.parent), coord = proj.locationCoordinate(loc).zoomTo(zoom);
  9. this.grid = new Grid.Grid(size.x, size.y, coord, 3);
  10. this.projection = proj;
  11. this.queue = new Queue(this.loaded_tiles);
  12. this.tile_queuer = this.getTileQueuer();
  13. this.tile_dequeuer = this.getTileDequeuer();
  14. this.tile_onloaded = this.getTileOnloaded();
  15. Mouse.link_control(this.selection, new Mouse.Control(this, true));
  16. Hash.link_control(this);
  17. var map = this;
  18. d3.select(window).on('resize.map', function () {
  19. map.update_gridsize();
  20. });
  21. this.selection.selectAll('img.tile').remove();
  22. this.redraw(true);
  23. }
  24. Map.prototype.update_gridsize = function () {
  25. var size = Mouse.element_size(this.parent);
  26. this.grid.resize(size.x, size.y);
  27. this.redraw(true);
  28. };
  29. Map.prototype.pointLocation = function (point) {
  30. var coord = this.grid.pointCoordinate(point ? point : this.grid.center);
  31. return this.projection.coordinateLocation(coord);
  32. };
  33. Map.prototype.locationPoint = function (loc) {
  34. var coord = this.projection.locationCoordinate(loc);
  35. return this.grid.coordinatePoint(coord);
  36. };
  37. Map.prototype.setCenterZoom = function (loc, zoom) {
  38. this.grid.setCenter(this.projection.locationCoordinate(loc, zoom));
  39. this.redraw(true);
  40. };
  41. Map.prototype.onMoved = function (callback) {
  42. var map = this, before = this.moved_callback;
  43. this.moved_callback = function () {
  44. if(before) {
  45. before();
  46. }
  47. callback(map);
  48. };
  49. };
  50. Map.prototype.redraw = function (moved) {
  51. var tiles = this.grid.visibleTiles(), join = this.selection.selectAll('img.tile').data(tiles, tile_key);
  52. join.exit().each(this.tile_dequeuer).remove();
  53. join.enter().append('img').attr('class', 'tile').attr('id', tile_key).style('z-index', tile_zoom).style('display', 'none').on('load', this.tile_onloaded).each(this.tile_queuer);
  54. if(Tile.transform_property) {
  55. this.selection.selectAll('img.tile').style(Tile.transform_property, tile_xform);
  56. } else {
  57. this.selection.selectAll('img.tile').style('left', tile_left).style('top', tile_top).style('width', tile_width).style('height', tile_height);
  58. }
  59. if(moved && this.moved_callback) {
  60. this.moved_callback();
  61. }
  62. this.queue.process();
  63. };
  64. Map.prototype.getTileOnloaded = function () {
  65. var map = this;
  66. return function (tile, i) {
  67. d3.select(this).style('display', 'block');
  68. map.loaded_tiles[this.src] = Date.now();
  69. map.queue.close(this);
  70. map.redraw(false);
  71. };
  72. };
  73. Map.prototype.getTileQueuer = function () {
  74. var map = this;
  75. return function (tile, i) {
  76. var src = map.template;
  77. src = src.replace('{z}', '{Z}').replace('{Z}', tile.coord.zoom.toFixed(0));
  78. src = src.replace('{x}', '{X}').replace('{X}', tile.coord.column.toFixed(0));
  79. src = src.replace('{y}', '{Y}').replace('{Y}', tile.coord.row.toFixed(0));
  80. map.queue.append(this, src);
  81. };
  82. };
  83. Map.prototype.getTileDequeuer = function () {
  84. var queue = this.queue;
  85. return function (tile, i) {
  86. queue.cancel(this);
  87. };
  88. };
  89. return Map;
  90. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement