| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- (function ($, Drupal) {
- "use strict";
- Drupal.openlayers = {
- instances: {},
- processMap: function (map_id, context) {
- var settings = $.extend({}, {
- layer: [],
- style: [],
- control: [],
- interaction: [],
- source: [],
- projection: [],
- component: []
- }, Drupal.settings.openlayers.maps[map_id]);
- var map = false;
- // If already processed just return the instance.
- if (Drupal.openlayers.instances[map_id] !== undefined) {
- return Drupal.openlayers.instances[map_id].map;
- }
- $(document).trigger('openlayers.build_start', [
- {
- 'type': 'objects',
- 'settings': settings,
- 'context': context
- }
- ]);
- try {
- $(document).trigger('openlayers.map_pre_alter', [
- {
- context: context,
- settings: settings,
- map_id: map_id
- }
- ]);
- map = Drupal.openlayers.getObject(context, 'maps', settings.map, map_id);
- $(document).trigger('openlayers.map_post_alter', [{map: Drupal.openlayers.instances[map_id].map}]);
- if (settings.style.length > 0) {
- $(document).trigger('openlayers.styles_pre_alter', [
- {
- styles: settings.style,
- map_id: map_id
- }
- ]);
- settings.style.map(function (data) {
- Drupal.openlayers.getObject(context, 'styles', data, map_id);
- });
- $(document).trigger('openlayers.styles_post_alter', [
- {
- styles: settings.style,
- map_id: map_id
- }
- ]);
- }
- if (settings.source.length > 0) {
- $(document).trigger('openlayers.sources_pre_alter', [
- {
- sources: settings.source,
- map_id: map_id
- }
- ]);
- settings.source.map(function (data) {
- if (data.opt !== undefined && data.opt.attributions !== undefined) {
- data.opt.attributions = [
- new ol.Attribution({
- 'html': data.opt.attributions
- })
- ];
- }
- Drupal.openlayers.getObject(context, 'sources', data, map_id);
- });
- $(document).trigger('openlayers.sources_post_alter', [
- {
- sources: settings.source,
- map_id: map_id
- }
- ]);
- }
- if (settings.interaction.length > 0) {
- $(document).trigger('openlayers.interactions_pre_alter', [
- {
- interactions: settings.interaction,
- map_id: map_id
- }
- ]);
- settings.interaction.map(function (data) {
- var interaction = Drupal.openlayers.getObject(context, 'interactions', data, map_id);
- if (interaction) {
- map.addInteraction(interaction);
- }
- });
- $(document).trigger('openlayers.interactions_post_alter', [
- {
- interactions: settings.interaction,
- map_id: map_id
- }
- ]);
- }
- if (settings.layer.length > 0) {
- $(document).trigger('openlayers.layers_pre_alter', [
- {
- layers: settings.layer,
- map_id: map_id
- }
- ]);
- var groups = {};
- var layers = {};
- settings.layer.map(function (data, key) {
- if (data.fs === 'openlayers.Layer:Group') {
- groups[data.mn] = data;
- }
- else {
- layers[data.mn] = data;
- }
- });
- for (var i in layers) {
- var data = jQuery.extend(true, {}, layers[i]);
- data.opt.source = Drupal.openlayers.instances[map_id].sources[data.opt.source];
- if (data.opt.style !== undefined && Drupal.openlayers.instances[map_id].styles[data.opt.style] !== undefined) {
- data.opt.style = Drupal.openlayers.instances[map_id].styles[data.opt.style];
- }
- var layer = Drupal.openlayers.getObject(context, 'layers', data, map_id);
- if (layer) {
- if (data.opt.name !== undefined) {
- layer.set('title', data.opt.name);
- }
- layers[i] = layer;
- }
- }
- for (var i in groups) {
- data = jQuery.extend(true, {}, groups[i]);
- var candidates = [];
- data.opt.grouplayers.map(function (layer_machine_name) {
- candidates.push(layers[layer_machine_name]);
- delete layers[layer_machine_name];
- });
- data.opt.grouplayers = candidates;
- layer = Drupal.openlayers.getObject(context, 'layers', data, map_id);
- if (layer) {
- groups[i] = layer;
- }
- }
- $.map(layers, function (layer) {
- map.addLayer(layer);
- });
- // Todo: See why it's not ordered properly automatically.
- var groupsOrdered = [];
- for (var i in groups) {
- groupsOrdered.push(groups[i]);
- }
- groupsOrdered.reverse().map(function (layer) {
- map.addLayer(layer);
- });
- $(document).trigger('openlayers.layers_post_alter', [
- {
- layers: settings.layer,
- map_id: map_id
- }
- ]);
- }
- if (settings.control.length > 0) {
- $(document).trigger('openlayers.controls_pre_alter', [
- {
- controls: settings.control,
- map_id: map_id
- }
- ]);
- settings.control.map(function (data) {
- var control = Drupal.openlayers.getObject(context, 'controls', data, map_id);
- if (control) {
- map.addControl(control);
- }
- });
- $(document).trigger('openlayers.controls_post_alter', [
- {
- controls: settings.control,
- map_id: map_id
- }
- ]);
- }
- if (settings.component.length > 0) {
- $(document).trigger('openlayers.components_pre_alter', [{components: settings.component}]);
- settings.component.map(function (data) {
- Drupal.openlayers.getObject(context, 'components', data, map_id);
- });
- }
- } catch (e) {
- $('#' + map_id).empty();
- $(document).trigger('openlayers.build_failed', [
- {
- 'error': e,
- 'settings': settings,
- 'context': context
- }
- ]);
- map = false;
- }
- $(document).trigger('openlayers.build_stop', [
- {
- 'type': 'objects',
- 'settings': settings,
- 'context': context
- }
- ]);
- return map;
- },
- /**
- * Return the map instance collection of a map_id.
- *
- * @param map_id
- * The id of the map.
- * @returns object/false
- * The object or false if not instantiated yet.
- */
- getMapById: function (map_id) {
- if (Drupal.settings.openlayers.maps[map_id] !== undefined) {
- // Return map if it is instantiated already.
- if (Drupal.openlayers.instances[map_id]) {
- return Drupal.openlayers.instances[map_id];
- }
- }
- return false;
- },
- // Holds dynamic created asyncIsReady callbacks for every map id.
- // The functions are named by the cleaned map id. Everything besides 0-9a-z
- // is replaced by an underscore (_).
- asyncIsReadyCallbacks: {},
- asyncIsReady: function (map_id) {
- if (Drupal.settings.openlayers.maps[map_id] !== undefined) {
- Drupal.settings.openlayers.maps[map_id].map.opt.async--;
- if (!Drupal.settings.openlayers.maps[map_id].map.opt.async) {
- $('#' + map_id).once('openlayers-map', function () {
- Drupal.openlayers.processMap(map_id, document);
- });
- }
- }
- },
- /**
- * Get an object of a map.
- *
- * If it isn't instantiated yet the instance is created.
- */
- getObject: (function (context, type, data, map_id) {
- // If the type is maps the structure is slightly different.
- var instances_type = type;
- if (type == 'maps') {
- instances_type = 'map';
- }
- // Prepare instances cache.
- if (Drupal.openlayers.instances[map_id] === undefined) {
- Drupal.openlayers.instances[map_id] = {
- map: null,
- layers: {},
- styles: {},
- controls: {},
- interactions: {},
- sources: {},
- projections: {},
- components: {}
- };
- }
- // Check if we've already an instance of this object for this map.
- if (Drupal.openlayers.instances[map_id] !== undefined && Drupal.openlayers.instances[map_id][instances_type] !== undefined) {
- if (instances_type !== 'map' && Drupal.openlayers.instances[map_id][instances_type][data.mn] !== undefined) {
- return Drupal.openlayers.instances[map_id][instances_type][data.mn];
- }
- else
- if (instances_type === 'map' && Drupal.openlayers.instances[map_id][instances_type]) {
- return Drupal.openlayers.instances[map_id][instances_type];
- }
- }
- var object = null;
- // Make sure that data.opt exist even if it's empty.
- data.opt = $.extend({}, {}, data.opt);
- if (Drupal.openlayers.pluginManager.isRegistered(data['fs'])) {
- $(document).trigger('openlayers.object_pre_alter', [
- {
- 'type': type,
- 'mn': data.mn,
- 'data': data,
- 'map': Drupal.openlayers.instances[map_id].map,
- 'objects': Drupal.openlayers.instances[map_id],
- 'context': context,
- 'map_id': map_id
- }
- ]);
- object = Drupal.openlayers.pluginManager.createInstance(data['fs'], {
- 'data': data,
- 'opt': data.opt,
- 'map': Drupal.openlayers.instances[map_id].map,
- 'objects': Drupal.openlayers.instances[map_id],
- 'context': context,
- 'map_id': map_id
- });
- $(document).trigger('openlayers.object_post_alter', [
- {
- 'type': type,
- 'mn': data.mn,
- 'data': data,
- 'map': Drupal.openlayers.instances[map_id].map,
- 'objects': Drupal.openlayers.instances[map_id],
- 'context': context,
- 'object': object,
- 'map_id': map_id
- }
- ]);
- // Store object to the instances cache.
- if (type == 'maps') {
- Drupal.openlayers.instances[map_id][instances_type] = object;
- }
- else {
- Drupal.openlayers.instances[map_id][instances_type][data.mn] = object;
- }
- return object;
- }
- else {
- $(document).trigger('openlayers.object_error', [
- {
- 'type': type,
- 'mn': data.mn,
- 'data': data,
- 'map': Drupal.openlayers.instances[map_id].map,
- 'objects': Drupal.openlayers.instances[map_id],
- 'context': context,
- 'object': object,
- 'map_id': map_id
- }
- ]);
- }
- }),
- log: function (string) {
- if (Drupal.openlayers.console !== undefined) {
- Drupal.openlayers.console.log(string);
- }
- }
- };
- }(jQuery, Drupal));
|