const core = {}; var _self = core; var $window = $(window); /* |-------------------------------------------------------------------------- | System |-------------------------------------------------------------------------- */ core.system = { version: '0.0.1', init: function() {}, initialiseModuleTreeFromString: function(obj, str, data) { var $this = this; var orginal_str = str; var str = str.split("."); for (var i = 0; i < str.length; i++) { if(obj) { var obj = (obj[str[i]]) ? obj[str[i]] : false; if(typeof obj.init == 'function') { if(obj.bootLoaderStatus == false || obj.bootLoaderStatus == null) { obj.bootLoaderStatus = true; console.info("core.js | Initialising Module:", [str[i]], (str.length > 1 ? '['+orginal_str+']' : '')); if(data && data[str[i]]) { console.info("core.js | ====> Passing Data to Module:", [str[i]], '| ', data[str[i]]); _self.system.setData(obj, data[str[i]]); } obj.init(); } else { console.warn("core.js | Skipping already initialised Module:", [str[i]], 'from module tree [', orginal_str, ']'); } } else { console.warn("core.js | Could not find init function for:", [str[i]], orginal_str); } } else { console.warn("core.js | Module Not Found:", orginal_str, [str[i]]); } } }, loadModule: function(modules, data) { if($.isArray(modules)) { for(var i = 0; i < modules.length; ++ i) { core.system.initialiseModuleTreeFromString(core, modules[i], data); } } else { core.system.initialiseModuleTreeFromString(core, modules, data); } return this; }, setData: function(obj, data) { var tempDefaults = obj.defaults; obj.data = $.extend(true, tempDefaults, data); } } core.loadModule = core.system.loadModule; /* * GoogleMapsApiComponent * A wrapper around the Google Maps Javascript API which removes most * of the heavy lifting thats involved with initalising a map and * apply markers and or other customisations... * * @typedef {Object} Marker * @property {Object} position Specifies a LatLng identifying the initial location of the Marker. * @property {Integer} position.lat The Latitude of the Marker. * @property {Integer} position.lng The Longitude of the Marker. * @property {String} [title] Text that is displayed when hovering on the Marker. * * @param {String} mapSelector Selector for the element that will be used as a Map. * @param {Object} params Customisation options. * @param {Object} params.center Coorditates of the center of the Map. * @param {Integer} params.zoom The initial Map zoom level. * @param {Integer|true} params.draggable If false, prevents the map from being dragged. * @param {Object[]|null} params.markers Array of Markers to be added to the Map. * @param {Marker} params.markers[] Marker to be added to the Map. * @param {String|null} params.markerIcon Image path to be used for all Markers. * @param {Object|null} params.markerIconScaledSize The size of the Marker image after scaling. * @param {Boolean|false} params.centerToMarkers Center the map so that all markers are visible. */ var GoogleMapsApiComponent = function(mapSelector, params) { // Mutate the default parameters with the supplied parameters... this.params = Object.assign({ center: { lat: -57.3856, lng: 18.7882 }, zoom: 8, draggable: true, markers: [], markerIcon: false, centerToMarkers: true, }, params); this.markers = []; this.mapElement = document.querySelectorAll(mapSelector)[0]; this.initGoogleMap(); if(this.params.markers.length > 0) { this.createAndAssociateMarkers(); } }; GoogleMapsApiComponent.prototype.initGoogleMap = function() { this._map = new google.maps.Map(this.mapElement, { center: this.params.center, zoom: this.params.zoom, draggable: this.params.draggable, }); } /* * Grab the supplied markers and build up a Google Marker * Object for each, then associate the marker with * our Google Map object... */ GoogleMapsApiComponent.prototype.createAndAssociateMarkers = function() { var $this = this; var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < $this.params.markers.length; i++) { var markerOptions = { position: $this.params.markers[i].position, map: $this._map, title: $this.params.markers[i].title }; if($this.params.markerIcon) { markerOptions.icon.url = $this.params.markerIcon; } if($this.params.markerIconScaledSize) { markerOptions.icon.scaledSize = $this.params.markerIconScaledSize; } $this.markers.push(new google.maps.Marker(markerOptions)); bounds.extend($this.params.markers[i].position); } // If desired, initially constain the map to the bounds of the Markers if(this.params.centerToMarkers) { this._map.fitBounds(bounds); } }