provider.dynamic.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * DevExtreme (ui/map/provider.dynamic.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var Promise = require("../../core/polyfills/promise");
  11. var extend = require("../../core/utils/extend").extend;
  12. var iteratorUtils = require("../../core/utils/iterator");
  13. var Provider = require("./provider");
  14. var abstract = Provider.abstract;
  15. var DynamicProvider = Provider.inherit({
  16. _geocodeLocation: function(location) {
  17. return new Promise(function(resolve) {
  18. var cache = this._geocodedLocations;
  19. var cachedLocation = cache[location];
  20. if (cachedLocation) {
  21. resolve(cachedLocation)
  22. } else {
  23. this._geocodeLocationImpl(location).then(function(geocodedLocation) {
  24. cache[location] = geocodedLocation;
  25. resolve(geocodedLocation)
  26. })
  27. }
  28. }.bind(this))
  29. },
  30. _renderImpl: function() {
  31. return this._load().then(function() {
  32. return this._init()
  33. }.bind(this)).then(function() {
  34. return Promise.all([this.updateMapType(), this._areBoundsSet() ? this.updateBounds() : this.updateCenter()])
  35. }.bind(this)).then(function() {
  36. this._attachHandlers();
  37. return new Promise(function(resolve) {
  38. var timeout = setTimeout(function() {
  39. clearTimeout(timeout);
  40. resolve()
  41. })
  42. })
  43. }.bind(this))
  44. },
  45. _load: function() {
  46. if (!this._mapsLoader) {
  47. this._mapsLoader = this._loadImpl()
  48. }
  49. this._markers = [];
  50. this._routes = [];
  51. return this._mapsLoader
  52. },
  53. _loadImpl: abstract,
  54. _init: abstract,
  55. _attachHandlers: abstract,
  56. addMarkers: function(options) {
  57. return Promise.all(iteratorUtils.map(options, function(options) {
  58. return this._addMarker(options)
  59. }.bind(this))).then(function(markerObjects) {
  60. this._fitBounds();
  61. return [false, iteratorUtils.map(markerObjects, function(markerObject) {
  62. return markerObject.marker
  63. })]
  64. }.bind(this))
  65. },
  66. _addMarker: function(options) {
  67. return this._renderMarker(options).then(function(markerObject) {
  68. this._markers.push(extend({
  69. options: options
  70. }, markerObject));
  71. this._fireMarkerAddedAction({
  72. options: options,
  73. originalMarker: markerObject.marker
  74. });
  75. return markerObject
  76. }.bind(this))
  77. },
  78. _renderMarker: abstract,
  79. removeMarkers: function(markersOptionsToRemove) {
  80. var that = this;
  81. iteratorUtils.each(markersOptionsToRemove, function(_, markerOptionToRemove) {
  82. that._removeMarker(markerOptionToRemove)
  83. });
  84. return Promise.resolve()
  85. },
  86. _removeMarker: function(markersOptionToRemove) {
  87. var that = this;
  88. iteratorUtils.each(this._markers, function(markerIndex, markerObject) {
  89. if (markerObject.options !== markersOptionToRemove) {
  90. return true
  91. }
  92. that._destroyMarker(markerObject);
  93. that._markers.splice(markerIndex, 1);
  94. that._fireMarkerRemovedAction({
  95. options: markerObject.options
  96. });
  97. return false
  98. })
  99. },
  100. _destroyMarker: abstract,
  101. _clearMarkers: function() {
  102. while (this._markers.length > 0) {
  103. this._removeMarker(this._markers[0].options)
  104. }
  105. },
  106. addRoutes: function(options) {
  107. return Promise.all(iteratorUtils.map(options, function(options) {
  108. return this._addRoute(options)
  109. }.bind(this))).then(function(routeObjects) {
  110. this._fitBounds();
  111. return [false, iteratorUtils.map(routeObjects, function(routeObject) {
  112. return routeObject.instance
  113. })]
  114. }.bind(this))
  115. },
  116. _addRoute: function(options) {
  117. return this._renderRoute(options).then(function(routeObject) {
  118. this._routes.push(extend({
  119. options: options
  120. }, routeObject));
  121. this._fireRouteAddedAction({
  122. options: options,
  123. originalRoute: routeObject.instance
  124. });
  125. return routeObject
  126. }.bind(this))
  127. },
  128. _renderRoute: abstract,
  129. removeRoutes: function(options) {
  130. var that = this;
  131. iteratorUtils.each(options, function(routeIndex, options) {
  132. that._removeRoute(options)
  133. });
  134. return Promise.resolve()
  135. },
  136. _removeRoute: function(options) {
  137. var that = this;
  138. iteratorUtils.each(this._routes, function(routeIndex, routeObject) {
  139. if (routeObject.options !== options) {
  140. return true
  141. }
  142. that._destroyRoute(routeObject);
  143. that._routes.splice(routeIndex, 1);
  144. that._fireRouteRemovedAction({
  145. options: options
  146. });
  147. return false
  148. })
  149. },
  150. _destroyRoute: abstract,
  151. _clearRoutes: function() {
  152. while (this._routes.length > 0) {
  153. this._removeRoute(this._routes[0].options)
  154. }
  155. },
  156. adjustViewport: function() {
  157. return this._fitBounds()
  158. },
  159. isEventsCanceled: function() {
  160. return true
  161. },
  162. _fitBounds: abstract,
  163. _updateBounds: function() {
  164. var that = this;
  165. this._clearBounds();
  166. if (!this._option("autoAdjust")) {
  167. return
  168. }
  169. iteratorUtils.each(this._markers, function(_, markerObject) {
  170. that._extendBounds(markerObject.location)
  171. });
  172. iteratorUtils.each(this._routes, function(_, routeObject) {
  173. routeObject.northEast && that._extendBounds(routeObject.northEast);
  174. routeObject.southWest && that._extendBounds(routeObject.southWest)
  175. })
  176. },
  177. _clearBounds: function() {
  178. this._bounds = null
  179. },
  180. _extendBounds: abstract
  181. });
  182. module.exports = DynamicProvider;