openlayers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. (function ($, Drupal) {
  2. "use strict";
  3. Drupal.openlayers = {
  4. instances: {},
  5. processMap: function (map_id, context) {
  6. var settings = $.extend({}, {
  7. layer: [],
  8. style: [],
  9. control: [],
  10. interaction: [],
  11. source: [],
  12. projection: [],
  13. component: []
  14. }, Drupal.settings.openlayers.maps[map_id]);
  15. var map = false;
  16. // If already processed just return the instance.
  17. if (Drupal.openlayers.instances[map_id] !== undefined) {
  18. return Drupal.openlayers.instances[map_id].map;
  19. }
  20. $(document).trigger('openlayers.build_start', [
  21. {
  22. 'type': 'objects',
  23. 'settings': settings,
  24. 'context': context
  25. }
  26. ]);
  27. try {
  28. $(document).trigger('openlayers.map_pre_alter', [
  29. {
  30. context: context,
  31. settings: settings,
  32. map_id: map_id
  33. }
  34. ]);
  35. map = Drupal.openlayers.getObject(context, 'maps', settings.map, map_id);
  36. $(document).trigger('openlayers.map_post_alter', [{map: Drupal.openlayers.instances[map_id].map}]);
  37. if (settings.style.length > 0) {
  38. $(document).trigger('openlayers.styles_pre_alter', [
  39. {
  40. styles: settings.style,
  41. map_id: map_id
  42. }
  43. ]);
  44. settings.style.map(function (data) {
  45. Drupal.openlayers.getObject(context, 'styles', data, map_id);
  46. });
  47. $(document).trigger('openlayers.styles_post_alter', [
  48. {
  49. styles: settings.style,
  50. map_id: map_id
  51. }
  52. ]);
  53. }
  54. if (settings.source.length > 0) {
  55. $(document).trigger('openlayers.sources_pre_alter', [
  56. {
  57. sources: settings.source,
  58. map_id: map_id
  59. }
  60. ]);
  61. settings.source.map(function (data) {
  62. if (data.opt !== undefined && data.opt.attributions !== undefined) {
  63. data.opt.attributions = [
  64. new ol.Attribution({
  65. 'html': data.opt.attributions
  66. })
  67. ];
  68. }
  69. Drupal.openlayers.getObject(context, 'sources', data, map_id);
  70. });
  71. $(document).trigger('openlayers.sources_post_alter', [
  72. {
  73. sources: settings.source,
  74. map_id: map_id
  75. }
  76. ]);
  77. }
  78. if (settings.interaction.length > 0) {
  79. $(document).trigger('openlayers.interactions_pre_alter', [
  80. {
  81. interactions: settings.interaction,
  82. map_id: map_id
  83. }
  84. ]);
  85. settings.interaction.map(function (data) {
  86. var interaction = Drupal.openlayers.getObject(context, 'interactions', data, map_id);
  87. if (interaction) {
  88. map.addInteraction(interaction);
  89. }
  90. });
  91. $(document).trigger('openlayers.interactions_post_alter', [
  92. {
  93. interactions: settings.interaction,
  94. map_id: map_id
  95. }
  96. ]);
  97. }
  98. if (settings.layer.length > 0) {
  99. $(document).trigger('openlayers.layers_pre_alter', [
  100. {
  101. layers: settings.layer,
  102. map_id: map_id
  103. }
  104. ]);
  105. var groups = {};
  106. var layers = {};
  107. settings.layer.map(function (data, key) {
  108. if (data.fs === 'openlayers.Layer:Group') {
  109. groups[data.mn] = data;
  110. }
  111. else {
  112. layers[data.mn] = data;
  113. }
  114. });
  115. for (var i in layers) {
  116. var data = jQuery.extend(true, {}, layers[i]);
  117. data.opt.source = Drupal.openlayers.instances[map_id].sources[data.opt.source];
  118. if (data.opt.style !== undefined && Drupal.openlayers.instances[map_id].styles[data.opt.style] !== undefined) {
  119. data.opt.style = Drupal.openlayers.instances[map_id].styles[data.opt.style];
  120. }
  121. var layer = Drupal.openlayers.getObject(context, 'layers', data, map_id);
  122. if (layer) {
  123. if (data.opt.name !== undefined) {
  124. layer.set('title', data.opt.name);
  125. }
  126. layers[i] = layer;
  127. }
  128. }
  129. for (var i in groups) {
  130. data = jQuery.extend(true, {}, groups[i]);
  131. var candidates = [];
  132. data.opt.grouplayers.map(function (layer_machine_name) {
  133. candidates.push(layers[layer_machine_name]);
  134. delete layers[layer_machine_name];
  135. });
  136. data.opt.grouplayers = candidates;
  137. layer = Drupal.openlayers.getObject(context, 'layers', data, map_id);
  138. if (layer) {
  139. groups[i] = layer;
  140. }
  141. }
  142. $.map(layers, function (layer) {
  143. map.addLayer(layer);
  144. });
  145. // Todo: See why it's not ordered properly automatically.
  146. var groupsOrdered = [];
  147. for (var i in groups) {
  148. groupsOrdered.push(groups[i]);
  149. }
  150. groupsOrdered.reverse().map(function (layer) {
  151. map.addLayer(layer);
  152. });
  153. $(document).trigger('openlayers.layers_post_alter', [
  154. {
  155. layers: settings.layer,
  156. map_id: map_id
  157. }
  158. ]);
  159. }
  160. if (settings.control.length > 0) {
  161. $(document).trigger('openlayers.controls_pre_alter', [
  162. {
  163. controls: settings.control,
  164. map_id: map_id
  165. }
  166. ]);
  167. settings.control.map(function (data) {
  168. var control = Drupal.openlayers.getObject(context, 'controls', data, map_id);
  169. if (control) {
  170. map.addControl(control);
  171. }
  172. });
  173. $(document).trigger('openlayers.controls_post_alter', [
  174. {
  175. controls: settings.control,
  176. map_id: map_id
  177. }
  178. ]);
  179. }
  180. if (settings.component.length > 0) {
  181. $(document).trigger('openlayers.components_pre_alter', [{components: settings.component}]);
  182. settings.component.map(function (data) {
  183. Drupal.openlayers.getObject(context, 'components', data, map_id);
  184. });
  185. }
  186. } catch (e) {
  187. $('#' + map_id).empty();
  188. $(document).trigger('openlayers.build_failed', [
  189. {
  190. 'error': e,
  191. 'settings': settings,
  192. 'context': context
  193. }
  194. ]);
  195. map = false;
  196. }
  197. $(document).trigger('openlayers.build_stop', [
  198. {
  199. 'type': 'objects',
  200. 'settings': settings,
  201. 'context': context
  202. }
  203. ]);
  204. return map;
  205. },
  206. /**
  207. * Return the map instance collection of a map_id.
  208. *
  209. * @param map_id
  210. * The id of the map.
  211. * @returns object/false
  212. * The object or false if not instantiated yet.
  213. */
  214. getMapById: function (map_id) {
  215. if (Drupal.settings.openlayers.maps[map_id] !== undefined) {
  216. // Return map if it is instantiated already.
  217. if (Drupal.openlayers.instances[map_id]) {
  218. return Drupal.openlayers.instances[map_id];
  219. }
  220. }
  221. return false;
  222. },
  223. // Holds dynamic created asyncIsReady callbacks for every map id.
  224. // The functions are named by the cleaned map id. Everything besides 0-9a-z
  225. // is replaced by an underscore (_).
  226. asyncIsReadyCallbacks: {},
  227. asyncIsReady: function (map_id) {
  228. if (Drupal.settings.openlayers.maps[map_id] !== undefined) {
  229. Drupal.settings.openlayers.maps[map_id].map.opt.async--;
  230. if (!Drupal.settings.openlayers.maps[map_id].map.opt.async) {
  231. $('#' + map_id).once('openlayers-map', function () {
  232. Drupal.openlayers.processMap(map_id, document);
  233. });
  234. }
  235. }
  236. },
  237. /**
  238. * Get an object of a map.
  239. *
  240. * If it isn't instantiated yet the instance is created.
  241. */
  242. getObject: (function (context, type, data, map_id) {
  243. // If the type is maps the structure is slightly different.
  244. var instances_type = type;
  245. if (type == 'maps') {
  246. instances_type = 'map';
  247. }
  248. // Prepare instances cache.
  249. if (Drupal.openlayers.instances[map_id] === undefined) {
  250. Drupal.openlayers.instances[map_id] = {
  251. map: null,
  252. layers: {},
  253. styles: {},
  254. controls: {},
  255. interactions: {},
  256. sources: {},
  257. projections: {},
  258. components: {}
  259. };
  260. }
  261. // Check if we've already an instance of this object for this map.
  262. if (Drupal.openlayers.instances[map_id] !== undefined && Drupal.openlayers.instances[map_id][instances_type] !== undefined) {
  263. if (instances_type !== 'map' && Drupal.openlayers.instances[map_id][instances_type][data.mn] !== undefined) {
  264. return Drupal.openlayers.instances[map_id][instances_type][data.mn];
  265. }
  266. else
  267. if (instances_type === 'map' && Drupal.openlayers.instances[map_id][instances_type]) {
  268. return Drupal.openlayers.instances[map_id][instances_type];
  269. }
  270. }
  271. var object = null;
  272. // Make sure that data.opt exist even if it's empty.
  273. data.opt = $.extend({}, {}, data.opt);
  274. if (Drupal.openlayers.pluginManager.isRegistered(data['fs'])) {
  275. $(document).trigger('openlayers.object_pre_alter', [
  276. {
  277. 'type': type,
  278. 'mn': data.mn,
  279. 'data': data,
  280. 'map': Drupal.openlayers.instances[map_id].map,
  281. 'objects': Drupal.openlayers.instances[map_id],
  282. 'context': context,
  283. 'map_id': map_id
  284. }
  285. ]);
  286. object = Drupal.openlayers.pluginManager.createInstance(data['fs'], {
  287. 'data': data,
  288. 'opt': data.opt,
  289. 'map': Drupal.openlayers.instances[map_id].map,
  290. 'objects': Drupal.openlayers.instances[map_id],
  291. 'context': context,
  292. 'map_id': map_id
  293. });
  294. $(document).trigger('openlayers.object_post_alter', [
  295. {
  296. 'type': type,
  297. 'mn': data.mn,
  298. 'data': data,
  299. 'map': Drupal.openlayers.instances[map_id].map,
  300. 'objects': Drupal.openlayers.instances[map_id],
  301. 'context': context,
  302. 'object': object,
  303. 'map_id': map_id
  304. }
  305. ]);
  306. // Store object to the instances cache.
  307. if (type == 'maps') {
  308. Drupal.openlayers.instances[map_id][instances_type] = object;
  309. }
  310. else {
  311. Drupal.openlayers.instances[map_id][instances_type][data.mn] = object;
  312. }
  313. return object;
  314. }
  315. else {
  316. $(document).trigger('openlayers.object_error', [
  317. {
  318. 'type': type,
  319. 'mn': data.mn,
  320. 'data': data,
  321. 'map': Drupal.openlayers.instances[map_id].map,
  322. 'objects': Drupal.openlayers.instances[map_id],
  323. 'context': context,
  324. 'object': object,
  325. 'map_id': map_id
  326. }
  327. ]);
  328. }
  329. }),
  330. log: function (string) {
  331. if (Drupal.openlayers.console !== undefined) {
  332. Drupal.openlayers.console.log(string);
  333. }
  334. }
  335. };
  336. }(jQuery, Drupal));