1/** 2 * Store Locator Plus® Power User Interface 3 * 2209.15.01 4 */ 5/* globals jQuery, google, slp_Filter, cslmap, slplus, slp, slppower_settings */ 6const SLPPOWER = { 7 ready: function() { 8 slp_Filter( 'slp_map_ready' ).subscribe( function( cslmap ) { 9 SLPPOWER.map.initialize(); 10 if ( slplus.options.use_sensor ) { 11 slp.sensor = new slp_LocationServices(); 12 slp.sensor.initialize(); 13 } 14 } ); 15 }, 16}; 17slp_Filter('ready').subscribe(SLPPOWER.ready); 18 19/** 20 * 21 * Class slp_LocationServices 22 * 23 * Handle the location sensor (or not) 24 */ 25var slp_LocationServices = function () { 26 this.service = null; 27 this.location_timeout = null; 28 this.lat = 0.00; 29 this.lng = 0.00; 30 this.timeout = 30000; 31 32 /** 33 * Set the current location. 34 * 35 * When setting get_user_location the callback and errorCallback functions must 36 * be defined. See the sensor.get_user_location call down below for the 37 * return-to place for these two functions passed as variables 38 * 39 * @param callback 40 * @param errorCallback 41 */ 42 this.get_user_location = function ( callback , errorCallback ) { 43 44 // Old school support 45 if ( typeof navigator.geolocation === 'undefined' ) { 46 if ( google.gears ) { 47 this.service = google.gears.factory.create( 'beta.geolocation' ); 48 } 49 50 // HTML 5 support 51 } else { 52 this.service = navigator.geolocation; 53 } 54 55 // If this browser supports location services, use them 56 // 57 if ( this.service ) { 58 59 // In 30 seconds run errorCallback 60 // 61 slp.sensor.location_timeout = setTimeout( errorCallback , this.timeout, { name: 'location_timeout' , message: 'Location sensor timed out' } ); 62 63 // Run the browser location service to get the current position 64 // 65 // on success run callback 66 // on failure run errorCallback 67 // 68 this.service.getCurrentPosition( callback , errorCallback , { 69 maximumAge: 60000 , 70 timeout: this.timeout , 71 enableHighAccuracy: true 72 } ); 73 74 // No location support, direct geocoding. 75 // 76 } else { 77 slp.sensor.geocode_with_no_sensor(); 78 } 79 80 }; 81 82 /** 83 * Initialize GPS services. 84 */ 85 this.initialize = function() { 86 cslmap.usingSensor = true; 87 if ( typeof cslmap.build_map !== 'function' ) { 88 slp_Filter( 'slp_map_ready' ).subscribe( this.initialize ); 89 return; 90 } 91 92 slp.sensor.get_user_location( 93 94 // Success 95 // 96 function ( loc ) { 97 clearTimeout( slp.sensor.location_timeout ); 98 slp.sensor.lat = loc.coords.latitude; 99 slp.sensor.lng = loc.coords.longitude; 100 101 if ( typeof cslmap.build_map === 'function' ) { 102 cslmap.build_map(new google.maps.LatLng(loc.coords.latitude, 103 loc.coords.longitude)); 104 } 105 } , 106 107 // Error 108 // 109 function ( err ) { 110 clearTimeout( slp.sensor.location_timeout ); 111 slp.sensor.geocode_with_no_sensor(); 112 } 113 ); 114 115 }; 116 117 // Turn off all sensor settings and geocode. 118 // 119 this.geocode_with_no_sensor = function() { 120 slplus.options.use_sensor = false; 121 cslmap.usingSensor = false; 122 cslmap.doGeocode(); 123 }; 124}; 125 126/** 127 * The Directory List Class 128 */ 129SLPPOWER.directory = ( function () { 130 131 /** 132 * Show locations on map. 133 */ 134 this.show_locations_on_map = function( text_value ) { 135 jQuery.ajax({ 136 method : 'GET', 137 url : mySLP.protocol + mySLP.rest_base + 'locations/', 138 data: { 139 api_key: mySLP.api_key, 140 filter: [ 141 { 'filter_by': 'sl_state', 'filter_value': text_value } 142 ] 143 }, 144 dataType: 'json' 145 146 }).fail(function( jqXHR, textStatus, errorThrown ) { 147 console.log( jqXHR, textStatus, errorThrown ); 148 if( jqXHR.responseJSON && jqXHR.responseJSON.message ){ 149 jQuery( "#" + mySLP.divID + " .map-messages" ).addClass('error').prepend( jqXHR.responseJSON.message ).show(); 150 } 151 152 }).done(function( result ){ 153 var ajax_formatted_result = {}, 154 valid_response 155 ; 156 157 if ( typeof result.data !== 'undefined' ) { 158 var idx, key, new_key, store; 159 160 ajax_formatted_result.response = []; 161 162 result.data.forEach( function( store ) { 163 new_element = {}; 164 for ( key in store ) { 165 new_key = key.replace('sl_', ''); 166 if ( new_key === 'store' ) { 167 new_key = 'name'; 168 } 169 new_element[ new_key ] = store[ key ]; 170 } 171 new_element.data = store; 172 ajax_formatted_result.response.push( new_element ); 173 }); 174 175 ajax_formatted_result.count = ajax_formatted_result.response.length; 176 ajax_formatted_result.success = true; 177 ajax_formatted_result.type = 'search'; 178 179 } else { 180 ajax_formatted_result = { 181 'success' : false 182 } 183 } 184 cslmap.process_ajax_response( ajax_formatted_result ); 185 }); 186 187 188 jQuery( 'div.store_locator_plus' ).removeClass( 'hidden' ); 189 }; 190 191 /** 192 * Public Methods 193 */ 194 return { 195 show_locations_on_map: show_locations_on_map 196 } 197 198} )(); 199 200/** 201 * The Location List Class 202 */ 203SLPPOWER.map = ( function () { 204 var center, 205 location_id; 206 207 /** 208 * Initialize the map. 209 */ 210 this.initialize = function () { 211 jQuery("[title='location_map']").each(function () { 212 location_id = jQuery(this).attr('data-location_id'); 213 get_location_data(location_id); 214 }); 215 }; 216 217 /** 218 * Add a location to the map. 219 * 220 * @typedef {Object} data 221 * @property {number} sl_id 222 * @property {number} sl_latitude 223 * @property {number} sl_longitude 224 */ 225 this.add_this_location = function ( data ) { 226 var add_map, 227 map_div_id, 228 marker_settings 229 ; 230
231 map_div_id = document.getElementById('map-canvas-'+data.sl_id); 232 233 center = new google.maps.LatLng( data.sl_latitude , data.sl_longitude ); 234 add_map = new slp_Map(); 235 236 slplus.options.immediately_show_locations = '0'; 237 238 slp_Filter('map_options').subscribe( this.modify_add_map_options ); 239 if ( typeof add_map.build_map === 'function' ) { 240 add_map.build_map(center, map_div_id); 241 marker_settings = { 242 position: center, 243 map: add_map.gmap, 244 icon: slppower_settings.map_end_icon 245 }; 246 new google.maps.Marker(marker_settings); 247 } 248 slp_Filter('map_options').unsubscribe( this.modify_add_map_options ); 249 250 }; 251 252 /** 253 * Request location data from the SLP REST service. 254 * 255 * @param location_id 256 */ 257 this.get_location_data = function ( location_id ) { 258 jQuery.ajax( 259 slppower_settings.resturl + 'locations/' + location_id , 260 { 261 success: add_this_location 262 } 263 ); 264 }; 265 266 /** 267 * Set Map options 268 * 269 * @param map_options 270 * zoom force to 14 271 */ 272 this.modify_add_map_options = function( map_options ) { 273 map_options.zoom = 14; 274 }; 275 276 /** 277 * Public Methods 278 */ 279 return { 280 initialize: initialize, 281 modify_add_map_options: modify_add_map_options 282 } 283 284} )();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.