PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mvc/models.js

https://github.com/dotmike/tilestream
JavaScript | 94 lines | 63 code | 5 blank | 26 comment | 13 complexity | f14c936cd94a893fec64a12eae2d84ae MD5 | raw file
  1. // Backbone Models and Collections to be used both client/server side.
  2. // Note that these models are read-only - no `PUT/POST/DELETE` expected.
  3. // Requires for the server-side context. *TODO* Note that `var` is omitted here
  4. // because even within the `if()` IE will wipe globally defined variables if
  5. // `var` is included, leaving us with broken objects.
  6. if (typeof require !== 'undefined') {
  7. Settings = require('settings'),
  8. Backbone = require('backbone.js'),
  9. _ = require('underscore')._;
  10. }
  11. // Tileset
  12. // ---
  13. // A single tileset, corresponding to an `.mbtiles` file. `model.id` is the
  14. // file basename, e.g. `foo.mbtiles` has an `id` of `foo`.
  15. var Tileset = Backbone.Model.extend({
  16. initialize: function(attributes) {
  17. Backbone.Model.prototype.initialize.call(this, attributes);
  18. // Convert representation of baselayer into a true Tileset model.
  19. if (typeof this.get('baselayer') !== 'undefined') {
  20. this.set({ baselayer: new Tileset(this.get('baselayer')) });
  21. }
  22. },
  23. parse: function(response){
  24. var model = Backbone.Model.prototype.parse.call(this, response);
  25. // Convert representation of baselayer into a true Tileset model.
  26. if (typeof model.baselayer !== 'undefined') {
  27. model.baselayer = new Tileset(model.baselayer);
  28. }
  29. return model;
  30. },
  31. url: function() {
  32. return '/api/Tileset/' + this.id;
  33. },
  34. // Return the base URLs of TileStream tile servers including a single
  35. // trailing slash, e.g. http://localhost:8889/ or http://mapbox/tilestream/
  36. // in an Array.
  37. layerURL: function() {
  38. // Servers defined in `settings.js`.
  39. if (Settings.tile_hostnames.length) {
  40. return Settings.tile_hostnames;
  41. // Autodetect server from window object.
  42. } else if (window.location && window.location.hostname) {
  43. // Attempt to autodetect URL.
  44. var baseURL = window.location.protocol + '//' + window.location.hostname + ':' + Settings.port;
  45. var args = window.location.pathname.split('/');
  46. // Path already ends with trailing slash.
  47. if (args[args.length - 1] === '') {
  48. return [baseURL + args.join('/')];
  49. // index.html or similar trailing filename.
  50. } else if (args[args.length - 1].indexOf('.') !== -1) {
  51. args.pop();
  52. return [baseURL + args.join('/') + '/'];
  53. // Path beyond domain.
  54. } else {
  55. return [baseURL + args.join('/') + '/'];
  56. }
  57. // Server side, *TODO* needs a solution.
  58. } else {
  59. return ['http://localhost:9000/'];
  60. }
  61. },
  62. // Get ZXY of tile of tileset's center and minzoom. From [OSM wiki][1].
  63. // [1]: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#lon.2Flat_to_tile_numbers_2
  64. toZXY: function() {
  65. var center = this.get('center');
  66. center.lat = -1 * center.lat; // TMS is flipped from OSM calc below.
  67. var z = this.get('minzoom');
  68. var lat_rad = center.lat * Math.PI / 180;
  69. var x = parseInt((center.lon + 180.0) / 360.0 * Math.pow(2, z));
  70. var y = parseInt((1.0 - Math.log(Math.tan(lat_rad) + (1 / Math.cos(lat_rad))) / Math.PI) / 2.0 * Math.pow(2, z));
  71. return [z, x, y];
  72. },
  73. thumb: function(zxy) {
  74. return this.layerURL()[0] + ['1.0.0', this.get('id'), zxy[0], zxy[1], zxy[2]].join('/') + '.png';
  75. }
  76. });
  77. // TilesetList
  78. // -------
  79. // Collection of all tileset models.
  80. var TilesetList = Backbone.Collection.extend({
  81. model: Tileset,
  82. url: '/api/Tileset'
  83. });
  84. if (typeof module !== 'undefined') {
  85. module.exports = {
  86. Tileset: Tileset,
  87. TilesetList: TilesetList
  88. };
  89. }